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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __all__ = [
  5.     'CSSParser']
  6. __docformat__ = 'restructuredtext'
  7. __version__ = '$Id: parse.py 1754 2009-05-30 14:50:13Z cthedot $'
  8. import helper
  9. import codecs
  10. import errorhandler
  11. import os
  12. import tokenize2
  13. import urllib
  14. import sys
  15.  
  16. class ErrorHandler(object):
  17.     
  18.     def __init__(self):
  19.         self._log = errorhandler.ErrorHandler()
  20.  
  21.     
  22.     def error(self, exception, token = None):
  23.         self._log.error(exception, token, neverraise = True)
  24.  
  25.     
  26.     def fatal(self, exception, token = None):
  27.         self._log.fatal(exception, token)
  28.  
  29.     
  30.     def warn(self, exception, token = None):
  31.         self._log.warn(exception, token, neverraise = True)
  32.  
  33.  
  34.  
  35. class DocumentHandler(object):
  36.     
  37.     def __init__(self):
  38.         
  39.         def log(msg):
  40.             sys.stderr.write('INFO\t%s\n' % msg)
  41.  
  42.         self._log = log
  43.  
  44.     
  45.     def comment(self, text, line = None, col = None):
  46.         self._log('comment %r at [%s, %s]' % (text, line, col))
  47.  
  48.     
  49.     def startDocument(self, encoding):
  50.         self._log('startDocument encoding=%s' % encoding)
  51.  
  52.     
  53.     def endDocument(self, source = None, line = None, col = None):
  54.         self._log('endDocument EOF')
  55.  
  56.     
  57.     def importStyle(self, uri, media, name, line = None, col = None):
  58.         self._log('importStyle at [%s, %s]' % (line, col))
  59.  
  60.     
  61.     def namespaceDeclaration(self, prefix, uri, line = None, col = None):
  62.         self._log('namespaceDeclaration at [%s, %s]' % (line, col))
  63.  
  64.     
  65.     def startSelector(self, selectors = None, line = None, col = None):
  66.         self._log('startSelector at [%s, %s]' % (line, col))
  67.  
  68.     
  69.     def endSelector(self, selectors = None, line = None, col = None):
  70.         self._log('endSelector at [%s, %s]' % (line, col))
  71.  
  72.     
  73.     def property(self, name, value = 'TODO', important = False, line = None, col = None):
  74.         self._log('property %r at [%s, %s]' % (name, line, col))
  75.  
  76.     
  77.     def ignorableAtRule(self, atRule, line = None, col = None):
  78.         self._log('ignorableAtRule %r at [%s, %s]' % (atRule, line, col))
  79.  
  80.  
  81.  
  82. class EchoHandler(DocumentHandler):
  83.     
  84.     def __init__(self):
  85.         super(EchoHandler, self).__init__()
  86.         self._out = []
  87.  
  88.     out = property((lambda self: u''.join(self._out)))
  89.     
  90.     def startDocument(self, encoding):
  91.         super(EchoHandler, self).startDocument(encoding)
  92.         if u'utf-8' != encoding:
  93.             self._out.append(u'@charset "%s";\n' % encoding)
  94.         
  95.  
  96.     
  97.     def importStyle(self, uri, media, name, line = None, col = None):
  98.         super(EchoHandler, self).importStyle(uri, media, name, line, col)
  99.         None(None % (self._out.append, u'@import %s%s%s;\n', helper.string(uri) if media else u'' if name else u''))
  100.  
  101.     
  102.     def namespaceDeclaration(self, prefix, uri, line = None, col = None):
  103.         super(EchoHandler, self).namespaceDeclaration(prefix, uri, line, col)
  104.         None(self._out.append % (u'@namespace %s%s;\n' if prefix else u'', helper.string(uri)))
  105.  
  106.     
  107.     def startSelector(self, selectors = None, line = None, col = None):
  108.         super(EchoHandler, self).startSelector(selectors, line, col)
  109.         if selectors:
  110.             self._out.append(u', '.join(selectors))
  111.         
  112.         self._out.append(u' {\n')
  113.  
  114.     
  115.     def endSelector(self, selectors = None, line = None, col = None):
  116.         self._out.append(u'    }')
  117.  
  118.     
  119.     def property(self, name, value, important = False, line = None, col = None):
  120.         super(EchoHandler, self).property(name, value, line, col)
  121.         None(self._out.append % (u'    %s: %s%s;\n', name, value if important else u''))
  122.  
  123.  
  124.  
  125. class Parser(object):
  126.     
  127.     def __init__(self, documentHandler = None, errorHandler = None):
  128.         self._tokenizer = tokenize2.Tokenizer()
  129.         if documentHandler:
  130.             self.setDocumentHandler(documentHandler)
  131.         else:
  132.             self.setDocumentHandler(DocumentHandler())
  133.         if errorHandler:
  134.             self.setErrorHandler(errorHandler)
  135.         else:
  136.             self.setErrorHandler(ErrorHandler())
  137.  
  138.     
  139.     def parseString(self, cssText, encoding = None):
  140.         if isinstance(cssText, str):
  141.             cssText = codecs.getdecoder('css')(cssText, encoding = encoding)[0]
  142.         
  143.         tokens = self._tokenizer.tokenize(cssText, fullsheet = True)
  144.         
  145.         def COMMENT(val, line, col):
  146.             self._handler.comment(val[2:-2], line, col)
  147.  
  148.         
  149.         def EOF(val, line, col):
  150.             self._handler.endDocument(val, line, col)
  151.  
  152.         
  153.         def simple(t):
  154.             map = {
  155.                 'COMMENT': COMMENT,
  156.                 'S': (lambda val, line, col: pass),
  157.                 'EOF': EOF }
  158.             (type_, val, line, col) = t
  159.             if type_ in map:
  160.                 map[type_](val, line, col)
  161.                 return True
  162.             return False
  163.  
  164.         t = tokens.next()
  165.         (type_, val, line, col) = t
  166.         encoding = 'utf-8'
  167.         self._handler.startDocument(encoding)
  168.         while True:
  169.             start = (line, col)
  170.             
  171.             try:
  172.                 if simple(t):
  173.                     pass
  174.                 elif 'ATKEYWORD' == type_ or type_ in ('PAGE_SYM', 'MEDIA_SYM', 'FONT_FACE_SYM'):
  175.                     atRule = [
  176.                         val]
  177.                     braces = 0
  178.                     while True:
  179.                         t = tokens.next()
  180.                         (type_, val, line, col) = t
  181.                         atRule.append(val)
  182.                         if u';' == val and not braces:
  183.                             break
  184.                             continue
  185.                         if u'{' == val:
  186.                             braces += 1
  187.                             continue
  188.                         if u'}' == val:
  189.                             braces -= 1
  190.                             if braces == 0:
  191.                                 break
  192.                             
  193.                         braces == 0
  194.                     self._handler.ignorableAtRule(u''.join(atRule), *start)
  195.                 elif 'IMPORT_SYM' == type_:
  196.                     (uri, media, name) = (None, None, None)
  197.                     while True:
  198.                         t = tokens.next()
  199.                         (type_, val, line, col) = t
  200.                         if 'STRING' == type_:
  201.                             uri = helper.stringvalue(val)
  202.                             continue
  203.                         if 'URI' == type_:
  204.                             uri = helper.urivalue(val)
  205.                             continue
  206.                         if u';' == val:
  207.                             break
  208.                             continue
  209.                     if uri:
  210.                         self._handler.importStyle(uri, media, name)
  211.                     else:
  212.                         self._errorHandler.error(u'Invalid @import declaration at %r' % (start,))
  213.                 elif 'NAMESPACE_SYM' == type_:
  214.                     (prefix, uri) = (None, None)
  215.                     while True:
  216.                         t = tokens.next()
  217.                         (type_, val, line, col) = t
  218.                         if 'IDENT' == type_:
  219.                             prefix = val
  220.                             continue
  221.                         if 'STRING' == type_:
  222.                             uri = helper.stringvalue(val)
  223.                             continue
  224.                         if 'URI' == type_:
  225.                             uri = helper.urivalue(val)
  226.                             continue
  227.                         if u';' == val:
  228.                             break
  229.                             continue
  230.                     if uri:
  231.                         self._handler.namespaceDeclaration(prefix, uri, *start)
  232.                     else:
  233.                         self._errorHandler.error(u'Invalid @namespace declaration at %r' % (start,))
  234.                 else:
  235.                     selector = []
  236.                     selectors = []
  237.                     while True:
  238.                         if 'S' == type_:
  239.                             selector.append(u' ')
  240.                         elif simple(t):
  241.                             pass
  242.                         elif u',' == val:
  243.                             selectors.append(u''.join(selector).strip())
  244.                             selector = []
  245.                         elif u'{' == val:
  246.                             selectors.append(u''.join(selector).strip())
  247.                             self._handler.startSelector(selectors, *start)
  248.                             break
  249.                         else:
  250.                             selector.append(val)
  251.                         t = tokens.next()
  252.                         (type_, val, line, col) = t
  253.                     end = None
  254.                     while True:
  255.                         name = None
  256.                         value = []
  257.                         important = False
  258.                         while True:
  259.                             t = tokens.next()
  260.                             (type_, val, line, col) = t
  261.                             if 'S' == type_:
  262.                                 continue
  263.                             if simple(t):
  264.                                 continue
  265.                             if 'IDENT' == type_:
  266.                                 if name:
  267.                                     self._errorHandler.error('more than one property name', t)
  268.                                 else:
  269.                                     name = val
  270.                             name
  271.                             if u':' == val:
  272.                                 if not name:
  273.                                     self._errorHandler.error('no property name', t)
  274.                                 
  275.                                 break
  276.                                 continue
  277.                             if u';' == val:
  278.                                 self._errorHandler.error('premature end of property', t)
  279.                                 end = val
  280.                                 break
  281.                                 continue
  282.                             if u'}' == val:
  283.                                 if name:
  284.                                     self._errorHandler.error('premature end of property', t)
  285.                                 
  286.                                 end = val
  287.                                 break
  288.                                 continue
  289.                             self._errorHandler.error('unexpected property name token %r' % val, t)
  290.                         while not (u';' == end) and not (u'}' == end):
  291.                             t = tokens.next()
  292.                             (type_, val, line, col) = t
  293.                             if 'S' == type_:
  294.                                 value.append(u' ')
  295.                                 continue
  296.                             if simple(t):
  297.                                 continue
  298.                             if u'!' == val and u';' == val or u'}' == val:
  299.                                 value = ''.join(value).strip()
  300.                                 if not value:
  301.                                     self._errorHandler.error('premature end of property (no value)', t)
  302.                                 
  303.                                 end = val
  304.                                 break
  305.                                 continue
  306.                             value.append(val)
  307.                         while u'!' == end:
  308.                             t = tokens.next()
  309.                             (type_, val, line, col) = t
  310.                             if simple(t):
  311.                                 continue
  312.                             if u'IDENT' == type_ and not important:
  313.                                 important = True
  314.                                 continue
  315.                             if u';' == val or u'}' == val:
  316.                                 end = val
  317.                                 break
  318.                                 continue
  319.                             self._errorHandler.error('unexpected priority token %r' % val)
  320.                         if name and value:
  321.                             self._handler.property(name, value, important)
  322.                         
  323.                         if u'}' == end:
  324.                             self._handler.endSelector(selectors, line = line, col = col)
  325.                             break
  326.                             continue
  327.                         end = None
  328.                     self._handler.endSelector(selectors, line = line, col = col)
  329.                 t = tokens.next()
  330.                 (type_, val, line, col) = t
  331.             continue
  332.             except StopIteration:
  333.                 None if 'CHARSET_SYM' == type_ else (None, ((None,),))
  334.                 None if 'CHARSET_SYM' == type_ else (None, ((None,),))
  335.                 break
  336.                 continue
  337.             
  338.  
  339.             None if 'CHARSET_SYM' == type_ else (None, ((None,),))<EXCEPTION MATCH>StopIteration
  340.  
  341.     
  342.     def setDocumentHandler(self, handler):
  343.         self._handler = handler
  344.  
  345.     
  346.     def setErrorHandler(self, handler):
  347.         self._errorHandler = handler
  348.  
  349.  
  350.