home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- __all__ = [
- 'CSSSerializer',
- 'Preferences']
- __docformat__ = 'restructuredtext'
- __version__ = '$Id: serialize.py 1898 2009-12-19 12:17:04Z cthedot $'
- import codecs
- import cssutils
- import helper
- import re
- import xml.dom as xml
-
- def _escapecss(e):
- s = e.object[e.start:e.end]
- return ([]([ u'\\%s ' % str(hex(ord(x)))[2:].upper() for x in s ]), e.end)
-
- codecs.register_error('escapecss', _escapecss)
-
- class Preferences(object):
-
- def __init__(self, **initials):
- self.useDefaults()
- for key, value in initials.items():
- if value:
- self.__setattr__(key, value)
- continue
-
-
-
- def __repr__(self):
- return u', '.join % ([], []([ '\n %s=%r' % (p, self.__getattribute__(p)) for p in self.__dict__ ]))
-
-
- def __str__(self):
- return u' '.join % ([], []([ '%s=%r' % (p, self.__getattribute__(p)) for p in self.__dict__ ]), id(self))
-
-
- def useDefaults(self):
- self.defaultAtKeyword = True
- self.defaultPropertyName = True
- self.defaultPropertyPriority = True
- self.importHrefFormat = None
- self.indent = u' '
- self.indentSpecificities = False
- self.keepAllProperties = True
- self.keepComments = True
- self.keepEmptyRules = False
- self.keepUnkownAtRules = True
- self.keepUsedNamespaceRulesOnly = False
- self.lineNumbers = False
- self.lineSeparator = u'\n'
- self.listItemSpacer = u' '
- self.omitLastSemicolon = True
- self.paranthesisSpacer = u' '
- self.propertyNameSpacer = u' '
- self.selectorCombinatorSpacer = u' '
- self.spacer = u' '
- self.validOnly = False
-
-
- def useMinified(self):
- self.importHrefFormat = 'string'
- self.indent = u''
- self.keepComments = False
- self.keepEmptyRules = False
- self.keepUnkownAtRules = False
- self.keepUsedNamespaceRulesOnly = True
- self.lineNumbers = False
- self.lineSeparator = u''
- self.listItemSpacer = u''
- self.omitLastSemicolon = True
- self.paranthesisSpacer = u''
- self.propertyNameSpacer = u''
- self.selectorCombinatorSpacer = u''
- self.spacer = u''
- self.validOnly = False
-
-
-
- class Out(object):
-
- def __init__(self, ser):
- self.ser = ser
- self.out = []
-
-
- def _remove_last_if_S(self):
- if self.out and not self.out[-1].strip():
- del self.out[-1]
-
-
-
- def append(self, val, typ = None, space = True, keepS = False, indent = False, lineSeparator = False):
- prefspace = self.ser.prefs.spacer
- if val or typ in ('STRING', 'URI'):
- if 'COMMENT' == typ:
- if self.ser.prefs.keepComments:
- val = val.cssText
- else:
- return None
- self.ser.prefs.keepComments
- if hasattr(val, 'cssText'):
- val = val.cssText
- elif 'S' == typ and not keepS:
- return None
- if 'S' == typ and keepS:
- val = u' '
- elif typ in ('NUMBER', 'DIMENSION', 'PERCENTAGE') and val == '0':
- if self.out and self.out[-1] in u'+-':
- del self.out[-1]
-
- elif 'STRING' == typ:
- if val is None:
- return None
- val = helper.string(val)
- if not prefspace:
- self._remove_last_if_S()
-
- elif 'URI' == typ:
- val = helper.uri(val)
- elif 'HASH' == typ:
- val = self.ser._hash(val)
- elif val in u'+>~,:{;)]/':
- self._remove_last_if_S()
-
- if indent:
- self.out.append(self.ser._indentblock(val, self.ser._level + 1))
- elif val.endswith(u' '):
- self._remove_last_if_S()
-
- self.out.append(val)
- if lineSeparator:
- pass
- elif val in u'+>~':
- self.out.insert(-1, self.ser.prefs.selectorCombinatorSpacer)
- self.out.append(self.ser.prefs.selectorCombinatorSpacer)
- elif u')' == val and not keepS:
- self.out.append(u' ')
- elif u',' == val:
- self.out.append(self.ser.prefs.listItemSpacer)
- elif u':' == val:
- self.out.append(self.ser.prefs.propertyNameSpacer)
- elif u'{' == val:
- self.out.insert(-1, self.ser.prefs.paranthesisSpacer)
- self.out.append(self.ser.prefs.lineSeparator)
- elif u';' == val:
- self.out.append(self.ser.prefs.lineSeparator)
- elif val not in u'}[]()/' and typ != 'FUNCTION' and space:
- self.out.append(self.ser.prefs.spacer)
- if typ != 'STRING' and not (self.ser.prefs.spacer) and self.out and not self.out[-1].endswith(u' '):
- self.out.append(u' ')
-
-
-
-
-
- def value(self, delim = u'', end = None, keepS = False):
- if not keepS:
- self._remove_last_if_S()
-
- if end:
- self.out.append(end)
-
- return delim.join(self.out)
-
-
-
- class CSSSerializer(object):
-
- def __init__(self, prefs = None):
- if not prefs:
- prefs = Preferences()
-
- self.prefs = prefs
- self._level = 0
- self._selectors = []
- self._selectorlevel = 0
-
-
- def _atkeyword(self, rule, default):
- if self.prefs.defaultAtKeyword:
- return default
- return rule.atkeyword
-
-
- def _indentblock(self, text, level):
- if not self.prefs.lineSeparator:
- return text
- return []([ u'%s%s' % (level * self.prefs.indent, line) for line in text.split(self.prefs.lineSeparator) ])
-
-
- def _propertyname(self, property, actual):
- if self.prefs.defaultPropertyName and not (self.prefs.keepAllProperties):
- return property.name
- return actual
-
-
- def _linenumnbers(self, text):
- if self.prefs.lineNumbers:
- pad = len(str(text.count(self.prefs.lineSeparator) + 1))
- out = []
- for i, line in enumerate(text.split(self.prefs.lineSeparator)):
- out.append(u'%*i: %s' % (pad, i + 1, line))
-
- text = self.prefs.lineSeparator.join(out)
-
- return text
-
-
- def _hash(self, val, type_ = None):
- if len(val) == 7 and val[1] == val[2] and val[3] == val[4] and val[5] == val[6]:
- return u'#%s%s%s' % (val[1], val[3], val[5])
- return val
-
-
- def _valid(self, x):
- if not (self.prefs.validOnly) and self.prefs.validOnly:
- pass
- return x.valid
-
-
- def do_CSSStyleSheet(self, stylesheet):
- useduris = stylesheet._getUsedURIs()
- out = []
- for rule in stylesheet.cssRules:
- if self.prefs.keepUsedNamespaceRulesOnly and rule.NAMESPACE_RULE == rule.type and rule.namespaceURI not in useduris:
- if rule.prefix or None not in useduris:
- continue
-
- cssText = rule.cssText
- if cssText:
- out.append(cssText)
- continue
-
- text = self._linenumnbers(self.prefs.lineSeparator.join(out))
-
- try:
- encoding = stylesheet.cssRules[0].encoding
- except (IndexError, AttributeError):
- encoding = 'UTF-8'
-
- return text.encode(encoding, 'escapecss')
-
-
- def do_CSSComment(self, rule):
- if rule._cssText and self.prefs.keepComments:
- return rule._cssText
- return u''
-
-
- def do_CSSCharsetRule(self, rule):
- if rule.wellformed:
- return u'@charset %s;' % helper.string(rule.encoding)
- return u''
-
-
- def do_CSSVariablesRule(self, rule):
- variablesText = rule.variables.cssText
- if variablesText and rule.wellformed:
- out = Out(self)
- out.append(self._atkeyword(rule, u'@variables'))
- for item in rule.seq:
- out.append(item.value, item.type)
-
- out.append(u'{')
- out.append(u'%s%s}' % (variablesText, self.prefs.lineSeparator), indent = 1)
- return out.value()
- return u''
-
-
- def do_CSSFontFaceRule(self, rule):
- styleText = self.do_css_CSSStyleDeclaration(rule.style)
- if styleText and rule.wellformed:
- out = Out(self)
- out.append(self._atkeyword(rule, u'@font-face'))
- for item in rule.seq:
- out.append(item.value, item.type)
-
- out.append(u'{')
- out.append(u'%s%s}' % (styleText, self.prefs.lineSeparator), indent = 1)
- return out.value()
- return u''
-
-
- def do_CSSImportRule(self, rule):
- if rule.wellformed:
- out = Out(self)
- out.append(self._atkeyword(rule, u'@import'))
- for item in rule.seq:
- typ = item.type
- val = item.value
- if 'href' == typ:
- if (self.prefs.importHrefFormat == 'string' or self.prefs.importHrefFormat != 'uri') and rule.hreftype == 'string':
- out.append(val, 'STRING')
- else:
- out.append(val, 'URI')
- rule.hreftype == 'string'
- if 'media' == typ:
- mediaText = self.do_stylesheets_medialist(val)
- if mediaText and mediaText != u'all':
- out.append(mediaText)
-
- mediaText != u'all'
- if 'name' == typ:
- out.append(val, 'STRING')
- continue
- out.append(val, typ)
-
- return out.value(end = u';')
- return u''
-
-
- def do_CSSNamespaceRule(self, rule):
- if rule.wellformed:
- out = Out(self)
- out.append(self._atkeyword(rule, u'@namespace'))
- for item in rule.seq:
- typ = item.type
- val = item.value
- if 'namespaceURI' == typ:
- out.append(val, 'STRING')
- continue
- out.append(val, typ)
-
- return out.value(end = u';')
- return u''
-
-
- def do_CSSMediaRule(self, rule):
- if not rule.media.wellformed:
- return u''
- out = [
- self._atkeyword(rule, u'@media')]
- if not len(self.prefs.spacer):
- out.append(u' ')
- else:
- out.append(self.prefs.spacer)
- out.append(self.do_stylesheets_medialist(rule.media))
- if rule.name:
- out.append(self.prefs.spacer)
- nameout = Out(self)
- nameout.append(helper.string(rule.name))
- for item in rule.seq:
- nameout.append(item.value, item.type)
-
- out.append(nameout.value())
-
- out.append(self.prefs.paranthesisSpacer)
- out.append(u'{')
- out.append(self.prefs.lineSeparator)
- rulesout = []
- for r in rule.cssRules:
- rtext = r.cssText
- if rtext:
- rulesout.append(self._indentblock(rtext, self._level + 1))
- rulesout.append(self.prefs.lineSeparator)
- continue
-
- if not (self.prefs.keepEmptyRules) and not u''.join(rulesout).strip():
- return u''
- out.extend(rulesout)
- out.append(u'%s}' % (self._level + 1) * self.prefs.indent)
- return u''.join(out)
-
-
- def do_CSSPageRule(self, rule):
- styleText = self.do_css_CSSStyleDeclaration(rule.style)
- if styleText and rule.wellformed:
- out = Out(self)
- out.append(self._atkeyword(rule, u'@page'))
- out.append(rule.selectorText)
- out.append(u'{')
- out.append(u'%s%s}' % (styleText, self.prefs.lineSeparator), indent = 1)
- return out.value()
- return u''
-
-
- def do_CSSPageRuleSelector(self, seq):
- out = Out(self)
- for item in seq:
- if item.type == 'IDENT':
- out.append(item.value, item.type, space = False)
- continue
- out.append(item.value, item.type)
-
- return out.value()
-
-
- def do_CSSUnknownRule(self, rule):
- if rule.wellformed and self.prefs.keepUnkownAtRules:
- out = Out(self)
- out.append(rule.atkeyword)
- stacks = []
- for item in rule.seq:
- typ = item.type
- val = item.value
- if u'}' == val:
- stackblock = stacks.pop().value()
- if stackblock:
- val = self._indentblock(stackblock + self.prefs.lineSeparator + val, min(1, len(stacks) + 1))
- else:
- val = self._indentblock(val, min(1, len(stacks) + 1))
-
- if stacks:
- stacks[-1].append(val, typ)
- else:
- out.append(val, typ)
- if u'{' == val:
- stacks.append(Out(self))
- continue
-
- return out.value()
- return u''
-
-
- def do_CSSStyleRule(self, rule):
- selectorText = self.do_css_SelectorList(rule.selectorList)
- if not selectorText or not (rule.wellformed):
- return u''
- self._level += 1
- styleText = u''
-
- try:
- styleText = self.do_css_CSSStyleDeclaration(rule.style)
- finally:
- self._level -= 1
-
- if not styleText:
- if self.prefs.keepEmptyRules:
- return u'%s%s{}' % (selectorText, self.prefs.paranthesisSpacer)
- else:
- return self._indentblock(u'%s%s{%s%s%s%s}' % (selectorText, self.prefs.paranthesisSpacer, self.prefs.lineSeparator, self._indentblock(styleText, self._level + 1), self.prefs.lineSeparator, (self._level + 1) * self.prefs.indent), self._selectorlevel)
- return self.prefs.keepEmptyRules
-
-
- def do_css_SelectorList(self, selectorlist):
- if selectorlist.wellformed:
- out = []
- for part in selectorlist.seq:
- if isinstance(part, cssutils.css.Selector):
- out.append(part.selectorText)
- continue
- out.append(part)
-
- sep = u',%s' % self.prefs.listItemSpacer
- return sep.join(out)
- return u''
-
-
- def do_css_Selector(self, selector):
- if selector.wellformed:
- out = Out(self)
- DEFAULTURI = selector._namespaces.get('', None)
- for item in selector.seq:
- typ = item.type
- val = item.value
- if type(val) == tuple:
- (namespaceURI, name) = val
- if (DEFAULTURI == namespaceURI or not DEFAULTURI) and namespaceURI is None:
- out.append(name, typ, space = False)
- elif namespaceURI == cssutils._ANYNS:
- prefix = u'*'
- else:
-
- try:
- prefix = selector._namespaces.prefixForNamespaceURI(namespaceURI)
- except IndexError:
- prefix = u''
-
- out.append(u'%s|%s' % (prefix, name), typ, space = False)
- continue
- out.append(val, typ, space = False, keepS = True)
-
- return out.value()
- return u''
-
-
- def do_css_CSSVariablesDeclaration(self, variables):
- if len(variables.seq) > 0:
- out = Out(self)
- lastitem = len(variables.seq) - 1
- for i, item in enumerate(variables.seq):
- type_ = item.type
- val = item.value
- if u'var' == type_:
- (name, cssvalue) = val
- out.append(name)
- out.append(u':')
- out.append(cssvalue.cssText)
- if i < lastitem or not (self.prefs.omitLastSemicolon):
- out.append(u';')
-
- not (self.prefs.omitLastSemicolon)
- if isinstance(val, cssutils.css.CSSComment):
- out.append(val, 'COMMENT')
- out.append(self.prefs.lineSeparator)
- continue
- out.append(val.cssText, type_)
- out.append(self.prefs.lineSeparator)
-
- return out.value().strip()
- return u''
-
-
- def do_css_CSSStyleDeclaration(self, style, separator = None):
- if len(style.seq) > 0:
- if separator is None:
- separator = self.prefs.lineSeparator
-
- out = []
- for i, item in enumerate(seq):
- typ = item.type
- val = item.value
- if isinstance(val, cssutils.css.CSSComment):
- if self.prefs.keepComments:
- out.append(val.cssText)
- out.append(separator)
-
- self.prefs.keepComments
- if isinstance(val, cssutils.css.Property):
- if val.cssText:
- out.append(val.cssText)
- if not self.prefs.omitLastSemicolon and i == len(seq) - 1:
- out.append(u';')
-
- out.append(separator)
-
- val.cssText
- if isinstance(val, cssutils.css.CSSUnknownRule):
- out.append(val.cssText)
- out.append(separator)
- continue
- [] if self.prefs.keepAllProperties else []
- out.append(val)
- out.append(separator)
-
- if out and out[-1] == separator:
- del out[-1]
-
- return u''.join(out)
- return u''
-
-
- def do_Property(self, property):
- out = []
- if property.seqs[0] and property.wellformed and self._valid(property):
- (nameseq, cssvalue, priorityseq) = property.seqs
- for part in nameseq:
- if hasattr(part, 'cssText'):
- out.append(part.cssText)
- continue
- if property.literalname == part:
- out.append(self._propertyname(property, part))
- continue
- out.append(part)
-
- if out:
- if (not (property._mediaQuery) or property._mediaQuery) and cssvalue.cssText:
- out.append(u':')
- out.append(self.prefs.propertyNameSpacer)
-
- out.append(cssvalue.cssText)
- if out and priorityseq:
- out.append(u' ')
- for part in priorityseq:
- if hasattr(part, 'cssText'):
- out.append(part.cssText)
- continue
- if part == property.literalpriority and self.prefs.defaultPropertyPriority:
- out.append(property.priority)
- continue
- out.append(part)
-
-
-
- return u''.join(out)
-
-
- def do_Property_priority(self, priorityseq):
- out = []
- for part in priorityseq:
- if hasattr(part, 'cssText'):
- out.append(u' ')
- out.append(part.cssText)
- out.append(u' ')
- continue
- out.append(part)
-
- return u''.join(out).strip()
-
-
- def do_css_CSSValue(self, cssvalue):
- if not cssvalue:
- return u''
- out = Out(self)
- for item in cssvalue.seq:
- type_ = item.type
- val = item.value
- if hasattr(val, 'cssText'):
- out.append(val.cssText, type_)
- continue
- cssvalue
- if val and val[0] == val[-1] and val[0] in '\'"':
- val = helper.string(val[1:-1])
-
- out.append(val, type_)
-
- return out.value()
-
-
- def do_css_CSSPrimitiveValue(self, cssvalue):
- if not cssvalue:
- return u''
- out = Out(self)
- for item in cssvalue.seq:
- type_ = item.type
- val = item.value
- if type_ in ('DIMENSION', 'NUMBER', 'PERCENTAGE'):
- (n, d) = cssvalue._getNumDim(val)
- if 0 == n:
- if cssvalue.primitiveType in cssvalue._lengthtypes:
- val = u'0'
- else:
- val = u'0' + d
- else:
- val = unicode(n) + d
-
- out.append(val, type_)
-
- return out.value()
-
-
- def do_css_CSSVariable(self, variable):
- if not variable:
- return u''
- out = Out(self)
- for item in variable.seq:
- type_ = item.type
- val = item.value
- out.append(val, type_)
-
- return out.value()
-
-
- def do_css_RGBColor(self, cssvalue):
- if not cssvalue:
- return u''
- out = Out(self)
- unary = None
- for item in cssvalue.seq:
- type_ = item.type
- val = item.value
- out.append(val, type_)
-
- return out.value()
-
-
- def do_stylesheets_medialist(self, medialist):
- if len(medialist) == 0:
- return u'all'
- sep = u',%s' % self.prefs.listItemSpacer
- return sep.join((lambda .0: for mq in .0:
- mq.mediaText)(medialist))
-
-
- def do_stylesheets_mediaquery(self, mediaquery):
- if mediaquery.wellformed:
- out = []
- for part in mediaquery.seq:
- if isinstance(part, cssutils.css.Property):
- out.append(u'(%s)' % part.cssText)
- continue
- if hasattr(part, 'cssText'):
- out.append(part.cssText)
- continue
- out.append(part)
-
- return u' '.join(out)
- return u''
-
-
- def do_css_ExpressionValue(self, cssvalue):
- if not cssvalue:
- return u''
- out = Out(self)
- for item in cssvalue.seq:
- type_ = item.type
- val = item.value
- if type_ in ('DIMENSION', 'NUMBER', 'PERCENTAGE'):
- (n, d) = cssvalue._getNumDim(val)
- if 0 == n:
- if cssvalue.primitiveType in cssvalue._lengthtypes:
- val = u'0'
- else:
- val = u'0' + d
- else:
- val = unicode(n) + d
-
- out.append(val, None, space = False)
-
- return out.value()
-
-
-