-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileCss.py
More file actions
247 lines (222 loc) · 7.08 KB
/
fileCss.py
File metadata and controls
247 lines (222 loc) · 7.08 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
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
#!/usr/bin/python3.6
# -*- coding: utf-8 -*-
from fileCls import File
from fileLocal import pathCss
import loggerFct as log
fileCssCommon = pathCss + '\t.css'
class RuleCss():
def __init__ (self):
self.query ="" # div.class-name
self.rules ={} # color: red
def fromText (self, text):
# text = div{color:red;font-size:1em;
d= text.find ('{')
self.query = text[:d]
text = text[d+1:-1]
if text[-1] ==';': text = text[:-1]
text = text.replace (';',':')
textList = text.split (':')
rangeList = range (0, len (textList) -1, 2)
for c in rangeList: self.rules [textList[c]] = textList[c+1]
def __str__ (self):
res = self.query +':'
rules = self.rules.keys()
for rule in rules: res = res +'\t' + rule +": "+ self.rules[rule]
return res
def __lt__ (self, otherRule):
if self.query >= otherRule.query: return False
ruleSelf = self.rules.keys()
ruleOTher = otherRule.rules.keys()
nbRules = len (ruleSelf)
if nbRules >= len (ruleOTher): return False
r=0
isBefore = True
while isBefore and r< nbRules:
if ruleSelf[r] in ruleOTher and self.rules [ruleSelf[r]] >= ruleOTher.rules [ruleSelf[r]]: isBefore = False
r+=1
return isBefore
def __gt__ (self, otherRule):
if self.query <= otherRule.query: return False
ruleSelf = self.rules.keys()
ruleOTher = otherRule.rules.keys()
nbRules = len (ruleSelf)
if nbRules <= len (ruleOTher): return False
r=0
isAfter = True
while isAfter and r< nbRules:
if ruleSelf[r] in ruleOTher and self.rules [ruleSelf[r]] <= ruleOTher.rules [ruleSelf[r]]: isAfter = False
r+=1
return isAfter
def __le__ (self, otherRule):
return not self.__gt__ (otherRule)
def __ge__ (self, otherRule):
return not self.__lt__ (otherRule)
def __eq__ (self, otherRule):
if self.query != otherRule.query: return False
ruleSelf = self.rules.keys()
ruleOTher = otherRule.rules.keys()
r=0
nbRules = len (ruleSelf)
isSameRule = True
while isSameRule and r< nbRules:
if ruleSelf[r] not in ruleOTher: isSameRule = False
elif self.rules [ruleSelf[r]] != ruleOTher.rules [ruleSelf[r]]: isSameRule = False
r+=1
return isSameRule
def __ne__ (self, otherRule):
return not self.__eq__ (otherRule)
def sameStructure (self, otherRule):
if self.query != otherRule.query: return False
ruleSelf = self.rules.keys()
ruleOTher = otherRule.rules.keys()
r=0
nbRules = len (ruleSelf)
isSameRule = True
while isSameRule and r< nbRules:
if ruleSelf[r] not in ruleOTher: isSameRule = False
r+=1
if not isSameRule: return False
r=0
nbRules = len (ruleOTher)
while isSameRule and r< nbRules:
if ruleOTher[r] not in ruleSelf: isSameRule = False
r+=1
return isSameRule
def __contains__ (self, rule):
# vérifier si un élément est dans self. rule = "nomRule" ou { nomRule: valeurRule }
isIn = True
ruleKeysSelf = self.rules.keys()
if isinstance (rule, str):
if rule not in ruleKeysSelf: isIn = False
return isIn
elif isinstance (rule, dict):
ruleKeys = rule.rules.keys()
r=0
nbRules = len (ruleKeys)
while isIn and r< nbRules:
if ruleKeys[r] not in ruleKeysSelf: isIn = False
r+=1
return isIn
else: return False
class QueryCss():
def __init__ (self):
self.query ="" # (max-width: 768px) ou print
self.rules =[] # RuleCss()
def fromText (self, text):
# text = myQuery{...}
d= text.find ('{')
self.query = text[:d]
text = text[d+1:-1]
# les règles
textList = text.split ('}')
rangeList = range (len (textList) -1)
for c in rangeList:
self.rules.append (RuleCss())
self.rules[-1].fromText (textList[c])
def __str__ (self):
res = self.query +':'
for rule in self.rules:
res = res +'\n\t'+ rule.__str__()
return res
def __eq__ (self, otherQuery):
if self.query != otherQuery.query: return False
nbRulesSelf = len (self.rules)
nbRulesOther = len (otherQuery.rules)
if nbRulesSelf != nbRulesOther: return False
s=0
o=0
isSameRule = True
while isSameRule and s< nbRulesSelf:
o=0
while isSameRule and o< nbRulesOther:
isSameRule = self.rules[s].__eq__ (otherRule.rules[o])
o+=1
s+=1
return isSameRule
def __ne__ (self, otherQuery):
return not self.__eq__ (otherQuery)
def __contains__ (self, rule):
if isinstance (rule, str): # nom de règle, div, p.class-name
isIn = False
r=0
nbRules = len (self.rules)
while not isIn and r< nbRules:
if self.rules[r].__contains__ (rule): isIn = True
r+=1
return isIn
elif isinstance (rule, RuleCss):
if not rule in self.rules: isIn = False
return isIn
else: return False
class FileCss (File):
def __init__ (self, file =None):
File.__init__ (self, file)
self.queries =[] # QueryCss
self.rules =[] # RuleCss
def getRulesForItem (self, item):
rule = RuleCss()
rule.query = item
for ruleSelf in self.rules:
if item not in ruleSelf.query or item +" " in ruleSelf.query: continue
elif item +'.' in ruleSelf.query or item +'#' in ruleSelf.query or item +'[' in ruleSelf.query: continue
elif item +':' in ruleSelf.query or item +'>' in ruleSelf.query or item +'=' in ruleSelf.query: continue
elif ruleSelf.query == item or " "+ item +',' in ruleSelf.query or ruleSelf.query.find (item +',') ==0:
# si la même règle est répétée plusieurs fois, la dernière occurence est gardée
rulesNames = ruleSelf.rules.keys()
for name in rulesNames: rule.rules[name] = ruleSelf.rules[name]
elif ", "+ item in ruleSelf.query:
# item est le dernier élément de la query
pos = ruleSelf.query.rfind (", "+ item)
if len (ruleSelf.query) -pos -2 == len (item):
rulesNames = ruleSelf.rules.keys()
for name in rulesNames: rule.rules[name] = ruleSelf.rules[name]
return rule
def read (self):
File.read (self)
self.replace ('\n'," ")
self.replace ('\t'," ")
self.replace ('\r'," ")
self.cleanForStandarding()
self.deleteComments()
self.listMediaQueries()
self.listRules()
def listMediaQueries (self):
if '@media' not in self.text: return
textList = self.text.split ('@media')
rangeList = range (1, len (textList))
for c in rangeList:
d= textList[c].find ('}')
nOpening = textList[c][:d].count ('{')
nClosing =1
while nOpening > nClosing:
d= textList[c].find ('}',d+1)
nOpening = textList[c][:d].count ('{')
nClosing =1+ textList[c][:d].count ('}')
d+=1
self.queries.append (QueryCss())
self.queries[-1].fromText (textList[c][:d])
textList[c] = textList[c][d:]
self.text = " ".join (textList)
self.cleanForStandarding()
def listRules (self):
textList = self.text.split ('}')
rangeList = range (len (textList) -1)
for c in rangeList:
self.rules.append (RuleCss())
self.rules[-1].fromText (textList[c])
self.text =""
def deleteComments (self):
if '/*' not in self.text: return
textList = self.text.split ('/*')
rangeList = range (1, len (textList))
for c in rangeList:
f=2+ textList[c].find ('*/')
textList[c] = textList[c][f:]
self.text = "".join (textList)
self.cleanForStandarding()
def cleanForStandarding (self):
while " " in self.text: self.replace (" "," ")
marquers ='{}:;'
for mark in marquers:
self.replace (" "+ mark, mark)
self.replace (mark +" ", mark)