Repository: ManiacalLabs/BiblioPixel
Pull request #140 (2017-03-25 12:59:22)

Simplify Project.
I "simplify" the class out of existence, preparatory to reading it all from the dotfiles.


rec: (And this reason for this pull request seems a little obscure - I pulled it from a longer chain to make reviewing easier.  The exact point will be clearer, later...)

Patch:
diff --git a/bibliopixel/main/run.py b/bibliopixel/main/run.py
index c0fbc7c..50ed58c 100644
--- a/bibliopixel/main/run.py
+++ b/bibliopixel/main/run.py
@@ -1,4 +1,4 @@
-from bibliopixel.util import project
+from bibliopixel.project import project
 
 HELP = """
 Run a project description file.
diff --git a/bibliopixel/project/__init__.py b/bibliopixel/project/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/bibliopixel/project/defaults.py b/bibliopixel/project/defaults.py
new file mode 100644
index 0000000..113faf0
--- /dev/null
+++ b/bibliopixel/project/defaults.py
@@ -0,0 +1,32 @@
+import copy
+
+DEFAULTS = {
+    'driver': {
+        'typename': 'bibliopixel.drivers.SimPixel.DriverSimPixel',
+        'num': 1024
+    },
+
+    'led': {
+        'typename': 'bibliopixel.led.matrix.LEDMatrix',
+        'width': 32,
+        'height': 32,
+    },
+
+    'animation': {
+        'typename': 'BiblioPixelAnimations.matrix.bloom.Bloom'
+    },
+
+    'maker': {},
+    'run': {},
+}
+
+
+def apply_defaults(desc):
+    result = copy.deepcopy(DEFAULTS)
+    for k, v in desc.items():
+        if isinstance(v, str):
+            result[k]['typename'] = v
+        else:
+            result[k].update(v)
+
+    return result
diff --git a/bibliopixel/project/project.py b/bibliopixel/project/project.py
new file mode 100644
index 0000000..92d2c61
--- /dev/null
+++ b/bibliopixel/project/project.py
@@ -0,0 +1,54 @@
+import sys, json
+from . defaults import apply_defaults, DEFAULTS
+from .. util.importer import make_object
+from .. import data_maker
+from .. layout import gen_matrix
+from .. led.multimap import MultiMapBuilder
+
+
+def make_animation(driver, led, animation, maker):
+    maker = data_maker.Maker(**maker)
+    drivers = []
+
+    def multi_drivers(device_ids, width, height, serpentine=False, **kwds):
+        build = MultiMapBuilder()
+
+        for id in device_ids:
+            build.addRow(gen_matrix(width, height, serpentine=serpentine))
+            d = make_object(width=width, height=height, deviceID=id, **kwds)
+            drivers.append(d)
+
+        return build.map
+
+    def make_drivers(multimap=False, **kwds):
+        if multimap:
+            return multi_drivers(**kwds)
+
+        drivers.append(make_object(**kwds))
+
+    coordMap = make_drivers(maker=maker, **driver)
+    led = make_object(drivers, coordMap=coordMap, maker=maker, **led)
+    return make_object(led, **animation)
+
+
+def run_animation(run, **kwds):
+    make_animation(**kwds).run(**run)
+
+
+def fix_desc(desc):
+    if isinstance(desc, str):
+        try:
+            desc = open(desc).read()
+        except:
+            pass
+        desc = json.loads(desc)
+
+    return apply_defaults(desc)
+
+
+def run(desc):
+    run_animation(**fix_desc(desc))
+
+
+if __name__ == '__main__':
+    run(*sys.argv[1:])
diff --git a/bibliopixel/util/project.py b/bibliopixel/util/project.py
deleted file mode 100644
index e029f95..0000000
--- a/bibliopixel/util/project.py
+++ /dev/null
@@ -1,90 +0,0 @@
-import sys, copy, json
-from . importer import make_object
-from .. import data_maker
-from .. led.multimap import MultiMapBuilder
-from .. layout import gen_matrix
-
-
-def multi_drivers(device_ids, width, height, serpentine=False, **kwds):
-    build = MultiMapBuilder()
-    drivers = []
-
-    for id in device_ids:
-        build.addRow(gen_matrix(width, height, serpentine=serpentine))
-        d = make_object(width=width, height=height, deviceID=id, **kwds)
-        drivers.append(d)
-
-    return drivers, build.map
-
-
-def make_drivers(multimap=False, **kwds):
-    return multi_drivers(**kwds) if multimap else ([make_object(**kwds)], None)
-
-
-DEFAULTS = {
-    'driver': {
-        'typename': 'bibliopixel.drivers.SimPixel.DriverSimPixel',
-        'num': 1024
-    },
-
-    'led': {
-        'typename': 'bibliopixel.led.matrix.LEDMatrix',
-        'width': 32,
-        'height': 32,
-    },
-
-    'animation': {
-        'typename': 'BiblioPixelAnimations.matrix.bloom.Bloom'
-    },
-
-    'run': {},
-    'maker': {},
-}
-
-
-def apply_defaults(desc):
-    result = copy.deepcopy(DEFAULTS)
-    for k, v in desc.items():
-        if isinstance(v, str):
-            result[k]['typename'] = v
-        else:
-            result[k].update(v)
-
-    return result
-
-
-class Project(object):
-    def __init__(self, driver, led, animation, run, maker):
-        self.run_arguments = run
-        self.maker = data_maker.Maker(**maker)
-        self.drivers, coordMap = make_drivers(maker=self.maker, **driver)
-        if coordMap:
-            self.led = make_object(
-                self.drivers, coordMap=coordMap, maker=self.maker, **led)
-        else:
-            self.led = make_object(self.drivers, maker=self.maker, **led)
-
-        self.animation = make_object(self.led, **animation)
-
-    def run(self):
-        return self.animation.run(**self.run_arguments)
-
-
-def make(desc):
-    if isinstance(desc, str):
-        try:
-            desc = open(desc).read()
-        except:
-            pass
-        desc = json.loads(desc)
-
-    desc = apply_defaults(desc)
-    return Project(**desc)
-
-
-def run(desc):
-    return make(desc).run()
-
-
-if __name__ == '__main__':
-    run(*sys.argv[1:])


Test patch:
diff --git a/test/project_test.py b/test/project_test.py
index d7ea0e7..3929520 100644
--- a/test/project_test.py
+++ b/test/project_test.py
@@ -1,6 +1,6 @@
 import json, unittest
 
-from bibliopixel.util import project
+from bibliopixel.project import project
 from . long_test import long_test