From b37113f7f2dbcfbcbf04f5dc1f8d8e882b800ef1 Mon Sep 17 00:00:00 2001 From: "A.Ne." <138010940+comasqw@users.noreply.github.com> Date: Sun, 24 Nov 2024 14:40:20 +0400 Subject: [PATCH] add multiple parents supproting (#598) * add multiple parents supproting * change yaml path for parsing * Update yaml_parser.py * Update localization_helper.py * fix --- .../LocalizationHelper/__init__.py | 2 +- .../LocalizationHelper/entity.py | 92 +++++++++++++++++++ .../LocalizationHelper/localization_helper.py | 46 +++++----- .../LocalizationHelper/logger.py | 2 +- .../LocalizationHelper/parsers/base_parser.py | 4 +- .../parsers/fluent/ftl_parser.py | 6 +- .../parsers/fluent/ftl_writer.py | 4 +- .../parsers/yaml/yaml_parser.py | 46 ++++++++-- Tools/_CP14/LocalizationHelper/main.py | 2 +- 9 files changed, 166 insertions(+), 38 deletions(-) create mode 100644 Tools/_CP14/LocalizationHelper/LocalizationHelper/entity.py diff --git a/Tools/_CP14/LocalizationHelper/LocalizationHelper/__init__.py b/Tools/_CP14/LocalizationHelper/LocalizationHelper/__init__.py index 949488486e..5361927d2d 100644 --- a/Tools/_CP14/LocalizationHelper/LocalizationHelper/__init__.py +++ b/Tools/_CP14/LocalizationHelper/LocalizationHelper/__init__.py @@ -1,5 +1,5 @@ from .logger import get_logger, LogText from .exceptions import * from .parsers import YamlParser, FtlParser -from .prototype import Prototype, check_prototype_attrs +from .entity import Entity, check_prototype_attrs from .localization_helper import LocalizationHelper diff --git a/Tools/_CP14/LocalizationHelper/LocalizationHelper/entity.py b/Tools/_CP14/LocalizationHelper/LocalizationHelper/entity.py new file mode 100644 index 0000000000..776ccd3430 --- /dev/null +++ b/Tools/_CP14/LocalizationHelper/LocalizationHelper/entity.py @@ -0,0 +1,92 @@ +class Entity: + def __init__(self, prototype: dict): + self._name = prototype.get("name") + self._description = prototype.get("description") + self._parent = prototype.get("parent") + self._id = prototype.get("id") + self._suffix = prototype.get("suffix") + self._attrs_dict = { + "id": self._id, + "name": self._name, + "description": self._description, + "parent": self._parent, + "suffix": self._suffix + } + + @property + def name(self): + return self._name + + @name.setter + def name(self, new_name: str): + self._name = new_name + self._attrs_dict["name"] = new_name + + @property + def description(self): + return self._description + + @description.setter + def description(self, new_description: str): + self._description = new_description + self._attrs_dict["description"] = new_description + + @property + def parent(self): + return self._parent + + @parent.setter + def parent(self, new_parent: str): + self._parent = new_parent + self._attrs_dict["parent"] = new_parent + + @property + def id(self): + return self._id + + @id.setter + def id(self, new_id: str): + self._id = new_id + self._attrs_dict["id"] = new_id + + @property + def suffix(self): + return self._suffix + + @suffix.setter + def suffix(self, new_suffix: str): + self._suffix = new_suffix + self._attrs_dict["suffix"] = new_suffix + + @property + def attrs_dict(self): + return self._attrs_dict + + def set_attrs_dict_value(self, key, value): + self._attrs_dict[key] = value + if key == "name": + self._name = value + elif key == "description": + self._description = value + elif key == "id": + self._id = value + elif key == "parent": + self._parent = value + elif key == "suffix": + self._suffix = value + + def __repr__(self): + return str(self._attrs_dict) + + +def check_prototype_attrs(prototype: Entity, without_parent_check: bool = False) -> bool: + if prototype.name: + return True + elif prototype.description: + return True + elif prototype.suffix: + return True + elif not without_parent_check and prototype.parent: + return True + + return False diff --git a/Tools/_CP14/LocalizationHelper/LocalizationHelper/localization_helper.py b/Tools/_CP14/LocalizationHelper/LocalizationHelper/localization_helper.py index b1779f29cc..1b71cb211d 100644 --- a/Tools/_CP14/LocalizationHelper/LocalizationHelper/localization_helper.py +++ b/Tools/_CP14/LocalizationHelper/LocalizationHelper/localization_helper.py @@ -1,7 +1,7 @@ import json import os -from . import Prototype, check_prototype_attrs, get_logger, LogText, ErrorWhileWritingToFile, ErrorWhileReadingFromFile +from . import Entity, check_prototype_attrs, get_logger, LogText, ErrorWhileWritingToFile, ErrorWhileReadingFromFile from .parsers import FtlParser, YamlParser, create_ftl logger = get_logger(__name__) @@ -36,7 +36,7 @@ class LocalizationHelper: raise ErrorWhileReadingFromFile(e) return {} - def _save_yaml_parser_last_launch_result(self, last_launch_result: dict[str, Prototype]): + def _save_yaml_parser_last_launch_result(self, last_launch_result: dict[str, Entity]): logger.debug("%s %s", LogText.SAVING_LAST_LAUNCH_RESULT, YAML_PARSER_LAST_LAUNCH_RESULT_PATH) prototypes_dict = {} @@ -45,19 +45,19 @@ class LocalizationHelper: self._save_to_json(YAML_PARSER_LAST_LAUNCH_RESULT_PATH, prototypes_dict) - def _read_prototypes_from_last_launch_result(self) -> dict[str, Prototype] | None: + def _read_prototypes_from_last_launch_result(self) -> dict[str, Entity] | None: if os.path.isfile(YAML_PARSER_LAST_LAUNCH_RESULT_PATH): last_launch_result = self._read_from_json(YAML_PARSER_LAST_LAUNCH_RESULT_PATH) last_launch_result_dict = {} for prototype_id, prototype_attrs in last_launch_result.items(): - last_launch_result_dict[prototype_id] = Prototype(prototype_attrs) + last_launch_result_dict[prototype_id] = Entity(prototype_attrs) return last_launch_result_dict return None @staticmethod - def _update_prototype_if_attrs_has_been_changed(yaml_prototype_obj: Prototype, last_launch_prototype_obj: Prototype, - final_prototype_obj: Prototype): + def _update_prototype_if_attrs_has_been_changed(yaml_prototype_obj: Entity, last_launch_prototype_obj: Entity, + final_prototype_obj: Entity): if yaml_prototype_obj.attrs_dict != last_launch_prototype_obj.attrs_dict: log_text = f"Has been updated from: {final_prototype_obj.attrs_dict}, to: " @@ -70,8 +70,8 @@ class LocalizationHelper: return final_prototype_obj - def _merge_yaml_parser_prototypes_and_ftl_parser_prototypes(self, yaml_parser_prototypes: dict[str, Prototype], - ftl_parser_prototypes: dict[str, Prototype]) -> dict[str, Prototype]: + def _merge_yaml_parser_prototypes_and_ftl_parser_prototypes(self, yaml_parser_prototypes: dict[str, Entity], + ftl_parser_prototypes: dict[str, Entity]) -> dict[str, Entity]: general_prototypes_dict = {} @@ -90,7 +90,7 @@ class LocalizationHelper: return general_prototypes_dict @staticmethod - def _set_parent_attrs(prototype_parent_id: str, prototype_obj: Prototype, parent_prototype_obj: Prototype): + def _set_parent_attrs(prototype_parent_id: str, prototype_obj: Entity, parent_prototype_obj: Entity): for attr_name, attr_value in prototype_obj.attrs_dict.items(): if attr_value or attr_name in ("parent", "id"): continue @@ -106,20 +106,24 @@ class LocalizationHelper: return prototype_obj - def _parent_checks(self, general_prototypes_dict: dict[str, Prototype]): + def _parent_checks(self, general_prototypes_dict: dict[str, Entity]): to_delete = [] for prototype_id, prototype_obj in general_prototypes_dict.items(): prototype_parent_id = prototype_obj.parent - if isinstance(prototype_parent_id, list): - continue + if not isinstance(prototype_parent_id, list): - parent_prototype_obj = general_prototypes_dict.get(prototype_parent_id) + parent_prototype_obj = general_prototypes_dict.get(prototype_parent_id) - if parent_prototype_obj and check_prototype_attrs(parent_prototype_obj, False): - self._set_parent_attrs(prototype_parent_id, prototype_obj, parent_prototype_obj) + if parent_prototype_obj and check_prototype_attrs(parent_prototype_obj, True): + self._set_parent_attrs(prototype_parent_id, prototype_obj, parent_prototype_obj) + else: + if not check_prototype_attrs(prototype_obj, True): + to_delete.append(prototype_id) else: - if not check_prototype_attrs(prototype_obj, False): - to_delete.append(prototype_id) + if not prototype_obj.name: + prototype_obj.name = f"CONFLICT{{ ent-{prototype_parent_id} }}" + if not prototype_obj.description: + prototype_obj.description = f"CONFLICT{{ ent-{prototype_parent_id}.desc }}" for prototype_id in to_delete: logger.debug("%s %s: %s", prototype_id, LogText.HAS_BEEN_DELETED, general_prototypes_dict[prototype_id]) @@ -127,8 +131,8 @@ class LocalizationHelper: return general_prototypes_dict - def _create_general_prototypes_dict(self, yaml_parser_prototypes: dict[str, Prototype], - ftl_parser_prototypes: dict[str, Prototype]) -> dict[str, Prototype]: + def _create_general_prototypes_dict(self, yaml_parser_prototypes: dict[str, Entity], + ftl_parser_prototypes: dict[str, Entity]) -> dict[str, Entity]: general_prototypes_dict = self._merge_yaml_parser_prototypes_and_ftl_parser_prototypes(yaml_parser_prototypes, ftl_parser_prototypes) @@ -138,13 +142,13 @@ class LocalizationHelper: return general_prototypes_dict @staticmethod - def _create_result_ftl(general_prototypes_dict: dict[str, Prototype]) -> str: + def _create_result_ftl(general_prototypes_dict: dict[str, Entity]) -> str: result = "" for prototype_obj in general_prototypes_dict.values(): result += create_ftl(prototype_obj) return result - def _save_result(self, general_prototypes_dict: dict[str, Prototype]): + def _save_result(self, general_prototypes_dict: dict[str, Entity]): logger.debug("%s: %s", LogText.SAVING_FINAL_RESULT, SAVE_RESULT_TO) result = self._create_result_ftl(general_prototypes_dict) try: diff --git a/Tools/_CP14/LocalizationHelper/LocalizationHelper/logger.py b/Tools/_CP14/LocalizationHelper/LocalizationHelper/logger.py index 5451f431a6..42835813b1 100644 --- a/Tools/_CP14/LocalizationHelper/LocalizationHelper/logger.py +++ b/Tools/_CP14/LocalizationHelper/LocalizationHelper/logger.py @@ -40,4 +40,4 @@ class LogText: UNKNOWN_ERROR = "An error occurred during execution" HAS_BEEN_DELETED = "Has been deleted due to lack of relevant data" HAS_BEEN_PROCESSED = "Has been processed" - FORMING_FTL_FOR_PROTOTYPE = "Forming FTL for Prototype" + FORMING_FTL_FOR_PROTOTYPE = "Forming FTL for Entity" diff --git a/Tools/_CP14/LocalizationHelper/LocalizationHelper/parsers/base_parser.py b/Tools/_CP14/LocalizationHelper/LocalizationHelper/parsers/base_parser.py index d6ea34ec5a..14e40fcdd2 100644 --- a/Tools/_CP14/LocalizationHelper/LocalizationHelper/parsers/base_parser.py +++ b/Tools/_CP14/LocalizationHelper/LocalizationHelper/parsers/base_parser.py @@ -2,7 +2,7 @@ import os from abc import ABC, abstractmethod -from LocalizationHelper.prototype import Prototype +from LocalizationHelper.entity import Entity class BaseParser(ABC): @@ -24,5 +24,5 @@ class BaseParser(ABC): return False @abstractmethod - def get_prototypes(self, prototypes_files_path: str) -> list[Prototype]: + def get_prototypes(self, prototypes_files_path: str) -> list[Entity]: pass diff --git a/Tools/_CP14/LocalizationHelper/LocalizationHelper/parsers/fluent/ftl_parser.py b/Tools/_CP14/LocalizationHelper/LocalizationHelper/parsers/fluent/ftl_parser.py index 3deab4f0b6..7943bef2a9 100644 --- a/Tools/_CP14/LocalizationHelper/LocalizationHelper/parsers/fluent/ftl_parser.py +++ b/Tools/_CP14/LocalizationHelper/LocalizationHelper/parsers/fluent/ftl_parser.py @@ -1,6 +1,6 @@ from . import read_ftl from LocalizationHelper import get_logger, LogText -from LocalizationHelper.prototype import Prototype +from LocalizationHelper.entity import Entity from LocalizationHelper.parsers import BaseParser logger = get_logger(__name__) @@ -10,7 +10,7 @@ class FtlParser(BaseParser): def __init__(self): logger.debug("%s FtlParser", LogText.CLASS_INITIALIZATION) - def get_prototypes(self, ftl_prototypes_path: str) -> dict[str, Prototype]: + def get_prototypes(self, ftl_prototypes_path: str) -> dict[str, Entity]: prototypes = {} ftl_prototypes_files_path = self._get_files_paths_in_dir(ftl_prototypes_path) @@ -21,7 +21,7 @@ class FtlParser(BaseParser): file_prototypes_dict = read_ftl(prototype_file_path) for prototype_dict in file_prototypes_dict.values(): - prototype_obj = Prototype(prototype_dict) + prototype_obj = Entity(prototype_dict) logger.debug("%s: %s", LogText.HAS_BEEN_PROCESSED, prototype_obj) prototypes[prototype_obj.id] = prototype_obj diff --git a/Tools/_CP14/LocalizationHelper/LocalizationHelper/parsers/fluent/ftl_writer.py b/Tools/_CP14/LocalizationHelper/LocalizationHelper/parsers/fluent/ftl_writer.py index 16bac25945..b0944cfeb3 100644 --- a/Tools/_CP14/LocalizationHelper/LocalizationHelper/parsers/fluent/ftl_writer.py +++ b/Tools/_CP14/LocalizationHelper/LocalizationHelper/parsers/fluent/ftl_writer.py @@ -1,12 +1,12 @@ from LocalizationHelper import get_logger, LogText -from LocalizationHelper.prototype import Prototype +from LocalizationHelper.entity import Entity logger = get_logger(__name__) INDENT = " " -def create_ftl(prototype: Prototype) -> str: +def create_ftl(prototype: Entity) -> str: logger.debug("%s: %s", LogText.FORMING_FTL_FOR_PROTOTYPE, prototype.attrs_dict) ftl = "" diff --git a/Tools/_CP14/LocalizationHelper/LocalizationHelper/parsers/yaml/yaml_parser.py b/Tools/_CP14/LocalizationHelper/LocalizationHelper/parsers/yaml/yaml_parser.py index cbe9a17bd0..7209292415 100644 --- a/Tools/_CP14/LocalizationHelper/LocalizationHelper/parsers/yaml/yaml_parser.py +++ b/Tools/_CP14/LocalizationHelper/LocalizationHelper/parsers/yaml/yaml_parser.py @@ -1,6 +1,6 @@ from . import read_yaml from LocalizationHelper import get_logger, LogText -from LocalizationHelper.prototype import Prototype, check_prototype_attrs +from LocalizationHelper.entity import Entity, check_prototype_attrs from LocalizationHelper.parsers import BaseParser logger = get_logger(__name__) @@ -10,8 +10,32 @@ class YamlParser(BaseParser): def __init__(self): logger.debug("%s YamlParser", LogText.CLASS_INITIALIZATION) - def get_prototypes(self, yaml_prototypes_path: str) -> dict[str, Prototype]: - prototypes = {} + @staticmethod + def _check_prototypes_with_multiple_parents(prototypes: dict[str, Entity], + prototypes_with_multiple_parents: dict[str, Entity]) -> dict[str, Entity]: + + for prototype_obj in prototypes_with_multiple_parents.values(): + available_parents = [] + for parent in prototype_obj.parent: + if parent in prototypes: + available_parents.append(parent) + + if len(available_parents) >= 2: + prototype_obj.parent = available_parents + elif len(available_parents) == 0: + prototype_obj.parent = None + else: + prototype_obj.parent = available_parents[0] + + logger.debug("%s: %s", LogText.HAS_BEEN_PROCESSED, prototype_obj) + prototypes[prototype_obj.id] = prototype_obj + + return prototypes + + def get_prototypes(self, yaml_prototypes_path: str) -> dict[str, Entity]: + prototypes: dict[str, Entity] = {} + prototypes_with_multiple_parents: dict[str, Entity] = {} + yaml_prototypes_files_path = self._get_files_paths_in_dir(yaml_prototypes_path) for prototype_file_path in yaml_prototypes_files_path: @@ -21,9 +45,17 @@ class YamlParser(BaseParser): file_prototypes_list: list[dict] = read_yaml(prototype_file_path) if file_prototypes_list: for prototype_dict in file_prototypes_list: - prototype_obj = Prototype(prototype_dict) - if check_prototype_attrs(prototype_obj): - logger.debug("%s: %s", LogText.HAS_BEEN_PROCESSED, prototype_obj) - prototypes[prototype_obj.id] = prototype_obj + prototype_type = prototype_dict.get("type") + if prototype_type == "entity": + prototype_obj = Entity(prototype_dict) + if check_prototype_attrs(prototype_obj): + if isinstance(prototype_obj.parent, list): + prototypes_with_multiple_parents[prototype_obj.id] = prototype_obj + continue + + logger.debug("%s: %s", LogText.HAS_BEEN_PROCESSED, prototype_obj) + prototypes[prototype_obj.id] = prototype_obj + + prototypes = self._check_prototypes_with_multiple_parents(prototypes, prototypes_with_multiple_parents) return prototypes diff --git a/Tools/_CP14/LocalizationHelper/main.py b/Tools/_CP14/LocalizationHelper/main.py index 3f0ea70bb1..2d3306b384 100644 --- a/Tools/_CP14/LocalizationHelper/main.py +++ b/Tools/_CP14/LocalizationHelper/main.py @@ -1,6 +1,6 @@ from LocalizationHelper import get_logger, LocalizationHelper -YAML_FILES_PATH = "../../../Resources/Prototypes/_CP14/Entities" +YAML_FILES_PATH = "../../../Resources/Prototypes/_CP14/" FTL_FILES_PATH = "../../../Resources/Locale/ru-RU/_CP14/_PROTO/entities" if __name__ == '__main__':