local helper update

This commit is contained in:
comasqw
2024-08-24 21:01:58 +04:00
parent ea9c6ee7bf
commit 57e9f73e15
6 changed files with 1492 additions and 35 deletions

View File

@@ -10,7 +10,7 @@ class BaseParser:
def __init__(self, paths: tuple):
self.path, self.errors_path = paths
def get_files_paths(self) -> list:
def _get_files_paths(self) -> list:
"""
The method gets the path to the yml folder of localization prototypes/files, e.g. "ftl",
then with the help of os library goes through each file in
@@ -31,7 +31,7 @@ class BaseParser:
json.dump(prototypes, json_file, indent=4)
@staticmethod
def check_file_extension(path: str, extension: str) -> bool:
def _check_file_extension(path: str, extension: str) -> bool:
if path.endswith(extension):
return True
return False

File diff suppressed because it is too large Load Diff

View File

@@ -4,9 +4,7 @@ def read_ftl(paths: tuple) -> dict:
file and determines by the indentation in the line whether
it is a new prototype or an attribute of an old one.
"""
prototypes = {
}
prototypes = {}
last_prototype = ""
path, error_log_path = paths
@@ -36,4 +34,4 @@ def read_ftl(paths: tuple) -> dict:
with open(error_log_path, "a") as file:
file.write(f"FTL-ERROR:\nAn error occurred while reading a file {path}, error - {e}\n")
return prototypes
return prototypes

View File

@@ -15,9 +15,9 @@ class FTLParser(BaseParser):
"""
prototypes = {}
for path in self.get_files_paths():
for path in self._get_files_paths():
if not self.check_file_extension(path, ".ftl"):
if not self._check_file_extension(path, ".ftl"):
continue
file = ftl_reader.read_ftl((path, self.errors_path))

View File

@@ -64,6 +64,7 @@ def save_result(entities: str, file_name: str) -> None:
print(f"{file_name} has been created\n")
def main():
"""
The function gets paths, creates dictionaries with the help of parsers,
@@ -120,7 +121,7 @@ def main():
if prototypes_dict[prototype].get("suffix"):
prototype_attrs["suffix"] = prototypes_dict[prototype]["suffix"]
if any(prototype_attrs[attr] is not None for attr in ["name", "desc", "suffix"]):
if any(prototype_attrs[attr] is not None for attr in ("name", "desc", "suffix")):
proto_ftl = ftl_writer.create_ftl(prototype, all_prototypes[prototype])
entities_ftl += proto_ftl
@@ -136,4 +137,4 @@ def main():
if __name__ == '__main__':
main()
main()

View File

@@ -1,5 +1,6 @@
import yaml
from base_parser import BaseParser
import re
class YMLParser(BaseParser):
@@ -8,7 +9,7 @@ class YMLParser(BaseParser):
"""
@staticmethod
def check_proto_attrs(prototype: dict) -> bool:
def _check_proto_attrs(prototype: dict) -> bool:
"""
The function checks that the prototype at least has some attribute from the "attrs_lst".
"""
@@ -21,7 +22,7 @@ class YMLParser(BaseParser):
return any(prototype.get(attr) is not None for attr in attrs_lst)
@staticmethod
def get_proto_attrs(prototypes: dict, prototype: dict) -> None:
def _get_proto_attrs(prototypes: dict, prototype: dict) -> None:
prototypes[prototype.get("id")] = {
"parent": prototype.get("parent"),
"name": prototype.get("name"),
@@ -30,16 +31,72 @@ class YMLParser(BaseParser):
}
@staticmethod
def create_proto(file) -> str:
proto = ""
for line in file.readlines():
# The PyYaml library cannot handle the following SpaceStation 14 prototype syntax - !type: ...
# We need to fix this :(
if "!type" in line:
continue
proto += line
def _fix_type_error_with_ignore(proto: str) -> str:
"""
Removes lines containing '!type' from the prototype string.
return proto
Args:
proto (str): The prototype string to be processed.
Returns:
str: The prototype string with lines containing '!type' removed.
"""
proto_lines = proto.splitlines()
fixed_proto = "\n".join(line for line in proto_lines if "!type" not in line)
return fixed_proto
def _load_proto(self, file, path) -> list[dict]:
"""
Loads and processes YAML prototypes from the file.
Args:
file: The file object containing YAML data.
path (str): The path of the file used for error reporting.
Returns:
list[dict]: A list of dictionaries representing the prototypes.
Note:
Error handling is designed so that if one prototype fails to process, other prototypes
in the same file will still be processed. Here's how it works:
1. **Prototype Splitting:** The file is read and split into prototypes using a regular expression.
Each prototype is processed separately.
2. **Error Handling for Each Prototype:**
- **First Attempt:** If `yaml.safe_load` cannot process the prototype, it tries replacing `!type:`
strings and loads YAML again.
- **Second Attempt:** If that fails, it applies `_fix_type_error_with_ignore` to remove lines
with `!type` and attempts to load YAML again.
- **Exception Handling:** If errors occur in both attempts, they are logged to an error file,
and the current prototype is skipped.
3. **Adding Successful Data:** If the prototype is successfully processed, it is added to the
`prototypes` list.
"""
content_str = file.read()
prototypes_lst = re.split(r"\n(?=- type:)", content_str)
prototypes = []
for proto in prototypes_lst:
try:
fixed_proto = proto.replace("!type:", "type: bobo")
data = yaml.safe_load(fixed_proto)
if data is None:
continue
data = data[0]
except Exception:
try:
fixed_proto = self._fix_type_error_with_ignore(proto)
data = yaml.safe_load(fixed_proto)[0]
except Exception as e:
with open(self.errors_path, "a") as error_file:
error_file.write(
f"YML-ERROR:\nAn error occurred during prototype processing {path}, error - {e}\n")
continue
prototypes.append(data)
return prototypes
def yml_parser(self) -> dict:
"""
@@ -49,21 +106,16 @@ class YMLParser(BaseParser):
"""
prototypes = {}
for path in self.get_files_paths():
if not self.check_file_extension(path, ".yml"):
for path in self._get_files_paths():
if not self._check_file_extension(path, ".yml"):
continue
try:
with open(path, encoding="utf-8") as file:
proto = self.create_proto(file)
data = yaml.safe_load(proto)
except Exception as e:
with open(self.errors_path, "a") as file:
file.write(f"YML-ERROR:\nAn error occurred during prototype processing {path}, error - {e}\n")
else:
if data is not None:
for prototype in data:
if self.check_proto_attrs(prototype):
self.get_proto_attrs(prototypes, prototype)
with open(path, encoding="utf-8") as file:
content = self._load_proto(file, path)
if content is not None:
for prototype in content:
if self._check_proto_attrs(prototype):
self._get_proto_attrs(prototypes, prototype)
return prototypes