Menu

[r1]: / PrimitiveValue.py  Maximize  Restore  History

Download this file

380 lines (259 with data), 11.4 kB

  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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
from Translator import *
import ValueList, pdb
class SSSPrimitiveValue(object):
CSS_UNKNOWN = 0
CSS_NUMBER = 1
CSS_PERCENTAGE = 2
CSS_EMS = 3
CSS_EXS = 4
CSS_PX = 5
CSS_CM = 6
CSS_MM = 7
CSS_IN = 8
CSS_PT = 9
CSS_PC = 10
CSS_DEG = 11
CSS_RAD = 12
CSS_GRAD = 13
CSS_MS = 14
CSS_S = 15
CSS_HZ = 16
CSS_KHZ = 17
CSS_DIMENSION = 18
CSS_STRING = 19
CSS_URI = 20
CSS_IDENT = 21
CSS_ATTR = 22
CSS_COUNTER = 23
CSS_RECT = 24
CSS_RBGCOLOR = 25
#/* Extensions */
CSS_HEXCOLOR = 26
CSS_OPERATOR = 27
lookup = {}
unit = {}
parentObject = None
_primitiveType = None
def __init__(self, value = None, parentObject = None):
self.prop = {}
self.nonascii = r'[^\0-\177]'
self.uni = r'\\[0-9a-f]{1,6}(\r\n|[ \n\r\t\f])?'
self.escape = self.uni + r'|\\[^\n\r\f0-9a-f]'
self.nmstart = r'[_a-z]|' + self.nonascii + r'|' + self.escape
self.nmchar = r'[_a-z0-9-]|' + self.nonascii + r'|' + self.escape
self.name = self.nmchar + '+'
self.ident = r'[-]?' + self.nmstart + self.nmchar + '*'
self.num = r'[0-9]+|[0-9]*\.[0-9]+'
self.nl = r'\n|\r\n|\r|\f'
self.w = r'[ \t\r\n\f]*'
self.string1 = r'\"([^\n\r\f\\"]|\\' + self.nl + r'|' + self.nonascii + r'|' + self.escape + r')*\"'
self.string2 = r"\'([^\n\r\f\\']|\\" + self.nl + r'|' + self.nonascii + r'|' + self.escape + r')*\''
self.string = self.string1 + '|' + self.string2
self.invalid1 = r'\"([^\n\r\f\\"]|\\' + self.nl + r'|' + self.nonascii + r'|' + self.escape + r')*'
self.invalid2 = r"\'([^\n\r\f\\']|\\" + self.nl + r'|' + self.nonascii + r'|' + self.escape + r')*'
self.invalid = self.invalid1 + '|' + self.invalid2
self.parentObject = parentObject
#/* NOTE: NEED TO IMPLEMENT ALL KNOWN VALUE TYPES */
if value: self.parse(value)
def getPropertyValueBySelector(self, property_name, selector):
selector = trim(selector)
rules = self.parentObject.hostStyleSheet.getRulesBySelector(selector)
if (rules):
for key in rules:
property_value = key.getPropertyValue(property_name)
if (property_value):
return property_value
return None
def evaluate(self, text):
values = ValueList.SSSValueList(text, self.parentObject)
string_rep = ''
unit = ''
for i in xrange(len(values)):
string_rep += values.item(i).value
if (values.item(i).unit_string != ''):
unit = values.item(i).unit_string
#uh... not so sure about this one...!?!
return (eval("return " + string_rep + ";") + unit)
def floor(self, number):
number = eval("return floor(" + number + ");")
return number
def ceil(self, number):
number = eval("return ceil(" + number + ");")
return number
def parse(self, value):
value = value.strip()
if (value == ''): return
#/* maybe a constant? */
if self.parentObject:
constant_value = self.parentObject.getConstant(value)
if (constant_value):
value = constant_value
#/* maybe a variable? */
matches = preg_match(r'^(\$\w+)$', value)
if (matches): #// variable, look up in parent
if self.parentObject:
#pdb.set_trace()
var_value = self.parentObject.getVar(matches[0])
if (var_value):
value = var_value
else:
value = ''
matches = preg_match(r'^floor\((.*)\)$', value)
if (matches):
value = self.floor(self.evaluate(matches[0]))
matches = preg_match(r'^ceil\((.*)\)$', value)
if (matches):
value = self.ceil(self.evaluate(matches[0]))
matches = preg_match(r'^abs\((.*)\)$', value)
if (matches):
value = self.abs(self.evaluate(matches[0]))
matches = preg_match(r'^([-]?)\((.*)\)(%|em|ex|px|cm|mm}in|pt|pc){0,1}$', value)
if (matches):
computed_value = self.evaluate(matches[1])
if preg_match(r'(%|em|ex|px|cm|mm}in|pt|pc)$', computed_value, get_class(self)) and matches[1]:
value = preg_replace('(%|em|ex|px|cm|mm}in|pt|pc)$', matches[1], computed_value)
else:
value = matches[0] + computed_value + matches[1]
matches = preg_match(r'^prop\(([^\,]*)(\,[^\)]*)?\)$', value)
if (matches): #// prop()
#pdb.set_trace()
provided_prop_name = matches[0][0]
provided_selector = matches[0][1]
if not provided_selector: #// host object not defined, assuming self...
prop = self.parentObject.parentObject.getPropertyValue(provided_prop_name)
if (prop):
value = prop
else:
value = self.getPropertyValueBySelector(provided_prop_name, self.parentObject.parentObject.selectorText)
else: #// host object has been defined, use selector to find objects
value = self.getPropertyValueBySelector(provided_prop_name, trim(str_replace(',', '', provided_selector)))
self.unit[self.CSS_PERCENTAGE] = '%'
self.unit[self.CSS_EMS] = 'em'
self.unit[self.CSS_EXS] = 'ex'
self.unit[self.CSS_PX] = 'px'
self.unit[self.CSS_CM] = 'cm'
self.unit[self.CSS_MM] = 'mm'
self.unit[self.CSS_IN] = 'in'
self.unit[self.CSS_PT] = 'pt'
self.unit[self.CSS_PC] = 'pc'
#/* assuming unknown value */
#self.prop['value'] = value
#self.prop['primitiveValue'] = ''
#self.prop['valueType'] = self.CSS_UNKNOWN
matches = preg_match(r'^([-]?[0-9]+|[0-9]*\.[0-9]+)\%$', value)
if matches:
self.prop['value'] = matches[0]
self.prop['primitiveValue'] = self.unit[self.CSS_PERCENTAGE]
self.prop['valueType'] = self.CSS_PERCENTAGE
return
matches = preg_match(r'^([-]?[0-9]+|[0-9]*\.[0-9]+)em$', value)
if matches:
self.prop['value'] = matches[0]
self.prop['primitiveValue'] = self.unit[self.CSS_EMS]
self.prop['valueType'] = self.CSS_EMS
return
matches = preg_match(r'^([-]?[0-9]+|[0-9]*\.[0-9]+)ex$', value)
if matches:
self.prop['value'] = matches[0]
self.prop['primitiveValue'] = self.unit[self.CSS_EXS]
self.prop['valueType'] = self.CSS_EXS
return
matches = preg_match(r'^([-]?[0-9]+)px$', value)
if matches:
self.prop['value'] = matches[0]
self.prop['primitiveValue'] = self.unit[self.CSS_PX]
self.prop['valueType'] = self.CSS_PX
return
matches = preg_match(r'^([-]?[0-9]+|[0-9]*\.[0-9]+)cm$', value)
if matches:
self.prop['value'] = matches[0]
self.prop['primitiveValue'] = self.unit[self.CSS_CM]
self.prop['valueType'] = self.CSS_CM
return
matches = preg_match(r'^([-]?[0-9]+|[0-9]*\.[0-9]+)mm$', value)
if matches:
self.prop['value'] = matches[0]
self.prop['primitiveValue'] = self.unit[self.CSS_MM]
self.prop['valueType'] = self.CSS_MM
return
matches = preg_match(r'^([-]?[0-9]+|[0-9]*\.[0-9]+)in$', value)
if matches:
self.prop['value'] = matches[0]
self.prop['primitiveValue'] = self.unit[self.CSS_IN]
self.prop['valueType'] = self.CSS_IN
return
matches = preg_match(r'^([-]?[0-9]+|[0-9]*\.[0-9]+)pt$', value)
if matches:
self.prop['value'] = matches[0]
self.prop['primitiveValue'] = self.unit[self.CSS_PT]
self.prop['valueType'] = self.CSS_PT
return
matches = preg_match(r'^([-]?[0-9]+|[0-9]*\.[0-9]+)pc$', value)
if matches:
self.prop['value'] = matches[0]
self.prop['primitiveValue'] = self.unit[self.CSS_PC]
self.prop['valueType'] = self.CSS_PC
return
matches = preg_match(r'^([-]?' + self.num + r')$', value)
if matches:
self.prop['value'] = matches[0]
self.prop['primitiveValue'] = ''
self.prop['valueType'] = self.CSS_NUMBER
return
#---------------------------------------------------------#
matches = preg_match(r'^url\(.{0,}\)$', value)
if matches:
string = ''.join(matches)
if "'" in string or '"' in string:
string = string.replace("'", "")
string = string.replace('"', '')
self.prop['value'] = string[4:-1]
self.prop['primitiveValue'] = ''
self.prop['valueType'] = self.CSS_URI
return
matches = preg_match(r'^rgb\([\w\W]+?\)$', value)
if matches:
string = ''.join(matches)
string = str_replace('rgb(', '', string)
string = str_replace(')', '', string)
self.prop['value'] = string
self.prop['primitiveValue'] = ''
self.prop['valueType'] = self.CSS_RBGCOLOR
return
matches = preg_match(r'^\#[a-fA-F0-9]{3,6}$', value)
if matches:
self.prop['value'] = matches[0]
self.prop['primitiveValue'] = ''
self.prop['valueType'] = self.CSS_HEXCOLOR
return
matches = preg_match(r'^[\-|\+|\*|\/|\%]$', value)
if matches:
self.prop['value'] = matches[0]
self.prop['primitiveValue'] = ''
self.prop['valueType'] = self.CSS_OPERATOR
return
self.prop['value'] = value
self.prop['primitiveValue'] = ''
self.prop['valueType'] = self.CSS_STRING
def set_cssText(self, text):
self.parse(text)
def get_cssText(self):
return self.__str__()
cssText = property(get_cssText, set_cssText)
def get_primitiveType(self):
return self.prop['valueType']
primitiveType = property(get_primitiveType)
def get_valueType(self):
return self.prop['valueType']
valueType = property(get_valueType)
def get_value(self):
return self.prop['value']
value = property(get_value)
def get(var):
if (var == 'cssValueType'): return SSSValue.CSS_PRIMITIVE_VALUE
if (var == 'unit_string'):
return self.unit[self.prop['primitiveType']]
def __str__(self):
if (self.prop['valueType'] == self.CSS_URI):
return 'url('+ self.prop['value'] + ')'
return self.prop['value'] + self.prop['primitiveValue']
MongoDB Logo MongoDB