home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- __all__ = [
- 'CSSParser']
- __docformat__ = 'restructuredtext'
- __version__ = '$Id: parse.py 1754 2009-05-30 14:50:13Z cthedot $'
- import helper
- import codecs
- import errorhandler
- import os
- import tokenize2
- import urllib
- import sys
-
- class ErrorHandler(object):
-
- def __init__(self):
- self._log = errorhandler.ErrorHandler()
-
-
- def error(self, exception, token = None):
- self._log.error(exception, token, neverraise = True)
-
-
- def fatal(self, exception, token = None):
- self._log.fatal(exception, token)
-
-
- def warn(self, exception, token = None):
- self._log.warn(exception, token, neverraise = True)
-
-
-
- class DocumentHandler(object):
-
- def __init__(self):
-
- def log(msg):
- sys.stderr.write('INFO\t%s\n' % msg)
-
- self._log = log
-
-
- def comment(self, text, line = None, col = None):
- self._log('comment %r at [%s, %s]' % (text, line, col))
-
-
- def startDocument(self, encoding):
- self._log('startDocument encoding=%s' % encoding)
-
-
- def endDocument(self, source = None, line = None, col = None):
- self._log('endDocument EOF')
-
-
- def importStyle(self, uri, media, name, line = None, col = None):
- self._log('importStyle at [%s, %s]' % (line, col))
-
-
- def namespaceDeclaration(self, prefix, uri, line = None, col = None):
- self._log('namespaceDeclaration at [%s, %s]' % (line, col))
-
-
- def startSelector(self, selectors = None, line = None, col = None):
- self._log('startSelector at [%s, %s]' % (line, col))
-
-
- def endSelector(self, selectors = None, line = None, col = None):
- self._log('endSelector at [%s, %s]' % (line, col))
-
-
- def property(self, name, value = 'TODO', important = False, line = None, col = None):
- self._log('property %r at [%s, %s]' % (name, line, col))
-
-
- def ignorableAtRule(self, atRule, line = None, col = None):
- self._log('ignorableAtRule %r at [%s, %s]' % (atRule, line, col))
-
-
-
- class EchoHandler(DocumentHandler):
-
- def __init__(self):
- super(EchoHandler, self).__init__()
- self._out = []
-
- out = property((lambda self: u''.join(self._out)))
-
- def startDocument(self, encoding):
- super(EchoHandler, self).startDocument(encoding)
- if u'utf-8' != encoding:
- self._out.append(u'@charset "%s";\n' % encoding)
-
-
-
- def importStyle(self, uri, media, name, line = None, col = None):
- super(EchoHandler, self).importStyle(uri, media, name, line, col)
- None(None % (self._out.append, u'@import %s%s%s;\n', helper.string(uri) if media else u'' if name else u''))
-
-
- def namespaceDeclaration(self, prefix, uri, line = None, col = None):
- super(EchoHandler, self).namespaceDeclaration(prefix, uri, line, col)
- None(self._out.append % (u'@namespace %s%s;\n' if prefix else u'', helper.string(uri)))
-
-
- def startSelector(self, selectors = None, line = None, col = None):
- super(EchoHandler, self).startSelector(selectors, line, col)
- if selectors:
- self._out.append(u', '.join(selectors))
-
- self._out.append(u' {\n')
-
-
- def endSelector(self, selectors = None, line = None, col = None):
- self._out.append(u' }')
-
-
- def property(self, name, value, important = False, line = None, col = None):
- super(EchoHandler, self).property(name, value, line, col)
- None(self._out.append % (u' %s: %s%s;\n', name, value if important else u''))
-
-
-
- class Parser(object):
-
- def __init__(self, documentHandler = None, errorHandler = None):
- self._tokenizer = tokenize2.Tokenizer()
- if documentHandler:
- self.setDocumentHandler(documentHandler)
- else:
- self.setDocumentHandler(DocumentHandler())
- if errorHandler:
- self.setErrorHandler(errorHandler)
- else:
- self.setErrorHandler(ErrorHandler())
-
-
- def parseString(self, cssText, encoding = None):
- if isinstance(cssText, str):
- cssText = codecs.getdecoder('css')(cssText, encoding = encoding)[0]
-
- tokens = self._tokenizer.tokenize(cssText, fullsheet = True)
-
- def COMMENT(val, line, col):
- self._handler.comment(val[2:-2], line, col)
-
-
- def EOF(val, line, col):
- self._handler.endDocument(val, line, col)
-
-
- def simple(t):
- map = {
- 'COMMENT': COMMENT,
- 'S': (lambda val, line, col: pass),
- 'EOF': EOF }
- (type_, val, line, col) = t
- if type_ in map:
- map[type_](val, line, col)
- return True
- return False
-
- t = tokens.next()
- (type_, val, line, col) = t
- encoding = 'utf-8'
- self._handler.startDocument(encoding)
- while True:
- start = (line, col)
-
- try:
- if simple(t):
- pass
- elif 'ATKEYWORD' == type_ or type_ in ('PAGE_SYM', 'MEDIA_SYM', 'FONT_FACE_SYM'):
- atRule = [
- val]
- braces = 0
- while True:
- t = tokens.next()
- (type_, val, line, col) = t
- atRule.append(val)
- if u';' == val and not braces:
- break
- continue
- if u'{' == val:
- braces += 1
- continue
- if u'}' == val:
- braces -= 1
- if braces == 0:
- break
-
- braces == 0
- self._handler.ignorableAtRule(u''.join(atRule), *start)
- elif 'IMPORT_SYM' == type_:
- (uri, media, name) = (None, None, None)
- while True:
- t = tokens.next()
- (type_, val, line, col) = t
- if 'STRING' == type_:
- uri = helper.stringvalue(val)
- continue
- if 'URI' == type_:
- uri = helper.urivalue(val)
- continue
- if u';' == val:
- break
- continue
- if uri:
- self._handler.importStyle(uri, media, name)
- else:
- self._errorHandler.error(u'Invalid @import declaration at %r' % (start,))
- elif 'NAMESPACE_SYM' == type_:
- (prefix, uri) = (None, None)
- while True:
- t = tokens.next()
- (type_, val, line, col) = t
- if 'IDENT' == type_:
- prefix = val
- continue
- if 'STRING' == type_:
- uri = helper.stringvalue(val)
- continue
- if 'URI' == type_:
- uri = helper.urivalue(val)
- continue
- if u';' == val:
- break
- continue
- if uri:
- self._handler.namespaceDeclaration(prefix, uri, *start)
- else:
- self._errorHandler.error(u'Invalid @namespace declaration at %r' % (start,))
- else:
- selector = []
- selectors = []
- while True:
- if 'S' == type_:
- selector.append(u' ')
- elif simple(t):
- pass
- elif u',' == val:
- selectors.append(u''.join(selector).strip())
- selector = []
- elif u'{' == val:
- selectors.append(u''.join(selector).strip())
- self._handler.startSelector(selectors, *start)
- break
- else:
- selector.append(val)
- t = tokens.next()
- (type_, val, line, col) = t
- end = None
- while True:
- name = None
- value = []
- important = False
- while True:
- t = tokens.next()
- (type_, val, line, col) = t
- if 'S' == type_:
- continue
- if simple(t):
- continue
- if 'IDENT' == type_:
- if name:
- self._errorHandler.error('more than one property name', t)
- else:
- name = val
- name
- if u':' == val:
- if not name:
- self._errorHandler.error('no property name', t)
-
- break
- continue
- if u';' == val:
- self._errorHandler.error('premature end of property', t)
- end = val
- break
- continue
- if u'}' == val:
- if name:
- self._errorHandler.error('premature end of property', t)
-
- end = val
- break
- continue
- self._errorHandler.error('unexpected property name token %r' % val, t)
- while not (u';' == end) and not (u'}' == end):
- t = tokens.next()
- (type_, val, line, col) = t
- if 'S' == type_:
- value.append(u' ')
- continue
- if simple(t):
- continue
- if u'!' == val and u';' == val or u'}' == val:
- value = ''.join(value).strip()
- if not value:
- self._errorHandler.error('premature end of property (no value)', t)
-
- end = val
- break
- continue
- value.append(val)
- while u'!' == end:
- t = tokens.next()
- (type_, val, line, col) = t
- if simple(t):
- continue
- if u'IDENT' == type_ and not important:
- important = True
- continue
- if u';' == val or u'}' == val:
- end = val
- break
- continue
- self._errorHandler.error('unexpected priority token %r' % val)
- if name and value:
- self._handler.property(name, value, important)
-
- if u'}' == end:
- self._handler.endSelector(selectors, line = line, col = col)
- break
- continue
- end = None
- self._handler.endSelector(selectors, line = line, col = col)
- t = tokens.next()
- (type_, val, line, col) = t
- continue
- except StopIteration:
- None if 'CHARSET_SYM' == type_ else (None, ((None,),))
- None if 'CHARSET_SYM' == type_ else (None, ((None,),))
- break
- continue
-
-
- None if 'CHARSET_SYM' == type_ else (None, ((None,),))<EXCEPTION MATCH>StopIteration
-
-
- def setDocumentHandler(self, handler):
- self._handler = handler
-
-
- def setErrorHandler(self, handler):
- self._errorHandler = handler
-
-
-