Skip to content
This repository was archived by the owner on May 13, 2025. It is now read-only.
This repository was archived by the owner on May 13, 2025. It is now read-only.

add CI action to create ahb.db from .json files in MR AHB repo #336

@hf-kklein

Description

@hf-kklein

based on Hochfrequenz/kohlrahbi#557

where ahb.db is a sqlite db:

  • pip install kohlrahbi[sqlmodels]>=1.13.0
  • loop over all .json files in this repository
  • upload the ahb.db as action or release artifact to github
from kohlrahbi.models.sqlmodels.anwendungshandbuch import AhbLine, AhbMetaInformation, FlatAnwendungshandbuch
from sqlmodel import Session, SQLModel, create_engine

def load_machine_readable_json_ahbs_to_db(session: Session) -> None:
    for json_path in mr_ahb_repository_root.rglob("FV*/**/*.json"):
        with open(json_path, "r", encoding="utf-8") as json_file:
            file_body = json.loads(json_file.read())
            flat_ahb = FlatAnwendungshandbuch.model_validate(file_body)
            edifact_format = EdifactFormat(json_path.parent.parent.name)  # e.g. 'COMDIS'
            edifact_format_version = EdifactFormatVersion(json_path.parent.parent.parent.name)
            file_body["meta"]["edifact_format"] = str(edifact_format)
            file_body["meta"]["edifact_format_version"] = str(edifact_format_version)
            meta = AhbMetaInformation.model_validate(file_body["meta"])
            session.add(meta)
            flat_ahb.meta = meta
            for line_index, raw_line in enumerate(file_body["lines"]):
                raw_line["position_inside_ahb"] = line_index
                line = AhbLine.model_validate(raw_line)
                flat_ahb.lines.append(line)
                session.add(line)
            session.add(flat_ahb)
            session.commit()
    session.flush()

if __name__ == '__main__':
  engine = create_engine(f"sqlite:///ahb.db")
  SQLModel.metadata.drop_all(engine)
  SQLModel.metadata.create_all(engine)
  with Session(bind=engine) as session:
     load_machine_readable_json_ahbs_to_db(session)
   # tada!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions