Skip to content

Commit

Permalink
Pecha write function
Browse files Browse the repository at this point in the history
  • Loading branch information
tenzin3 committed Jul 4, 2024
1 parent c6b4fa0 commit 12a379f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
32 changes: 29 additions & 3 deletions src/openpecha/pecha/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
)
6 changes: 4 additions & 2 deletions src/openpecha/pecha/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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))

0 comments on commit 12a379f

Please sign in to comment.