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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __all__ = [
  5.     'CSSVariablesDeclaration']
  6. __docformat__ = 'restructuredtext'
  7. __version__ = '$Id: cssstyledeclaration.py 1819 2009-08-01 20:52:43Z cthedot $'
  8. from cssutils.prodparser import *
  9. from cssvalue import CSSValue
  10. import cssutils
  11. import itertools
  12. import xml.dom as xml
  13.  
  14. class CSSVariablesDeclaration(cssutils.util._NewBase):
  15.     
  16.     def __init__(self, cssText = u'', parentRule = None, readonly = False):
  17.         super(CSSVariablesDeclaration, self).__init__()
  18.         self._parentRule = parentRule
  19.         self._vars = { }
  20.         if cssText:
  21.             self.cssText = cssText
  22.         
  23.         self._readonly = readonly
  24.  
  25.     
  26.     def __repr__(self):
  27.         return 'cssutils.css.%s(cssText=%r)' % (self.__class__.__name__, self.cssText)
  28.  
  29.     
  30.     def __str__(self):
  31.         return '<cssutils.css.%s object length=%r at 0x%x>' % (self.__class__.__name__, self.length, id(self))
  32.  
  33.     
  34.     def __contains__(self, variableName):
  35.         return variableName.lower() in self.keys()
  36.  
  37.     
  38.     def __getitem__(self, variableName):
  39.         return self.getVariableValue(variableName.lower())
  40.  
  41.     
  42.     def __setitem__(self, variableName, value):
  43.         self.setVariable(variableName.lower(), value)
  44.  
  45.     
  46.     def __delitem__(self, variableName):
  47.         return self.removeVariable(variableName.lower())
  48.  
  49.     
  50.     def __iter__(self):
  51.         for name in self.keys():
  52.             yield name
  53.         
  54.  
  55.     
  56.     def _absorb(self, other):
  57.         self._parentRule = other._parentRule
  58.         self.seq.absorb(other.seq)
  59.         self._readonly = other._readonly
  60.  
  61.     
  62.     def keys(self):
  63.         return self._vars.keys()
  64.  
  65.     
  66.     def _getCssText(self):
  67.         return cssutils.ser.do_css_CSSVariablesDeclaration(self)
  68.  
  69.     
  70.     def _setCssText(self, cssText):
  71.         self._checkReadonly()
  72.         vardeclaration = Sequence(PreDef.ident(), PreDef.char(u':', u':', toSeq = False), Prod(name = u'term', match = (lambda t, v: True), toSeq = (lambda t, tokens: (u'value', CSSValue(itertools.chain([
  73. t], tokens))))), PreDef.char(u';', u';', toSeq = False, optional = True))
  74.         prods = Sequence(vardeclaration, minmax = (lambda : (0, None)))
  75.         (wellformed, seq, store, notused) = ProdParser().parse(cssText, u'CSSVariableDeclaration', prods)
  76.         if wellformed:
  77.             newseq = self._tempSeq()
  78.             lastname = None
  79.             for item in seq:
  80.                 if u'IDENT' == item.type:
  81.                     lastname = item
  82.                     self._vars[lastname.value.lower()] = None
  83.                     continue
  84.                 if u'value' == item.type:
  85.                     self._vars[lastname.value.lower()] = item.value
  86.                     newseq.append((lastname.value, item.value), 'var', lastname.line, lastname.col)
  87.                     continue
  88.                 newseq.appendItem(item)
  89.             
  90.             self._setSeq(newseq)
  91.             self.wellformed = True
  92.         
  93.  
  94.     cssText = property(_getCssText, _setCssText, doc = '(DOM) A parsable textual representation of the declaration        block excluding the surrounding curly braces.')
  95.     
  96.     def _setParentRule(self, parentRule):
  97.         self._parentRule = parentRule
  98.  
  99.     parentRule = property((lambda self: self._parentRule), _setParentRule, doc = '(DOM) The CSS rule that contains this declaration block or None if this block is not attached to a CSSRule.')
  100.     
  101.     def getVariableValue(self, variableName):
  102.         
  103.         try:
  104.             return self._vars[variableName.lower()].cssText
  105.         except KeyError:
  106.             e = None
  107.             return u''
  108.  
  109.  
  110.     
  111.     def removeVariable(self, variableName):
  112.         
  113.         try:
  114.             r = self._vars[variableName.lower()]
  115.         except KeyError:
  116.             e = None
  117.             return u''
  118.  
  119.         self.seq._readonly = False
  120.         if variableName in self._vars:
  121.             for i, x in enumerate(self.seq):
  122.                 if x.value[0] == variableName:
  123.                     del self.seq[i]
  124.                     continue
  125.             
  126.         
  127.         self.seq._readonly = True
  128.         del self._vars[variableName.lower()]
  129.         return r.cssText
  130.  
  131.     
  132.     def setVariable(self, variableName, value):
  133.         self._checkReadonly()
  134.         (wellformed, seq, store, unused) = ProdParser().parse(variableName.lower(), u'variableName', Sequence(PreDef.ident()))
  135.         if not wellformed:
  136.             self._log.error(u'Invalid variableName: %r: %r' % (variableName, value))
  137.         elif isinstance(value, CSSValue):
  138.             v = value
  139.         else:
  140.             v = CSSValue(cssText = value)
  141.         if not v.wellformed:
  142.             self._log.error(u'Invalid variable value: %r: %r' % (variableName, value))
  143.         else:
  144.             self.seq._readonly = False
  145.             if variableName in self._vars:
  146.                 for i, x in enumerate(self.seq):
  147.                     if x.value[0] == variableName:
  148.                         x.replace(i, [
  149.                             variableName,
  150.                             v], x.type, x.line, x.col)
  151.                         break
  152.                         continue
  153.                 
  154.             else:
  155.                 self.seq.append([
  156.                     variableName,
  157.                     v], 'var')
  158.             self.seq._readonly = True
  159.             self._vars[variableName] = v
  160.  
  161.     
  162.     def item(self, index):
  163.         
  164.         try:
  165.             return self.keys()[index]
  166.         except IndexError:
  167.             return u''
  168.  
  169.  
  170.     length = property((lambda self: len(self._vars)), doc = 'The number of variables that have been explicitly set in this variable declaration block. The range of valid indices is 0 to length-1 inclusive.')
  171.  
  172.