-
-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathconvert.py
More file actions
1196 lines (1005 loc) · 48.2 KB
/
convert.py
File metadata and controls
1196 lines (1005 loc) · 48.2 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
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
import argparse
import fnmatch
import logging
import os
import platform
import re
import shutil
import sys
import subprocess
import yaml
import zipfile
import xml.etree.ElementTree as ElTree
from defusedxml import ElementTree as DefusedElTree
from typing import Any, Dict, List, Tuple, cast
from operator import itemgetter
from itertools import groupby
from pathlib import Path
from pathvalidate.argparse import validate_filepath_arg
from pathvalidate import sanitize_filepath
class ConvertVars:
BASE_PATH = os.path.split(os.path.dirname(os.path.realpath(__file__)))[0]
EDITION_CHOICES: List[str] = ["all", "webapp", "mobileapp", "against-security"]
FILETYPE_CHOICES: List[str] = ["all", "docx", "odt", "pdf", "idml"]
LAYOUT_CHOICES: List[str] = ["all", "leaflet", "guide", "cards"]
LANGUAGE_CHOICES: List[str] = ["all", "en", "es", "fr", "nl", "no-nb", "pt-pt", "pt-br", "hu", "it", "ru"]
VERSION_CHOICES: List[str] = ["all", "latest", "1.0", "1.1", "2.2", "3.0", "5.0"]
LATEST_VERSION_CHOICES: List[str] = ["1.1", "3.0"]
TEMPLATE_CHOICES: List[str] = ["all", "bridge", "bridge_qr", "tarot", "tarot_qr"]
EDITION_VERSION_MAP: Dict[str, Dict[str, str]] = {
"webapp": {"2.2": "2.2", "3.0": "3.0"},
"against-security": {"1.0": "1.0"},
"mobileapp": {"1.0": "1.0", "1.1": "1.1"},
"all": {"2.2": "2.2", "1.0": "1.0", "1.1": "1.1", "3.0": "3.0", "5.0": "5.0"},
}
DEFAULT_TEMPLATE_FILENAME: str = os.sep.join(
["resources", "templates", "owasp_cornucopia_edition_ver_layout_document_template_lang"]
)
DEFAULT_OUTPUT_FILENAME: str = os.sep.join(["output", "owasp_cornucopia_edition_ver_layout_document_template_lang"])
args: argparse.Namespace
can_convert_to_pdf: bool = False
def check_fix_file_extension(filename: str, file_type: str) -> str:
if filename and not filename.endswith(file_type):
filename_split = os.path.splitext(filename)
if filename_split[1].strip(".").isnumeric():
filename = filename + "." + file_type.strip(".")
else:
filename = ".".join([os.path.splitext(filename)[0], file_type.strip(".")])
logging.debug(f" --- output_filename with new ext = {filename}")
return filename
def check_make_list_into_text(var: List[str]) -> str:
if not isinstance(var, list):
return str(var)
var = group_number_ranges(var)
text_output = ", ".join(str(s) for s in var)
if not text_output.strip():
text_output = " - "
return text_output
def _validate_file_paths(source_filename: str, output_pdf_filename: str) -> Tuple[bool, str, str]:
"""Validate and sanitize file paths to prevent command injection."""
source_path = os.path.abspath(source_filename)
output_dir = os.path.abspath(os.path.dirname(output_pdf_filename))
# Additional security checks
if not os.path.isfile(source_path):
return False, f"Source file does not exist: {source_path}", ""
if not os.path.isdir(output_dir):
return False, f"Output directory does not exist: {output_dir}", ""
# Ensure paths are within expected directories to prevent path traversal
base_path = os.path.abspath(convert_vars.BASE_PATH)
if not source_path.startswith(base_path):
return False, f"Source path outside base directory: {source_path}", ""
if not output_dir.startswith(base_path):
return False, f"Output directory outside base directory: {output_dir}", ""
return True, source_path, output_dir
def _safe_extractall(archive: zipfile.ZipFile, target_dir: str) -> None:
"""Extract zip members only if their resolved paths stay within target_dir.
Prevents Zip Slip / path traversal (CWE-22) by resolving symlinks and all
'..' components before comparing each member path against the target root.
Degenerate root entries ('.', '', './') are skipped rather than extracted,
because they carry no file content and resolve to the target directory itself.
"""
abs_target = os.path.realpath(target_dir)
for member in archive.infolist():
member_path = os.path.realpath(os.path.join(abs_target, member.filename))
# Root/degenerate entries ('.', '', './') resolve to abs_target itself.
# They are directory metadata with no content; skip them safely.
if member_path == abs_target:
continue
# Block any member whose resolved path escapes the target directory.
# The os.sep suffix prevents prefix collisions (e.g. /tmp/d vs /tmp/d_evil).
if not member_path.startswith(abs_target + os.sep):
raise ValueError(f"Zip Slip blocked: member '{member.filename}' would extract outside target directory")
archive.extract(member, target_dir)
def _validate_command_args(cmd_args: List[str]) -> bool:
"""Validate command arguments for dangerous characters."""
dangerous_chars = ["&", "|", ";", "$", "`", "(", ")", "<", ">", "*", "?", "[", "]", "{", "}", "\\"]
for arg in cmd_args:
if any(char in arg for char in dangerous_chars):
logging.warning(f"Potentially dangerous character found in argument: {arg}")
return False
return True
def _convert_with_libreoffice(source_filename: str, output_pdf_filename: str) -> bool:
libreoffice_bin = shutil.which("libreoffice") or shutil.which("soffice")
if not libreoffice_bin and platform.system() == "Windows":
potential_soffice = Path("C:/Program Files/LibreOffice/program/soffice.exe")
if potential_soffice.exists():
libreoffice_bin = str(potential_soffice)
if not libreoffice_bin:
return False
try:
logging.info(f"Using LibreOffice for conversion: {libreoffice_bin}")
# Validate file paths
is_valid, source_path, output_dir = _validate_file_paths(source_filename, output_pdf_filename)
if not is_valid:
logging.warning(source_path) # source_path contains the error message
return False
# Create user profile directory safely
user_profile_dir = os.path.abspath(os.path.join(convert_vars.BASE_PATH, "output", "lo_profile"))
os.makedirs(user_profile_dir, exist_ok=True)
user_profile_url = "file:///" + user_profile_dir.replace("\\", "/")
# Build command arguments
cmd_args = [
libreoffice_bin,
"--headless",
f"-env:UserInstallation={user_profile_url}",
"--convert-to",
"pdf",
"--outdir",
output_dir,
source_path,
]
# Validate command arguments
if not _validate_command_args(cmd_args):
return False
# Execute conversion
subprocess.run(
cmd_args, check=True, capture_output=True, text=True, timeout=300 # 5 minute timeout to prevent hanging
)
return True
except subprocess.TimeoutExpired:
logging.warning("LibreOffice conversion timed out after 5 minutes")
return False
except Exception as e:
logging.warning(f"LibreOffice conversion failed: {e}")
return False
def _convert_with_docx2pdf(source_filename: str, output_pdf_filename: str) -> bool:
if source_filename.endswith(".docx") and convert_vars.can_convert_to_pdf:
try:
import docx2pdf # type: ignore
docx2pdf.convert(source_filename, output_pdf_filename)
logging.info(f"New file saved: {output_pdf_filename}")
return True
except Exception as e:
logging.warning(f"\nConvert error: {e}")
return False
def _handle_conversion_failure(source_filename: str) -> None:
error_msg = (
f"Error. A temporary file {source_filename} was created in the output folder but cannot be converted "
f"to pdf on operating system: {platform.system()}.\n"
"Please install LibreOffice for cross-platform PDF support."
)
# Check if we should suggest MS Word
is_win_or_mac = platform.system().lower() in ["windows", "darwin"]
libreoffice_bin = shutil.which("libreoffice") or shutil.which("soffice")
if not libreoffice_bin and is_win_or_mac:
error_msg += " This does work with MS Word installed for .docx files."
logging.warning(error_msg)
def _cleanup_temp_file(filename: str) -> None:
if not convert_vars.args.debug:
try:
os.remove(filename)
except OSError:
pass
def _rename_libreoffice_output(source_filename: str, output_pdf_filename: str) -> None:
# LibreOffice outputs to the same name as source but with .pdf
default_out = str(Path(source_filename).with_suffix(".pdf"))
if os.path.normpath(default_out) != os.path.normpath(output_pdf_filename):
if os.path.exists(output_pdf_filename):
os.remove(output_pdf_filename)
os.rename(default_out, output_pdf_filename)
logging.info(f"New file saved: {output_pdf_filename}")
def convert_to_pdf(source_filename: str, output_pdf_filename: str) -> None:
"""Convert a document file (ODF, DOCX) to PDF using LibreOffice or docx2pdf.
Note: The source file is preserved after conversion, as it's typically an output
file that should be kept alongside the PDF, not a temporary file.
"""
logging.debug(
f" --- source_file = {source_filename} convert to {output_pdf_filename}\n--- starting pdf conversion now."
)
# 1. Attempt using LibreOffice
if _convert_with_libreoffice(source_filename, output_pdf_filename):
_rename_libreoffice_output(source_filename, output_pdf_filename)
# Don't delete the source file - we want to keep both ODF and PDF
return
# 2. Fallback to docx2pdf
if _convert_with_docx2pdf(source_filename, output_pdf_filename):
_cleanup_temp_file(source_filename)
return
# 3. Everything failed
_handle_conversion_failure(source_filename)
# Don't delete the source file even on failure - it may still be useful
def create_edition_from_template(
layout: str, language: str = "en", template: str = "bridge", version: str = "3.0", edition: str = "webapp"
) -> None:
# Get the list of available translation files
yaml_files = get_files_from_of_type(os.sep.join([convert_vars.BASE_PATH, "source"]), "yaml")
if not yaml_files:
return
mapping: Dict[str, Any] = get_mapping_for_edition(yaml_files, version, language, edition, template, layout)
if not mapping:
logging.warning(
f"No mapping file found for version: {version}, lang: {language}, edition: {edition},"
f" template: {template}, layout: {layout}"
)
# return
# Get the language data from the correct language file (checks vars.args.language to select the correct file)
language_data: Dict[str, Dict[str, str]] = get_language_data(yaml_files, language, version, edition)
# Transform the language data into the template mapping
language_dict: Dict[str, str] = map_language_data_to_template(language_data)
# Get meta data from language data
meta: Dict[str, str] = get_meta_data(language_data)
if not meta:
logging.error("No metadata found. Cannot proceed.")
return
template_doc: str = get_template_for_edition(layout, template, edition)
if template_doc == "None":
return
file_name, file_extension = os.path.splitext(template_doc)
logging.debug(f"template_doc: {template_doc}")
# Name output file with correct edition, component, language & version
output_file: str = rename_output_file(file_extension, template, layout, meta)
ensure_folder_exists(os.path.dirname(output_file))
# Work with docx/odt file (and maybe convert to pdf afterwards)
if file_extension in (".docx", ".odt"):
language_dict.update(mapping)
if file_extension == ".docx":
# Get the input (template) document
doc = get_docx_document(template_doc)
if doc:
doc = replace_docx_inline_text(doc, language_dict)
doc.save(output_file)
else:
save_odt_file(template_doc, language_dict, output_file)
if convert_vars.args.pdf:
# If file type is pdf, then convert the generated file to pdf
pdf_output_file = str(Path(output_file).with_suffix(".pdf"))
convert_to_pdf(output_file, pdf_output_file)
elif file_extension == ".idml":
language_dict.update(mapping)
save_idml_file(template_doc, language_dict, output_file)
logging.info(f"New file saved: {output_file}")
def valid_meta(meta: Dict[str, Any], language: str, edition: str, version: str, template: str, layout: str) -> bool:
if not has_translation_for_edition(meta, language):
logging.warning(
f"Translation in {language} does not exist for edition: {edition}, version: {version} "
"or the translation choices are missing from the meta -> languages section in the mappings file"
)
return False
if not has_template_for_edition(meta, template) and not convert_vars.args.inputfile:
logging.warning(
f"The template: {template} does not exist for edition: {edition}, version: {version} "
"or the template choices are missing from the meta templates section in the mappings file"
)
return False
if not has_layout_for_edition(meta, layout) and not convert_vars.args.inputfile:
logging.warning(
f"The layout: {layout} does not exist for edition: {edition}, version: {version} "
"or the layout choices are missing from the meta -> layouts section in the mappings file"
)
return False
return True
def has_translation_for_edition(meta: Dict[str, Any], language: str) -> bool:
if meta and "languages" in meta and language in meta["languages"]:
return True
return False
def has_template_for_edition(meta: Dict[str, Any], template: str) -> bool:
if meta and "templates" in meta and template in meta["templates"]:
return True
return False
def has_layout_for_edition(meta: Dict[str, Any], layout: str) -> bool:
if meta and "layouts" in meta and layout in meta["layouts"]:
return True
return False
def ensure_folder_exists(folder_path: str) -> None:
"""Check if folder exists and if not, create folders recursively."""
if not os.path.exists(folder_path):
os.makedirs(folder_path)
def main() -> None:
convert_vars.args = parse_arguments(sys.argv[1:])
set_logging()
logging.debug(" --- args = %s", str(convert_vars.args))
set_can_convert_to_pdf()
libreoffice_available = bool(shutil.which("libreoffice") or shutil.which("soffice"))
can_make_pdf = convert_vars.can_convert_to_pdf or libreoffice_available
if convert_vars.args.pdf and not can_make_pdf and not convert_vars.args.debug:
logging.error(
"Cannot convert to pdf on this system. "
"Pdf conversion is available on Windows and Mac (with MS Word), or on any system with LibreOffice."
)
return
# Create output files
for edition in get_valid_edition_choices():
for layout in get_valid_layout_choices():
for language in get_valid_language_choices():
for template in get_valid_templates():
for version in get_valid_version_choices():
create_edition_from_template(layout, language, template, version, edition)
def parse_arguments(input_args: List[str]) -> argparse.Namespace:
"""Parse and validate the input arguments. Return object containing argument values."""
description = "Tool to output OWASP Cornucopia playing cards into different file types and languages. "
description += "\nExample usage: $ scripts/convert.py --pdf -lt guide -l es -v 2.2"
description += "\nExample usage: $ scripts/convert.py -t tarot -l en -lt cards -v 5.0 -e eop -i "
description += "./resources/templates/eop_ver_cards_tarot_lang.idml -o ./output/eop-5.0-cards-en.idml"
description += "\nExample usage: c:\\cornucopia\\scripts\\convert.py -t bridge -lt cards -l fr -v 2.2 -o"
description += " my_output_folder\\owasp_cornucopia_edition_version_layout_language_template.idml"
parser = argparse.ArgumentParser(
description=description, formatter_class=argparse.RawTextHelpFormatter, exit_on_error=False
)
parser.add_argument(
"-i",
"--inputfile",
type=validate_filepath_arg,
default="",
help=(
"Input (template) file to use."
f"\nDefault={convert_vars.DEFAULT_TEMPLATE_FILENAME}.(docx|idml)"
"\nTemplate type is dependent on the file (-o) specified."
),
)
parser.add_argument(
"-v",
"--version",
type=is_valid_string_argument,
required=False,
default="latest",
help=(
"Output version to produce. [`all`, `latest`, `1.0`, `1.1`, `2.2`, `3.0`] "
"\nFor the Website edition:"
"\nVersion 3.0 will deliver cards mapped to ASVS 5.0"
"\nVersion 2.2 will deliver cards mapped to ASVS 4.0"
"\nFor the Mobile edition:"
"\nVersion 1.0 will deliver cards mapped to MASVS 2.0"
"\nVersion 1.1 will deliver cards mapped to MASVS 2.0"
"\nVersion all will deliver all versions of cornucopia"
"\nVersion latest will deliver the latest deck versions of cornucopia"
"\nYou can also specify another version explicitly if needed. "
"If so, there needs to be a yaml file in the source folder where the name contains "
"the version code. Eg. edition-template-ver-lang.yaml"
),
)
parser.add_argument(
"-o",
"--outputfile",
default="",
type=validate_filepath_arg,
help=(
"Specify a path and name of output file to generate. (caution: existing file will be overwritten). "
f"\nEg. {convert_vars.DEFAULT_OUTPUT_FILENAME}.(docx|pdf|idml)"
),
)
parser.add_argument(
"-p",
"--pdf",
action="store_true",
default=False,
help=(
"Whether to generate a pdf in addition to the printable document. "
"Does not generate a pdf by default. Only docx can be converted to pdf for the moment."
),
)
parser.add_argument(
"-d",
"--debug",
action="store_true",
help="Output additional information to debug script",
)
group = parser.add_mutually_exclusive_group(required=False)
group.add_argument(
"-l",
"--language",
type=is_valid_string_argument,
default="en",
help=(
"Output language to produce. [`en`, `es`, `fr`, `nl`, `no-nb`, `pt-pt`, `pt-br`, `it`, `ru`] "
"you can also specify your own language file. If so, there needs to be a yaml "
"file in the source folder where the name ends with the language code. Eg. edition-template-ver-lang.yaml"
),
)
group = parser.add_mutually_exclusive_group(required=False)
group.add_argument(
"-t",
"--template",
type=is_valid_string_argument,
default="bridge",
help=(
"From which template to produce the document. [`bridge`, `tarot` or `tarot_qr`]\n"
"Templates need to be added to ./resource/templates or specified with (-i or --inputfile)\n"
"Bridge cards are 2.25 x 3.5 inch and have the mappings printed on them, \n"
"tarot cards are 2.75 x 4.75 (71 x 121 mm) inch large, \n"
"qr cards have a QRCode that points to an maintained list.\n"
"You can also speficy your own template. If so, there needs to be a file in the templates folder "
"where the name contains the template code. Eg. owasp_cornucopia_edition_ver_layout_template_lang.idml"
),
)
group = parser.add_mutually_exclusive_group(required=False)
group.add_argument(
"-e",
"--edition",
type=is_valid_string_argument,
default="all",
help=(
"Output decks to produce. [`all`, `webapp` or `mobileapp`]\n"
"The various Cornucopia decks. `web` will give you the Website App edition.\n"
"`mobileapp` will give you the Mobile App edition.\n"
"You can also speficy your own edition. If so, there needs to be a yaml "
"file in the source folder where the name contains the edition code. Eg. edition-template-ver-lang.yaml"
),
)
group = parser.add_mutually_exclusive_group(required=False)
group.add_argument(
"-lt",
"--layout",
type=is_valid_string_argument,
default="all",
help=(
"Document layouts to produce. [`all`, `guide`, `leaflet` or `cards`]\n"
"The various Cornucopia document layouts.\n"
"`cards` will output the high quality print card deck.\n"
"`guide` will generate the docx guide with the low quality print deck.\n"
"`leaflet` will output the high quality print leaflet.\n"
"You can also speficy your own layout. If so, there needs to be a yaml "
"file in the source folder where the name contains the layout code. Eg. edition-layout-ver-lang.yaml"
),
)
try:
args = parser.parse_args(input_args)
except argparse.ArgumentError as exc:
# sys.tracebacklimit = 0
logging.error(exc.message)
sys.exit()
return args
def is_valid_string_argument(argument: str) -> str:
if len(argument) > 255:
raise argparse.ArgumentTypeError("The option can not have more the 255 char.")
if not re.match(
r"^[\u0600-\u06FF\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF\uFB50-\uFDFF\uFE70-\uFEFF\uFDF2\uFDF3\uFDF4\uFDFD\u3040-\u309F\u30A0-\u30FF\u4E00-\u9FFF\uFF66-\uFF9Fー々〆〤\u3400-\u4DBF\uF900-\uFAFF\u0900-\u097F\u0621-\u064A\u0660-\u0669\u4E00-\u9FFF\u0E00-\u0E7F«»฿ฯ๏๚๛\u0400-\u04FF\u0500-\u052F\u2DE0-\u2DFF\uA640-\uA69FЮ́ю́Я́я́\u0370-\u03FF\u1F00-\u1FFFA-Za-zÀ-ÖØ-öø-ÿĀ-ž0-9._\--ءآأؤإئابةتثجحخدذرزسشصضطظعغفقكلمنهوي ًٌٍَُِّْٰﷲﷴﷺﷻ ٠١٢٣٤٥٦٧٨٩ \s]+$", # noqa: E501
argument,
):
raise argparse.ArgumentTypeError(
"The option can only contain a-z letters, numbers, periods, dash or underscore"
)
return argument
def is_valid_argument_list(arguments: List[str]) -> Any:
if not isinstance(arguments, List):
return arguments
for argument in arguments:
is_valid_string_argument(argument)
return arguments
def get_document_paragraphs(doc: Any) -> List[Any]:
paragraphs = list(doc.paragraphs)
l1 = len(paragraphs)
for table in doc.tables:
paragraphs += get_paragraphs_from_table_in_doc(table)
l2 = len(paragraphs)
if not len(paragraphs):
logging.error("No paragraphs found in doc")
logging.debug(f" --- count doc paragraphs = {l1}, with table paragraphs = {l2}")
return paragraphs
def get_docx_document(docx_file: str) -> Any:
"""Open the file and return the docx document."""
import docx # type: ignore[import-untyped]
if os.path.isfile(docx_file):
return docx.Document(docx_file)
else:
logging.error("Could not find file at: %s", str(docx_file))
# Create a blank document if it fails
return docx.Document()
def get_files_from_of_type(path: str, ext: str) -> List[str]:
"""Get a list of files from a specified folder recursively, that have the specified extension."""
files = []
for root, dirnames, filenames in os.walk(path):
for filename in fnmatch.filter(filenames, "*." + str(ext)):
files.append(os.path.join(root, filename))
if not files:
logging.error("No language files found in folder: %s", str(os.sep.join([convert_vars.BASE_PATH, "source"])))
return files
logging.debug(
"%s%s", f" --- found {len(files)} files of type {ext}. Showing first few:\n* ", str("\n* ".join(files[:3]))
)
return files
def get_find_replace_list(meta: Dict[str, str], template: str, layout: str) -> List[Tuple[str, str]]:
ll: List[Tuple[str, str]] = [
("_edition", "_" + meta["edition"].lower()),
("_layout", "_" + layout.lower()),
("_document_template", "_" + template.lower()),
("_lang", "_" + meta["language"].lower()),
("_ver", "_" + meta["version"].lower()),
]
return ll
def get_full_tag(cat_id: str, id: str, tag: str) -> str:
if cat_id == "Common":
full_tag = "${{{}}}".format("_".join([cat_id, id]))
else:
full_tag = "${{{}}}".format("_".join([cat_id, id, tag]))
return full_tag
def get_mapping_for_edition(
yaml_files: List[str], version: str, language: str, edition: str, template: str, layout: str
) -> Dict[str, Any]:
mapping_data: Dict[str, Any] = get_mapping_data_for_edition(yaml_files, language, version, edition)
if not mapping_data:
logging.warning("No mapping file found")
return {}
if "meta" not in mapping_data or not valid_meta(mapping_data["meta"], language, edition, version, template, layout):
logging.warning("Metadata is missing or invalid in mapping file")
return {}
try:
mapping_data = build_template_dict(mapping_data)
except Exception as e:
logging.warning(f"Failed to build template mapping: {e}")
return mapping_data
def get_mapping_data_for_edition(
yaml_files: List[str],
language: str,
version: str = "3.0",
edition: str = "webapp",
) -> Dict[Any, Dict[Any, Any]]:
"""Get the raw data of the replacement text from correct yaml file"""
data: Dict[Any, Dict[Any, Any]] = {}
logging.debug(
" --- Starting get_mapping_data_for_edition() for edition: "
f"{edition} , language: {language} and version: {version} "
f" with mapping to version {get_valid_mapping_for_version(version, edition)}"
)
mappingfile: str = ""
for file in yaml_files:
if is_yaml_file(file) and is_mapping_file_for_version(file, version, edition):
mappingfile = file
if not mappingfile:
return data
with open(mappingfile, "r", encoding="utf-8") as f:
try:
data = yaml.safe_load(f)
except yaml.YAMLError as e:
logging.info(f"Error loading yaml file: {mappingfile}. Error = {e}")
data = {}
if "meta" in data.keys() and "component" in data["meta"].keys() and data["meta"]["component"] == "mappings":
logging.debug(" --- found mappings file: " + os.path.split(mappingfile)[1])
else:
logging.debug(" --- found source file, but it was missing metadata: " + os.path.split(mappingfile)[1])
if "meta" in list(data.keys()):
meta_keys = data["meta"].keys()
logging.debug(f" --- data.keys() = {data.keys()}, data[meta].keys() = {meta_keys}")
data = {}
logging.debug(f" --- Len = {len(data)}.")
return data
def build_template_dict(input_data: Dict[str, Any]) -> Dict[str, Any]:
"""Build template dictionary from the input data"""
data: Dict[str, Any] = {"meta": get_meta_data(input_data)}
for key in list(k for k in input_data.keys() if k != "meta"):
for paragraphs in input_data[key]:
text_type = ""
if key == "suits":
text_type = "cards"
if key == "paragraphs":
text_type = "sentences"
logging.debug(f" --- key = {key}.")
logging.debug(f" --- suit name = {paragraphs['name']}")
logging.debug(f" --- suit id = {is_valid_string_argument(paragraphs['id'])}")
full_tag = "${{{}}}".format("_".join([is_valid_string_argument(paragraphs["id"]), "suit"]))
logging.debug(f" --- suit tag = {full_tag}")
if data["meta"]["component"] == "cards":
data[full_tag] = paragraphs["name"]
for paragraph in paragraphs[text_type]:
for tag, text_output in paragraph.items():
logging.debug(f" --- tag = {tag}")
logging.debug(f" --- text = {text_output}")
logging.debug(f" --- paragraph = {paragraph}")
full_tag = get_full_tag(
is_valid_string_argument(paragraphs["id"]), is_valid_string_argument(paragraph["id"]), tag
)
logging.debug(f" --- full tag = {full_tag}")
data[full_tag] = check_make_list_into_text(text_output)
return data
def get_meta_data(data: Dict[str, Any]) -> Dict[str, Any]:
meta: Dict[str, Any] = {}
if not data or "meta" not in data:
logging.error("Could not find meta tag in the language data.")
return meta
raw_meta = data["meta"]
if not isinstance(raw_meta, dict):
logging.error("Meta tag is not a dictionary.")
return meta
valid_keys = ("edition", "component", "language", "version", "languages", "layouts", "templates")
for key in valid_keys:
if key in raw_meta:
value = raw_meta[key]
# Simple validation: must be string or list of strings
if isinstance(value, str) and value.strip():
meta[key] = value
elif isinstance(value, list) and all(isinstance(v, str) for v in value):
meta[key] = value
logging.info(f" --- extracted meta data = {meta}")
return meta
def get_paragraphs_from_table_in_doc(doc_table: Any) -> List[Any]:
paragraphs: List[Any] = []
for row in doc_table.rows:
for cell in row.cells:
for paragraph in cell.paragraphs:
if len(paragraph.runs):
paragraphs.append(paragraph)
for t2 in cell.tables:
paragraphs += get_paragraphs_from_table_in_doc(t2)
return paragraphs
def get_language_data(
yaml_files: List[str],
language: str,
version: str = "3.0",
edition: str = "webapp",
) -> Dict[Any, Any]:
"""Get the raw data of the replacement text from correct yaml file"""
language_file: str = ""
for file in yaml_files:
if is_yaml_file(file) and is_lang_file_for_version(file, version, language, edition):
language_file = file
if not language_file:
logging.error(f"Did not find translation for version: {version}, lang: {language}, edition: {edition}")
return {}
logging.debug(f" --- Loading language file: {language_file}")
with open(language_file, "r", encoding="utf-8") as f:
try:
data = yaml.safe_load(f)
except yaml.YAMLError as e:
logging.error(f"Error loading yaml file: {language_file}. Error = {e}")
data = {}
if not data or "meta" not in data:
logging.error(f"Invalid or empty language file: {language_file}")
return {}
return cast(Dict[Any, Any], data)
def is_mapping_file_for_version(path: str, version: str, edition: str) -> bool:
return (
os.path.basename(path).find("mappings") >= 0
and os.path.basename(path).find(edition) >= 0
and os.path.basename(path).find(version) >= 0
)
def is_lang_file_for_version(path: str, version: str, lang: str, edition: str) -> bool:
filename = os.path.basename(path).lower()
# Support both -en. and -en_US. style
lang_patterns = ["-" + lang.lower() + ".", "-" + lang.lower().replace("-", "_") + "."]
has_lang = any(filename.find(p) != -1 for p in lang_patterns)
return (
filename.find(edition.lower()) != -1
and filename.find(version.lower()) != -1
and has_lang
and filename.find("mappings") == -1
)
def is_yaml_file(path: str) -> bool:
return os.path.splitext(path)[1] in (".yaml", ".yml")
def map_language_data_to_template(input_data: Dict[str, Any]) -> Dict[str, str]:
try:
data = build_template_dict(input_data)
except Exception as e:
logging.warning(f"Could not build valid template mapping. The Yaml file is not valid. Got exception: {e}")
data = input_data
if convert_vars.args.debug:
debug_txt = " --- Translation data showing First 4 (key: text):\n* "
debug_txt += "\n* ".join(l1 + ": " + str(data[l1]) for l1 in list(data.keys())[:4])
logging.debug(debug_txt)
debug_txt = " --- Translation data showing Last 4 (key: text):\n* "
debug_txt += "\n* ".join(l1 + ": " + str(data[l1]) for l1 in list(data.keys())[-4:])
logging.debug(debug_txt)
return data
def get_replacement_mapping_value(k: str, v: str, el_text: str) -> str:
reg_str: str = (
"^(OWASP MASTG|OWASP MASVS|OWASP SCP|OWASP ASVS|OWASP AppSensor|CAPEC™|SAFECODE)\u2028"
+ k.replace("$", "\\$").strip()
+ "$"
)
if re.match(reg_str, el_text.strip()):
if len(v) >= 38:
return el_text[: el_text.find("\u2028")] + ": " + v
else:
return el_text.replace(k, v)
return ""
def get_replacement_value_from_dict(el_text: str, replacement_values: List[Tuple[str, str]]) -> str:
# Fast path: if no $ and no OWASP, likely no tags
if "$" not in el_text and "OWASP" not in el_text:
return el_text
for k, v in replacement_values:
# Avoid expensive regex if key is not even in text
if k.strip() not in el_text:
continue
el_new = get_replacement_mapping_value(k, v, el_text)
if el_new:
return el_new
reg = r"(?<!\S)" + re.escape(k.strip()) + r"(?!\S)"
el_text = re.sub(reg, v, el_text)
return el_text
def get_suit_tags_and_key(key: str, edition: str) -> Tuple[List[str], str]:
# Short tags to match the suits in the template documents
suit_tags: List[str] = []
suit_key: str = ""
if key == "suits" and edition == "webapp":
suit_tags = ["VE", "AT", "SM", "AZ", "CR", "C", "WC"]
suit_key = "cards"
if key == "suits" and edition == "mobileapp":
suit_tags = ["PC", "AA", "NS", "RS", "CRM", "CM", "WC"]
suit_key = "cards"
elif key == "paragraphs":
suit_tags = ["Common"]
suit_key = "sentences"
return suit_tags, suit_key
def get_template_for_edition(layout: str = "guide", template: str = "bridge", edition: str = "webapp") -> str:
template_doc: str
args_input_file: str = convert_vars.args.inputfile
sfile_ext = "idml"
if layout == "guide":
sfile_ext = "odt"
if args_input_file:
# Input file was specified
if os.path.isabs(args_input_file):
template_doc = args_input_file
elif os.path.isfile(convert_vars.BASE_PATH + os.sep + args_input_file):
template_doc = os.path.normpath(convert_vars.BASE_PATH + os.sep + args_input_file)
elif os.path.isfile(convert_vars.BASE_PATH + os.sep + args_input_file.replace(".." + os.sep, "")):
template_doc = os.path.normpath(
convert_vars.BASE_PATH + os.sep + args_input_file.replace(".." + os.sep, "")
)
elif args_input_file.find("..") == -1 and os.path.isfile(
convert_vars.BASE_PATH + os.sep + ".." + os.sep + args_input_file
):
template_doc = os.path.normpath(convert_vars.BASE_PATH + os.sep + ".." + os.sep + args_input_file)
elif os.path.isfile(convert_vars.BASE_PATH + os.sep + args_input_file.replace("scripts" + os.sep, "")):
template_doc = os.path.normpath(
convert_vars.BASE_PATH + os.sep + args_input_file.replace("scripts" + os.sep, "")
)
else:
template_doc = args_input_file
logging.debug(f" --- Template_doc NOT found. Input File = {args_input_file}")
else:
# No input file specified - using defaults
template_doc = os.path.normpath(
convert_vars.BASE_PATH
+ os.sep
+ convert_vars.DEFAULT_TEMPLATE_FILENAME.replace("edition", edition)
.replace("layout", layout)
.replace("document_template", template)
+ "."
+ sfile_ext
)
template_doc = template_doc.replace("\\ ", " ")
template_doc = str(Path(sanitize_filepath(template_doc)))
if os.path.isfile(template_doc):
template_doc = check_fix_file_extension(template_doc, sfile_ext)
logging.debug(f" --- Returning template_doc = {template_doc}")
return template_doc
else:
logging.error(f"Source file not found: {template_doc}. Please ensure file exists and try again.")
return "None"
def get_valid_layout_choices() -> List[str]:
layouts = []
if convert_vars.args.layout.lower() == "all" or convert_vars.args.layout == "":
for layout in convert_vars.LAYOUT_CHOICES:
if layout not in ("all", "guide"):
layouts.append(layout)
if layout == "guide" and convert_vars.args.edition.lower() in "webapp":
layouts.append(layout)
else:
layouts.append(convert_vars.args.layout)
return layouts
def get_valid_language_choices() -> List[str]:
languages = []
if convert_vars.args.language.lower() == "all":
for language in convert_vars.LANGUAGE_CHOICES:
if language not in ("all", "template"):
languages.append(language)
elif convert_vars.args.language == "":
languages.append("en")
else:
languages.append(convert_vars.args.language)
return languages
def get_valid_version_choices() -> List[str]:
versions = []
edition: str = convert_vars.args.edition.lower()
if convert_vars.args.version.lower() == "all":
for version in convert_vars.VERSION_CHOICES:
if version not in ("all", "latest") and not get_valid_mapping_for_version(version, edition) == "":
versions.append(version)
elif convert_vars.args.version == "" or convert_vars.args.version == "latest":
for version in convert_vars.LATEST_VERSION_CHOICES:
if not get_valid_mapping_for_version(version, edition) == "":
versions.append(version)
else:
versions.append(convert_vars.args.version)
if not versions:
logging.debug(f"No deck with version: {convert_vars.args.version} for edition: {edition} exists")
return versions
def get_valid_mapping_for_version(version: str, edition: str) -> str:
return ConvertVars.EDITION_VERSION_MAP.get(edition, {}).get(version, "")
def get_valid_templates() -> List[str]:
templates = []
if convert_vars.args.template.lower() == "all":
for template in [t for t in convert_vars.TEMPLATE_CHOICES if t not in "all"]:
templates.append(template)
elif convert_vars.args.template == "":
templates.append("bridge")
templates.append("tarot_qr")
else:
templates.append(convert_vars.args.template)
return templates
def get_valid_edition_choices() -> List[str]:
editions = []
if convert_vars.args.edition.lower() == "all" or not convert_vars.args.edition.lower():
for edition in convert_vars.EDITION_CHOICES:
if edition not in "all":
editions.append(edition)
if convert_vars.args.edition and convert_vars.args.edition not in "all":
editions.append(convert_vars.args.edition)
return editions
def group_number_ranges(data: List[str]) -> List[str]:
if len(data) < 2 or len([s for s in data if not str(s).isnumeric()]):
return data
list_ranges: List[str] = []
data_numbers = [int(s) for s in data]
for k, g in groupby(enumerate(data_numbers), lambda x: x[0] - x[1]):
group: List[int] = list(map(itemgetter(1), g))
group = list(map(int, group))
if group[0] == group[-1]:
list_ranges.append(str(group[0]))
else:
list_ranges.append(str(group[0]) + "-" + str(group[-1]))
return list_ranges
def save_docx_file(doc: Any, output_file: str) -> None:
ensure_folder_exists(os.path.dirname(output_file))
doc.save(output_file)
def save_odt_file(template_doc: str, language_dict: Dict[str, str], output_file: str) -> None:
# Get the output path and temp output path to put the temp xml files
output_path = os.path.join(convert_vars.BASE_PATH, "output")
temp_output_path = os.path.join(output_path, "temp_odt")
# Ensure the output folder and temp output folder exist
ensure_folder_exists(temp_output_path)
logging.debug(" --- temp_folder for extraction of xml files = %s", str(temp_output_path))
# Unzip source xml files and place in temp output folder
with zipfile.ZipFile(template_doc) as odt_archive:
_safe_extractall(odt_archive, temp_output_path)
# ODT text is usually in content.xml and sometimes styles.xml
targets = ["content.xml", "styles.xml"]
replacement_values = sort_keys_longest_to_shortest(language_dict)