author: | $LastChangedBy: cthedot $ |
---|---|
date: | $LastChangedDate: 2007-08-08 16:14:03 +0200 (Mi, 08 Aug 2007) $ |
version: | $LastChangedRevision: 206 $ |
Contents
Contains tokenizer, parser and helper classes used by subpackages css and stylesheets. Implemented is also the interface DOMImplementationCSS.
codec: | A CSS codec registered at standard module codecs |
---|---|
CSSParser: | A CSS parser |
CSSSerializer: | A configurable CSS serializer |
log: | A configurable cssutils.errorhandler.ErrorHandler() logger |
DOMImplementationCSS: | Registers cssutils at standard package xml.dom |
You may use the provided CSS codec to read or write a CSS stylesheet. Handling is similar to other codecs. The codec handles the text encoding of the file automatically (regarding BOM or @charset rules in the stylesheet).
Example:
import codecs import cssutils cssText = codecs.open('sheets/test-unicode.css', encoding='css').read() sheet = cssutils.parseString(cssText)
Options to parse a given stylesheet:
There is only one global serializer used throughout the library. You may configure it or even replace it with your own.
A custom serializer must implement all methods the default one implements. Easiest would be to subclass cssutils.serialize.Serializer.
To set a new serializer, use:
sheet = CSSStyleSheet() sheet.setSerializer(newser)
You may also set cssutils.ser directly but the above method is the preferred one.
Quite a few preferences of the cssutils serializer may be tweaked.
To set a preference use:
sheet = CSSStyleSheet() sheet.setSerializerPref(pref, value)
You may also set cssutils.ser.prefs.pref directly but the above method is the preferred one.
Preferences are always used globally, so for all stylesheets until preferences are set again.
Two methods are available to use a predefined set of setting:
The following preferences are currently available (name = default value):
Should the normalized propertyname be used or the one given in the src file, e.g. if True: color else e.g.: c\olor
Only used if keepAllProperties==False.
If True only valid (Properties or Rules) are kept.
A Property is valid if it is a known Property with a valid value. Currently CSS 2.1 values as defined in cssproperties.py would be valid.
A global logger is used throughout the library. You may configure it or even replace it with your own. Customizing the default log should be sufficient for most purposes though.
The default logger is available as cssutils.log. It has the following methods:
log.setlog(newlog): To redirect logging output, the default output is sent to stderr
log.setloglevel(level): To change the log level. This should be a level defined by the logging module, e.g.:
import logging cssutils.log.setloglevel(logging.FATAL)