def compact(*args): """Returns a new list after removing any non-true values""" ret = {} if len(args) == 1: args = args[0] if isinstance(args, ListValue): args = args.value if isinstance(args, dict): for i, item in args.items(): if False if isinstance(item, basestring) and _undefined_re.match(item) else bool(item): ret[i] = item elif False if isinstance(args, basestring) and _undefined_re.match(args) else bool(args): ret[0] = args else: ret['_'] = ',' for i, item in enumerate(args): if False if isinstance(item, basestring) and _undefined_re.match(item) else bool(item): ret[i] = item if isinstance(args, ListValue): args = args.value if isinstance(args, dict): separator = args.get('_', None) if separator is not None: ret['_'] = separator return ListValue(ret)
def a_expr(self, R): m_expr = self.m_expr(R) v = m_expr while self._peek(self.a_expr_rsts) in self.a_expr_chks: _token_ = self._peek(self.a_expr_chks) if _token_ == 'ADD': ADD = self._scan('ADD') m_expr = self.m_expr(R) v = 'undefined' if isinstance(v, basestring) and _undefined_re.match(v) or isinstance(m_expr, basestring) and _undefined_re.match(m_expr) else (v + m_expr) else: # == 'SUB' SUB = self._scan('SUB') m_expr = self.m_expr(R) v = 'undefined' if isinstance(v, basestring) and _undefined_re.match(v) or isinstance(m_expr, basestring) and _undefined_re.match(m_expr) else (v - m_expr) return v
def m_expr(self, R): u_expr = self.u_expr(R) v = u_expr while self._peek(self.m_expr_rsts) in self.m_expr_chks: _token_ = self._peek(self.m_expr_chks) if _token_ == 'MUL': MUL = self._scan('MUL') u_expr = self.u_expr(R) v = 'undefined' if isinstance(v, basestring) and _undefined_re.match(v) or isinstance(u_expr, basestring) and _undefined_re.match(u_expr) else (v * u_expr) else: # == 'DIV' DIV = self._scan('DIV') u_expr = self.u_expr(R) v = 'undefined' if isinstance(v, basestring) and _undefined_re.match(v) or isinstance(u_expr, basestring) and _undefined_re.match(u_expr) else (v / u_expr) return v
def first(self): for v in self.values(): if isinstance(v, basestring) and _undefined_re.match(v): continue if bool(v): return v return v
def __init__(self, tokens, type=None): self.tokens = tokens self.units = {} if tokens is None: self.value = 0.0 elif isinstance(tokens, ParserValue): self.value = float(tokens.value) elif isinstance(tokens, NumberValue): self.value = tokens.value self.units = tokens.units.copy() if tokens.units: type = None elif isinstance(tokens, (StringValue, basestring)): tokens = getattr(tokens, 'value', tokens) if _undefined_re.match(tokens): raise ValueError("Value is not a Number! (%s)" % tokens) try: if tokens and tokens[-1] == '%': self.value = to_float(tokens[:-1]) / 100.0 self.units = {'%': _units_weights.get('%', 1), '_': '%'} else: self.value = to_float(tokens) except ValueError: raise ValueError("Value is not a Number! (%s)" % tokens) elif isinstance(tokens, (int, float)): self.value = float(tokens) else: raise ValueError("Can't convert to CSS number: %r" % tokens) if type is not None: self.units = {type: _units_weights.get(type, 1), '_': type}
def u_expr(self, R): _token_ = self._peek(self.u_expr_rsts) if _token_ == 'SIGN': SIGN = self._scan('SIGN') u_expr = self.u_expr(R) return 'undefined' if isinstance(u_expr, basestring) and _undefined_re.match(u_expr) else _inv('-', u_expr) elif _token_ == 'ADD': ADD = self._scan('ADD') u_expr = self.u_expr(R) return 'undefined' if isinstance(u_expr, basestring) and _undefined_re.match(u_expr) else u_expr else: # in self.u_expr_chks atom = self.atom(R) v = atom if self._peek(self.u_expr_rsts_) == 'UNITS': UNITS = self._scan('UNITS') v = call(UNITS, ListValue(ParserValue({0: v, 1: UNITS})), R, self._library, False) return v
def and_test(self, R): not_test = self.not_test(R) v = not_test while self._peek(self.and_test_rsts) == 'AND': AND = self._scan('AND') not_test = self.not_test(R) v = 'undefined' if isinstance(v, basestring) and _undefined_re.match(v) else (v and not_test) return v
def expr(self, R): and_test = self.and_test(R) v = and_test while self._peek(self.expr_rsts) == 'OR': OR = self._scan('OR') and_test = self.and_test(R) v = and_test if isinstance(v, basestring) and _undefined_re.match(v) else (v or and_test) return v
def not_test(self, R): _token_ = self._peek(self.not_test_rsts) if _token_ not in self.not_test_chks: comparison = self.comparison(R) return comparison else: # in self.not_test_chks while 1: _token_ = self._peek(self.not_test_chks) if _token_ == 'NOT': NOT = self._scan('NOT') not_test = self.not_test(R) v = 'undefined' if isinstance(not_test, basestring) and _undefined_re.match(not_test) else (not not_test) else: # == 'INV' INV = self._scan('INV') not_test = self.not_test(R) v = 'undefined' if isinstance(not_test, basestring) and _undefined_re.match(not_test) else _inv('!', not_test) if self._peek(self.not_test_rsts_) not in self.not_test_chks: break return v
def comparison(self, R): a_expr = self.a_expr(R) v = a_expr while self._peek(self.comparison_rsts) in self.comparison_chks: _token_ = self._peek(self.comparison_chks) if _token_ == 'LT': LT = self._scan('LT') a_expr = self.a_expr(R) v = 'undefined' if isinstance(v, basestring) and _undefined_re.match(v) or isinstance(a_expr, basestring) and _undefined_re.match(a_expr) else (v < a_expr) elif _token_ == 'GT': GT = self._scan('GT') a_expr = self.a_expr(R) v = 'undefined' if isinstance(v, basestring) and _undefined_re.match(v) or isinstance(a_expr, basestring) and _undefined_re.match(a_expr) else (v > a_expr) elif _token_ == 'LE': LE = self._scan('LE') a_expr = self.a_expr(R) v = 'undefined' if isinstance(v, basestring) and _undefined_re.match(v) or isinstance(a_expr, basestring) and _undefined_re.match(a_expr) else (v <= a_expr) elif _token_ == 'GE': GE = self._scan('GE') a_expr = self.a_expr(R) v = 'undefined' if isinstance(v, basestring) and _undefined_re.match(v) or isinstance(a_expr, basestring) and _undefined_re.match(a_expr) else (v >= a_expr) elif _token_ == 'EQ': EQ = self._scan('EQ') a_expr = self.a_expr(R) v = (None if isinstance(v, basestring) and _undefined_re.match(v) else v) == (None if isinstance(a_expr, basestring) and _undefined_re.match(a_expr) else a_expr) else: # == 'NE' NE = self._scan('NE') a_expr = self.a_expr(R) v = (None if isinstance(v, basestring) and _undefined_re.match(v) else v) != (None if isinstance(a_expr, basestring) and _undefined_re.match(a_expr) else a_expr) return v
def __init__(self, tokens): self.tokens = tokens self.value = (0, 0, 0, 1) self.types = {} if tokens is None: self.value = (0, 0, 0, 1) elif isinstance(tokens, ParserValue): hex = tokens.value self.value = self.HEX2RGBA[len(hex)](hex) self.types = {'rgba': 1} elif isinstance(tokens, ColorValue): self.value = tokens.value self.types = tokens.types.copy() elif isinstance(tokens, NumberValue): val = tokens.value self.value = (val, val, val, 1) elif isinstance(tokens, (list, tuple)): c = tokens[:4] r = 255.0, 255.0, 255.0, 1.0 c = [0.0 if c[i] < 0 else r[i] if c[i] > r[i] else c[i] for i in range(4)] self.value = tuple(c) type = tokens[-1] if type in ('rgb', 'rgba', 'hsl', 'hsla'): self.types = {type: 1} elif isinstance(tokens, (int, float)): val = float(tokens) self.value = (val, val, val, 1) else: if isinstance(tokens, StringValue): tokens = tokens.value tokens = to_str(tokens) tokens.replace(' ', '').lower() if _undefined_re.match(tokens): raise ValueError("Value is not a Color! (%s)" % tokens) try: self.value = self.HEX2RGBA[len(tokens)](tokens) except: try: val = to_float(tokens) self.value = (val, val, val, 1) except ValueError: try: type, _, colors = tokens.partition('(') colors = colors.rstrip(')') if type in ('rgb', 'rgba'): c = tuple(colors.split(',')) try: c = [to_float(c[i]) for i in range(4)] col = [0.0 if c[i] < 0 else 255.0 if c[i] > 255 else c[i] for i in range(3)] col += [0.0 if c[3] < 0 else 1.0 if c[3] > 1 else c[3]] self.value = tuple(col) self.types = {type: 1} except: raise ValueError("Value is not a Color! (%s)" % tokens) elif type in ('hsl', 'hsla'): c = colors.split(',') try: c = [to_float(c[i]) for i in range(4)] col = [c[0] % 360.0] / 360.0 col += [0.0 if c[i] < 0 else 1.0 if c[i] > 1 else c[i] for i in range(1, 4)] self.value = tuple([c * 255.0 for c in colorsys.hls_to_rgb(col[0], 0.999999 if col[2] == 1 else col[2], 0.999999 if col[1] == 1 else col[1])] + [col[3]]) self.types = {type: 1} except: raise ValueError("Value is not a Color! (%s)" % tokens) else: raise ValueError("Value is not a Color! (%s)" % tokens) except: raise ValueError("Value is not a Color! (%s)" % tokens)