/usr/lib/python2.7/dist-packages/cssutils/script.py is in python-cssutils 1.0-4.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | """classes and functions used by cssutils scripts
"""
__all__ = ['CSSCapture', 'csscombine']
__docformat__ = 'restructuredtext'
__version__ = '$Id: parse.py 1323 2008-07-06 18:13:57Z cthedot $'
import HTMLParser
import codecs
import cssutils
import errno
import logging
import os
import sys
import urllib2
import urlparse
try:
    import cssutils.encutils as encutils
except ImportError:
    try:
        import encutils
    except ImportError:
        sys.exit("You need encutils from http://cthedot.de/encutils/")
# types of sheets in HTML
LINK = 0 # <link rel="stylesheet" type="text/css" href="..." [@title="..." @media="..."]/>
STYLE = 1 # <style type="text/css" [@title="..."]>...</style>
class CSSCaptureHTMLParser(HTMLParser.HTMLParser):
    """CSSCapture helper: Parse given data for link and style elements"""
    curtag = u''
    sheets = [] # (type, [atts, cssText])
    def _loweratts(self, atts):
        return dict([(a.lower(), v.lower()) for a, v in atts])
    def handle_starttag(self, tag, atts):
        if tag == u'link':
            atts = self._loweratts(atts)
            if u'text/css' == atts.get(u'type', u''):
                self.sheets.append((LINK, atts))
        elif tag == u'style':
            # also get content of style
            atts = self._loweratts(atts)
            if u'text/css' == atts.get(u'type', u''):
                self.sheets.append((STYLE, [atts, u'']))
                self.curtag = tag
        else:
            # close as only intersting <style> cannot contain any elements
            self.curtag = u''
    def handle_data(self, data):
        if self.curtag == u'style':
            self.sheets[-1][1][1] = data # replace cssText
    def handle_comment(self, data):
        # style might have comment content, treat same as data
        self.handle_data(data)
    def handle_endtag(self, tag):
        # close as style cannot contain any elements
        self.curtag = u''
