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  __version__ = '$Id: parse.py 1197 2008-03-23 00:17:51Z cthedot $'
      7
      8  import os
      9  import codecs
     10  import urllib
     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          sheet = p.parse('test1.css', 'ascii')
     22
     23          print sheet.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, encoding=None, href=None, media=None,
     43                      title=None):
     44          """Return parsed CSSStyleSheet from given string cssText.
     45
     46          cssText
     47              CSS string to parse
     48          encoding
     49              encoding of the CSS string. if ``None`` the encoding will be read
     50              from a @charset rule. If there is none, the parser will fall back
     51              to UTF-8. If cssText is a unicode string encoding will be ignored.
     52          href
     53              The href attribute to assign to the parsed style sheet.
     54              Used to resolve other urls in the parsed sheet like @import hrefs
     55          media
     56              The media attribute to assign to the parsed style sheet
     57              (may be a MediaList, list or a string)
     58          title
     59              The title attribute to assign to the parsed style sheet
     60          """
     61          if isinstance(cssText, str):
     62              cssText = codecs.getdecoder('css')(cssText, encoding=encoding)[0]
     63          sheet = cssutils.css.CSSStyleSheet()
     64          sheet._href = href
     65          sheet.media = cssutils.stylesheets.MediaList(media)
     66          sheet.title = title
     67          # does close open constructs and adds EOF
     68          sheet.cssText = self.__tokenizer.tokenize(cssText, fullsheet=True)
     69          return sheet
     70
     71 -    def parse(self, filename, encoding=None, href=None, media=None, title=None):
     72          """Retrieve and return a CSSStyleSheet from given filename.
     73
     74          filename
     75              of the CSS file to parse, if no ``href`` is given filename is
     76              converted to a (file:) URL and set as ``href`` of resulting
     77              stylesheet.
     78              If href is given it is set as ``sheet.href``. Either way
     79              ``sheet.href`` is used to resolve e.g. stylesheet imports via
     80              @import rules.
     81          encoding
     82              of the CSS file, ``None`` defaults to encoding detection from
     83              BOM or an @charset rule. ``encoding`` is used for the sheet at
     84              ``filename`` (and may override a file internal encoding)
     85              but **not** any imported sheets where the file internal encoding
     86              is detected.
     87
     88          for other parameters see ``parseString``
     89          """
     90          if not href:
     91              # prepend // for file URL, urllib does not do this?
     92              href = u'file:' + urllib.pathname2url(os.path.abspath(filename))
     93
     94          return self.parseString(open(filename, 'rb').read(), encoding=encoding,
     95                                  href=href, media=media, title=title)
     96
     97 -    def parseUrl(self, href, encoding=None, media=None, title=None):
     98          """Retrieve and return a CSSStyleSheet from given href (a URL).
     99
    100          href
    101              URL of the CSS file to parse, will also be set as ``href`` of
    102              resulting stylesheet
    103          encoding
    104              if given overrides detected HTTP or file internal encoding for
    105              sheet at ``href`` but **not** any imported sheets where the
    106              encoding if always detected via HTTP or from the file.
    107
    108          for other parameters see ``parseString``
    109          """
    110          return self.parseString(cssutils.util._readURL(href, encoding),
    111                                  href=href, media=media, title=title)
    112