8
8
from collections import namedtuple
9
9
10
10
# Define namedtuples for storing results
11
- Violation = namedtuple ('Violation' , ['file_path' , 'css_file' , 'reason' ])
12
- CorrectImport = namedtuple ('CorrectImport' , ['file_path' , 'css_file' ])
13
- EmbeddedViolation = namedtuple ('EmbeddedViolation' , ['file_path' , 'css_codes' ])
14
- CSSCheckResult = namedtuple ('CSSCheckResult' , ['violations' , 'correct_imports' , 'embedded_violations' ])
11
+ Violation = namedtuple ("Violation" , ["file_path" , "css_file" , "reason" ])
12
+ CorrectImport = namedtuple ("CorrectImport" , ["file_path" , "css_file" ])
13
+ EmbeddedViolation = namedtuple ("EmbeddedViolation" , ["file_path" , "css_codes" ])
14
+ CSSCheckResult = namedtuple (
15
+ "CSSCheckResult" , ["violations" , "correct_imports" , "embedded_violations" ]
16
+ )
17
+
15
18
16
19
def check_embedded_css (content : str ) -> list :
17
20
"""
@@ -26,8 +29,12 @@ def check_embedded_css(content: str) -> list:
26
29
embedded_css_pattern = r"#([0-9a-fA-F]{3}){1,2}" # Matches CSS color codes
27
30
return re .findall (embedded_css_pattern , content )
28
31
32
+
29
33
def check_files (
30
- directory : str , exclude_files : list , exclude_directories : list , allowed_css_patterns : list
34
+ directory : str ,
35
+ exclude_files : list ,
36
+ exclude_directories : list ,
37
+ allowed_css_patterns : list ,
31
38
) -> CSSCheckResult :
32
39
"""
33
40
Check TypeScript files for CSS violations and correct CSS imports.
@@ -83,20 +90,29 @@ def check_files(
83
90
84
91
# Check if the CSS file exists
85
92
if not os .path .exists (css_file_path ):
86
- violations .append (Violation (file_path , css_file , "File not found" ))
93
+ violations .append (
94
+ Violation (file_path , css_file , "File not found" )
95
+ )
87
96
# Check if the CSS import matches the allowed patterns
88
- elif any (css_file .endswith (pattern ) for pattern in allowed_css_patterns ):
97
+ elif any (
98
+ css_file .endswith (pattern ) for pattern in allowed_css_patterns
99
+ ):
89
100
correct_css_imports .append (CorrectImport (file_path , css_file ))
90
101
else :
91
- violations .append (Violation (file_path , css_file , "Invalid import" ))
102
+ violations .append (
103
+ Violation (file_path , css_file , "Invalid import" )
104
+ )
92
105
93
106
# Check for embedded CSS
94
107
embedded_css = check_embedded_css (content )
95
108
if embedded_css :
96
- embedded_css_violations .append (EmbeddedViolation (file_path , embedded_css ))
109
+ embedded_css_violations .append (
110
+ EmbeddedViolation (file_path , embedded_css )
111
+ )
97
112
98
113
return CSSCheckResult (violations , correct_css_imports , embedded_css_violations )
99
114
115
+
100
116
def main ():
101
117
"""Run the CSS check script."""
102
118
parser = argparse .ArgumentParser (
@@ -140,31 +156,35 @@ def main():
140
156
if result .violations :
141
157
output .append ("CSS Import Violations:" )
142
158
for violation in result .violations :
143
- output .append (f"- { violation .file_path } : { violation .css_file } ({ violation .reason } )" )
159
+ output .append (
160
+ f"- { violation .file_path } : { violation .css_file } ({ violation .reason } )"
161
+ )
144
162
exit_code = 1
145
163
146
164
if result .embedded_violations :
147
165
output .append ("\n Embedded CSS Violations:" )
148
166
for violation in result .embedded_violations :
149
167
output .append (f"- { violation .file_path } : { ', ' .join (violation .css_codes )} " )
150
- exit_code = 1
168
+ exit_code = 1
151
169
152
170
if output :
153
171
print ("\n " .join (output ))
154
- print ("""
172
+ print (
173
+ """
155
174
Please address the above CSS violations:
156
175
1. For invalid CSS imports, ensure you're using the correct import syntax and file paths.
157
176
2. For embedded CSS, move the CSS to appropriate stylesheet files and import them correctly.
158
177
3. Make sure to use only the allowed CSS patterns as specified in the script arguments.
159
178
4. Check that all imported CSS files exist in the specified locations.
160
- """ )
179
+ """
180
+ )
161
181
if args .show_success and result .correct_imports :
162
182
print ("\n Correct CSS Imports:" )
163
183
for import_ in result .correct_imports :
164
184
print (f"- { import_ .file_path } : { import_ .css_file } " )
165
185
166
186
sys .exit (exit_code )
167
187
188
+
168
189
if __name__ == "__main__" :
169
190
main ()
170
-
0 commit comments