-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_config.py
1044 lines (748 loc) · 24.8 KB
/
update_config.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# Python 2 or 3 should work.
import base64, datetime, io, json, math, os, re, subprocess, sys, time, zipfile
# Use colored text if available:
try:
from termcolor import colored, cprint
import colorama
colorama.init()
except ImportError:
def colored(*list_args, **keyword_args): return list_args[0]
def cprint(*list_args, **keyword_args): print(list_args[0])
# Workaround for Python 2:
# https://stackoverflow.com/a/26745443
# https://github.com/pytorch/pytorch/issues/6932#issuecomment-388586780
try:
FileNotFoundError
except NameError:
FileNotFoundError = IOError
try:
BadZipFileError = zipfile.BadZipFile # New in Python 3.2
except:
BadZipFileError = zipfile.BadZipfile # Deprecated since Python 3.2
file_encoding = print_encoding = sys.getfilesystemencoding() or 'utf-8'
# - Config - source files and result content: -------------------------------
src_root_path = 'example_project_files'
dest_file_path = 'config.js'
src_dir_var_name = 'exampleRootDir'
filelist_var_name = 'exampleProjectFiles'
preview_size_var_name = 'PREVIEW_SIZE'
thumbnail_size_var_name = 'THUMBNAIL_SIZE'
preview_size = '80'
thumbnail_size = '20'
resize_filter = 'Welch'
quote = '"'
is_dest_array = True
# - Command line arguments: -------------------------------------------------
def get_arg_undashed(arg_name):
return arg_name.replace('-', '').replace('_', '') or arg_name
def get_first_found_arg_in_cmd(arg_names):
for arg_name in arg_names:
undashed_arg = get_arg_undashed(arg_name)
if undashed_arg in undashed_args:
return undashed_arg
return None
def get_cmd_arg_after_arg(first_arg_names, natural_number=False):
first_arg_name = get_first_found_arg_in_cmd(first_arg_names)
if first_arg_name:
i = undashed_args.index(first_arg_name) + 1
if len(undashed_args) > i:
next_arg = sys.argv[i]
if natural_number:
try:
if int(next_arg) < 1:
return None
except:
return None
return next_arg
return None
undashed_args = list(map(get_arg_undashed, sys.argv))
TEST = get_first_found_arg_in_cmd(['t', 'test'])
TEST_FILTERS = get_first_found_arg_in_cmd(['f', 'test_filters', 'filters'])
IM_6 = get_first_found_arg_in_cmd(['imagemagick6', 'im6', '6'])
IM_7 = get_first_found_arg_in_cmd(['imagemagick7', 'im7', '7'])
IM_UNDEFINED = not (IM_6 or IM_7)
USE_OPTIPNG = get_first_found_arg_in_cmd(['o', 'optipng'])
USE_LEANIFY = get_first_found_arg_in_cmd(['l', 'leanify'])
arg_src_root_path = get_cmd_arg_after_arg(['src', 'src_path', 'src_root_path'])
arg_dest_file_path = get_cmd_arg_after_arg(['dest', 'dest_path', 'dest_file_path'])
arg_resize_filter = get_cmd_arg_after_arg(['r', 'filter', 'thumb_filter', 'thumbnail_filter', 'resize_filter'])
arg_thumbnail_size = get_cmd_arg_after_arg(['s', 'size', 'thumb_size', 'thumbnail_size'], natural_number=True)
arg_preview_size = get_cmd_arg_after_arg(['z', 'zoom_size', 'preview_size'], natural_number=True)
# - Config - external helper programs: --------------------------------------
# Full path example: 'd:/programs/ImageMagick_7/magick.exe'
# Directory part of this path will be used for version 6 commands, if present.
converter_exe_path = 'magick'
converter_filters = [] # <- get automatically once for each run
merged_layer_suffix = '[0]' # <- to use pre-merged layer
temp_extracted_file_path = 'temp_extracted.png'
temp_resized_file_path = 'temp_resized.png'
src_file_path_placeholder = '<src_placeholder>'
new_size_placeholder = '<size_placeholder>'
filter_placeholder = '<filter_placeholder>'
layer_suffix_file_types = [
'psd'
, 'psb'
]
zip_file_types = [
'ora'
, 'kra'
]
zipped_thumbnail_filenames = [
'preview.png'
, 'thumbnail.png'
, 'mergedimage.png'
]
zipped_fullsize_filenames = [
'mergedimage.png'
]
cmd_args_to_get_image_size = [
'identify'
, '-verbose'
, '-format'
, '%Wx%H/'
, src_file_path_placeholder
]
cmd_args_to_make_resized_image = [
'convert'
, src_file_path_placeholder
, '-verbose'
, '-filter'
, filter_placeholder
, '-thumbnail'
, new_size_placeholder
, 'png32:' + temp_resized_file_path
]
cmd_args_to_get_filters = [
'convert'
, '-list'
, 'filter'
]
cmd_args_to_optimize_image_with_optipng = [
'optipng'
, '-i'
, '0'
, '-fix'
, src_file_path_placeholder
]
cmd_args_to_optimize_image_with_leanify = [
'leanify'
, '-v'
, src_file_path_placeholder
]
pat_check_image_size = re.compile(r'^(\d+)x(\d+)$', re.U | re.I)
# - Config - internal, do not change: ---------------------------------------
pat_comment_line = re.compile(r'((?:^|[\r\n])\s*)(//([^\r\n]*))', re.U | re.S)
pat_comment_block = re.compile(r'((?:^|[\r\n])\s*)(/\*(.*?)\*/)', re.U | re.S)
pat_trailing_space = re.compile(r'[^\S\r\n]+(?=$|[\r\n])', re.U)
pat_unslash = re.compile(r'\\(.)', re.U | re.S)
format_epoch = '%Epoch' # <- not from python library, but custom str-replaced
# format_ymd = '%Y-%m-%d'
# format_hms = '%H-%M-%S'
format_print = '%Y-%m-%d %H:%M:%S'
must_quote_chars = ' ,;>='
horizontal_separator_line_text = '-------- -------- -------- --------'
s_type = type('')
u_type = type(u'')
converter_exe_path_dir = os.path.dirname(converter_exe_path)
# - Functions: --------------------------------------------------------------
def print_tabs(*tabs):
print('\t'.join(tabs))
def print_with_colored_title(title=None, content=None, title_color=None, content_color=None):
print('')
if not title_color:
if title.find('Error') >= 0:
title_color = 'red'
elif title.find('Completed') >= 0:
title_color = 'green'
elif title.find('Running') >= 0:
title_color = 'magenta'
elif (
title.find('Done') >= 0
or title.find('Result') >= 0
or title.find(horizontal_separator_line_text) >= 0
):
title_color = 'cyan'
else:
title_color = 'yellow'
if title is not None:
if title_color:
cprint(title, title_color)
else:
print(title)
if content is not None:
if content_color:
cprint(content, content_color)
else:
print(content)
def is_type_str(v):
return isinstance(v, (s_type, u_type))
def get_str_from_bytes(v):
return v if is_type_str(v) else v.decode()
def get_text_lowercase(text):
return str(text).lower()
def get_text_encoded_for_print(text):
return text.encode(print_encoding) if sys.version_info.major == 2 else text
def is_any_char_of_a_in_b(chars, text):
for char in chars:
if text.find(char) >= 0:
return True
return False
def is_any_char_code_out_of_normal_range(text):
for char in text:
if ord(char) > 127:
return True
return False
def is_quoted(text):
for char in '\'"':
if text[0] == char and text[-1 : ][0] == char:
return True
return False
def quoted_if_must(text):
text = get_text_encoded_for_print(text)
return (
'"{}"'.format(text)
if not is_quoted(text) and (
is_any_char_of_a_in_b(must_quote_chars, text)
or is_any_char_code_out_of_normal_range(text)
)
else text
)
def quoted_list(args):
return list(map(quoted_if_must, args))
def cmd_args_to_text(args):
return ' '.join(quoted_list(args))
def timestamp(str_format=format_print):
return time.strftime(format_print)
def timestamp_now(str_format=format_epoch):
return time.strftime(str_format.replace(format_epoch, str(int(time.time()))))
def get_formatted_time(t, str_format=format_print):
return datetime.datetime.fromtimestamp(t).strftime(str_format)
# https://gist.github.com/cbwar/d2dfbc19b140bd599daccbe0fe925597#gistcomment-2845059
def get_formatted_filesize(full_num, space=' ', binary='i', suffix='B'):
magnitude = int(math.floor(math.log(full_num, 1000)))
shown_val = full_num / math.pow(1024, magnitude)
if magnitude > 7:
return '{:.1f}{}{}{}{}'.format(shown_val, space, 'Y', binary, suffix)
return '{:3.1f}{}{}{}{}'.format(shown_val, space, ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z'][magnitude], binary, suffix)
def get_quoted_value_text(value):
return (
quote
+ (
str(value)
.replace('\\', '\\\\')
.replace(quote, '\\' + quote)
)
+ quote
)
def remove_comments(text):
text = re.sub(pat_comment_line, r'\1', text)
text = re.sub(pat_comment_block, r'\1', text)
return text
def normalize_slashes(path):
return path.replace('\\', '/')
def fix_slashes(path):
if os.sep != '/':
path = path.replace('/', os.sep)
if os.sep != '\\':
path = path.replace('\\', os.sep)
return path
def get_file_name(path):
return normalize_slashes(path).rsplit('/', 1)[-1:][0]
def get_file_ext(path):
return get_file_name(path).rsplit('.', 1)[-1:][0]
def read_file(path, mode='r'):
if not os.path.isfile(path):
return ''
if 'b' not in mode:
file = io.open(path, mode, encoding=file_encoding)
else:
file = open(path, mode)
result = file.read()
file.close()
return result
def write_file(path, content, mode='a'):
result = None
if 'b' not in mode and isinstance(content, u_type):
file = io.open(path, mode, encoding=file_encoding)
else:
file = open(path, mode)
try:
result = file.write(content)
except Exception as exception:
print_with_colored_title('Error writing file:', exception)
# result = file.write(exception) # <- why? I don't remember
file.close()
return result
def rewrite_file(path, content, mode='w'):
return write_file(path, content, mode)
def remove_temp_file(path=None):
if path:
path = fix_slashes(path)
if os.path.isfile(path):
print_with_colored_title('Removing temporary file:', path)
os.remove(path)
else:
remove_temp_file(temp_extracted_file_path)
remove_temp_file(temp_resized_file_path)
return path
def get_separate_lists_of_files_and_dirs(root_path):
files = []
dirs = []
for name in os.listdir(root_path):
path = root_path + '/' + name
if os.path.isfile(path): files.append(name)
elif os.path.isdir(path): dirs.append(name)
return (
sorted(files)
, sorted(dirs)
)
def get_old_field(old_files, dir_name, filename, field_name):
try:
for old_dir in old_files:
if old_dir.get('subdir', '') == dir_name:
for old_file in old_dir.get('files', []):
if old_file.get('name', '') == filename:
return old_file.get(field_name, '')
except:
pass
return ''
def get_image_cmd_versions(cmd_args):
cmd_versions = []
if IM_7 or IM_UNDEFINED:
cmd_versions.append(
[converter_exe_path]
+ cmd_args
)
if IM_6 or IM_UNDEFINED:
cmd_versions.append(
[converter_exe_path_dir + cmd_args[0]]
+ cmd_args[1:]
)
return cmd_versions
def get_image_path_for_cmd(src_file_path, check_thumbnail=False):
print_with_colored_title('Parsing image file:', src_file_path)
src_file_path = normalize_slashes(src_file_path)
src_file_ext = get_file_ext(src_file_path)
print_with_colored_title('File type:', src_file_ext)
if merged_layer_suffix in src_file_ext:
return fix_slashes(src_file_path)
unzipped_file_path = None
try:
src_zip = zipfile.ZipFile(src_file_path, 'r')
if src_zip:
names_to_search = (
zipped_thumbnail_filenames
if check_thumbnail
else zipped_fullsize_filenames
)
for zipped_path in src_zip.namelist():
name = get_file_name(zipped_path)
if name in names_to_search:
print_with_colored_title('Found full merged image file:', zipped_path)
unzipped_content = src_zip.read(zipped_path)
unzipped_file_path = remove_temp_file(temp_extracted_file_path)
write_file(unzipped_file_path, unzipped_content, mode='w+b')
break
except BadZipFileError as exception:
if src_file_ext in zip_file_types:
print_with_colored_title('Error reading file as ZIP:', exception)
else:
if unzipped_file_path:
src_file_path = normalize_slashes(unzipped_file_path)
src_file_ext = get_file_ext(src_file_path)
if src_file_ext in layer_suffix_file_types:
src_file_path += merged_layer_suffix
return fix_slashes(src_file_path)
def get_cmd_result(cmd_args):
print_with_colored_title('Running command:', cmd_args_to_text(cmd_args))
# https://stackoverflow.com/a/16198668
try:
# Note: IM does not work in Linux terminal with shell=True.
# Note: stderr without override could be shown before the next print.
output = subprocess.check_output(cmd_args, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as exception:
print_with_colored_title(
'Error code {}, command output:'.format(exception.returncode)
, get_str_from_bytes(exception.output)
)
except (FileNotFoundError, IOError, OSError) as exception:
print_with_colored_title('Error:', exception)
else:
output = get_str_from_bytes(output)
print_with_colored_title('Done, command output:', output)
return output
return ''
def get_converter_filters():
if not len(converter_filters):
for cmd_args in get_image_cmd_versions(cmd_args_to_get_filters):
cmd_result = get_cmd_result(cmd_args)
if cmd_result:
a = cmd_result.split('\n')
a = [line.strip() for line in a]
a = filter(None, a)
a = sorted(list(set(a)))
converter_filters.extend(a)
break
return converter_filters
def get_image_cmd_result(src_file_path, cmd_args, new_size_arg=None, check_thumbnail=False):
if not new_size_arg:
new_size_arg = thumbnail_size_arg
src_file_name = get_file_name(src_file_path)
src_file_path = get_image_path_for_cmd(src_file_path, check_thumbnail=False)
# must get list instead of map object, or it will not run in python3:
cmd_args_with_src_path = [
(
resize_filter if (not TEST_FILTERS) and (arg == filter_placeholder)
else new_size_arg if (arg == new_size_placeholder)
else src_file_path if (arg == src_file_path_placeholder)
else arg
) for arg in cmd_args
]
if filter_placeholder in cmd_args_with_src_path:
if not os.path.isdir(new_size_arg):
os.makedirs(new_size_arg)
for filter_name in get_converter_filters():
cmd_args_with_filter = [
(
filter_name if (arg == filter_placeholder)
else arg if (arg.find(temp_resized_file_path) < 0)
else (
'_' + src_file_name +
'_' + new_size_arg +
'_' + filter_name +
'.'
).join(
(
(new_size_arg + '/' + arg) if (arg.find(':') < 0)
else (
':' + new_size_arg + '/'
).join(
arg.split(':', 1)
)
).rsplit('.', 1)
)
) for arg in cmd_args_with_src_path
]
get_cmd_result(cmd_args_with_filter)
i = cmd_args_with_src_path.index(filter_placeholder)
cmd_args_with_src_path = cmd_args_with_src_path[: i - 2] + cmd_args_with_src_path[i + 1 :]
return get_cmd_result(cmd_args_with_src_path)
def get_image_sizes(src_file_path):
for try_cmd_args_to_get_image_size in get_image_cmd_versions(cmd_args_to_get_image_size):
cmd_result = get_image_cmd_result(
src_file_path
, try_cmd_args_to_get_image_size
)
image_sizes = list(map(get_text_lowercase, cmd_result.strip().strip('/').split('/', 2)))
if len(image_sizes) > 0:
image_size = image_sizes[0]
if re.match(pat_check_image_size, image_size):
print_with_colored_title('Got image size:', image_size)
return image_sizes
return ''
def get_resized_image_as_base64(src_file_path, new_size_arg=None):
if not new_size_arg:
new_size_arg = thumbnail_size_arg
for try_cmd_args_to_resize_image in get_image_cmd_versions(cmd_args_to_make_resized_image):
temp_file_path = remove_temp_file(temp_resized_file_path)
cmd_result = get_image_cmd_result(
src_file_path
, try_cmd_args_to_resize_image
, new_size_arg=new_size_arg
, check_thumbnail=True
)
if os.path.isfile(temp_file_path):
optimizer_commands = []
if USE_OPTIPNG:
optimizer_commands.append(cmd_args_to_optimize_image_with_optipng)
if USE_LEANIFY:
optimizer_commands.append(cmd_args_to_optimize_image_with_leanify)
for try_cmd_args_to_optimize_image in optimizer_commands:
cmd_result = get_cmd_result(
[
(
temp_file_path
if arg == src_file_path_placeholder
else arg
) for arg in try_cmd_args_to_optimize_image
]
if src_file_path_placeholder in try_cmd_args_to_optimize_image
else (try_cmd_args_to_optimize_image + [temp_file_path])
)
raw_content = read_file(temp_file_path, mode='r+b')
base64_content = (
'data:image/'
+ get_file_ext(temp_file_path).replace('jpg', 'jpeg')
+ ';base64,'
+ base64.b64encode(raw_content).decode()
)
print_with_colored_title('Got resized image content:', '{} bytes'.format(len(base64_content)))
return base64_content
return ''
# - Check old list content: -------------------------------------------------
if arg_dest_file_path:
dest_file_path = arg_dest_file_path
print_with_colored_title('Reading old file:', dest_file_path)
t = timestamp()
mark_before = '// * generated file list start, saved at ' + t
mark_after = '// * generated file list end, saved at ' + t
content_before = content_after = ''
old_files = None
old_content = read_file(dest_file_path)
if old_content:
print('{} bytes'.format(len(old_content)))
pat_array_value = re.compile(
r'''^
(?P<Before>.*?\b''' + filelist_var_name + r'''\b\s*=\s*)
(?P<OldFileList>[\[\{].*?[\]\}])
(?P<After>;[\r\n]+.*)
$'''
, re.I | re.S | re.U | re.X
)
pat_var_value = re.compile(
r'''
(?P<Comment>
(?P<CommentLine>(^|[\r\n])\s*//[^\r\n]*)
| (?P<CommentBlock>/[*].*?[*]/)
)
| (?P<Declaration>\b(var|let|const|,)\s+)
(?P<VarName>[^\s'"`~!@#$%^&*{}()\[\].,;+=-]+)
(?P<Operator>\s*=\s*)
(?P<VarValue>
[+-]?(?P<Numeric>\d+)
| '(?P<Quoted1>([\\\\]'|[^'])*)'
| "(?P<Quoted2>([\\\\]"|[^"])*)"
)
'''
, re.I | re.S | re.U | re.X
)
match = re.search(pat_array_value, old_content)
if match:
content_before = match.group('Before')
content_after = match.group('After')
old_vars_text = remove_comments(content_before + content_after)
old_files_text = remove_comments(match.group('OldFileList'))
print_with_colored_title('Parsing old file list:', '{} bytes'.format(len(old_files_text)))
try:
old_files = json.loads(old_files_text) # <- 'encoding' is ignored and deprecated.
print_with_colored_title('Done parsing old file list:')
except Exception as exception:
old_files = []
print_with_colored_title('Error parsing old file list JSON:', exception)
for group in old_files:
files = group.get('files')
if files:
subdir = group.get('subdir', '')
if subdir:
subdir += '/'
for file in files:
print_tabs(
subdir +
file.get('name', colored('<no name>', 'red'))
, file.get('pixels', colored('<no WxH>', 'red'))
, file.get('filesize', colored('<no size>', 'red'))
, file.get('modtime', colored('<no time>', 'red'))
)
print_with_colored_title('Parsing optional old variables:')
for match in re.finditer(pat_var_value, old_vars_text):
if match.group('Comment'):
continue
var_value = (
match.group('Numeric')
or match.group('Quoted1')
or match.group('Quoted2')
)
if len(var_value) > 0:
var_name = match.group('VarName')
if var_name == src_dir_var_name:
src_root_path = re.sub(pat_unslash, r'\1', var_value)
elif var_name == preview_size_var_name:
try:
if int(var_value) > 0:
preview_size = var_value
except:
continue
elif var_name == thumbnail_size_var_name:
try:
if int(var_value) > 0:
thumbnail_size = var_value
except:
continue
else:
continue
print('{0} = {1}'.format(var_name, var_value))
print_with_colored_title('Done parsing old variables.')
else:
print('Empty or none.')
# - Override old values with command-line arguments: ------------------------
if arg_src_root_path: src_root_path = arg_src_root_path
if arg_resize_filter: resize_filter = arg_resize_filter
if arg_preview_size: preview_size = arg_preview_size
if arg_thumbnail_size: thumbnail_size = arg_thumbnail_size
preview_size_arg = '{0}x{0}'.format(preview_size)
thumbnail_size_arg = '{0}x{0}'.format(thumbnail_size)
# - Get list of files: ------------------------------------------------------
print_with_colored_title('Searching files in:', src_root_path)
src_filenames_by_subdir = {}
files, dirs = get_separate_lists_of_files_and_dirs(src_root_path)
if files:
src_filenames_by_subdir[''] = files
if dirs:
for name in dirs:
files, dirs2 = get_separate_lists_of_files_and_dirs(src_root_path + '/' + name)
src_filenames_by_subdir[name] = files
print_with_colored_title('Found files:')
for dir_name, filenames in sorted(src_filenames_by_subdir.items()):
for filename in sorted(filenames):
print(src_root_path + '/' + dir_name + '/' + filename)
# - Get metadata of files: --------------------------------------------------
# https://stackoverflow.com/a/49868510
pat_separate_thousands = re.compile(r'(?<!^)(?=(\d{3})+$)')
new_files = [] if is_dest_array else {}
for dir_name, filenames in sorted(src_filenames_by_subdir.items()):
files = []
for filename in sorted(filenames):
path = src_root_path + '/' + dir_name + '/' + filename
num_filesize = os.path.getsize(path)
num_modtime = os.path.getmtime(path)
text_filesize_short = get_formatted_filesize(num_filesize, space=' ', binary='', suffix='')
text_filesize_bytes = re.sub(pat_separate_thousands, r' ', str(num_filesize))
text_modtime = get_formatted_time(num_modtime)
image_path = path
image_size = None
image_sizes = get_image_sizes(path)
if not is_type_str(image_sizes) and not is_type_str(image_size):
if len(image_sizes) > 0:
image_size = image_sizes[0]
if len(image_sizes) > 1:
image_path += merged_layer_suffix
text_image_size = (
image_size
or get_old_field(old_files, dir_name, filename, 'pixels')
)
text_preview_base64 = (
get_resized_image_as_base64(image_path, preview_size_arg)
or get_old_field(old_files, dir_name, filename, 'preview')
)
text_thumbnail_base64 = (
get_resized_image_as_base64(image_path, thumbnail_size_arg)
or get_old_field(old_files, dir_name, filename, 'thumbnail')
)
files.append(
{
'name': filename
, 'pixels': text_image_size
, 'filesize': text_filesize_short
, 'bytes': text_filesize_bytes
, 'modtime': text_modtime
, 'preview': text_preview_base64
, 'thumbnail': text_thumbnail_base64
}
)
if is_dest_array:
new_files.append(
{
'subdir': dir_name
, 'files': files
}
)
else:
new_files[dir_name] = files
# - Compile results: --------------------------------------------------------
try:
json_text = json.dumps(new_files, sort_keys=True, indent='\t', default=repr) # <- use tabs, only in python 3
except TypeError:
json_text = json.dumps(new_files, sort_keys=True, indent=4, default=repr) # <- use spaces, fallback for python 2
json_lines = (
re.sub(
pat_trailing_space
, ''
, json_text
)
.replace(' '*4, '\t') # <- use tabs anyway
.split('\n')
)
if old_content:
replacements = [
{
'var_name': src_dir_var_name
, 'new_value': get_quoted_value_text(src_root_path)
, 'encounters': 0
}
, {
'var_name': preview_size_var_name
, 'new_value': preview_size
, 'encounters': 0
}
, {
'var_name': thumbnail_size_var_name
, 'new_value': thumbnail_size
, 'encounters': 0
}
]
def replace_var(match):
if match.group('Comment'):
return match.group(0)
var_name = match.group('VarName')
for replacement in replacements:
if var_name == replacement['var_name']:
replacement['encounters'] += 1
return ''.join([
match.group('Declaration')
, var_name
, match.group('Operator')
, replacement['new_value']
])
return match.group(0)
content_before = re.sub(pat_var_value, replace_var, content_before)
content_after = re.sub(pat_var_value, replace_var, content_after)
for replacement in replacements:
if not replacement['encounters']:
content_before = '\nvar {} = {};\n{}'.format(
replacement['var_name']
, replacement['new_value']
, content_before
)
else:
content_before = '\n\n'.join([
'\n'.join([
''
, '// * Remove slashes before the variables to override default values.'
, '// * See more available settings in the main script file (index.js).'
, '// * Add any overrides only here, to this file.'
, '// * Notes to avoid generation trouble:'
, '// * Keep paths as simple one-line strings.'
, '// * Keep JSON as strictly simple and valid.'
])