Skip to content

Pecha

Create new pecha

from openpecha.core.annotations import Citation, Span
from openpecha.core.layer import Layer, LayerEnum
from openpecha.core.metadata import InitialCreationType, InitialPechaMetadata
from openpecha.core.pecha import OpenPechaFS

# create new pecha
metadata = InitialPechaMetadata(initial_creation_type=InitialCreationType.input)
pecha = OpenPechaFS(metadata=metadata)

# create a simple layer
ann = Citation(span=Span(start=10, end=20))
layer = Layer(annotation_type=LayerEnum.citation)
layer.set_annotation(ann)

base_name = pecha.set_base("base content")
pecha.set_layer(base_name, layer)

# pecha.save()

When we are creating a brand new pecha, first we must create an instance of PechaMetadata, which will automatically issue a unique id for the pecha and allow us to specify other metadata about the pecha.

Then, we will use this metadata object to create pecha container, instance of OpenPechaFS.

After adding base and layer(s) we need to call pecha.save() to save pecha to file system in OpenPecha Format.

Adding Base and Layer

Refer layer docs to how to create layer.

from openpecha.core.annotations import Citation, Span
from openpecha.core.layer import Layer, LayerEnum
from openpecha.core.pecha import OpenPechaFS

pecha = OpenPechaFS(path="<path_to_pecha>")

# create a simple layer
ann = Citation(span=Span(start=10, end=20))
layer = Layer(annotation_type=LayerEnum.citation)
layer.set_annotation(ann)

base_name = pecha.set_base("base content", metadata={"title": "title", "order": 1})
pecha.set_layer(base_name, layer)

pecha.save()

assert pecha.layers[base_name][LayerEnum.citation].id == layer.id

After successfully parsing the input, you should be able to get base text and layers.

Since, layers are based on the base text, first you need to set base to pecha with pecha.set_base('base content) which will return base_name, which is identifier for base and it's associated layers. Then you use base_name to set layer to pecha.

Notice, you can also set base metadata with metadata argument in pecha.set_base() method.

After adding all the bases and layer, call pecha.save() to save bases and layers.

Update Base layer

In order to update a base, we need to know the base_name of base that we want to update.

from openpecha.core.pecha import OpenPechaFS

pecha = OpenPechaFS(path="<path_to_pecha>")

pecha.update_base("v001", "new content")
pecha.save()