-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcss.py
81 lines (75 loc) · 2.33 KB
/
css.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# -*- coding: utf-8 -*-
"""
@ Autor: [[Usuário:Danilo.mac]]
@ Licença: GNU General Public License 2.0 (GPLv2)
Script para ler configurações escritas em formato .css
"""
import re
def iteritems(text):
"""
Analisador léxico baseado em regex
"""
reTokens = re.compile(ur'''\s*
(?:
(?P<quote>["'])|
(?P<open>\{)|
(?P<close>\})|
(?P<escape>\\)|
(?P<colon>:)|
(?P<comma>,)|
(?P<comment>/\*)|
(?P<word>[\w\d]+|\S)
)''', re.X)
reEndCom = re.compile(ur'\*/')
def fword(w):
w = w.strip()
return w[0] + w[-1] in ('""', "''") and w[1:-1] or w
quote, escape, word, key = (None,
False,
u'',
u'')
name, item = u'', {}
c = 0
m = reTokens.match(text)
while True:
c += 1
if not m or c > 50000:
break
if not quote and m.lastgroup == 'comment':
e = reEndCom.search(text, m.end())
if not e:
break
m = reTokens.match(text, e.end())
c += 1
if c > 2:
return
if escape:
escape = False
if m.lastgroup == 'quote':
if not quote:
quote = m.group('quote')
elif not escape and quote == m.group('quote'):
quote = None
elif m.lastgroup == 'escape':
escape = True
if m.lastgroup in ('word', 'quote', 'escape') or quote or not name and m.lastgroup != 'open':
word += word and m.group(0) or m.group(m.lastgroup)
if quote:
m = reTokens.match(text, m.end())
continue
elif not name and m.lastgroup == 'open':
name, word = fword(word), u''
elif name and m.lastgroup == 'colon':
key, word = fword(word), u''
elif name and m.lastgroup in ('comma', 'close'):
if key and word:
item[key] = fword(word)
key, word = (u'', u'')
if m.lastgroup == 'close':
yield (name, item)
name, item = u'', {}
else:
print u'um tokem não foi corretamente processado:', m.lastgroup
m = reTokens.match(text, m.end())
def items(text):
return dict(((name, item) for name, item in iteritems(text)))