diff --git a/api/__init__.py b/api/__init__.py index 6681eaf..748504c 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -2,7 +2,7 @@ from .project import list_project, have_project, create_project, delete_project from .project import is_project_open, get_project_open_count, open_project, close_project from .project import copy_project -from .parser import read_inp, dump_inp, export_inp +from .parser import read_inp, dump_inp, import_inp, export_inp from .database import API_ADD, API_UPDATE, API_DELETE from .database import ChangeSet diff --git a/api/parser.py b/api/parser.py index 096e6dc..e1d4324 100644 --- a/api/parser.py +++ b/api/parser.py @@ -73,9 +73,40 @@ def _parse_inp(inp: str) -> dict[str, list[str]]: return file -def _read_inp(inp: str) -> ChangeSet: - file = _parse_inp(inp) +def _parse_cs(cs: ChangeSet) -> dict[str, list[str]]: + file: dict[str, list[str]] = {} + for s in section_name: + file[s] = [] + section = '' + + for line in str(cs.operations[0]['inp']).split('\n'): + line = line.strip() + if line == '': + # skip empty line for control and rule + if section == 'CONTROLS' or section == 'RULES': + pass + else: + section = '' + continue + + if line.startswith('['): + is_section = False + for s in section_name: + if line.startswith(f'[{s}'): + section = s + is_section = True + break + if is_section: + continue + + if section != '': + file[section].append(line) + + return file + + +def _read_inp(file: dict[str, list[str]]) -> ChangeSet: file_cs: dict[str, ChangeSet] = {} for s in section_name: file_cs[s] = ChangeSet() @@ -227,10 +258,38 @@ def read_inp(project: str, inp: str): create_project(project) open_project(project) - execute_batch_commands(project, _read_inp(inp)) + file = _parse_inp(inp) + cs = _read_inp(file) + + execute_batch_commands(project, cs) op = get_current_operation(project) set_restore_operation(project, op) + close_project(project) + + +def import_inp(project: str, cs: ChangeSet) -> ChangeSet: + if is_project_open(project): + close_project(project) + + if have_project(project): + delete_project(project) + + create_project(project) + open_project(project) + + file = _parse_cs(cs) + new_cs = _read_inp(file) + + success_cs = execute_batch_commands(project, new_cs) + op = get_current_operation(project) + set_restore_operation(project, op) + + close_project(project) + + # return ? + return success_cs + def dump_inp(project: str, inp: str): if not have_project(project): diff --git a/tjnetwork.py b/tjnetwork.py index b746f82..4f7e4ba 100644 --- a/tjnetwork.py +++ b/tjnetwork.py @@ -142,6 +142,9 @@ def read_inp(name: str, inp: str) -> None: def dump_inp(name: str, inp: str) -> None: return api.dump_inp(name, inp) +def import_inp(name: str, cs: ChangeSet) -> ChangeSet: + return api.import_inp(name, cs) + def export_inp(name: str) -> ChangeSet: return api.export_inp(name)