home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1519 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  12.1 KB  |  305 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __all__ = [
  5.     'CSSStyleDeclaration',
  6.     'Property']
  7. __docformat__ = 'restructuredtext'
  8. __version__ = '$Id: cssstyledeclaration.py 1879 2009-11-17 20:35:04Z cthedot $'
  9. from cssproperties import CSS2Properties
  10. from property import Property
  11. import cssutils
  12. import xml.dom as xml
  13.  
  14. class CSSStyleDeclaration(CSS2Properties, cssutils.util.Base2):
  15.     
  16.     def __init__(self, cssText = u'', parentRule = None, readonly = False):
  17.         super(CSSStyleDeclaration, self).__init__()
  18.         self._parentRule = parentRule
  19.         self.cssText = cssText
  20.         self._readonly = readonly
  21.  
  22.     
  23.     def __contains__(self, nameOrProperty):
  24.         if isinstance(nameOrProperty, Property):
  25.             name = nameOrProperty.name
  26.         else:
  27.             name = self._normalize(nameOrProperty)
  28.         return name in self._CSSStyleDeclaration__nnames()
  29.  
  30.     
  31.     def __iter__(self):
  32.         
  33.         def properties():
  34.             for name in self._CSSStyleDeclaration__nnames():
  35.                 yield self.getProperty(name)
  36.             
  37.  
  38.         return properties()
  39.  
  40.     
  41.     def keys(self):
  42.         return list(self._CSSStyleDeclaration__nnames())
  43.  
  44.     
  45.     def __getitem__(self, CSSName):
  46.         return self.getPropertyValue(CSSName)
  47.  
  48.     
  49.     def __setitem__(self, CSSName, value):
  50.         priority = None
  51.         if type(value) == tuple:
  52.             (value, priority) = value
  53.         
  54.         return self.setProperty(CSSName, value, priority)
  55.  
  56.     
  57.     def __delitem__(self, CSSName):
  58.         return self.removeProperty(CSSName)
  59.  
  60.     
  61.     def __setattr__(self, n, v):
  62.         known = [
  63.             '_tokenizer',
  64.             '_log',
  65.             '_ttypes',
  66.             '_seq',
  67.             'seq',
  68.             'parentRule',
  69.             '_parentRule',
  70.             'cssText',
  71.             'valid',
  72.             'wellformed',
  73.             '_readonly',
  74.             '_profiles']
  75.         known.extend(CSS2Properties._properties)
  76.         if n in known:
  77.             super(CSSStyleDeclaration, self).__setattr__(n, v)
  78.         else:
  79.             raise AttributeError('Unknown CSS Property, ``CSSStyleDeclaration.setProperty("%s", ...)`` MUST be used.' % n)
  80.         return n in known
  81.  
  82.     
  83.     def __repr__(self):
  84.         return 'cssutils.css.%s(cssText=%r)' % (self.__class__.__name__, self.getCssText(separator = u' '))
  85.  
  86.     
  87.     def __str__(self):
  88.         return '<cssutils.css.%s object length=%r (all: %r) at 0x%x>' % (self.__class__.__name__, self.length, len(self.getProperties(all = True)), id(self))
  89.  
  90.     
  91.     def __nnames(self):
  92.         names = []
  93.         for item in reversed(self.seq):
  94.             val = item.value
  95.             if isinstance(val, Property) and val.name not in names:
  96.                 names.append(val.name)
  97.                 continue
  98.         
  99.         return reversed(names)
  100.  
  101.     
  102.     def _absorb(self, other):
  103.         self._parentRule = other._parentRule
  104.         self.seq.absorb(other.seq)
  105.         self._readonly = other._readonly
  106.  
  107.     
  108.     def _getP(self, CSSName):
  109.         return self.getPropertyValue(CSSName)
  110.  
  111.     
  112.     def _setP(self, CSSName, value):
  113.         self.setProperty(CSSName, value)
  114.  
  115.     
  116.     def _delP(self, CSSName):
  117.         self.removeProperty(CSSName)
  118.  
  119.     
  120.     def children(self):
  121.         for item in self._seq:
  122.             yield item.value
  123.         
  124.  
  125.     
  126.     def _getCssText(self):
  127.         return cssutils.ser.do_css_CSSStyleDeclaration(self)
  128.  
  129.     
  130.     def _setCssText(self, cssText):
  131.         self._checkReadonly()
  132.         tokenizer = self._tokenize2(cssText)
  133.         new = {
  134.             'wellformed': True }
  135.         
  136.         def ident(expected, seq, token, tokenizer = (None,)):
  137.             tokens = self._tokensupto2(tokenizer, starttoken = token, semicolon = True)
  138.             if self._tokenvalue(tokens[-1]) == u';':
  139.                 tokens.pop()
  140.             
  141.             property = Property(parent = self)
  142.             property.cssText = tokens
  143.             if property.wellformed:
  144.                 seq.append(property, 'Property')
  145.             else:
  146.                 self._log.error(u'CSSStyleDeclaration: Syntax Error in Property: %s' % self._valuestr(tokens))
  147.             return expected
  148.  
  149.         
  150.         def unexpected(expected, seq, token, tokenizer = (None,)):
  151.             ignored = self._tokenvalue(token) + self._valuestr(self._tokensupto2(tokenizer, propertyvalueendonly = True))
  152.             self._log.error(u'CSSStyleDeclaration: Unexpected token, ignoring upto %r.' % ignored, token)
  153.             return expected
  154.  
  155.         newseq = self._tempSeq()
  156.         (wellformed, expected) = self._parse(expected = None, seq = newseq, tokenizer = tokenizer, productions = {
  157.             'IDENT': ident }, default = unexpected)
  158.         for item in newseq:
  159.             item.value._parent = self
  160.         
  161.         self._setSeq(newseq)
  162.  
  163.     cssText = property(_getCssText, _setCssText, doc = '(DOM) A parsable textual representation of the declaration        block excluding the surrounding curly braces.')
  164.     
  165.     def getCssText(self, separator = None):
  166.         return cssutils.ser.do_css_CSSStyleDeclaration(self, separator)
  167.  
  168.     
  169.     def _setParentRule(self, parentRule):
  170.         self._parentRule = parentRule
  171.         for x in self.children():
  172.             x.parent = self
  173.         
  174.  
  175.     parentRule = property((lambda self: self._parentRule), _setParentRule, doc = '(DOM) The CSS rule that contains this declaration block or None if this CSSStyleDeclaration is not attached to a CSSRule.')
  176.     
  177.     def getProperties(self, name = None, all = False):
  178.         if name and not all:
  179.             p = self.getProperty(name)
  180.             if p:
  181.                 return [
  182.                     p]
  183.             return []
  184.         not all
  185.         if not all:
  186.             return [ self.getProperty(name) for name in self._CSSStyleDeclaration__nnames() ]
  187.         nname = self._normalize(name)
  188.         properties = []
  189.         for item in self.seq:
  190.             val = item.value
  191.             if isinstance(val, Property):
  192.                 if bool(nname) == False or val.name == nname:
  193.                     properties.append(val)
  194.                     continue
  195.             all
  196.         
  197.         return properties
  198.  
  199.     
  200.     def getProperty(self, name, normalize = True):
  201.         nname = self._normalize(name)
  202.         found = None
  203.         for item in reversed(self.seq):
  204.             val = item.value
  205.             if isinstance(val, Property):
  206.                 if normalize or nname == val.name or name == val.literalname:
  207.                     if val.priority:
  208.                         return val
  209.                     if not found:
  210.                         found = val
  211.                     
  212.                 
  213.             name == val.literalname
  214.         
  215.         return found
  216.  
  217.     
  218.     def getPropertyCSSValue(self, name, normalize = True):
  219.         nname = self._normalize(name)
  220.         if nname in self._SHORTHANDPROPERTIES:
  221.             self._log.info(u'CSSValue for shorthand property "%s" should be None, this may be implemented later.' % nname, neverraise = True)
  222.         
  223.         p = self.getProperty(name, normalize)
  224.         if p:
  225.             return p.cssValue
  226.         return None
  227.  
  228.     
  229.     def getPropertyValue(self, name, normalize = True):
  230.         p = self.getProperty(name, normalize)
  231.         if p:
  232.             return p.value
  233.         return u''
  234.  
  235.     
  236.     def getPropertyPriority(self, name, normalize = True):
  237.         p = self.getProperty(name, normalize)
  238.         if p:
  239.             return p.priority
  240.         return u''
  241.  
  242.     
  243.     def removeProperty(self, name, normalize = True):
  244.         self._checkReadonly()
  245.         r = self.getPropertyValue(name, normalize = normalize)
  246.         newseq = self._tempSeq()
  247.         if normalize:
  248.             nname = self._normalize(name)
  249.             for item in self.seq:
  250.                 if not isinstance(item.value, Property) and item.value.name == nname:
  251.                     newseq.appendItem(item)
  252.                     continue
  253.             
  254.         else:
  255.             for item in self.seq:
  256.                 if not isinstance(item.value, Property) and item.value.literalname == name:
  257.                     newseq.appendItem(item)
  258.                     continue
  259.             
  260.         self._setSeq(newseq)
  261.         return r
  262.  
  263.     
  264.     def setProperty(self, name, value = None, priority = u'', normalize = True):
  265.         self._checkReadonly()
  266.         if isinstance(name, Property):
  267.             newp = name
  268.             name = newp.literalname
  269.         elif not value:
  270.             return self.removeProperty(name)
  271.         newp = Property(name, value, priority)
  272.         if not newp.wellformed:
  273.             self._log.warn(u'Invalid Property: %s: %s %s' % (name, value, priority))
  274.         else:
  275.             nname = self._normalize(name)
  276.             properties = self.getProperties(name, all = not normalize)
  277.             for property in reversed(properties):
  278.                 if normalize and property.name == nname:
  279.                     property.cssValue = newp.cssValue.cssText
  280.                     property.priority = newp.priority
  281.                     break
  282.                     continue
  283.                 if property.literalname == name:
  284.                     property.cssValue = newp.cssValue.cssText
  285.                     property.priority = newp.priority
  286.                     break
  287.                     continue
  288.             else:
  289.                 newp.parent = self
  290.                 self.seq._readonly = False
  291.                 self.seq._readonly = True
  292.  
  293.     
  294.     def item(self, index):
  295.         names = list(self._CSSStyleDeclaration__nnames())
  296.         
  297.         try:
  298.             return names[index]
  299.         except IndexError:
  300.             return u''
  301.  
  302.  
  303.     length = property((lambda self: len(list(self._CSSStyleDeclaration__nnames()))), doc = '(DOM) The number of distinct properties that have been explicitly in this declaration block. The range of valid indices is 0 to length-1 inclusive. These are properties with a different ``name`` only. :meth:`item` and :attr:`length` work on the same set here.')
  304.  
  305.