72 lines
2.0 KiB
Python
72 lines
2.0 KiB
Python
"""CLI for importing the approved booking Markdown fixture."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
from pathlib import Path
|
|
|
|
from .postgres import DatabaseConfig, import_fixture
|
|
|
|
|
|
ENV_KEYS = {
|
|
"ARR_DB_HOST",
|
|
"ARR_DB_PORT",
|
|
"ARR_DB_USER",
|
|
"ARR_DB_PASSWORD",
|
|
"ARR_DB_NAME",
|
|
}
|
|
|
|
|
|
def load_config(path: Path) -> DatabaseConfig:
|
|
values: dict[str, str] = {}
|
|
for raw_line in path.read_text(encoding="utf-8").splitlines():
|
|
line = raw_line.strip()
|
|
if not line or line.startswith("#"):
|
|
continue
|
|
key, separator, value = line.partition("=")
|
|
if not separator or key.strip() in values:
|
|
raise RuntimeError("invalid database config structure")
|
|
values[key.strip()] = value.strip().strip('"').strip("'")
|
|
if set(values) != ENV_KEYS or not all(values.values()):
|
|
raise RuntimeError("database config keys are incomplete")
|
|
return DatabaseConfig(
|
|
host=values["ARR_DB_HOST"],
|
|
port=int(values["ARR_DB_PORT"]),
|
|
user=values["ARR_DB_USER"],
|
|
password=values["ARR_DB_PASSWORD"],
|
|
database=values["ARR_DB_NAME"],
|
|
)
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--fixture", required=True, type=Path)
|
|
parser.add_argument("--contract", required=True, type=Path)
|
|
parser.add_argument("--db-env", required=True, type=Path)
|
|
args = parser.parse_args()
|
|
result = import_fixture(
|
|
load_config(args.db_env),
|
|
args.fixture,
|
|
args.contract,
|
|
)
|
|
print(
|
|
json.dumps(
|
|
{
|
|
"status": result.status,
|
|
"source_batch_id": result.source_batch_id,
|
|
"source_rows": result.source_rows,
|
|
"current_parses": result.current_parses,
|
|
"room_items": result.room_items,
|
|
"distinct_group_codes": result.distinct_group_codes,
|
|
},
|
|
ensure_ascii=False,
|
|
indent=2,
|
|
)
|
|
)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|