6
2025-08-05 10:00:54 +03:00

92 lines
2.6 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import typing
from fluent.syntax import ast
from yamlmodels import YAMLElements
import os
class File:
def __init__(self, full_path):
self.full_path = full_path
def read_data(self):
file = open(self.full_path, 'r', encoding='utf8')
# replace необходим для того, чтобы 1-е сообщение не считалось ast.Junk
file_data = file.read().replace('', '')
file.close()
return file_data
def save_data(self, file_data: typing.AnyStr):
os.makedirs(os.path.dirname(self.full_path), exist_ok=True)
file = open(self.full_path, 'w', encoding='utf8')
file.write(file_data)
file.close()
def get_relative_path(self, base_path):
return os.path.relpath(self.full_path, base_path)
def get_relative_path_without_extension(self, base_path):
return self.get_relative_path(base_path).split('.', maxsplit=1)[0]
def get_relative_parent_dir(self, base_path):
return os.path.relpath(self.get_parent_dir(), base_path)
def get_parent_dir(self):
return os.path.dirname(self.full_path)
def get_name(self):
return os.path.basename(self.full_path).split('.')[0]
class FluentFile(File):
def __init__(self, full_path):
super().__init__(full_path)
self.full_path = full_path
def parse_data(self, file_data: typing.AnyStr):
from fluent.syntax import FluentParser
return FluentParser().parse(file_data)
def serialize_data(self, parsed_file_data: ast.Resource):
from fluent.syntax import FluentSerializer
return FluentSerializer(with_junk=True).serialize(parsed_file_data)
def read_serialized_data(self):
return self.serialize_data(self.parse_data(self.read_data()))
def read_parsed_data(self):
return self.parse_data(self.read_data())
class YAMLFluentFileAdapter(File):
def __init__(self, full_path):
super().__init__(full_path)
self.full_path = full_path
# def create_fluent_from_yaml_elements(self, yaml_elements):
class YAMLFile(File):
def __init__(self, full_path):
super().__init__(full_path)
def parse_data(self, file_data: typing.AnyStr):
import yaml
return yaml.load(file_data, Loader=yaml.BaseLoader)
def get_elements(self, parsed_data):
if isinstance(parsed_data, list):
elements = YAMLElements(parsed_data).elements
# элемент может быть None, если имеет неизвестный тип
exist_elements = list(filter(lambda el: el, elements))
return exist_elements
return []