26
26
from .html_minifier import html_minify
27
27
from .js_minifier import js_minify
28
28
29
- from anglerfish import (check_encoding , check_folder , make_logger ,
30
- make_post_exec_msg , set_process_name ,
31
- set_single_instance , walk2list , beep ,
32
- set_terminal_title )
33
29
34
-
35
- __all__ = ('process_multiple_files' , 'prefixer_extensioner' , 'prepare' ,
30
+ __all__ = ('process_multiple_files' , 'prefixer_extensioner' ,
36
31
'process_single_css_file' , 'process_single_html_file' ,
37
32
'process_single_js_file' , 'make_arguments_parser' , 'main' )
38
- start_time = datetime .now ()
39
33
40
34
41
35
##############################################################################
@@ -45,18 +39,17 @@ def process_multiple_files(file_path, watch=False, wrap=False, timestamp=False,
45
39
comments = False , sort = False , overwrite = False ,
46
40
zipy = False , prefix = '' , add_hash = False ):
47
41
"""Process multiple CSS, JS, HTML files with multiprocessing."""
48
- log . debug ( "Process {} is Compressing {}." . format ( os . getpid (), file_path ) )
42
+ print ( f "Process { os . getpid () } is Compressing { file_path } ." )
49
43
if watch :
50
44
previous = int (os .stat (file_path ).st_mtime )
51
- log . info ( "Process {} is Watching {}." . format ( os . getpid (), file_path ) )
45
+ print ( f "Process { os . getpid () } is Watching { file_path } ." )
52
46
while True :
53
47
actual = int (os .stat (file_path ).st_mtime )
54
48
if previous == actual :
55
49
sleep (60 )
56
50
else :
57
51
previous = actual
58
- log .debug ("Modification detected on {0}." .format (file_path ))
59
- check_folder (os .path .dirname (file_path ))
52
+ print (f"Modification detected on { file_path } ." )
60
53
if file_path .endswith (".css" ):
61
54
process_single_css_file (
62
55
file_path , wrap = wrap , timestamp = timestamp ,
@@ -95,13 +88,13 @@ def prefixer_extensioner(file_path, old, new,
95
88
>>> prefixer_extensioner('/tmp/test.js', '.js', '.min.js')
96
89
'/tmp/test.min.js'
97
90
"""
98
- log . debug ( "Prepending '{}' Prefix to {}." . format ( new . upper (), file_path ) )
91
+ print ( f "Prepending '{ new . upper () } ' Prefix to { file_path } ." )
99
92
extension = os .path .splitext (file_path )[1 ].lower ().replace (old , new )
100
93
filenames = os .path .splitext (os .path .basename (file_path ))[0 ]
101
94
filenames = prefix + filenames if prefix else filenames
102
95
if add_hash and file_content : # http://stackoverflow.com/a/25568916
103
96
filenames += "-" + sha1 (file_content .encode ("utf-8" )).hexdigest ()[:11 ]
104
- log . debug ( "Appending SHA1 HEX-Digest Hash to '{}'." . format ( file_path ) )
97
+ print ( f "Appending SHA1 HEX-Digest Hash to '{ file_path } '." )
105
98
dir_names = os .path .dirname (file_path )
106
99
file_path = os .path .join (dir_names , filenames + extension )
107
100
return file_path
@@ -112,10 +105,10 @@ def process_single_css_file(css_file_path, wrap=False, timestamp=False,
112
105
zipy = False , prefix = '' , add_hash = False ,
113
106
output_path = None ):
114
107
"""Process a single CSS file."""
115
- log . info ( "Processing CSS file: {0 }." . format ( css_file_path ) )
108
+ print ( f "Processing CSS file: { css_file_path } ." )
116
109
with open (css_file_path , encoding = "utf-8" ) as css_file :
117
110
original_css = css_file .read ()
118
- log . debug ( "INPUT: Reading CSS file {}." . format ( css_file_path ) )
111
+ print ( f "INPUT: Reading CSS file { css_file_path } ." )
119
112
minified_css = css_minify (original_css , wrap = wrap ,
120
113
comments = comments , sort = sort )
121
114
if timestamp :
@@ -130,7 +123,7 @@ def process_single_css_file(css_file_path, wrap=False, timestamp=False,
130
123
css_file_path , ".css" ,
131
124
".css.gz" if overwrite else ".min.css.gz" , original_css ,
132
125
prefix = prefix , add_hash = add_hash )
133
- log . debug ( "OUTPUT: Writing ZIP CSS {}." . format ( gz_file_path ) )
126
+ print ( f "OUTPUT: Writing ZIP CSS { gz_file_path } ." )
134
127
else :
135
128
min_css_file_path = gz_file_path = output_path
136
129
if not zipy or output_path is None :
@@ -140,17 +133,17 @@ def process_single_css_file(css_file_path, wrap=False, timestamp=False,
140
133
if zipy :
141
134
with gzip .open (gz_file_path , "wt" , encoding = "utf-8" ) as output_gz :
142
135
output_gz .write (minified_css )
143
- log . debug ( "OUTPUT: Writing CSS Minified {0 }." . format ( min_css_file_path ) )
136
+ print ( f "OUTPUT: Writing CSS Minified { min_css_file_path } ." )
144
137
return min_css_file_path
145
138
146
139
147
140
def process_single_html_file (html_file_path , comments = False , overwrite = False ,
148
141
prefix = '' , add_hash = False , output_path = None ):
149
142
"""Process a single HTML file."""
150
- log . info ( "Processing HTML file: {0 }." . format ( html_file_path ) )
143
+ print ( f "Processing HTML file: { html_file_path } ." )
151
144
with open (html_file_path , encoding = "utf-8" ) as html_file :
152
145
minified_html = html_minify (html_file .read (), comments = comments )
153
- log . debug ( "INPUT: Reading HTML file {0 }." . format ( html_file_path ) )
146
+ print ( f "INPUT: Reading HTML file { html_file_path } ." )
154
147
if output_path is None :
155
148
html_file_path = prefixer_extensioner (
156
149
html_file_path , ".html" if overwrite else ".htm" , ".html" ,
@@ -159,17 +152,17 @@ def process_single_html_file(html_file_path, comments=False, overwrite=False,
159
152
html_file_path = output_path
160
153
with open (html_file_path , "w" , encoding = "utf-8" ) as output_file :
161
154
output_file .write (minified_html )
162
- log . debug ( "OUTPUT: Writing HTML Minified {0 }." . format ( html_file_path ) )
155
+ print ( f "OUTPUT: Writing HTML Minified { html_file_path } ." )
163
156
return html_file_path
164
157
165
158
166
159
def process_single_js_file (js_file_path , timestamp = False , overwrite = False ,
167
160
zipy = False , output_path = None ):
168
161
"""Process a single JS file."""
169
- log . info ( "Processing JS file: {0 }." . format ( js_file_path ) )
162
+ print ( f "Processing JS file: { js_file_path } ." )
170
163
with open (js_file_path , encoding = "utf-8" ) as js_file :
171
164
original_js = js_file .read ()
172
- log . debug ( "INPUT: Reading JS file {0 }." . format ( js_file_path ) )
165
+ print ( f "INPUT: Reading JS file { js_file_path } ." )
173
166
minified_js = js_minify (original_js )
174
167
if timestamp :
175
168
taim = "/* {} */ " .format (datetime .now ().isoformat ()[:- 7 ].lower ())
@@ -182,7 +175,7 @@ def process_single_js_file(js_file_path, timestamp=False, overwrite=False,
182
175
gz_file_path = prefixer_extensioner (
183
176
js_file_path , ".js" , ".js.gz" if overwrite else ".min.js.gz" ,
184
177
original_js )
185
- log . debug ( "OUTPUT: Writing ZIP JS {}." . format ( gz_file_path ) )
178
+ print ( f "OUTPUT: Writing ZIP JS { gz_file_path } ." )
186
179
else :
187
180
min_js_file_path = gz_file_path = output_path
188
181
if not zipy or output_path is None :
@@ -192,7 +185,7 @@ def process_single_js_file(js_file_path, timestamp=False, overwrite=False,
192
185
if zipy :
193
186
with gzip .open (gz_file_path , "wt" , encoding = "utf-8" ) as output_gz :
194
187
output_gz .write (minified_js )
195
- log . debug ( "OUTPUT: Writing JS Minified {0 }." . format ( min_js_file_path ) )
188
+ print ( f "OUTPUT: Writing JS Minified { min_js_file_path } ." )
196
189
return min_js_file_path
197
190
198
191
@@ -232,56 +225,53 @@ def make_arguments_parser():
232
225
parser .add_argument ('--watch' , action = 'store_true' , help = "Watch changes." )
233
226
parser .add_argument ('--multiple' , action = 'store_true' ,
234
227
help = "Allow Multiple instances (Not Recommended)." )
235
- parser .add_argument ('--beep' , action = 'store_true' ,
236
- help = "Beep sound will be played when it ends at exit." )
237
228
return parser .parse_args ()
238
229
239
230
240
- def prepare ():
241
- """Prepare basic setup for main loop running."""
242
- global log
243
- log = make_logger ("css-html-js-minify" , emoji = True ) # Make a Logger Log.
244
- set_terminal_title ("css-html-js-minify" )
245
- check_encoding () # AutoMagically Check Encodings/root
246
- set_process_name ("css-html-js-minify" ) # set Name
247
- set_single_instance ("css-html-js-minify" ) # set Single Instance
248
- return log
231
+ def walk2list (folder : str , target : tuple , omit : tuple = (),
232
+ showhidden : bool = False , topdown : bool = True ,
233
+ onerror : object = None , followlinks : bool = False ) -> tuple :
234
+ """Perform full walk, gather full path of all files."""
235
+ oswalk = os .walk (folder , topdown = topdown ,
236
+ onerror = onerror , followlinks = followlinks )
237
+
238
+ return [os .path .abspath (os .path .join (r , f ))
239
+ for r , d , fs in oswalk
240
+ for f in fs if not f .startswith (() if showhidden else "." ) and
241
+ not f .endswith (omit ) and f .endswith (target )]
249
242
250
243
251
244
def main ():
252
245
"""Main Loop."""
253
246
args = make_arguments_parser ()
254
- log .disable (log .CRITICAL ) if args .quiet else log .debug ("Max Logging ON" )
255
- check_folder (os .path .dirname (args .fullpath ))
256
- atexit .register (beep ) if args .beep else log .debug ("Beep sound at exit OFF" )
257
247
if os .path .isfile (args .fullpath ) and args .fullpath .endswith (".css" ):
258
- log . info ("Target is a CSS File." ) # Work based on if argument is
248
+ print ("Target is a CSS File." ) # Work based on if argument is
259
249
list_of_files = str (args .fullpath ) # file or folder, folder is slower.
260
250
process_single_css_file (
261
251
args .fullpath , wrap = args .wrap , timestamp = args .timestamp ,
262
252
comments = args .comments , sort = args .sort , overwrite = args .overwrite ,
263
253
zipy = args .zipy , prefix = args .prefix , add_hash = args .hash )
264
254
elif os .path .isfile (args .fullpath ) and args .fullpath .endswith (
265
255
".html" if args .overwrite else ".htm" ):
266
- log . info ("Target is HTML File." )
256
+ print ("Target is HTML File." )
267
257
list_of_files = str (args .fullpath )
268
258
process_single_html_file (
269
259
args .fullpath , comments = args .comments ,
270
260
overwrite = args .overwrite , prefix = args .prefix , add_hash = args .hash )
271
261
elif os .path .isfile (args .fullpath ) and args .fullpath .endswith (".js" ):
272
- log . info ("Target is a JS File." )
262
+ print ("Target is a JS File." )
273
263
list_of_files = str (args .fullpath )
274
264
process_single_js_file (
275
265
args .fullpath , timestamp = args .timestamp ,
276
266
overwrite = args .overwrite , zipy = args .zipy )
277
267
elif os .path .isdir (args .fullpath ):
278
- log . info ("Target is a Folder with CSS, HTML, JS files !." )
279
- log . warning ("Processing a whole Folder may take some time..." )
268
+ print ("Target is a Folder with CSS, HTML, JS files !." )
269
+ print ("Processing a whole Folder may take some time..." )
280
270
list_of_files = walk2list (
281
271
args .fullpath ,
282
272
(".css" , ".js" , ".html" if args .overwrite else ".htm" ),
283
273
(".min.css" , ".min.js" , ".htm" if args .overwrite else ".html" ))
284
- log . info ( 'Total Maximum CPUs used: ~{0 } Cores.' . format ( cpu_count ()) )
274
+ print ( f 'Total Maximum CPUs used: ~{ cpu_count () } Cores.' )
285
275
pool = Pool (cpu_count ()) # Multiprocessing Async
286
276
pool .map_async (partial (
287
277
process_multiple_files , watch = args .watch ,
@@ -293,12 +283,10 @@ def main():
293
283
pool .close ()
294
284
pool .join ()
295
285
else :
296
- log . critical ("File or folder not found,or cant be read,or I/O Error." )
286
+ print ("File or folder not found,or cant be read,or I/O Error." )
297
287
sys .exit (1 )
298
288
if args .after and getoutput :
299
- log .info (getoutput (str (args .after )))
300
- log .info ('\n {0} \n Files Processed: {1}.' .format ('-' * 80 , list_of_files ))
301
- log .info ('Number of Files Processed: {0}.' .format (
302
- len (list_of_files ) if isinstance (list_of_files , tuple ) else 1 ))
303
- set_terminal_title ()
304
- make_post_exec_msg (start_time )
289
+ print (getoutput (str (args .after )))
290
+ print (f'\n { "-" * 80 } \n Files Processed: { list_of_files } .' )
291
+ print (f'''Number of Files Processed:
292
+ { len (list_of_files ) if isinstance (list_of_files , tuple ) else 1 } .''' )
0 commit comments