From 12a379fa977894cca418db5ff42bf37daea7c71d Mon Sep 17 00:00:00 2001 From: Tenzin Date: Thu, 4 Jul 2024 09:52:34 +0530 Subject: [PATCH] Pecha write function --- src/openpecha/pecha/__init__.py | 32 +++++++++++++++++++++++++++++--- src/openpecha/pecha/layer.py | 6 ++++-- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/openpecha/pecha/__init__.py b/src/openpecha/pecha/__init__.py index 2e761ef..781d99e 100644 --- a/src/openpecha/pecha/__init__.py +++ b/src/openpecha/pecha/__init__.py @@ -20,9 +20,9 @@ class Pecha: def __init__( self, pecha_id: str, - bases: Dict[str, str] = None, - layers: Dict[str, Dict[LayerEnum, Layer]] = None, - metadata: Dict[str, str] = None, + bases: Dict[str, str], + layers: Dict[str, Dict[LayerEnum, Layer]], + metadata: Dict[str, str], ) -> None: self.pecha_id = pecha_id self.bases = bases @@ -36,3 +36,29 @@ def from_path(cls, path: str): @classmethod def from_id(cls, pecha_id: str): pass + + def write(self, export_path: Path = PECHAS_PATH): + + pecha_dir = _mkdir(export_path / self.pecha_id) + self.base_path = _mkdir(pecha_dir / f"{self.pecha_id}.opf") + """ write metadata """ + self.metadata_fn = self.base_path / "metadata.json" + self.metadata_fn.write_text( + json.dumps(self.metadata, indent=4, ensure_ascii=False), encoding="utf-8" + ) + + """ write base file""" + base_dir = _mkdir(self.base_path / "base") + for base_fname, base_text in self.bases.items(): + base_fn = base_dir / f"{base_fname}.txt" + base_fn.write_text(base_text, encoding="utf-8") + + layer_dir = _mkdir(self.base_path / "layers") + """ write annotation layers""" + for layer_fname, layer_data in self.layers.items(): + for _, layer in layer_data.items(): + _mkdir(layer_dir / layer_fname) + layer.write( + base_file_path=base_dir / layer_fname, + export_path=layer_dir / layer_fname, + ) diff --git a/src/openpecha/pecha/layer.py b/src/openpecha/pecha/layer.py index 038336b..671896c 100644 --- a/src/openpecha/pecha/layer.py +++ b/src/openpecha/pecha/layer.py @@ -34,7 +34,7 @@ def covert_to_relative_path(self, json_string: str, export_path: Path): resource["@include"] = str(original_path.relative_to(export_path)) return json_object - def write_layer(self, base_file_path: Path, export_path: Path): + def write(self, base_file_path: Path, export_path: Path): """write annotations in stam data model""" self.annotation_store = AnnotationStore(id=PECHA_ANNOTATION_STORE_ID) self.resource = self.annotation_store.add_resource( @@ -67,8 +67,10 @@ def write_layer(self, base_file_path: Path, export_path: Path): """ save annotations in json""" json_string = self.annotation_store.to_json_string() json_object = self.covert_to_relative_path(json_string, export_path) + """ add four uuid digits to the layer file name for uniqueness""" + layer_fname = f"{self.annotation_label.value}-{get_uuid()[:4]}.json" with open( - export_path / f"{self.annotation_label.value}.json", + export_path / layer_fname, "w", ) as f: f.write(json.dumps(json_object, indent=4, ensure_ascii=False))