diff --git a/onec_codetemplate_parser/__init__.py b/onec_codetemplate_parser/__init__.py index e69de29..205a0d0 100644 --- a/onec_codetemplate_parser/__init__.py +++ b/onec_codetemplate_parser/__init__.py @@ -0,0 +1,3 @@ +from .api import parse_to_src, render_from_src + +__all__ = ["parse_to_src", "render_from_src"] diff --git a/onec_codetemplate_parser/__main__.py b/onec_codetemplate_parser/__main__.py new file mode 100644 index 0000000..70ccfe2 --- /dev/null +++ b/onec_codetemplate_parser/__main__.py @@ -0,0 +1,4 @@ +from .cli import app + +if __name__ == "__main__": + app() diff --git a/onec_codetemplate_parser/api.py b/onec_codetemplate_parser/api.py new file mode 100644 index 0000000..44d2bd2 --- /dev/null +++ b/onec_codetemplate_parser/api.py @@ -0,0 +1,15 @@ +from .core import parse_skobkofile as parser +from .core import Root +from pathlib import Path + + +def parse_to_src(path: str, src: str): + """Парсит шаблон 1С-файла и сохраняет структуру файлов в папку""" + root = parser(path) + root.to_files(src) + +def render_from_src(src: str, path: str): + """Генерирует код шаблона из исходников""" + root = Root.from_files(src) + text = root.compile() + Path(path).write_text(text, encoding='utf-8-sig') \ No newline at end of file diff --git a/onec_codetemplate_parser/cli.py b/onec_codetemplate_parser/cli.py new file mode 100644 index 0000000..e6cb294 --- /dev/null +++ b/onec_codetemplate_parser/cli.py @@ -0,0 +1,18 @@ +import typer +from .api import parse_to_src, render_from_src + +app = typer.Typer(help="Парсер шаблонов 1С") + +@app.command() +def parse(path: str, src: str): + """Разобрать шаблон из 1с-файла *.st в исходники src""" + result = parse_to_src(path, src) + typer.echo(result) + +@app.command() +def render(src: str, path: str): + """Собрать шаблон из исходников src в 1с-файл *.st""" + render_from_src(src, path) + +if __name__ == "__main__": + app() diff --git a/onec_codetemplate_parser/main.py b/onec_codetemplate_parser/core.py similarity index 96% rename from onec_codetemplate_parser/main.py rename to onec_codetemplate_parser/core.py index 9610d0c..20fe620 100644 --- a/onec_codetemplate_parser/main.py +++ b/onec_codetemplate_parser/core.py @@ -176,7 +176,7 @@ class Root(Node): def from_files(cls, path): assert not os.path.isfile(path), f"Путь '{path}' является файлом, а не директорией" - assert os.path.exists(path), f"Дирректория '{path}' не существует" + assert os.path.exists(path), f"Директория '{path}' не существует" #прочитать все файлы и собрать обратно в дерево entries = sorted(os.listdir(path)) diff --git a/tests/conftest.py b/tests/conftest.py index c7ad990..8041954 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,4 @@ import pytest -from onec_codetemplate_parser import main from pathlib import Path TEST_FILE = 'Documents/1C/1c-text-tempates/Надулич.st' diff --git a/tests/test_01.py b/tests/test_01.py index e4fde75..a2d60da 100644 --- a/tests/test_01.py +++ b/tests/test_01.py @@ -1,4 +1,4 @@ -from onec_codetemplate_parser import main +from onec_codetemplate_parser import core from tests.common import check_files_sequential class Test_Read_Skobkofile: @@ -7,12 +7,12 @@ class Test_Read_Skobkofile: assert test_file_path.exists() def test_01_parse_eq_compile(self, test_data): - root = main.parse_skobkofile(test_data) + root = core.parse_skobkofile(test_data) new_data = root.compile() assert new_data == test_data def test_02_save_and_read(self, test_data, tmp_path): - root = main.parse_skobkofile(test_data) + root = core.parse_skobkofile(test_data) new_data = root.compile() tmp_file = tmp_path / 'tmp.st' @@ -24,7 +24,7 @@ class Test_Read_Skobkofile: class Test_Write_To_Files: def test_00_to_file(self, test_data, temp_src): - root = main.parse_skobkofile(test_data) + root = core.parse_skobkofile(test_data) root.to_files(temp_src) def test_01_to_files(self, temp_src): @@ -39,7 +39,7 @@ class Test_Write_To_Files: def test_02_sequential_name(self, temp_src): d = temp_src / "001.0_Надулич" / "002.0_Комментарии" - subfiles = [p.name for p in d.iterdir() if p.is_dir()] + subfiles = [p.name for p in d.iterdir()] check_files_sequential(subfiles)