-
Notifications
You must be signed in to change notification settings - Fork 0
/
cssextract.py
60 lines (49 loc) · 1.75 KB
/
cssextract.py
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
#!/usr/bin/python
import argparse
import os
import sys
import cssutils
import cssutils.css
### Parse args
cliParser = argparse.ArgumentParser(
description="CSSExtract - Extract CSS affecting specific properties")
cliParser.add_argument('-f',
'--force',
help="Overwrite without prompting.",
action='store_true')
cliParser.add_argument('-p',
'--property',
help="CSS property to filter",
action='append')
cliParser.add_argument('infile', help="Input file names", nargs='+')
cliParser.add_argument('outfile', help="Output file name")
args = cliParser.parse_args()
### Process
outCss = cssutils.css.CSSStyleSheet()
for inFile in args.infile:
inCss = cssutils.parseFile(inFile)
for rule in inCss.cssRules.rulesOfType(cssutils.css.CSSRule.STYLE_RULE):
for prop in rule.style:
if prop.name not in args.property:
continue
targetRule = None
outRules = outCss.cssRules.rulesOfType(cssutils.css.CSSRule.STYLE_RULE)
for outRule in outRules:
if outRule.selectorText == rule.selectorText:
targetRule = outRule
break
if not targetRule:
targetRule = cssutils.css.CSSStyleRule(rule.selectorText)
outCss.add(targetRule)
if prop.priority == 'important':
targetRule.style[prop.name] = (prop.value, 'important')
else:
targetRule.style[prop.name] = prop.value
if os.path.exists(args.outfile) and not args.force:
print ('File ' + args.outfile + ' already exists.')
input = input('Overwrite? [y/N]: ')
if input.strip() != 'y':
print ('Nothing done.')
sys.exit(1)
with open(args.outfile, 'wb') as file:
file.write(outCss.cssText)