Files
crystall-punk-14/Tools/_CP14/LocalizationHelper/yml_parser/yml_parser.py
comasqw b592842a66 fix localization helper & protos descriptions in ftl (#282)
* fix localization helper & protos descriptions in ftl

* Update main.py

* Update objects.ftl

* добавил ярлык напрямую к файлу локализации

* megasuperduperpuperTWEAK

* Update yml_parser.py

* Update yml_parser.py

* Update yml_parser.py

* Update yml_parser.py

* Update main.py

* tweak

---------

Co-authored-by: Ed <96445749+TheShuEd@users.noreply.github.com>
Co-authored-by: Ed <edwardxperia2000@gmail.com>
2024-06-27 22:07:25 +03:00

70 lines
2.4 KiB
Python

import yaml
from base_parser import BaseParser
class YMLParser(BaseParser):
"""
The class inherits from the "BaseParser" class, parses yml prototypes.
"""
@staticmethod
def check_proto_attrs(prototype: dict) -> bool:
"""
The function checks that the prototype at least has some attribute from the "attrs_lst".
"""
attrs_lst = ["name", "description", "suffix"]
# In some cases a parent can be a list (because of multiple parents),
# the game will not be able to handle such cases in ftl files.
if not isinstance(prototype.get("parent"), list):
attrs_lst.append("parent")
return any(prototype.get(attr) is not None for attr in attrs_lst)
@staticmethod
def get_proto_attrs(prototypes: dict, prototype: dict) -> None:
prototypes[prototype.get("id")] = {
"parent": prototype.get("parent"),
"name": prototype.get("name"),
"desc": prototype.get("description"),
"suffix": prototype.get("suffix")
}
@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
return proto
def yml_parser(self) -> dict:
"""
The function gets the path, then with the help of the os library
goes through each file,checks that the file extension is "yml",
then processes the file using the "PyYaml" library
"""
prototypes = {}
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)
return prototypes