class CSSCapture(object):
    """
    Retrieve all CSS stylesheets including embedded for a given URL.
    Optional setting of User-Agent used for retrieval possible
    to handle browser sniffing servers.
    raises urllib2.HTTPError
    """
    def __init__(self, ua=None, log=None, defaultloglevel=logging.INFO):
        """
        initialize a new Capture object
        ua
            init User-Agent to use for requests
        log
            supply a log object which is used instead of the default
            log which writes to sys.stderr
        defaultloglevel
            constant of logging package which defines the level of the
            default log if no explicit log given
        """
        self._ua = ua
        if log:
            self._log = log
        else:
            self._log = logging.getLogger('CSSCapture')
            hdlr = logging.StreamHandler(sys.stderr)
            formatter = logging.Formatter('%(message)s')
            hdlr.setFormatter(formatter)
            self._log.addHandler(hdlr)
            self._log.setLevel(defaultloglevel)
            self._log.debug(u'Using default log')
        self._htmlparser = CSSCaptureHTMLParser()
        self._cssparser = cssutils.CSSParser(log = self._log)
    def _doRequest(self, url):
        """Do an HTTP request
        Return (url, rawcontent)
            url might have been changed by server due to redirects etc
        """
        self._log.debug(u'    CSSCapture._doRequest\n        * URL: %s' % url)
        req = urllib2.Request(url)
        if self._ua:
            req.add_header('User-agent', self._ua)
            self._log.info('        * Using User-Agent: %s', self._ua)
        try:
            res = urllib2.urlopen(req)
        except urllib2.HTTPError, e:
            self._log.critical('    %s\n%s %s\n%s' % (
                e.geturl(), e.code, e.msg, e.headers))
            return None, None
        # get real url
        if url != res.geturl():
            url = res.geturl()
            self._log.info('        URL retrieved: %s', url)
        return url, res
    def _createStyleSheet(self, href=None,
                          media=None,
                          parentStyleSheet=None,
                          title=u'',
                          cssText=None,
                          encoding=None):
        """
        Return CSSStyleSheet read from href or if cssText is given use that.
        encoding
            used if inline style found, same as self.docencoding
        """
        if cssText is None:
            encoding, enctype, cssText = cssutils.util._readUrl(href, parentEncoding=self.docencoding)
            encoding = None # already decoded???
        sheet = self._cssparser.parseString(cssText, href=href, media=media, title=title,
                                            encoding=encoding)
        if not sheet:
            return None
        else:
            self._log.info(u'    %s\n' % sheet)
            self._nonparsed[sheet] = cssText
            return sheet
    def _findStyleSheets(self, docurl, doctext):
        """
        parse text for stylesheets
        fills stylesheetlist with all found StyleSheets
        docurl
            to build a full url of found StyleSheets @href
        doctext
            to parse
        """
        # TODO: ownerNode should be set to the <link> node
        self._htmlparser.feed(doctext)
        for typ, data in self._htmlparser.sheets:
            sheet = None
            if LINK == typ:
                self._log.info(u'+ PROCESSING <link> %r' % data)
                atts = data
                href = urlparse.urljoin(docurl, atts.get(u'href', None))
                sheet = self._createStyleSheet(href=href,
                                               media=atts.get(u'media', None),
                                               title=atts.get(u'title', None))
            elif STYLE == typ:
                self._log.info(u'+ PROCESSING <style> %r' % data)
                atts, cssText = data
                sheet = self._createStyleSheet(cssText=cssText,
                                               href = docurl,
                                               media=atts.get(u'media', None),
                                               title=atts.get(u'title', None),
                                               encoding=self.docencoding)
                if sheet:
                    sheet._href = None # inline have no href!
                print sheet.cssText
            if sheet:
                self.stylesheetlist.append(sheet)
                self._doImports(sheet, base=docurl)
    def _doImports(self, parentStyleSheet, base=None):
        """
        handle all @import CSS stylesheet recursively
        found CSS stylesheets are appended to stylesheetlist
        """
        # TODO: only if not parsed these have to be read extra!
        for rule in parentStyleSheet.cssRules:
            if rule.type == rule.IMPORT_RULE:
                self._log.info(u'+ PROCESSING @import:')
                self._log.debug(u'    IN: %s\n' % parentStyleSheet.href)
                sheet = rule.styleSheet
                href = urlparse.urljoin(base, rule.href)
                if sheet:
                    self._log.info(u'    %s\n' % sheet)
                    self.stylesheetlist.append(sheet)
                    self._doImports(sheet, base=href)
    def capture(self, url):
        """
        Capture all stylesheets at given URL's HTML document.
        Any HTTPError is raised to caller.
        url
            to capture CSS from
        Returns ``cssutils.stylesheets.StyleSheetList``.
        """
        self._log.info(u'\nCapturing CSS from URL:\n    %s\n', url)
        self._nonparsed = {}
        self.stylesheetlist = cssutils.stylesheets.StyleSheetList()
        # used to save inline styles
        scheme, loc, path, query, fragment = urlparse.urlsplit(url)
        self._filename = os.path.basename(path)
        # get url content
        url, res = self._doRequest(url)
        if not res:
            sys.exit(1)
        rawdoc = res.read()
        self.docencoding = encutils.getEncodingInfo(
            res, rawdoc, log=self._log).encoding
        self._log.info(u'\nUsing Encoding: %s\n', self.docencoding)
        doctext = rawdoc.decode(self.docencoding)
        # fill list of stylesheets and list of raw css
        self._findStyleSheets(url, doctext)
        return self.stylesheetlist
    def saveto(self, dir, saveraw=False, minified=False):
        """
        saves css in "dir" in the same layout as on the server
        internal stylesheets are saved as "dir/__INLINE_STYLE__.html.css"
        dir
            directory to save files to
        saveparsed
            save literal CSS from server or save the parsed CSS
        minified
            save minified CSS
        Both parsed and minified (which is also parsed of course) will
        loose information which cssutils is unable to understand or where
        it is simple buggy. You might to first save the raw version before
        parsing of even minifying it.
        """
        msg = 'parsed'
        if saveraw:
            msg = 'raw'
        if minified:
            cssutils.ser.prefs.useMinified()
            msg = 'minified'
        inlines = 0
        for i, sheet in enumerate(self.stylesheetlist):
            url = sheet.href
            if not url:
                inlines += 1
                url = u'%s_INLINE_%s.css' % (self._filename, inlines)
            # build savepath
            scheme, loc, path, query, fragment = urlparse.urlsplit(url)
            # no absolute path
            if path and path.startswith('/'):
                path = path[1:]
            path = os.path.normpath(path)
            path, fn = os.path.split(path)
            savepath = os.path.join(dir, path)
            savefn = os.path.join(savepath, fn)
            try:
                os.makedirs(savepath)
            except OSError, e:
                if e.errno != errno.EEXIST:
                    raise e
                self._log.debug(u'Path "%s" already exists.', savepath)
            self._log.info(u'SAVING %s, %s %r' % (i+1, msg, savefn))
            sf = open(savefn, 'wb')
            if saveraw:
                cssText = self._nonparsed[sheet]
                uf = codecs.getwriter('css')(sf)
                uf.write(cssText)
            else:
                sf.write(sheet.cssText)
            sf.close()
def csscombine(path=None, url=None, cssText=None, href=None,
               sourceencoding=None, targetencoding=None, 
               minify=True, resolveVariables=True):
    """Combine sheets referred to by @import rules in given CSS proxy sheet
    into a single new sheet.
    :returns: combined cssText, normal or minified
    :Parameters:
        `path` or `url` or `cssText` + `href`
            path or URL to a CSSStyleSheet or a cssText of a sheet which imports
            other sheets which are then combined into one sheet.
            `cssText` normally needs `href` to be able to resolve relative
            imports.
        `sourceencoding` = 'utf-8'
            explicit encoding of the source proxysheet
        `targetencoding`
            encoding of the combined stylesheet
        `minify` = True
            defines if the combined sheet should be minified, in this case
            comments are not parsed at all!
        `resolveVariables` = True
            defines if variables in combined sheet should be resolved
    """
    cssutils.log.info(u'Combining files from %r' % url, 
                      neverraise=True)
    if sourceencoding is not None:
        cssutils.log.info(u'Using source encoding %r' % sourceencoding,
                          neverraise=True)
        
    parser = cssutils.CSSParser(parseComments=not minify)
        
    if path and not cssText:
        src = parser.parseFile(path, encoding=sourceencoding)
    elif url:
        src = parser.parseUrl(url, encoding=sourceencoding)
    elif cssText:
        src = parser.parseString(cssText, href=href, encoding=sourceencoding)
    else:
        sys.exit('Path or URL must be given')
    result = cssutils.resolveImports(src)
    result.encoding = targetencoding
    cssutils.log.info(u'Using target encoding: %r' % targetencoding, neverraise=True)
    oldser = cssutils.ser
    cssutils.setSerializer(cssutils.serialize.CSSSerializer())
    if minify:
        cssutils.ser.prefs.useMinified()
    cssutils.ser.prefs.resolveVariables = resolveVariables
    cssText = result.cssText
    cssutils.setSerializer(oldser)
    
    return cssText
 |