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

Source Code for Module cssutils.parse'

  1  #!/usr/bin/env python 
  2  """a validating CSSParser 
  3  """ 
  4  __all__ = ['CSSParser'] 
  5  __docformat__ = 'restructuredtext' 
  6  __version__ = '$Id: parse.py 1323 2008-07-06 18:13:57Z cthedot $' 
  7   
  8  import codecs 
  9  import os 
 10  import sys 
 11  import urllib 
 12  import cssutils 
 13  from cssutils.helper import Deprecated 
14 15 -class CSSParser(object):
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=False, 32 fetcher=None):
33 """ 34 log 35 logging object 36 loglevel 37 logging loglevel 38 raiseExceptions 39 if log should simple log (default) or raise errors 40 fetcher 41 see ``setFetchUrl(fetcher)`` 42 """ 43 if log is not None: 44 cssutils.log.setLog(log) 45 if loglevel is not None: 46 cssutils.log.setLevel(loglevel) 47 48 cssutils.log.raiseExceptions = raiseExceptions 49 self.__tokenizer = cssutils.tokenize2.Tokenizer() 50 self.setFetcher(fetcher)
51
52 - def parseString(self, cssText, encoding=None, href=None, media=None, 53 title=None):
54 """Return parsed CSSStyleSheet from given string cssText. 55 Raises errors during retrieving (e.g. UnicodeDecodeError). 56 57 cssText 58 CSS string to parse 59 encoding 60 If ``None`` the encoding will be read from BOM or an @charset 61 rule or defaults to UTF-8. 62 If given overrides any found encoding including the ones for 63 imported sheets. 64 It also will be used to decode ``cssText`` if given as a (byte) 65 string. 66 href 67 The href attribute to assign to the parsed style sheet. 68 Used to resolve other urls in the parsed sheet like @import hrefs 69 media 70 The media attribute to assign to the parsed style sheet 71 (may be a MediaList, list or a string) 72 title 73 The title attribute to assign to the parsed style sheet 74 """ 75 if isinstance(cssText, str): 76 cssText = codecs.getdecoder('css')(cssText, encoding=encoding)[0] 77 78 sheet = cssutils.css.CSSStyleSheet(href=href, 79 media=cssutils.stylesheets.MediaList(media), 80 title=title) 81 sheet._setFetcher(self.__fetcher) 82 # tokenizing this ways closes open constructs and adds EOF 83 sheet._setCssTextWithEncodingOverride(self.__tokenizer.tokenize(cssText, 84 fullsheet=True), 85 encoding) 86 return sheet
87
88 - def parseFile(self, filename, encoding=None, 89 href=None, media=None, title=None):
90 """Retrieve and return a CSSStyleSheet from given filename. 91 Raises errors during retrieving (e.g. IOError). 92 93 filename 94 of the CSS file to parse, if no ``href`` is given filename is 95 converted to a (file:) URL and set as ``href`` of resulting 96 stylesheet. 97 If href is given it is set as ``sheet.href``. Either way 98 ``sheet.href`` is used to resolve e.g. stylesheet imports via 99 @import rules. 100 encoding 101 Value ``None`` defaults to encoding detection via BOM or an 102 @charset rule. 103 Other values override detected encoding for the sheet at 104 ``filename`` including any imported sheets. 105 106 for other parameters see ``parseString`` 107 """ 108 if not href: 109 # prepend // for file URL, urllib does not do this? 110 href = u'file:' + urllib.pathname2url(os.path.abspath(filename)) 111 112 return self.parseString(open(filename, 'rb').read(), 113 encoding=encoding, # read returns a str 114 href=href, media=media, title=title)
115
116 - def parseUrl(self, href, encoding=None, media=None, title=None):
117 """Retrieve and return a CSSStyleSheet from given href (an URL). 118 In case of any errors while reading the URL returns None. 119 120 href 121 URL of the CSS file to parse, will also be set as ``href`` of 122 resulting stylesheet 123 encoding 124 Value ``None`` defaults to encoding detection via HTTP, BOM or an 125 @charset rule. 126 A value overrides detected encoding for the sheet at ``href`` 127 including any imported sheets. 128 129 for other parameters see ``parseString`` 130 """ 131 encoding, text = cssutils.util._readUrl(href, 132 overrideEncoding=encoding) 133 if text is not None: 134 return self.parseString(text, encoding=encoding, 135 href=href, media=media, title=title)
136
137 - def setFetcher(self, fetcher=None):
138 """Replace the default URL fetch function with a custom one. 139 The fetcher function gets a single parameter 140 141 ``url`` 142 the URL to read 143 144 and returns ``(encoding, content)`` where ``encoding`` is the HTTP 145 charset normally given via the Content-Type header (which may simply 146 omit the charset) and ``content`` being the (byte) string content. 147 The Mimetype should be 'text/css' but this has to be checked by the 148 fetcher itself (the default fetcher emits a warning if encountering 149 a different mimetype). 150 151 Calling ``setFetcher`` with ``fetcher=None`` resets cssutils 152 to use its default function. 153 """ 154 self.__fetcher = fetcher
155 156 @Deprecated('Use cssutils.CSSParser().parseFile() instead.')
157 - def parse(self, filename, encoding=None, 158 href=None, media=None, title=None):
159 self.parseFile(filename, encoding, href, media, title)
160