-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
75 lines (50 loc) · 2.05 KB
/
main.py
File metadata and controls
75 lines (50 loc) · 2.05 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
import sys
import re
import os
from time import time
def remove_comments(lines_list):
# remove one line // comments
for i, line in enumerate(lines_list):
lines_list[i] = re.sub(r'(\/+\/+).*', '', line)
string = "".join(lines_list)
return string
def cut_whitespace_chars(list_of_chars, string):
final_string = string
for char in list_of_chars:
pattern1 = re.compile(rf'[\s\t]+({char})')
pattern2 = re.compile(rf'({char})[\s\t]+')
final_char = char.replace('\\', '')
final_string = re.sub(pattern1, final_char, final_string)
final_string = re.sub(pattern2, final_char, final_string)
return final_string
def minify(path):
try:
with open(path, mode='r') as file:
lines_list = file.readlines()
string_without_comments = remove_comments(lines_list)
list_of_chars = list(string_without_comments)
counter = list_of_chars.count('\n')
for i in range(0, counter):
list_of_chars.remove('\n')
string = "".join(list_of_chars)
list_of_chars = [r'\=', r'\(', r'\)', r'\{', r'\}', r'\,', r'\:', r'\+', r'\-', r'\*', r'\/',
r'\=\=', r'\=\=\=', r'\=\>', r'\<', r'\>', r'\<\=', r'\>\=', r'\+\+', r'\-\-',
r'\+\=', r'\-\=', r'\*\*', r'\*\=', r'\/\=', r'\%', r'\?', r'\;'
]
final_string = cut_whitespace_chars(list_of_chars, string)
with open(f'./minified/{path}', mode='w') as minified_file:
minified_file.write(final_string)
print(f'File {path} minified successfully')
except FileNotFoundError as error:
print(f'I did not find file {path}')
def main():
start = time()
dir_path = './minified'
if not os.path.exists(dir_path):
os.mkdir(dir_path)
for i in range(1, len(sys.argv)):
minify(sys.argv[i])
stop = time()
print(f'Took {round(stop - start, 4)}ms')
if __name__ == '__main__' and len(sys.argv) > 1:
main()