Skip to content

Commit a343532

Browse files
Merge pull request #99 from chrissimpkins/fmt-config
Python source formatting updates
2 parents 151ec79 + 65dbfb6 commit a343532

File tree

4 files changed

+59
-84
lines changed

4 files changed

+59
-84
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ test-coverage:
7070

7171
test-python:
7272
tox
73-
flake8 --ignore=E501,W503,E121,E123,E126,E226,E24,E704,W503,W504,N806 src/crunch.py
73+
flake8 src/crunch.py
7474

7575
test-shell:
7676
shellcheck --exclude=2046 src/*.sh

setup.cfg

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[flake8]
2+
max-line-length = 90

src/crunch.py

+55-82
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656
Source: https://github.com/chrissimpkins/Crunch
5757
==================================================
5858
59-
crunch is a command line executable that performs lossy optimization of one or more png image files with pngquant and zopflipng.
59+
crunch is a command line executable that performs lossy optimization of one
60+
or more png image files with pngquant and zopflipng.
6061
6162
Usage:
6263
$ crunch [image path 1]...[image path n]
@@ -95,8 +96,8 @@ def main(argv):
9596

9697
if len(argv) == 0:
9798
sys.stderr.write(
98-
ERROR_STRING + " Please include one or more paths to PNG image files as "
99-
"arguments to the script." + os.linesep
99+
f"{ERROR_STRING} Please include one or more paths to PNG image files as "
100+
"arguments to the script.{os.linesep}"
100101
)
101102
sys.exit(1)
102103

@@ -138,63 +139,50 @@ def main(argv):
138139
# Not a file test
139140
if not os.path.isfile(png_path): # is not an existing file
140141
sys.stderr.write(
141-
ERROR_STRING
142-
+ " '"
143-
+ png_path
144-
+ "' does not appear to be a valid path to a PNG file"
145-
+ os.linesep
142+
f"{ERROR_STRING} '{png_path}' does not appear to be a "
143+
f"valid path to a PNG file{os.linesep}"
146144
)
147145
sys.exit(1) # not a file, abort immediately
148146
# PNG validity test
149147
if not is_valid_png(png_path):
150148
sys.stderr.write(
151-
ERROR_STRING
152-
+ " '"
153-
+ png_path
154-
+ "' is not a valid PNG file."
155-
+ os.linesep
149+
f"{ERROR_STRING} '{png_path}' is not a valid PNG file.{os.linesep}"
156150
)
157151
if is_gui(argv):
158-
log_error(png_path + " is not a valid PNG file.")
152+
log_error(f"{png_path} is not a valid PNG file.")
159153
NOTPNG_ERROR_FOUND = True
160154

161-
# Exit after checking all file requests and reporting on all invalid file paths (above)
155+
# Exit after checking all file requests and reporting on all invalid
156+
# file paths (above)
162157
if NOTPNG_ERROR_FOUND is True:
163158
sys.stderr.write(
164-
"The request was not executed successfully. Please try again with one or more valid PNG files."
165-
+ os.linesep
159+
f"The request was not executed successfully. Please try again "
160+
f"with one or more valid PNG files.{os.linesep}"
166161
)
167162
if is_gui(argv):
168163
log_error(
169-
"The request was not executed successfully. Please try again with one or more valid PNG files."
164+
"The request was not executed successfully. Please try "
165+
"again with one or more valid PNG files."
170166
)
171167
sys.exit(1)
172168

173169
# Dependency error handling
174170
if not os.path.exists(PNGQUANT_EXE_PATH):
175171
sys.stderr.write(
176-
ERROR_STRING
177-
+ " pngquant executable was not identified on path '"
178-
+ PNGQUANT_EXE_PATH
179-
+ "'"
180-
+ os.linesep
172+
f"{ERROR_STRING} pngquant executable was not identified on path "
173+
f"'{PNGQUANT_EXE_PATH}'{os.linesep}"
181174
)
182175
if is_gui(argv):
183-
log_error(
184-
"pngquant was not found on the expected path " + PNGQUANT_EXE_PATH
185-
)
176+
log_error(f"pngquant was not found on the expected path {PNGQUANT_EXE_PATH}")
186177
sys.exit(1)
187178
elif not os.path.exists(ZOPFLIPNG_EXE_PATH):
188179
sys.stderr.write(
189-
ERROR_STRING
190-
+ " zopflipng executable was not identified on path '"
191-
+ ZOPFLIPNG_EXE_PATH
192-
+ "'"
193-
+ os.linesep
180+
f"{ERROR_STRING} zopflipng executable was not identified on path "
181+
f"'{ZOPFLIPNG_EXE_PATH}'{os.linesep}"
194182
)
195183
if is_gui(argv):
196184
log_error(
197-
"zopflipng was not found on the expected path " + ZOPFLIPNG_EXE_PATH
185+
f"zopflipng was not found on the expected path {ZOPFLIPNG_EXE_PATH}"
198186
)
199187
sys.exit(1)
200188

@@ -208,33 +196,31 @@ def main(argv):
208196
optimize_png(png_path_list[0])
209197
else:
210198
processes = PROCESSES
211-
# if not defined by user, start by defining spawned processes as number of available cores
199+
# if not defined by user, start by defining spawned processes as number
200+
# of available cores
212201
if processes == 0:
213202
processes = cpu_count()
214203

215-
# if total cores available is greater than number of files requested, limit to the latter number
204+
# if total cores available is greater than number of files requested,
205+
# limit to the latter number
216206
if processes > len(png_path_list):
217207
processes = len(png_path_list)
218208

219209
print(
220-
"Spawning "
221-
+ str(processes)
222-
+ " processes to optimize "
223-
+ str(len(png_path_list))
224-
+ " image files..."
210+
f"Spawning {processes} processes to optimize {len(png_path_list)} "
211+
f"image files..."
225212
)
226213
p = Pool(processes)
227214
try:
228215
p.map(optimize_png, png_path_list)
229216
except Exception as e:
230217
stdstream_lock.acquire()
231-
sys.stderr.write("-----" + os.linesep)
218+
sys.stderr.write(f"-----{os.linesep}")
232219
sys.stderr.write(
233-
ERROR_STRING
234-
+ " Error detected during execution of the request."
235-
+ os.linesep
220+
f"{ERROR_STRING} Error detected during execution of the request."
221+
f"{os.linesep}"
236222
)
237-
sys.stderr.write(str(e) + os.linesep)
223+
sys.stderr.write(f"{e}{os.linesep}")
238224
stdstream_lock.release()
239225
if is_gui(argv):
240226
log_error(str(e))
@@ -272,49 +258,40 @@ def optimize_png(png_path):
272258
pngquant_options = (
273259
" --quality=80-98 --skip-if-larger --force --strip --speed 1 --ext -crunch.png "
274260
)
275-
pngquant_command = (
276-
PNGQUANT_EXE_PATH + pngquant_options + shellquote(img.pre_filepath)
277-
)
261+
pngquant_command = PNGQUANT_EXE_PATH + pngquant_options + shellquote(img.pre_filepath)
278262
try:
279263
subprocess.check_output(pngquant_command, stderr=subprocess.STDOUT, shell=True)
280264
except CalledProcessError as cpe:
281265
if cpe.returncode == 98:
282266
# this is the status code when file size increases with execution of pngquant.
283-
# ignore at this stage, original file copied at beginning of zopflipng processing
284-
# below if it is not present due to these errors
267+
# ignore at this stage, original file copied at beginning of zopflipng
268+
# processing below if it is not present due to these errors
285269
pass
286270
elif cpe.returncode == 99:
287271
# this is the status code when the image quality falls below the set min value
288-
# ignore at this stage, original lfile copied at beginning of zopflipng processing
289-
# below if it is not present to these errors
272+
# ignore at this stage, original lfile copied at beginning of zopflipng
273+
# processing below if it is not present to these errors
290274
pass
291275
else:
292276
stdstream_lock.acquire()
293277
sys.stderr.write(
294-
ERROR_STRING
295-
+ " "
296-
+ img.pre_filepath
297-
+ " processing failed at the pngquant stage."
298-
+ os.linesep
278+
f"{ERROR_STRING} {img.pre_filepath} processing failed at the pngquant "
279+
f"stage.{os.linesep}"
299280
)
300281
stdstream_lock.release()
301282
if is_gui(sys.argv):
302283
log_error(
303-
img.pre_filepath
304-
+ " processing failed at the pngquant stage. "
305-
+ os.linesep
306-
+ str(cpe)
284+
f"{img.pre_filepath} processing failed at the pngquant stage."
285+
f"{os.linesep}{cpe}"
307286
)
308287
return None
309288
else:
310289
raise cpe
311290
except Exception as e:
312291
if is_gui(sys.argv):
313292
log_error(
314-
img.pre_filepath
315-
+ " processing failed at the pngquant stage. "
316-
+ os.linesep
317-
+ str(e)
293+
f"{img.pre_filepath} processing failed at the pngquant stage."
294+
f"{os.linesep}{e}"
318295
)
319296
return None
320297
else:
@@ -329,9 +306,10 @@ def optimize_png(png_path):
329306
# pngquant does not write expected file path if the file was larger after processing
330307
if not os.path.exists(img.post_filepath):
331308
shutil.copy(img.pre_filepath, img.post_filepath)
332-
# If pngquant did not quantize the file, permit zopflipng to attempt compression with mulitple
333-
# filters. This achieves better compression than the default approach for non-quantized PNG
334-
# files, but takes significantly longer (based upon testing by CS)
309+
# If pngquant did not quantize the file, permit zopflipng to attempt compression
310+
# with mulitple filters. This achieves better compression than the default
311+
# approach for non-quantized PNG files, but takes significantly longer
312+
# (based upon testing by CS)
335313
zopflipng_options = " -y --lossy_transparent "
336314
zopflipng_command = (
337315
ZOPFLIPNG_EXE_PATH
@@ -345,30 +323,23 @@ def optimize_png(png_path):
345323
except CalledProcessError as cpe:
346324
stdstream_lock.acquire()
347325
sys.stderr.write(
348-
ERROR_STRING
349-
+ " "
350-
+ img.pre_filepath
351-
+ " processing failed at the zopflipng stage."
352-
+ os.linesep
326+
f"{ERROR_STRING} {img.pre_filepath} processing failed at the zopflipng "
327+
f"stage.{os.linesep}"
353328
)
354329
stdstream_lock.release()
355330
if is_gui(sys.argv):
356331
log_error(
357-
img.pre_filepath
358-
+ " processing failed at the zopflipng stage. "
359-
+ os.linesep
360-
+ str(cpe)
332+
f"{img.pre_filepath} processing failed at the zopflipng stage."
333+
f"{os.linesep}{cpe}"
361334
)
362335
return None
363336
else:
364337
raise cpe
365338
except Exception as e:
366339
if is_gui(sys.argv):
367340
log_error(
368-
img.pre_filepath
369-
+ " processing failed at the pngquant stage. "
370-
+ os.linesep
371-
+ str(e)
341+
f"{img.pre_filepath} processing failed at the zopflipng stage."
342+
f"{os.linesep}{e}"
372343
)
373344
return None
374345
else:
@@ -383,7 +354,8 @@ def optimize_png(png_path):
383354
if not is_gui(sys.argv) and percent < 100:
384355
percent_string = format_ansi_green(percent_string)
385356

386-
# report percent original file size / post file path / size (bytes) to stdout (command line executable)
357+
# report percent original file size / post file path / size (bytes) to
358+
# stdout (command line executable)
387359
stdstream_lock.acquire()
388360
print(
389361
"[ "
@@ -396,7 +368,8 @@ def optimize_png(png_path):
396368
)
397369
stdstream_lock.release()
398370

399-
# report percent original file size / post file path / size (bytes) to log file (macOS GUI + right-click service)
371+
# report percent original file size / post file path / size (bytes) to log file
372+
# (macOS GUI + right-click service)
400373
if is_gui(sys.argv):
401374
log_info(
402375
"[ "

tox.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tox]
22
skipsdist = True
3-
envlist = py27,py38
3+
envlist = py310
44

55
[testenv]
66
deps = pytest

0 commit comments

Comments
 (0)