home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1657 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  18.7 KB  |  495 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __all__ = [
  5.     'Selector']
  6. __docformat__ = 'restructuredtext'
  7. __version__ = '$Id: selector.py 1868 2009-10-17 19:36:54Z cthedot $'
  8. from cssutils.util import _SimpleNamespaces
  9. from cssutils.helper import Deprecated
  10. import cssutils
  11. import xml.dom as xml
  12.  
  13. class Selector(cssutils.util.Base2):
  14.     
  15.     def __init__(self, selectorText = None, parent = None, readonly = False):
  16.         super(Selector, self).__init__()
  17.         self._Selector__namespaces = _SimpleNamespaces(log = self._log)
  18.         self._element = None
  19.         self._parent = parent
  20.         self._specificity = (0, 0, 0, 0)
  21.         if selectorText:
  22.             self.selectorText = selectorText
  23.         
  24.         self._readonly = readonly
  25.  
  26.     
  27.     def __repr__(self):
  28.         if self._Selector__getNamespaces():
  29.             st = (self.selectorText, self._getUsedNamespaces())
  30.         else:
  31.             st = self.selectorText
  32.         return u'cssutils.css.%s(selectorText=%r)' % (self.__class__.__name__, st)
  33.  
  34.     
  35.     def __str__(self):
  36.         return u'<cssutils.css.%s object selectorText=%r specificity=%r _namespaces=%r at 0x%x>' % (self.__class__.__name__, self.selectorText, self.specificity, self._getUsedNamespaces(), id(self))
  37.  
  38.     
  39.     def _getUsedUris(self):
  40.         uris = set()
  41.         for item in self.seq:
  42.             type_ = item.type
  43.             val = item.value
  44.             if (type_.endswith(u'-selector') or type_ == u'universal') and type(val) == tuple and val[0] not in (None, u'*'):
  45.                 uris.add(val[0])
  46.                 continue
  47.         
  48.         return uris
  49.  
  50.     
  51.     def _getUsedNamespaces(self):
  52.         useduris = self._getUsedUris()
  53.         namespaces = _SimpleNamespaces(log = self._log)
  54.         for p, uri in self._namespaces.items():
  55.             if uri in useduris:
  56.                 namespaces[p] = uri
  57.                 continue
  58.         
  59.         return namespaces
  60.  
  61.     
  62.     def __getNamespaces(self):
  63.         
  64.         try:
  65.             return self._parent.parentRule.parentStyleSheet.namespaces
  66.         except AttributeError:
  67.             return self._Selector__namespaces
  68.  
  69.  
  70.     _namespaces = property(__getNamespaces, doc = 'If this Selector is attached\n        to a CSSStyleSheet the namespaces of that sheet are mirrored here.\n        While the Selector (or parent SelectorList or parentRule(s) of that are\n        not attached a own dict of {prefix: namespaceURI} is used.')
  71.     element = property((lambda self: self._element), doc = u'Effective element target of this selector.')
  72.     parent = property((lambda self: self._parent), doc = '(DOM) The SelectorList that contains this Selector or        None if this Selector is not attached to a SelectorList.')
  73.     
  74.     def _getSelectorText(self):
  75.         return cssutils.ser.do_css_Selector(self)
  76.  
  77.     
  78.     def _setSelectorText(self, selectorText):
  79.         self._checkReadonly()
  80.         (selectorText, namespaces) = self._splitNamespacesOff(selectorText)
  81.         
  82.         try:
  83.             namespaces = self.parent.parentRule.parentStyleSheet.namespaces
  84.         except AttributeError:
  85.             pass
  86.  
  87.         tokenizer = self._tokenize2(selectorText)
  88.         if not tokenizer:
  89.             self._log.error(u'Selector: No selectorText given.')
  90.         else:
  91.             tokens = []
  92.             for t in tokenizer:
  93.                 (typ, val, lin, col) = t
  94.                 if val == u':' and tokens and self._tokenvalue(tokens[-1]) == ':':
  95.                     tokens[-1] = (typ, u'::', lin, col)
  96.                     continue
  97.                 if typ == 'IDENT' and tokens and self._tokenvalue(tokens[-1]) == u'.':
  98.                     tokens[-1] = ('class', u'.' + val, lin, col)
  99.                     continue
  100.                 if typ == 'IDENT' and tokens and self._tokenvalue(tokens[-1]).startswith(u':') and not self._tokenvalue(tokens[-1]).endswith(u'('):
  101.                     if self._tokenvalue(tokens[-1]).startswith(u'::'):
  102.                         t = 'pseudo-element'
  103.                     else:
  104.                         t = 'pseudo-class'
  105.                     tokens[-1] = (t, self._tokenvalue(tokens[-1]) + val, lin, col)
  106.                     continue
  107.                 if typ == 'FUNCTION' and val == u'not(' and tokens and u':' == self._tokenvalue(tokens[-1]):
  108.                     tokens[-1] = ('negation', u':' + val, lin, tokens[-1][3])
  109.                     continue
  110.                 if typ == 'FUNCTION' and tokens and self._tokenvalue(tokens[-1]).startswith(u':'):
  111.                     if self._tokenvalue(tokens[-1]).startswith(u'::'):
  112.                         t = 'pseudo-element'
  113.                     else:
  114.                         t = 'pseudo-class'
  115.                     tokens[-1] = (t, self._tokenvalue(tokens[-1]) + val, lin, col)
  116.                     continue
  117.                 if val == u'*' and tokens and self._type(tokens[-1]) == 'namespace_prefix' and self._tokenvalue(tokens[-1]).endswith(u'|'):
  118.                     tokens[-1] = ('universal', self._tokenvalue(tokens[-1]) + val, lin, col)
  119.                     continue
  120.                 if val == u'*':
  121.                     tokens.append(('universal', val, lin, col))
  122.                     continue
  123.                 if val == u'|' and tokens and self._type(tokens[-1]) in (self._prods.IDENT, 'universal') and self._tokenvalue(tokens[-1]).find(u'|') == -1:
  124.                     tokens[-1] = ('namespace_prefix', self._tokenvalue(tokens[-1]) + u'|', lin, col)
  125.                     continue
  126.                 if val == u'|':
  127.                     tokens.append(('namespace_prefix', val, lin, col))
  128.                     continue
  129.                 tokens.append(t)
  130.             
  131.             tokenizer = (lambda .0: for t in .0:
  132. t)(tokens)
  133.             new = {
  134.                 'context': [
  135.                     ''],
  136.                 'element': None,
  137.                 '_PREFIX': None,
  138.                 'specificity': [
  139.                     0,
  140.                     0,
  141.                     0,
  142.                     0],
  143.                 'wellformed': True }
  144.             S = u' '
  145.             
  146.             def append(seq, val, typ = None, token = (None, None, None)):
  147.                 context = new['context'][-1]
  148.                 if token:
  149.                     line = token[2]
  150.                     col = token[3]
  151.                 else:
  152.                     (line, col) = (None, None)
  153.                 if typ == '_PREFIX':
  154.                     new['_PREFIX'] = val[:-1]
  155.                     return None
  156.                 if new['_PREFIX'] is not None:
  157.                     prefix = new['_PREFIX']
  158.                     new['_PREFIX'] = None
  159.                 elif typ == 'universal' and '|' in val:
  160.                     (prefix, val) = val.split('|')
  161.                 else:
  162.                     prefix = None
  163.                 if typ.endswith('-selector') or typ == 'universal':
  164.                     if 'attribute-selector' == typ:
  165.                         pass
  166.                     if not (not prefix):
  167.                         if prefix == u'*':
  168.                             namespaceURI = cssutils._ANYNS
  169.                         elif prefix is None:
  170.                             namespaceURI = namespaces.get(u'', None)
  171.                         elif prefix == u'':
  172.                             namespaceURI = u''
  173.                         else:
  174.                             namespaceURI = namespaces[prefix]
  175.                             if namespaceURI is None:
  176.                                 new['wellformed'] = False
  177.                                 self._log.error(u'Selector: No namespaceURI found for prefix %r' % prefix, token = token, error = xml.dom.NamespaceErr)
  178.                                 return None
  179.                         val = (namespaceURI, val)
  180.                     
  181.                 if not context or context == 'negation':
  182.                     if 'id' == typ:
  183.                         new['specificity'][1] += 1
  184.                     elif 'class' == typ or '[' == val:
  185.                         new['specificity'][2] += 1
  186.                     elif typ in ('type-selector', 'negation-type-selector', 'pseudo-element'):
  187.                         new['specificity'][3] += 1
  188.                     
  189.                 
  190.                 if not context and typ in ('type-selector', 'universal'):
  191.                     new['element'] = val
  192.                 
  193.                 seq.append(val, typ, line = line, col = col)
  194.  
  195.             simple_selector_sequence = 'type_selector universal HASH class attrib pseudo negation '
  196.             simple_selector_sequence2 = 'HASH class attrib pseudo negation '
  197.             element_name = 'element_name'
  198.             negation_arg = 'type_selector universal HASH class attrib pseudo'
  199.             negationend = ')'
  200.             attname = 'prefix attribute'
  201.             attname2 = 'attribute'
  202.             attcombinator = 'combinator ]'
  203.             attvalue = 'value'
  204.             attend = ']'
  205.             expressionstart = 'PLUS - DIMENSION NUMBER STRING IDENT'
  206.             expression = expressionstart + ' )'
  207.             combinator = ' combinator'
  208.             
  209.             def _COMMENT(expected, seq, token, tokenizer = (None,)):
  210.                 append(seq, cssutils.css.CSSComment([
  211.                     token]), 'COMMENT', token = token)
  212.                 return expected
  213.  
  214.             
  215.             def _S(expected, seq, token, tokenizer = (None, None, None, None, None)):
  216.                 context = new['context'][-1]
  217.                 if context.startswith('pseudo-'):
  218.                     if seq and seq[-1].value not in u'+-':
  219.                         append(seq, S, 'S', token = token)
  220.                     
  221.                     return expected
  222.                 if context != 'attrib' and 'combinator' in expected:
  223.                     append(seq, S, 'descendant', token = token)
  224.                     return simple_selector_sequence + combinator
  225.                 return expected
  226.  
  227.             
  228.             def _universal(expected, seq, token, tokenizer = (None, None, None, None, None, None)):
  229.                 context = new['context'][-1]
  230.                 val = self._tokenvalue(token)
  231.                 if 'universal' in expected:
  232.                     append(seq, val, 'universal', token = token)
  233.                     if 'negation' == context:
  234.                         return negationend
  235.                     return simple_selector_sequence2 + combinator
  236.                 'universal' in expected
  237.                 new['wellformed'] = False
  238.                 self._log.error(u'Selector: Unexpected universal.', token = token)
  239.                 return expected
  240.  
  241.             
  242.             def _namespace_prefix(expected, seq, token, tokenizer = (None, None, None, None, None)):
  243.                 context = new['context'][-1]
  244.                 val = self._tokenvalue(token)
  245.                 if 'attrib' == context and 'prefix' in expected:
  246.                     append(seq, val, '_PREFIX', token = token)
  247.                     return attname2
  248.                 if 'type_selector' in expected:
  249.                     append(seq, val, '_PREFIX', token = token)
  250.                     return element_name
  251.                 new['wellformed'] = False
  252.                 self._log.error(u'Selector: Unexpected namespace prefix.', token = token)
  253.                 return expected
  254.  
  255.             
  256.             def _pseudo(expected, seq, token, tokenizer = (None, None, None, None, None, None, None)):
  257.                 context = new['context'][-1]
  258.                 val = self._tokenvalue(token, normalize = True)
  259.                 typ = self._type(token)
  260.                 if 'pseudo' in expected:
  261.                     if val in (':first-line', ':first-letter', ':before', ':after'):
  262.                         typ = 'pseudo-element'
  263.                     
  264.                     append(seq, val, typ, token = token)
  265.                     if val.endswith(u'('):
  266.                         new['context'].append(typ)
  267.                         return expressionstart
  268.                     if 'negation' == context:
  269.                         return negationend
  270.                     if 'pseudo-element' == typ:
  271.                         return combinator
  272.                     return simple_selector_sequence2 + combinator
  273.                 'pseudo' in expected
  274.                 new['wellformed'] = False
  275.                 self._log.error(u'Selector: Unexpected start of pseudo.', token = token)
  276.                 return expected
  277.  
  278.             
  279.             def _expression(expected, seq, token, tokenizer = (None, None, None, None)):
  280.                 context = new['context'][-1]
  281.                 val = self._tokenvalue(token)
  282.                 typ = self._type(token)
  283.                 if context.startswith('pseudo-'):
  284.                     append(seq, val, typ, token = token)
  285.                     return expression
  286.                 new['wellformed'] = False
  287.                 self._log.error(u'Selector: Unexpected %s.' % typ, token = token)
  288.                 return expected
  289.  
  290.             
  291.             def _attcombinator(expected, seq, token, tokenizer = (None, None, None, None)):
  292.                 context = new['context'][-1]
  293.                 val = self._tokenvalue(token)
  294.                 typ = self._type(token)
  295.                 if 'attrib' == context and 'combinator' in expected:
  296.                     append(seq, val, typ.lower(), token = token)
  297.                     return attvalue
  298.                 new['wellformed'] = False
  299.                 self._log.error(u'Selector: Unexpected %s.' % typ, token = token)
  300.                 return expected
  301.  
  302.             
  303.             def _string(expected, seq, token, tokenizer = (None, None, None, None, None)):
  304.                 context = new['context'][-1]
  305.                 typ = self._type(token)
  306.                 val = self._stringtokenvalue(token)
  307.                 if 'attrib' == context and 'value' in expected:
  308.                     append(seq, val, typ, token = token)
  309.                     return attend
  310.                 if context.startswith('pseudo-'):
  311.                     append(seq, val, typ, token = token)
  312.                     return expression
  313.                 new['wellformed'] = False
  314.                 self._log.error(u'Selector: Unexpected STRING.', token = token)
  315.                 return expected
  316.  
  317.             
  318.             def _ident(expected, seq, token, tokenizer = (None, None, None, None, None, None, None, None, None, None)):
  319.                 context = new['context'][-1]
  320.                 val = self._tokenvalue(token)
  321.                 typ = self._type(token)
  322.                 if 'attrib' == context and 'attribute' in expected:
  323.                     append(seq, val, 'attribute-selector', token = token)
  324.                     return attcombinator
  325.                 if 'attrib' == context and 'value' in expected:
  326.                     append(seq, val, 'attribute-value', token = token)
  327.                     return attend
  328.                 if 'negation' == context:
  329.                     append(seq, val, 'negation-type-selector', token = token)
  330.                     return negationend
  331.                 if context.startswith('pseudo-'):
  332.                     append(seq, val, typ, token = token)
  333.                     return expression
  334.                 if 'type_selector' in expected or element_name == expected:
  335.                     append(seq, val, 'type-selector', token = token)
  336.                     return simple_selector_sequence2 + combinator
  337.                 new['wellformed'] = False
  338.                 self._log.error(u'Selector: Unexpected IDENT.', token = token)
  339.                 return expected
  340.  
  341.             
  342.             def _class(expected, seq, token, tokenizer = (None, None, None, None, None, None)):
  343.                 context = new['context'][-1]
  344.                 val = self._tokenvalue(token)
  345.                 if 'class' in expected:
  346.                     append(seq, val, 'class', token = token)
  347.                     if 'negation' == context:
  348.                         return negationend
  349.                     return simple_selector_sequence2 + combinator
  350.                 'class' in expected
  351.                 new['wellformed'] = False
  352.                 self._log.error(u'Selector: Unexpected class.', token = token)
  353.                 return expected
  354.  
  355.             
  356.             def _hash(expected, seq, token, tokenizer = (None, None, None, None, None, None)):
  357.                 context = new['context'][-1]
  358.                 val = self._tokenvalue(token)
  359.                 if 'HASH' in expected:
  360.                     append(seq, val, 'id', token = token)
  361.                     if 'negation' == context:
  362.                         return negationend
  363.                     return simple_selector_sequence2 + combinator
  364.                 'HASH' in expected
  365.                 new['wellformed'] = False
  366.                 self._log.error(u'Selector: Unexpected HASH.', token = token)
  367.                 return expected
  368.  
  369.             
  370.             def _char(expected, seq, token, tokenizer = (None, None, None, None, None, None, None, None, None, None, None)):
  371.                 context = new['context'][-1]
  372.                 val = self._tokenvalue(token)
  373.                 if u']' == val and 'attrib' == context and ']' in expected:
  374.                     append(seq, val, 'attribute-end', token = token)
  375.                     context = new['context'].pop()
  376.                     context = new['context'][-1]
  377.                     if 'negation' == context:
  378.                         return negationend
  379.                     return simple_selector_sequence2 + combinator
  380.                 ']' in expected
  381.                 if u'=' == val and 'attrib' == context and 'combinator' in expected:
  382.                     append(seq, val, 'equals', token = token)
  383.                     return attvalue
  384.                 if u')' == val and 'negation' == context and u')' in expected:
  385.                     append(seq, val, 'negation-end', token = token)
  386.                     new['context'].pop()
  387.                     context = new['context'][-1]
  388.                     return simple_selector_sequence + combinator
  389.                 if val in u'+-' and context.startswith('pseudo-'):
  390.                     _names = {
  391.                         '+': 'plus',
  392.                         '-': 'minus' }
  393.                     return expression
  394.                 if u')' == val and context.startswith('pseudo-') and expression == expected:
  395.                     append(seq, val, 'function-end', token = token)
  396.                     new['context'].pop()
  397.                     if 'pseudo-element' == context:
  398.                         return combinator
  399.                     return simple_selector_sequence + combinator
  400.                 expression == expected
  401.                 if u'[' == val and 'attrib' in expected:
  402.                     append(seq, val, 'attribute-start', token = token)
  403.                     new['context'].append('attrib')
  404.                     return attname
  405.                 if val in u'+>~' and 'combinator' in expected:
  406.                     _names = {
  407.                         '>': 'child',
  408.                         '+': 'adjacent-sibling',
  409.                         '~': 'following-sibling' }
  410.                     return simple_selector_sequence
  411.                 if u',' == val:
  412.                     new['wellformed'] = False
  413.                     self._log.error(u'Selector: Single selector only.', error = xml.dom.InvalidModificationErr, token = token)
  414.                     return expected
  415.                 new['wellformed'] = False
  416.                 self._log.error(u'Selector: Unexpected CHAR.', token = token)
  417.                 return expected
  418.  
  419.             
  420.             def _negation(expected, seq, token, tokenizer = (None, None, None, None)):
  421.                 context = new['context'][-1]
  422.                 val = self._tokenvalue(token, normalize = True)
  423.                 if 'negation' in expected:
  424.                     new['context'].append('negation')
  425.                     append(seq, val, 'negation-start', token = token)
  426.                     return negation_arg
  427.                 new['wellformed'] = False
  428.                 self._log.error(u'Selector: Unexpected negation.', token = token)
  429.                 return expected
  430.  
  431.             
  432.             def _atkeyword(expected, seq, token, tokenizer = (None, None)):
  433.                 new['wellformed'] = False
  434.                 self._log.error(u'Selector: Unexpected ATKEYWORD.', token = token)
  435.                 return expected
  436.  
  437.             newseq = self._tempSeq()
  438.             (wellformed, expected) = self._parse(expected = simple_selector_sequence, seq = newseq, tokenizer = tokenizer, productions = {
  439.                 'CHAR': _char,
  440.                 'class': _class,
  441.                 'HASH': _hash,
  442.                 'STRING': _string,
  443.                 'IDENT': _ident,
  444.                 'namespace_prefix': _namespace_prefix,
  445.                 'negation': _negation,
  446.                 'pseudo-class': _pseudo,
  447.                 'pseudo-element': _pseudo,
  448.                 'universal': _universal,
  449.                 'NUMBER': _expression,
  450.                 'DIMENSION': _expression,
  451.                 'PREFIXMATCH': _attcombinator,
  452.                 'SUFFIXMATCH': _attcombinator,
  453.                 'SUBSTRINGMATCH': _attcombinator,
  454.                 'DASHMATCH': _attcombinator,
  455.                 'INCLUDES': _attcombinator,
  456.                 'S': _S,
  457.                 'COMMENT': _COMMENT,
  458.                 'ATKEYWORD': _atkeyword })
  459.             if wellformed:
  460.                 pass
  461.             wellformed = new['wellformed']
  462.             if len(new['context']) > 1 or not newseq:
  463.                 wellformed = False
  464.                 self._log.error(u'Selector: Invalid or incomplete selector: %s' % self._valuestr(selectorText))
  465.             
  466.             if expected == 'element_name':
  467.                 wellformed = False
  468.                 self._log.error(u'Selector: No element name found: %s' % self._valuestr(selectorText))
  469.             
  470.             if expected == simple_selector_sequence and newseq:
  471.                 wellformed = False
  472.                 self._log.error(u'Selector: Cannot end with combinator: %s' % self._valuestr(selectorText))
  473.             
  474.             if newseq and hasattr(newseq[-1].value, 'strip') and newseq[-1].value.strip() == u'':
  475.                 del newseq[-1]
  476.             
  477.             if wellformed:
  478.                 self._Selector__namespaces = namespaces
  479.                 self._element = new['element']
  480.                 self._specificity = tuple(new['specificity'])
  481.                 self._setSeq(newseq)
  482.                 self._Selector__namespaces = self._getUsedNamespaces()
  483.             
  484.  
  485.     selectorText = property(_getSelectorText, _setSelectorText, doc = '(DOM) The parsable textual representation of the selector.')
  486.     specificity = property((lambda self: self._specificity), doc = 'Specificity of this selector (READONLY). \n                Tuple of (a, b, c, d) where: \n                \n                a\n                    presence of style in document, always 0 if not used on a document\n                b\n                    number of ID selectors\n                c \n                    number of .class selectors\n                d \n                    number of Element (type) selectors\n                    ')
  487.     wellformed = property((lambda self: bool(len(self.seq))))
  488.     
  489.     def _getParentList(self):
  490.         return self.parent
  491.  
  492.     _getParentList = Deprecated('Use property parent instead')(_getParentList)
  493.     parentList = property(_getParentList, doc = 'DEPRECATED, see property parent instead')
  494.  
  495.