-
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathcssparse.py
More file actions
76 lines (64 loc) · 2.03 KB
/
cssparse.py
File metadata and controls
76 lines (64 loc) · 2.03 KB
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
"""utility script to parse given filenames or string"""
import logging
import optparse
import sys
import cssutils
def main(args=None):
"""
Parses given filename(s) or string or URL (using optional encoding) and
prints the parsed style sheet to stdout.
Redirect stdout to save CSS. Redirect stderr to save parser log infos.
"""
usage = """usage: %prog [options] filename1.css [filename2.css ...]
[>filename_combined.css] [2>parserinfo.log] """
p = optparse.OptionParser(usage=usage)
p.add_option(
'-s', '--string', action='store_true', dest='string', help='parse given string'
)
p.add_option('-u', '--url', action='store', dest='url', help='parse given url')
p.add_option(
'-e',
'--encoding',
action='store',
dest='encoding',
help='encoding of the file or override encoding found',
)
p.add_option(
'-m',
'--minify',
action='store_true',
dest='minify',
help='minify parsed CSS',
default=False,
)
p.add_option(
'-d',
'--debug',
action='store_true',
dest='debug',
help='activate debugging output',
)
(options, params) = p.parse_args(args)
if not params and not options.url:
p.error("no filename given")
if options.debug:
p = cssutils.CSSParser(loglevel=logging.DEBUG)
else:
p = cssutils.CSSParser()
if options.minify:
cssutils.ser.prefs.useMinified()
if options.string:
sheet = p.parseString(''.join(params), encoding=options.encoding)
print(sheet.cssText)
elif options.url:
sheet = p.parseUrl(options.url, encoding=options.encoding)
print(sheet.cssText)
else:
for filename in params:
sys.stderr.write('=== CSS FILE: "%s" ===\n' % filename)
sheet = p.parseFile(filename, encoding=options.encoding)
print(sheet.cssText)
print()
sys.stderr.write('\n')
if __name__ == "__main__":
sys.exit(main())