Logo Search packages:      
Sourcecode: calibre version File versions


#!/usr/bin/env python
"""A validating CSSParser"""
__all__ = ['CSSParser']
__docformat__ = 'restructuredtext'
__version__ = '$Id: parse.py 1754 2009-05-30 14:50:13Z cthedot $'

from helper import Deprecated, path2url
import codecs
import cssutils
import os
import tokenize2
import urllib

00014 class CSSParser(object):
    """Parse a CSS StyleSheet from URL, string or file and return a DOM Level 2
    CSS StyleSheet object.


        parser = CSSParser()
        # optionally
        sheet = parser.parseFile('test1.css', 'ascii')
        print sheet.cssText
00026     def __init__(self, log=None, loglevel=None, raiseExceptions=None,
            logging object
            logging loglevel
            if log should simply log (default) or raise errors during
            parsing. Later while working with the resulting sheets
            the setting used in cssutils.log.raiseExeptions is used
            see ``setFetcher(fetcher)``
        if log is not None:
        if loglevel is not None:

        # remember global setting
        self.__globalRaising = cssutils.log.raiseExceptions
        if raiseExceptions:
            self.__parseRaising = raiseExceptions
            # DEFAULT during parse
            self.__parseRaising = False

        self.__tokenizer = tokenize2.Tokenizer()

00056     def __parseSetting(self, parse):
        """during parse exceptions may be handled differently depending on
        init parameter ``raiseExceptions``
        if parse:
            cssutils.log.raiseExceptions = self.__parseRaising
            cssutils.log.raiseExceptions = self.__globalRaising

00065     def parseString(self, cssText, encoding=None, href=None, media=None,
        """Parse `cssText` as :class:`~cssutils.css.CSSStyleSheet`.
        Errors may be raised (e.g. UnicodeDecodeError).

        :param cssText:
            CSS string to parse
        :param encoding:
            If ``None`` the encoding will be read from BOM or an @charset
            rule or defaults to UTF-8.
            If given overrides any found encoding including the ones for
            imported sheets.
            It also will be used to decode `cssText` if given as a (byte)
        :param href:
            The ``href`` attribute to assign to the parsed style sheet.
            Used to resolve other urls in the parsed sheet like @import hrefs.
        :param media:
            The ``media`` attribute to assign to the parsed style sheet
            (may be a MediaList, list or a string).
        :param title:
            The ``title`` attribute to assign to the parsed style sheet.
        if isinstance(cssText, str):
            cssText = codecs.getdecoder('css')(cssText, encoding=encoding)[0]

        sheet = cssutils.css.CSSStyleSheet(href=href,
        # tokenizing this ways closes open constructs and adds EOF
        return sheet

00105     def parseFile(self, filename, encoding=None,
                  href=None, media=None, title=None):
        """Retrieve content from `filename` and parse it. Errors may be raised
        (e.g. IOError).
        :param filename:
            of the CSS file to parse, if no `href` is given filename is
            converted to a (file:) URL and set as ``href`` of resulting
            If `href` is given it is set as ``sheet.href``. Either way
            ``sheet.href`` is used to resolve e.g. stylesheet imports via
            @import rules.
        :param encoding:
            Value ``None`` defaults to encoding detection via BOM or an
            @charset rule.
            Other values override detected encoding for the sheet at
            `filename` including any imported sheets.
        if not href:
            # prepend // for file URL, urllib does not do this?
            #href = u'file:' + urllib.pathname2url(os.path.abspath(filename))          
            href = path2url(filename)

        return self.parseString(open(filename, 'rb').read(),
                                encoding=encoding, # read returns a str
                                href=href, media=media, title=title)

00134     def parseUrl(self, href, encoding=None, media=None, title=None):
        """Retrieve content from URL `href` and parse it. Errors may be raised
        (e.g. URLError).
        :param href:
            URL of the CSS file to parse, will also be set as ``href`` of
            resulting stylesheet
        :param encoding:
            Value ``None`` defaults to encoding detection via HTTP, BOM or an
            @charset rule.
            A value overrides detected encoding for the sheet at ``href``
            including any imported sheets.
        encoding, enctype, text = cssutils.util._readUrl(href,
        if enctype == 5:
            # do not used if defaulting to UTF-8
            encoding = None
        if text is not None:
            return self.parseString(text, encoding=encoding,
                                    href=href, media=media, title=title)

00159     def setFetcher(self, fetcher=None):
        """Replace the default URL fetch function with a custom one.
        :param fetcher:
            A function which gets a single parameter

                the URL to read

            and must return ``(encoding, content)`` where ``encoding`` is the 
            HTTP charset normally given via the Content-Type header (which may
            simply omit the charset in which case ``encoding`` would be 
            ``None``) and ``content`` being the string (or unicode) content.
            The Mimetype should be 'text/css' but this has to be checked by the
            fetcher itself (the default fetcher emits a warning if encountering
            a different mimetype).

            Calling ``setFetcher`` with ``fetcher=None`` resets cssutils
            to use its default function.
        self.__fetcher = fetcher

    @Deprecated('Use cssutils.CSSParser().parseFile() instead.')
    def parse(self, filename, encoding=None,
              href=None, media=None, title=None):
        self.parseFile(filename, encoding, href, media, title)

Generated by  Doxygen 1.6.0   Back to index