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

# Class CSSParser

source code

    object --+
             |
            CSSParser

parses a CSS StyleSheet string or file and returns a DOM Level 2 CSS StyleSheet object

Usage:

    parser = CSSParser()

    # optionally
    parser.setFetcher(fetcher)

    sheet = parser.parseFile('test1.css', 'ascii')

    print sheet.cssText

Instance Methods | [hide private]
__init__(self, log=None, loglevel=None, raiseExceptions=False, fetcher=None) logging object | source code
parseString(self, cssText, encoding=None, href=None, media=None, title=None) Return parsed CSSStyleSheet from given string cssText. | source code
parseFile(self, filename, encoding=None, href=None, media=None, title=None) Retrieve and return a CSSStyleSheet from given filename. | source code
parseUrl(self, href, encoding=None, media=None, title=None) Retrieve and return a CSSStyleSheet from given href (an URL). | source code
setFetcher(self, fetcher=None) Replace the default URL fetch function with a custom one. | source code
parse(*args, **kwargs) | source code
Inherited from `object`: `__delattr__`, `__getattribute__`, `__hash__`, `__new__`, `__reduce__`, `__reduce_ex__`, `__repr__`, `__setattr__`, `__str__`

Properties | [hide private]
Inherited from `object`: `__class__`

Method Details | [hide private]

### __init__(self, log=None, loglevel=None, raiseExceptions=False, fetcher=None) (Constructor) | source code
log logging object loglevel logging loglevel raiseExceptions if log should simple log (default) or raise errors fetcher see `setFetchUrl(fetcher)` Overrides: object.__init__

### parseString(self, cssText, encoding=None, href=None, media=None, title=None) | source code
Return parsed CSSStyleSheet from given string cssText. Raises errors during retrieving (e.g. UnicodeDecodeError). cssText CSS string to parse 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) string. href The href attribute to assign to the parsed style sheet. Used to resolve other urls in the parsed sheet like @import hrefs media The media attribute to assign to the parsed style sheet (may be a MediaList, list or a string) title The title attribute to assign to the parsed style sheet

### parseFile(self, filename, encoding=None, href=None, media=None, title=None) | source code
Retrieve and return a CSSStyleSheet from given filename. Raises errors during retrieving (e.g. IOError). 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 stylesheet. 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. 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. for other parameters see `parseString`

### parseUrl(self, href, encoding=None, media=None, title=None) | source code
Retrieve and return a CSSStyleSheet from given href (an URL). In case of any errors while reading the URL returns None. href URL of the CSS file to parse, will also be set as `href` of resulting stylesheet 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. for other parameters see `parseString`

### setFetcher(self, fetcher=None) | source code
Replace the default URL fetch function with a custom one. The fetcher function gets a single parameter `url` the URL to read and returns `(encoding, content)` where `encoding` is the HTTP charset normally given via the Content-Type header (which may simply omit the charset) and `content` being the (byte) string 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.

### parse(*args, **kwargs) | source code
Decorators: `@Deprecated('Use cssutils.CSSParser().parseFile() instead.')`