Package cssutils :: Module parse' |
[hide private]
[frames] | no frames]

# Source Code for Module cssutils.parse'

     1  #!/usr/bin/env python
     2  """a validating CSSParser
     3  """
     4  __all__ = ['CSSParser']
     5  __docformat__ = 'restructuredtext'
     6  __author__ = '$LastChangedBy: cthedot $'
     7  __date__ = '$LastChangedDate: 2007-12-28 12:35:14 +0100 (Fr, 28 Dez 2007) $'
     8  __version__ = '$LastChangedRevision: 751 $'
     9
    10  import codecs
    11  import cssutils
    12
    13 -class CSSParser(object):
    14      """
    15      parses a CSS StyleSheet string or file and
    16      returns a DOM Level 2 CSS StyleSheet object
    17
    18      Usage::
    19
    20          parser = CSSParser()
    21          stylesheet = p.parse('test1.css', 'ascii')
    22
    23          print stylesheet.cssText
    24      """
    25 -    def __init__(self, log=None, loglevel=None, raiseExceptions=False):
    26          """
    27          log
    28              logging object
    29          loglevel
    30              logging loglevel
    31          raiseExceptions
    32              if log should simple log (default) or raise errors
    33          """
    34          if log is not None:
    35              cssutils.log.setlog(log)
    36          if loglevel is not None:
    37              cssutils.log.setloglevel(loglevel)
    38
    39          cssutils.log.raiseExceptions = raiseExceptions
    40          self.__tokenizer = cssutils.tokenize2.Tokenizer()
    41
    42 -    def parseString(self, cssText, href=None, media=None):
    43          """
    44          parse a CSSStyleSheet string
    45          returns the parsed CSS as a CSSStyleSheet object
    46
    47          cssText
    48              CSS string to parse
    49          href
    50              The href attribute to assign to the generated stylesheet
    51          media
    52              The media attribute to assign to the generated stylesheet
    53              (may be a MediaList, list or a string)
    54          """
    55          sheet = cssutils.css.CSSStyleSheet()
    56          sheet.cssText = self.__tokenizer.tokenize(cssText, fullsheet=True)
    57          sheet.href = href
    58          sheet.media = cssutils.stylesheets.MediaList(media)
    59          return sheet
    60
    61 -    def parse(self, filename, encoding=None, href=None, media=None):
    62          """
    63          parse a CSSStyleSheet file
    64          returns the parsed CSS as a CSSStyleSheet object
    65
    66          filename
    67              name of the CSS file to parse
    68          encoding
    69              of the CSS file, defaults to 'css' codec encoding
    70          href
    71              The href attribute to assign to the generated stylesheet
    72          media
    73              The media attribute to assign to the generated stylesheet
    74              (may be a MediaList or a string)
    75          """
    76          if not encoding:
    77              encoding = 'css'
    78          return self.parseString(codecs.open(filename, 'r', encoding).read(),
    79                                  href=href, media=media)
    80