ckparser 0.3.4__tar.gz → 0.3.6__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ckparser
3
- Version: 0.3.4
3
+ Version: 0.3.6
4
4
  Summary: Parse Paradox Jomini data files to Python/JSON and revert them back experimentally.
5
5
  Author: Marc Debureaux (debnet)
6
6
  License-Expression: MIT
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ckparser
3
- Version: 0.3.4
3
+ Version: 0.3.6
4
4
  Summary: Parse Paradox Jomini data files to Python/JSON and revert them back experimentally.
5
5
  Author: Marc Debureaux (debnet)
6
6
  License-Expression: MIT
@@ -41,7 +41,7 @@ import re
41
41
  import time
42
42
 
43
43
  # Script version
44
- __version__ = "0.3.4"
44
+ __version__ = "0.3.6"
45
45
 
46
46
  # Logger (because logging is awesome)
47
47
  logger = logging.getLogger(__name__)
@@ -116,7 +116,7 @@ regex_inline = re.compile(r"([^\s\"]+\s*[?!<=>]+\s*(([^@\"]\[?[^\s]+\]?)|(\"[^\"
116
116
  # Regex to parse blocks with bracket below the key/operator
117
117
  regex_block = re.compile(r"(\s*([?!<=>]+)\s+\{)|(\s+\s*([?!<=>])+\s*\{)", re.MULTILINE)
118
118
  # Regex to parse lines with format key=value
119
- regex_line = re.compile(r"\"?(?P<key>[^\s\"]+)\"?\s*(?P<operator>[?!<=>]+)\s*(list\s*)?(?P<value>.*)")
119
+ regex_line = re.compile(r"\"?(?P<key>[^\s\"?!<=>]+)\"?\s*(?P<operator>[?!<=>]+)\s*(list\s*)?(?P<value>.*)")
120
120
  # Regex to parse independent items in a list
121
121
  regex_item = re.compile(r"(\"[^\"]+\"|[\d\.]+|[^\s]+)")
122
122
  # Regex to parse dates
@@ -547,14 +547,14 @@ def parse_file(
547
547
  start_time = time.monotonic()
548
548
  if base_dir:
549
549
  base_dir = os.sep.join(str(base_dir).rstrip(os.sep).split(os.sep)[:-1]) + os.sep
550
- base_dir = os.path.dirname(path.replace(base_dir, ""))
550
+ base_dir = os.path.normpath(os.path.dirname(path.replace(base_dir, "")))
551
551
  base_dir = base_dir or "."
552
552
  text = read_file(path, encoding)
553
553
  if not text or not text.strip():
554
554
  return None
555
555
  for pattern, replacement in patch or []:
556
556
  text = re.sub(pattern, replacement, text)
557
- filename = os.path.join(base_dir, os.path.basename(path))
557
+ filename = os.path.normpath(os.path.join(base_dir, os.path.basename(path)))
558
558
  logger.debug(f"Parsing {filename}")
559
559
  data = parse_text(
560
560
  text,
@@ -594,7 +594,8 @@ def parse_all_files(
594
594
  comments=False,
595
595
  dates=False,
596
596
  as_object=False,
597
- variables_first=True,
597
+ patches=None,
598
+ variables_first=False,
598
599
  _variables_only=False,
599
600
  ):
600
601
  """
@@ -608,6 +609,7 @@ def parse_all_files(
608
609
  :param dates: (default false) Include dates?
609
610
  :param as_object: (default false) Return data as an object-like instead of dict
610
611
  :param variables_first: Try to parse variables first
612
+ :param patches: String replacement patterns per file or globally
611
613
  :return: Dictionary (key: file, value: parsed data if keep_data=True)
612
614
  """
613
615
  start_time = time.monotonic()
@@ -624,17 +626,19 @@ def parse_all_files(
624
626
  dates=dates,
625
627
  as_object=as_object,
626
628
  variables_first=False,
629
+ patches=patches,
627
630
  _variables_only=True,
628
631
  )
629
632
  )
630
- for current_path, _, all_files in os.walk(path):
633
+ for current_path, _, all_files in os.walk(os.path.normpath(path)):
631
634
  is_script_values = current_path.endswith("script_values")
632
635
  if (_variables_only and not is_script_values) or (not _variables_only and variables_first and is_script_values):
633
636
  continue
634
637
  for filename in all_files:
635
638
  if not filename.lower().endswith(".txt"):
636
639
  continue
637
- filepath = os.path.join(current_path, filename)
640
+ filepath = os.path.normpath(os.path.join(current_path, filename))
641
+ normpath = filepath.replace(os.path.normpath(path), "").lstrip(os.sep).replace(os.sep, "/")
638
642
  data = parse_file(
639
643
  filepath,
640
644
  output_dir=output_dir,
@@ -645,12 +649,12 @@ def parse_all_files(
645
649
  dates=dates,
646
650
  as_object=as_object,
647
651
  is_global=_variables_only,
652
+ patch=patches.get(normpath) if isinstance(patches, dict) else patches,
648
653
  )
649
654
  if isinstance(data, str):
650
- errors.append(filepath)
655
+ errors.append(normpath)
651
656
  continue
652
- filepath = filepath.replace(str(path), "").lstrip(os.sep)
653
- success[filepath] = data if keep_data else True
657
+ success[normpath] = data if keep_data else True
654
658
  total_time = time.monotonic() - start_time
655
659
  logger.info(f"{len(success)} parsed file(s) and {len(errors)} errors in {total_time:0.3f}s!")
656
660
  for error in errors:
@@ -679,11 +683,11 @@ def parse_all_locales(path, encoding="utf_8_sig", language="english", save=False
679
683
  key, _, value = match.groups()
680
684
  locales[key] = value
681
685
  else:
682
- for current_path, _, all_files in os.walk(path):
686
+ for current_path, _, all_files in os.walk(os.path.normpath(path)):
683
687
  for filename in all_files:
684
688
  if not filename.lower().endswith(".yml"):
685
689
  continue
686
- filepath = os.path.join(current_path, filename)
690
+ filepath = os.path.normpath(os.path.join(current_path, filename))
687
691
  with open(filepath, encoding=encoding) as file:
688
692
  while line := file.readline():
689
693
  if line.strip().lower() == f"l_{language}:":
@@ -930,14 +934,14 @@ def revert_file(path, output_dir=None, encoding="utf_8_sig", base_dir=None, save
930
934
  base_dir = base_dir or "."
931
935
  with open(path, "r", encoding="utf-8") as file:
932
936
  data = json.load(file)
933
- filename = os.path.join(str(base_dir), os.path.basename(path))
937
+ filename = os.path.normpath(os.path.join(str(base_dir), os.path.basename(path)))
934
938
  logger.debug(f"Reverting {filename}")
935
939
  text = revert(data)
936
940
  if save:
937
941
  filename, _ = os.path.splitext(os.path.basename(path))
938
- directory = os.path.join(output_dir or "output", *base_dir.split(os.sep))
942
+ directory = os.path.normpath(os.path.join(output_dir or "output", *base_dir.split(os.sep)))
939
943
  os.makedirs(directory, exist_ok=True)
940
- filename = os.path.join(directory, filename + ".txt")
944
+ filename = os.path.normpath(os.path.join(directory, filename + ".txt"))
941
945
  with open(filename, "w", encoding=encoding) as file:
942
946
  file.write(text)
943
947
  total_time = time.monotonic() - start_time
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "ckparser"
7
- version = "0.3.4"
7
+ version = "0.3.6"
8
8
  description = "Parse Paradox Jomini data files to Python/JSON and revert them back experimentally."
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.9"
File without changes
File without changes
File without changes
File without changes