|
1 | 1 | import os
|
2 |
| -import json |
3 |
| -import re |
4 |
| -from os import listdir |
5 |
| -from os.path import isfile, join |
| 2 | +from functions import * |
6 | 3 |
|
| 4 | +config, _PATH = loadconfig() |
7 | 5 |
|
8 |
| -def read(file): |
9 |
| - with open(file, 'r') as f: |
10 |
| - lines = f.readlines() |
11 |
| - f.close() |
12 |
| - return lines |
13 |
| - |
14 |
| - |
15 |
| -def write(root, new_name, lines): |
16 |
| - with open(os.path.join(root, new_name), 'w') as exf: |
17 |
| - exf.writelines(lines) |
18 |
| - exf.close() |
19 |
| - |
20 |
| - |
21 |
| -def getVars(file, config): |
22 |
| - # Get the extension and use as a flag e.g.: HTML, CSS... |
23 |
| - pattern_file = file.split('.')[1].upper() |
24 |
| - |
25 |
| - lines = read(file) |
26 |
| - |
27 |
| - # Use the right regex to find the classes on the file |
28 |
| - substring = re.findall(config['pattern' + pattern_file], str(lines)) |
29 |
| - |
30 |
| - name, ext = file.split('.') |
31 |
| - new_name = name + config['extCopy'] + '.' + ext |
32 |
| - |
33 |
| - return new_name, substring |
| 6 | +search_files = lookfiles() |
34 | 7 |
|
35 | 8 |
|
36 | 9 | def main():
|
37 |
| - # Load config.json |
38 |
| - with open("config.json") as json_data_file: |
39 |
| - config = json.load(json_data_file) |
40 |
| - _PATH = os.getcwd() + '/' |
41 |
| - |
42 |
| - search_files = [] |
43 |
| - # Look for files |
44 |
| - for root, subdirectories, files in os.walk(_PATH): |
45 |
| - # Comprehension to break outter loop |
46 |
| - if any([dirsIgnore for dirsIgnore in config['dirsIgnore'] if dirsIgnore in root]): |
47 |
| - continue |
| 10 | + search_files = lookfiles() |
48 | 11 |
|
49 |
| - # And all its files |
50 |
| - for index, file in enumerate(files): |
51 |
| - for filesSearch in config['filesSearch']: |
52 |
| - for filesIgnore in config['filesIgnore']: |
53 |
| - if(filesSearch in file and filesIgnore not in file): |
54 |
| - search_files.append((root, file)) |
| 12 | + if(config['output']): |
55 | 13 |
|
56 |
| - #print(f'files: {search_files}\n') |
| 14 | + #print(f'files: {search_files}\n') |
57 | 15 |
|
58 |
| - print('CSS Hashfy found files:', end='\n\n') |
59 |
| - for root, file in search_files: |
60 |
| - print(os.path.join(root, file)) |
61 |
| - print() |
| 16 | + print('CSS Hashfy found files:', end='\n\n') |
| 17 | + for root, file in search_files: |
| 18 | + print(os.path.join(root, file)) |
| 19 | + print() |
62 | 20 |
|
63 |
| - print('Starting hashing...', end='\n\n') |
| 21 | + print('Starting hashing...', end='\n\n') |
64 | 22 |
|
65 | 23 | # Initializing alg vars
|
66 |
| - count = 0 |
67 |
| - classes_dict = {} |
68 |
| - |
69 |
| - # Get all the files with the '.css' extension |
70 |
| - css_files = [(root, file) |
71 |
| - for root, file in search_files if file.endswith('.css')] |
72 |
| - |
73 |
| - # Copy the files of the search |
74 |
| - for root, file in css_files: |
75 |
| - lines = read(os.path.join(root, file)) |
76 |
| - |
77 |
| - new_name, substring = getVars(os.path.join(root, file), config) |
78 |
| - |
79 |
| - # If the file doesn't exist, create a new one |
80 |
| - if(config['overwriteFiles'] or not isfile(join(root, new_name))): |
81 |
| - substring = [s.translate({ord('.'): None, ord( |
82 |
| - '{'): None, ord('#'): None}).strip() for s in substring] |
83 |
| - |
84 |
| - # Create the dictionary with the classe's hashes of the CSS |
85 |
| - classes_dict.update( |
86 |
| - {s: str(abs(hash(s)) % (10 ** config['hashLength'])) for s in substring}) |
87 |
| - |
88 |
| - # CSS WRITE |
89 |
| - |
90 |
| - # Copy the hashes to the copied lines of the file |
91 |
| - for index, l in enumerate(lines): |
92 |
| - for k, v in classes_dict.items(): |
93 |
| - if k in l: |
94 |
| - l = l.replace(k, 'c'+v) |
95 |
| - lines[index] = l |
96 |
| - |
97 |
| - write(root, new_name, lines) |
98 |
| - count += 1 |
99 |
| - print(f"{10*'*'} \t {new_name.split('/')[-1]} \t {10*'*'}") |
100 |
| - |
101 |
| - # If it exists, pass |
102 |
| - else: |
103 |
| - print(f'!! file already existed: {new_name}') |
104 |
| - |
105 |
| - # Overwrite HTML classes |
106 |
| - html_files = [(root, file) |
107 |
| - for root, file in search_files if file.endswith('.html')] |
108 |
| - |
109 |
| - # Copy the files of the search |
110 |
| - for root, file in html_files: |
111 |
| - lines = read(os.path.join(root, file)) |
112 |
| - |
113 |
| - new_name, substring = getVars(os.path.join(root, file), config) |
114 |
| - |
115 |
| - # HTML WRITE |
116 |
| - |
117 |
| - # HTML overwrite link tags |
118 |
| - if(config['overwriteFiles'] or not isfile(join(root, new_name))): |
119 |
| - for index, l in enumerate(lines): |
120 |
| - for root, file in css_files: |
121 |
| - if file in l and 'link' in l: |
122 |
| - l = l.replace( |
123 |
| - file, f"{file.split('.')[0]}{config['extCopy']}.css") |
124 |
| - lines[index] = l |
125 |
| - |
126 |
| - # Overwrite html lines with the hashed css classes |
127 |
| - for index, l in enumerate(lines): |
128 |
| - for k, v in classes_dict.items(): |
129 |
| - if k in l: |
130 |
| - l = l.replace(k, 'c'+v) |
131 |
| - lines[index] = l |
| 24 | + classes_dict, css_files, css_count = csshash(search_files) |
132 | 25 |
|
133 |
| - write(root, new_name, lines) |
134 |
| - count += 1 |
135 |
| - print(f"{10*'*'} \t {new_name.split('/')[-1]} \t {10*'*'}") |
| 26 | + html_count = htmlhash(search_files, classes_dict, css_files) |
136 | 27 |
|
137 |
| - # If it exists, pass |
138 |
| - else: |
139 |
| - print(f'!! file already existed: {new_name}') |
| 28 | + if(config['output']): |
140 | 29 |
|
141 |
| - print() |
142 |
| - print(f'finished! {str(int(count/len(search_files))*100)}% done.') |
| 30 | + print() |
| 31 | + print( |
| 32 | + f'finished! {str(int((html_count + css_count)/len(search_files))*100)}% done.') |
143 | 33 |
|
144 | 34 |
|
145 | 35 | main()
|
0 commit comments