1
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
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):
41
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