-
Notifications
You must be signed in to change notification settings - Fork 5
/
tools.py
245 lines (205 loc) · 8.02 KB
/
tools.py
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import os, re, yaml
def extractFunctions(searchText):
return re.findall("((void|bool|int|float|vec\\d|mat\\d)\\s(((?!=).)*)(\\(.*\\)))", searchText)
def isDocumentationIn(yaml_block):
return 'doc' in yaml_block
def isDescriptionIn(yaml_block):
if isDocumentationIn(yaml_block):
return 'description' in yaml_block['doc']
return False
def getDescriptionOf(yaml_block):
if isDescriptionIn(yaml_block):
return yaml_block['doc']['description'] + '\n'
else:
return ''
def isShaderIn(yaml_block):
return 'shaders' in yaml_block
def isShaderBlocksIn(yaml_block):
if isShaderIn(yaml_block):
return 'blocks' in yaml_block['shaders']
return False
def isGlobalBlockIn(yaml_block):
if isShaderBlocksIn(yaml_block):
return 'global' in yaml_block['shaders']['blocks']
def getGlobalBlockIn(yaml_block):
if isGlobalBlockIn(yaml_block):
return "\n"+yaml_block['shaders']['blocks']['global']
return ""
def getGlobalFunctionsIn(yaml_block):
return extractFunctions(getGlobalBlockIn(yaml_block))
def isNormalBlockIn(yaml_block):
if isShaderBlocksIn(yaml_block):
return 'normal' in yaml_block['shaders']['blocks']
def getNormalBlockIn(yaml_block):
if isNormalBlockIn(yaml_block):
return "\n"+yaml_block['shaders']['blocks']['normal']
return ""
def isColorBlockIn(yaml_block):
if isShaderBlocksIn(yaml_block):
return 'color' in yaml_block['shaders']['blocks']
def getColorBlockIn(yaml_block):
if isColorBlockIn(yaml_block):
return "\n"+yaml_block['shaders']['blocks']['color']
return ""
def isFilterBlockIn(yaml_block):
if isShaderBlocksIn(yaml_block):
return 'filter' in yaml_block['shaders']['blocks']
def getFilterBlockIn(yaml_block):
if isFilterBlockIn(yaml_block):
return "\n"+yaml_block['shaders']['blocks']['filter']
return ""
def isUniformsIn(yaml_block):
return 'uniforms' in yaml_block['shaders']
def isDefinesIn(yaml_block):
return 'defines' in yaml_block['shaders']
def getDefinesIn(yaml_block):
if isDefinesIn(yaml_block):
return yaml_block['shaders']['defines'].keys()
return []
def isUniformsIn(yaml_block):
return 'uniforms' in yaml_block['shaders']
def getUniformsIn(yaml_block):
if isUniformsIn(yaml_block):
return yaml_block['shaders']['uniforms'].keys()
return []
def getUniformType(uniform_value):
if type(uniform_value) is str:
return 'sampler2D'
elif type(uniform_value) is float:
return 'float'
elif isinstance(uniform_value, list):
return 'vec'+len(uniform_value)
def uniformHaveMetadata(uniform_name, yaml_block):
if 'ui' in yaml_block:
if 'shaders' in yaml_block['ui']:
if 'uniforms' in yaml_block['ui']['shaders']:
if uniform_name in yaml_block['ui']['shaders']['uniforms']:
return True
return False
def defineHaveMetadata(define_name, yaml_block):
if 'ui' in yaml_block:
if 'shaders' in yaml_block['ui']:
if 'defines' in yaml_block['ui']['shaders']:
if define_name in yaml_block['ui']['shaders']['defines']:
return True
return False
def isTestIn(yaml_block):
return 'test' in yaml_block
def isExamplesIn(yaml_block):
if isDocumentationIn(yaml_block):
return 'examples' in yaml_block['doc']
return False
def isDependenceIn(yaml_block):
return 'mix' in yaml_block
def getDependencesIn(yaml_block):
if isDependenceIn(yaml_block):
if isinstance(yaml_block['mix'], (str, unicode)):
return [yaml_block['mix']]
else:
return yaml_block['mix']
return []
def collectDefines(yaml_file, block_name, defines_dict):
block = yaml_file['styles'][block_name]
depts = getDependencesIn(block)
for dep_block_name in depts:
collectDefines(yaml_file, dep_block_name, defines_dict)
defines = getDefinesIn(block)
for define_name in defines:
defines_dict[define_name] = block['shaders']['defines'][define_name]
def collectUniforms(yaml_file, block_name, uniforms_dict):
block = yaml_file['styles'][block_name]
depts = getDependencesIn(block)
for dep_block_name in depts:
collectUniforms(yaml_file, dep_block_name, uniforms_dict)
uniforms = getUniformsIn(block)
for uniform_name in uniforms:
uniforms_dict[uniform_name] = block['shaders']['uniforms'][uniform_name]
def getAllGlobals(yaml_file, block_name):
rta = ""
depts = getDependencesIn(yaml_file['styles'][block_name])
for dep_block_name in depts:
rta += getAllGlobals(yaml_file, dep_block_name)
rta += getGlobalBlockIn(yaml_file['styles'][block_name])
return rta
def getAllNormals(yaml_file, block_name):
rta = ""
depts = getDependencesIn(yaml_file['styles'][block_name])
for dep_block_name in depts:
rta += getAllNormals(yaml_file, dep_block_name)
rta += getNormalBlockIn(yaml_file['styles'][block_name])
return rta
def getAllColors(yaml_file, block_name):
rta = ""
depts = getDependencesIn(yaml_file['styles'][block_name])
for dep_block_name in depts:
rta += getAllColors(yaml_file, dep_block_name)
rta += getColorBlockIn(yaml_file['styles'][block_name])
return rta
def getAllFilters(yaml_file, block_name):
rta = ""
depts = getDependencesIn(yaml_file['styles'][block_name])
for dep_block_name in depts:
rta += getAllFilters(yaml_file, dep_block_name)
rta += getFilterBlockIn(yaml_file['styles'][block_name])
return rta
# Recursive dict merge (From https://gist.github.com/angstwad/bf22d1822c38a92ec0a9)
def dict_merge(dct, merge_dct):
""" Recursive dict merge. Inspired by :meth:``dict.update()``, instead of
updating only top-level keys, dict_merge recurses down into dicts nested
to an arbitrary depth, updating keys. The ``merge_dct`` is merged into
``dct``.
:param dct: dict onto which the merge is executed
:param merge_dct: dct merged into dct
:return: None
"""
for k, v in merge_dct.iteritems():
if (k in dct and isinstance(dct[k], dict)
and isinstance(merge_dct[k], dict)):
dict_merge(dct[k], merge_dct[k])
else:
dct[k] = merge_dct[k]
# Append yaml dependences in yaml_file ('import' files) to another yaml file (full_yaml_file)
def appendDependencies(full_yaml, filename):
# print "Append all dependences of " + filename
folder = os.path.dirname(filename)
yaml_file = yaml.safe_load(open(filename))
dict_merge(full_yaml, yaml_file)
if 'import' in yaml_file:
if (type(yaml_file['import']) is str):
dep = folder + '/' + yaml_file['import']
# print "\tMerging " + dep
appendDependencies(full_yaml, dep)
else:
for file in yaml_file['import']:
dep = folder + '/' + file
# print "\tMerging " + dep
appendDependencies(full_yaml, dep)
# From https://github.com/adafruit/Adafruit_Python_GPIO/blob/master/Adafruit_GPIO/Platform.py
def isRPi():
"""Detect the version of the Raspberry Pi. Returns either 1, 2 or
None depending on if it's a Raspberry Pi 1 (model A, B, A+, B+),
Raspberry Pi 2 (model B+), or not a Raspberry Pi.
"""
# Check /proc/cpuinfo for the Hardware field value.
# 2708 is pi 1
# 2709 is pi 2
# Anything else is not a pi.
try:
with open('/proc/cpuinfo', 'r') as infile:
cpuinfo = infile.read()
# Match a line like 'Hardware : BCM2709'
match = re.search('^Hardware\s+:\s+(\w+)$', cpuinfo, flags=re.MULTILINE | re.IGNORECASE)
if not match:
# Couldn't find the hardware, assume it isn't a pi.
return None
if match.group(1) == 'BCM2708':
# Pi 1
return True
elif match.group(1) == 'BCM2709':
# Pi 2
return True
else:
# Something else, not a pi.
return False
except:
return False