1
2 """a validating CSSParser
3 """
4 __all__ = ['CSSParser']
5 __docformat__ = 'restructuredtext'
6 __version__ = '$Id: parse.py 1367 2008-07-13 18:16:09Z cthedot $'
7
8 import codecs
9 import os
10 import sys
11 import urllib
12 import cssutils
13 from cssutils.helper import Deprecated
16 """
17 parses a CSS StyleSheet string or file and
18 returns a DOM Level 2 CSS StyleSheet object
19
20 Usage::
21
22 parser = CSSParser()
23
24 # optionally
25 parser.setFetcher(fetcher)
26
27 sheet = parser.parseFile('test1.css', 'ascii')
28
29 print sheet.cssText
30 """
31 - def __init__(self, log=None, loglevel=None, raiseExceptions=None,
32 fetcher=None):
33 """
34 log
35 logging object
36 loglevel
37 logging loglevel
38 raiseExceptions
39 if log should simply log (default) or raise errors during
40 parsing. Later while working with the resulting sheets
41 the setting used in cssutils.log.raiseExeptions is used
42 fetcher
43 see ``setFetchUrl(fetcher)``
44 """
45 if log is not None:
46 cssutils.log.setLog(log)
47 if loglevel is not None:
48 cssutils.log.setLevel(loglevel)
49
50
51 self.__globalRaising = cssutils.log.raiseExceptions
52 if raiseExceptions:
53 self.__parseRaising = raiseExceptions
54 else:
55
56 self.__parseRaising = False
57
58 self.__tokenizer = cssutils.tokenize2.Tokenizer()
59 self.setFetcher(fetcher)
60
62 """during parse exceptions may be handled differently depending on
63 init parameter ``raiseExceptions``
64 """
65 if parse:
66 cssutils.log.raiseExceptions = self.__parseRaising
67 else:
68 cssutils.log.raiseExceptions = self.__globalRaising
69
70 - def parseString(self, cssText, encoding=None, href=None, media=None,
71 title=None):
72 """Return parsed CSSStyleSheet from given string cssText.
73 Raises errors during retrieving (e.g. UnicodeDecodeError).
74
75 cssText
76 CSS string to parse
77 encoding
78 If ``None`` the encoding will be read from BOM or an @charset
79 rule or defaults to UTF-8.
80 If given overrides any found encoding including the ones for
81 imported sheets.
82 It also will be used to decode ``cssText`` if given as a (byte)
83 string.
84 href
85 The href attribute to assign to the parsed style sheet.
86 Used to resolve other urls in the parsed sheet like @import hrefs
87 media
88 The media attribute to assign to the parsed style sheet
89 (may be a MediaList, list or a string)
90 title
91 The title attribute to assign to the parsed style sheet
92 """
93 self.__parseSetting(True)
94
95 if isinstance(cssText, str):
96 cssText = codecs.getdecoder('css')(cssText, encoding=encoding)[0]
97
98 sheet = cssutils.css.CSSStyleSheet(href=href,
99 media=cssutils.stylesheets.MediaList(media),
100 title=title)
101 sheet._setFetcher(self.__fetcher)
102
103 sheet._setCssTextWithEncodingOverride(self.__tokenizer.tokenize(cssText,
104 fullsheet=True),
105 encoding)
106 self.__parseSetting(False)
107 return sheet
108
109 - def parseFile(self, filename, encoding=None,
110 href=None, media=None, title=None):
111 """Retrieve and return a CSSStyleSheet from given filename.
112 Raises errors during retrieving (e.g. IOError).
113
114 filename
115 of the CSS file to parse, if no ``href`` is given filename is
116 converted to a (file:) URL and set as ``href`` of resulting
117 stylesheet.
118 If href is given it is set as ``sheet.href``. Either way
119 ``sheet.href`` is used to resolve e.g. stylesheet imports via
120 @import rules.
121 encoding
122 Value ``None`` defaults to encoding detection via BOM or an
123 @charset rule.
124 Other values override detected encoding for the sheet at
125 ``filename`` including any imported sheets.
126
127 for other parameters see ``parseString``
128 """
129 if not href:
130
131 href = u'file:' + urllib.pathname2url(os.path.abspath(filename))
132
133 return self.parseString(open(filename, 'rb').read(),
134 encoding=encoding,
135 href=href, media=media, title=title)
136
137 - def parseUrl(self, href, encoding=None, media=None, title=None):
138 """Retrieve and return a CSSStyleSheet from given href (an URL).
139 In case of any errors while reading the URL returns None.
140
141 href
142 URL of the CSS file to parse, will also be set as ``href`` of
143 resulting stylesheet
144 encoding
145 Value ``None`` defaults to encoding detection via HTTP, BOM or an
146 @charset rule.
147 A value overrides detected encoding for the sheet at ``href``
148 including any imported sheets.
149
150 for other parameters see ``parseString``
151 """
152 encoding, text = cssutils.util._readUrl(href,
153 overrideEncoding=encoding)
154 if text is not None:
155 return self.parseString(text, encoding=encoding,
156 href=href, media=media, title=title)
157
159 """Replace the default URL fetch function with a custom one.
160 The fetcher function gets a single parameter
161
162 ``url``
163 the URL to read
164
165 and returns ``(encoding, content)`` where ``encoding`` is the HTTP
166 charset normally given via the Content-Type header (which may simply
167 omit the charset) and ``content`` being the (byte) string content.
168 The Mimetype should be 'text/css' but this has to be checked by the
169 fetcher itself (the default fetcher emits a warning if encountering
170 a different mimetype).
171
172 Calling ``setFetcher`` with ``fetcher=None`` resets cssutils
173 to use its default function.
174 """
175 self.__fetcher = fetcher
176
177 @Deprecated('Use cssutils.CSSParser().parseFile() instead.')
178 - def parse(self, filename, encoding=None,
179 href=None, media=None, title=None):
181