def mediaquery(): return Prod(name='MediaQueryStart', match=lambda t, v: t == 'IDENT' or v == '(', toSeq=lambda t, tokens: ('MediaQuery', MediaQuery(pushtoken(t, tokens), _partof=True)) ) prods = Sequence(Sequence(PreDef.comment(parent=self),
def mediaquery(): return Prod(name='MediaQueryStart', match=lambda t, v: t == 'IDENT' or v == '(', toSeq=lambda t, tokens: ('MediaQuery', MediaQuery(pushtoken(t, tokens), _partof = True)) ) prods = Sequence(Sequence(PreDef.comment(parent=self),
def _productions(self): """Return definition used for parsing.""" types = self._prods # rename! func = Prod(name='MSValue-Sub', match=lambda t, v: t == self._prods.FUNCTION, toSeq=lambda t, tokens: (MSValue._functionName, MSValue(pushtoken(t, tokens), parent=self))) funcProds = Sequence( Prod(name='FUNCTION', match=lambda t, v: t == types.FUNCTION, toSeq=lambda t, tokens: (t[0], t[1])), Sequence( Choice( _ColorProd(self), _DimensionProd(self), _URIProd(self), _ValueProd(self), _MSValueProd(self), # _CalcValueProd(self), _CSSVariableProd(self), func, # _CSSFunctionProd(self), Prod(name='MSValuePart', match=lambda t, v: v != ')', toSeq=lambda t, tokens: (t[0], t[1]))), minmax=lambda: (0, None)), PreDef.funcEnd(stop=True)) return funcProds
def _URIProd(parent, nextSor=False, toStore=None): return Prod(name='URIValue', match=lambda t, v: t == 'URI', toStore=toStore, nextSor=nextSor, toSeq=lambda t, tokens: ('URIValue', URIValue(pushtoken(t, tokens), parent=parent)))
def _ValueProd(parent, nextSor=False, toStore=None): return Prod(name='Value', match=lambda t, v: t in ('IDENT', 'STRING', 'UNICODE-RANGE'), nextSor=nextSor, toStore=toStore, toSeq=lambda t, tokens: ('Value', Value(pushtoken(t, tokens), parent=parent)))
def _MSValueProd(parent, nextSor=False): return Prod(name=MSValue._functionName, match=lambda t, v: ( # t == self._prods.FUNCTION and ( normalize(v) in ('expression(', 'alpha(', 'blur(', 'chroma(', 'dropshadow(', 'fliph(', 'flipv(', 'glow(', 'gray(', 'invert(', 'mask(', 'shadow(', 'wave(', 'xray(') or v.startswith('progid:DXImageTransform.Microsoft.') ), nextSor=nextSor, toSeq=lambda t, tokens: (MSValue._functionName, MSValue(pushtoken(t, tokens ), parent=parent ) ) )
def _DimensionProd(parent, nextSor=False, toStore=None): return Prod( name='Dimension', match=lambda t, v: t in ('DIMENSION', 'NUMBER', 'PERCENTAGE'), nextSor=nextSor, toStore=toStore, toSeq=lambda t, tokens: ('DIMENSION', DimensionValue(pushtoken(t, tokens), parent=parent)))
def _CalcValueProd(parent, nextSor=False, toStore=None): return Prod( name=CSSCalc._functionName, match=lambda t, v: t == PreDef.types.FUNCTION and normalize( v) == 'calc(', toStore=toStore, toSeq=lambda t, tokens: (CSSCalc._functionName, CSSCalc(pushtoken(t, tokens), parent=parent)), nextSor=nextSor)
def _CSSVariableProd(parent, nextSor=False, toStore=None): return PreDef.variable(nextSor=nextSor, toStore=toStore, toSeq=lambda t, tokens: (CSSVariable._functionName, CSSVariable( pushtoken(t, tokens), parent=parent) ) )
def _ValueProd(parent, nextSor=False, toStore=None): return Prod(name='Value', match=lambda t, v: t in ('IDENT', 'STRING', 'UNICODE-RANGE'), nextSor=nextSor, toStore=toStore, toSeq=lambda t, tokens: ('Value', Value(pushtoken(t, tokens), parent=parent) ) )
def _CalcValueProd(parent, nextSor=False, toStore=None): return Prod(name=CSSCalc._functionName, match=lambda t, v: t == PreDef.types.FUNCTION and normalize(v) == 'calc(', toStore=toStore, toSeq=lambda t, tokens: (CSSCalc._functionName, CSSCalc( pushtoken(t, tokens), parent=parent) ), nextSor=nextSor)
def _URIProd(parent, nextSor=False, toStore=None): return Prod(name='URIValue', match=lambda t, v: t == 'URI', toStore=toStore, nextSor=nextSor, toSeq=lambda t, tokens: ('URIValue', URIValue( pushtoken(t, tokens), parent=parent) ) )
def _ColorProd(parent, nextSor=False, toStore=None): return Prod( name='ColorValue', match=lambda t, v: (t == 'HASH' and reHexcolor.match(v)) or (t == 'FUNCTION' and normalize(v) in ('rgb(', 'rgba(', 'hsl(', 'hsla(')) or (t == 'IDENT' and normalize(v) in as_list(ColorValue.COLORS.keys())), nextSor=nextSor, toStore=toStore, toSeq=lambda t, tokens: ('ColorValue', ColorValue(pushtoken(t, tokens), parent=parent)))
def _DimensionProd(parent, nextSor=False, toStore=None): return Prod(name='Dimension', match=lambda t, v: t in ('DIMENSION', 'NUMBER', 'PERCENTAGE'), nextSor=nextSor, toStore=toStore, toSeq=lambda t, tokens: ('DIMENSION', DimensionValue( pushtoken(t, tokens), parent=parent) ) )
def _productions(self): """Return definition used for parsing.""" types = self._prods # rename! func = Prod(name='MSValue-Sub', match=lambda t, v: t == self._prods.FUNCTION, toSeq=lambda t, tokens: (MSValue._functionName, MSValue(pushtoken(t, tokens ), parent=self ) ) ) funcProds = Sequence(Prod(name='FUNCTION', match=lambda t, v: t == types.FUNCTION, toSeq=lambda t, tokens: (t[0], t[1]) ), Sequence(Choice(_ColorProd(self), _DimensionProd(self), _URIProd(self), _ValueProd(self), _MSValueProd(self), # _CalcValueProd(self), _CSSVariableProd(self), func, # _CSSFunctionProd(self), Prod(name='MSValuePart', match=lambda t, v: v != ')', toSeq=lambda t, tokens: (t[0], t[1]) ) ), minmax=lambda: (0, None) ), PreDef.funcEnd(stop=True) ) return funcProds
def _ColorProd(parent, nextSor=False, toStore=None): return Prod(name='ColorValue', match=lambda t, v: (t == 'HASH' and reHexcolor.match(v) ) or (t == 'FUNCTION' and normalize(v) in ('rgb(', 'rgba(', 'hsl(', 'hsla(') ) or (t == 'IDENT' and normalize(v) in as_list(ColorValue.COLORS.keys()) ), nextSor=nextSor, toStore=toStore, toSeq=lambda t, tokens: ('ColorValue', ColorValue( pushtoken(t, tokens), parent=parent) ) )
def mediaquery(): return Prod( name='MediaQueryStart', match=lambda t, v: t == 'IDENT' or v == '(', toSeq=lambda t, tokens: ('MediaQuery', MediaQuery(pushtoken(t, tokens), _partof=True)))
def _setCssText(self, cssText): self._checkReadonly() types = self._prods # rename! component = Choice(PreDef.unary(toSeq=lambda t, tokens: (t[0], DimensionValue(pushtoken(t, tokens), parent=self) )), PreDef.number(toSeq=lambda t, tokens: (t[0], DimensionValue(pushtoken(t, tokens), parent=self) )), PreDef.percentage(toSeq=lambda t, tokens: (t[0], DimensionValue(pushtoken(t, tokens), parent=self) )) ) noalp = Sequence(Prod(name='FUNCTION', match=lambda t, v: t == types.FUNCTION and v.lower() in ('rgb(', 'hsl('), toSeq=lambda t, tokens: (t[0], normalize(t[1]))), component, Sequence(PreDef.comma(optional=True), component, minmax=lambda: (2, 2) ), PreDef.funcEnd(stop=True) ) witha = Sequence(Prod(name='FUNCTION', match=lambda t, v: t == types.FUNCTION and v.lower() in ('rgba(', 'hsla('), toSeq=lambda t, tokens: (t[0], normalize(t[1])) ), component, Sequence(PreDef.comma(optional=True), component, minmax=lambda: (3, 3) ), PreDef.funcEnd(stop=True) ) namedcolor = Prod(name='Named Color', match=lambda t, v: t == 'IDENT' and ( normalize(v) in as_list(self.COLORS.keys()) ), stop=True) prods = Choice(PreDef.hexcolor(stop=True), namedcolor, noalp, witha) ok, seq, store, unused = ProdParser().parse(cssText, self.type, prods) self.wellformed = ok if ok: t, v = seq[0].type, seq[0].value if 'IDENT' == t: rgba = self.COLORS[normalize(v)] if 'HASH' == t: if len(v) == 4: # HASH #rgb rgba = (int(2*v[1], 16), int(2*v[2], 16), int(2*v[3], 16), 1.0) else: # HASH #rrggbb rgba = (int(v[1:3], 16), int(v[3:5], 16), int(v[5:7], 16), 1.0) elif 'FUNCTION' == t: functiontype, raw, check = None, [], '' HSL = False for item in seq: try: type_ = item.value.type except AttributeError: # type of function, e.g. rgb( if item.type == 'FUNCTION': functiontype = item.value HSL = functiontype in ('hsl(', 'hsla(') continue # save components if type_ == Value.NUMBER: raw.append(item.value.value) check += 'N' elif type_ == Value.PERCENTAGE: if HSL: # save as percentage fraction raw.append(item.value.value / 100.0) else: # save as real value of percentage of 255 raw.append(int(255 * item.value.value / 100)) check += 'P' if HSL: # convert to rgb # h is 360 based (circle) h, s, l_ = raw[0] / 360.0, raw[1], raw[2] # ORDER h l_ s !!! r, g, b = colorsys.hls_to_rgb(h, l_, s) # back to 255 based rgba = [int(round(r*255)), int(round(g*255)), int(round(b*255))] if len(raw) > 3: rgba.append(raw[3]) else: # rgb, rgba rgba = raw if len(rgba) < 4: rgba.append(1.0) # validate checks = {'rgb(': ('NNN', 'PPP'), 'rgba(': ('NNNN', 'PPPN'), 'hsl(': ('NPP',), 'hsla(': ('NPPN',) } if check not in checks[functiontype]: self._log.error('ColorValue has invalid %s) parameters: ' '%s (N=Number, P=Percentage)' % (functiontype, check)) self._colorType = t self._red, self._green, self._blue, self._alpha = tuple(rgba) self._setSeq(seq)
def _setCssText(self, cssText): self._checkReadonly() types = self._prods # rename! component = Choice( PreDef.unary(toSeq=lambda t, tokens: (t[ 0], DimensionValue(pushtoken(t, tokens), parent=self))), PreDef.number(toSeq=lambda t, tokens: (t[ 0], DimensionValue(pushtoken(t, tokens), parent=self))), PreDef.percentage(toSeq=lambda t, tokens: (t[ 0], DimensionValue(pushtoken(t, tokens), parent=self)))) noalp = Sequence( Prod(name='FUNCTION', match=lambda t, v: t == types.FUNCTION and v.lower() in ('rgb(', 'hsl('), toSeq=lambda t, tokens: (t[0], normalize(t[1]))), component, Sequence(PreDef.comma(optional=True), component, minmax=lambda: (2, 2)), PreDef.funcEnd(stop=True)) witha = Sequence( Prod(name='FUNCTION', match=lambda t, v: t == types.FUNCTION and v.lower() in ('rgba(', 'hsla('), toSeq=lambda t, tokens: (t[0], normalize(t[1]))), component, Sequence(PreDef.comma(optional=True), component, minmax=lambda: (3, 3)), PreDef.funcEnd(stop=True)) namedcolor = Prod(name='Named Color', match=lambda t, v: t == 'IDENT' and (normalize(v) in as_list(self.COLORS.keys())), stop=True) prods = Choice(PreDef.hexcolor(stop=True), namedcolor, noalp, witha) ok, seq, store, unused = ProdParser().parse(cssText, self.type, prods) self.wellformed = ok if ok: t, v = seq[0].type, seq[0].value if 'IDENT' == t: rgba = self.COLORS[normalize(v)] if 'HASH' == t: if len(v) == 4: # HASH #rgb rgba = (int(2 * v[1], 16), int(2 * v[2], 16), int(2 * v[3], 16), 1.0) else: # HASH #rrggbb rgba = (int(v[1:3], 16), int(v[3:5], 16), int(v[5:7], 16), 1.0) elif 'FUNCTION' == t: functiontype, raw, check = None, [], '' HSL = False for item in seq: try: type_ = item.value.type except AttributeError: # type of function, e.g. rgb( if item.type == 'FUNCTION': functiontype = item.value HSL = functiontype in ('hsl(', 'hsla(') continue # save components if type_ == Value.NUMBER: raw.append(item.value.value) check += 'N' elif type_ == Value.PERCENTAGE: if HSL: # save as percentage fraction raw.append(item.value.value / 100.0) else: # save as real value of percentage of 255 raw.append(int(255 * item.value.value / 100)) check += 'P' if HSL: # convert to rgb # h is 360 based (circle) h, s, l_ = raw[0] / 360.0, raw[1], raw[2] # ORDER h l_ s !!! r, g, b = colorsys.hls_to_rgb(h, l_, s) # back to 255 based rgba = [ int(round(r * 255)), int(round(g * 255)), int(round(b * 255)) ] if len(raw) > 3: rgba.append(raw[3]) else: # rgb, rgba rgba = raw if len(rgba) < 4: rgba.append(1.0) # validate checks = { 'rgb(': ('NNN', 'PPP'), 'rgba(': ('NNNN', 'PPPN'), 'hsl(': ('NPP', ), 'hsla(': ('NPPN', ) } if check not in checks[functiontype]: self._log.error('ColorValue has invalid %s) parameters: ' '%s (N=Number, P=Percentage)' % (functiontype, check)) self._colorType = t self._red, self._green, self._blue, self._alpha = tuple(rgba) self._setSeq(seq)