-
Notifications
You must be signed in to change notification settings - Fork 7
/
pixelweb_genmanifest
109 lines (101 loc) · 3.56 KB
/
pixelweb_genmanifest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#! /usr/bin/env python
import inspect
from bibliopixel import *
from bibliopixel.animation import *
import pixelweb.loader as loader
from pixelweb.util import d
# import config
import os, sys
import json
def animationPredicate(obj):
if not inspect.isclass(obj): return False
return issubclass(obj, BaseAnimation)
def loadClasses(mod):
results = []
for name, obj in inspect.getmembers(mod, predicate=animationPredicate):
if obj.__module__ == mod.__name__:
results.append(d({"name":name, "cls":obj}))
return results
def loadParams(cls):
spec = inspect.getargspec(cls.__init__)
args = list(reversed(spec.args))
defaults = spec.defaults
if not defaults: defaults = []
defaults = list(reversed(list(defaults)))
result = []
count = 0
for a in args:
if a not in ["self", "led"]:
d = None
dtype = ""
if count < len(defaults):
d = defaults[count]
if type(d) is int:
dtype = "int"
elif type(d) is bool:
dtype = "bool"
elif type(d) is str:
dtype = "str"
elif (type(d) is list or type(d) is tuple) and len(d) == 3:
dtype = "color"
i = {
"id": a,
"label": "",
"type": dtype,
"default": d,
"help":""
}
result.append(i)
count += 1
result = result[::-1]
return result
def getControllerType(cls):
if issubclass(cls, BaseMatrixAnim): return "matrix"
if issubclass(cls, BaseStripAnim): return "strip"
if issubclass(cls, BaseCircleAnim): return "circle"
return ""
if __name__ == "__main__":
if len(sys.argv) < 2:
print "Must supply a file to process!"
else:
files = sys.argv[1:]
cwd = os.getcwdu()
for f in files:
if not os.path.isabs(f):
f = os.path.join(cwd, f)
if not os.path.exists(f):
print "File does not exist! - {}".format(f)
continue
print "Loading: {}".format(f)
mod = loader.load_module(f)[1]
if hasattr(mod, "MANIFEST"):
print "Module already contains manifest. Skipping..."
continue
results = []
clist = loadClasses(mod)
for c in clist:
params = loadParams(c.cls)
manifest = {
"id": c.name,
"class": "##" + c.name,
"type": "animation",
"display": c.name,
"desc": None,
"controller": getControllerType(c.cls),
"params": params
}
results.append(manifest)
if len(results) > 0:
manifest = json.dumps(results, indent=4, sort_keys=True)
for c in clist:
manifest = manifest.replace('"##' + c.name + '"', c.name)
manifest = manifest.replace(": null", ": None")
manifest = manifest.replace(": false", ": False")
manifest = manifest.replace(": true", ": True")
manifest = "\n\n\nMANIFEST = " + manifest
with open(f, "a") as fp:
print "Writing manifest..."
fp.write(manifest)
else:
print "No valid animations to parse..."
print "\nComplete! Please fill in the rest of the manifest manually."