Skip to content

Commit 8a01b37

Browse files
committed
draft group regulation config classes (WIP)
1 parent 81e45c3 commit 8a01b37

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
from __future__ import annotations
2+
3+
import logging
4+
from dataclasses import dataclass
5+
from typing import TYPE_CHECKING
6+
7+
import yaml
8+
9+
if TYPE_CHECKING:
10+
from pathlib import Path
11+
12+
logger = logging.getLogger(__name__)
13+
14+
15+
class ConfigSyntaxError(Exception):
16+
def __init__(self, message: str):
17+
super().__init__(f"Invalid config syntax: {message}")
18+
19+
20+
@dataclass
21+
class PlanRegulationGroupLibraryConfig:
22+
"""Describes the configuration of a plan regulation group library"""
23+
24+
name: str
25+
version: str
26+
plan_regulation_groups: list[PlanRegulationGroupConfig]
27+
28+
@classmethod
29+
def from_dict(cls, data: dict) -> PlanRegulationGroupLibraryConfig:
30+
try:
31+
return cls(
32+
name=data["name"],
33+
version=data["version"],
34+
plan_regulation_groups=[
35+
PlanRegulationGroupConfig.from_dict(group) for group in data["plan_regulation_groups"]
36+
],
37+
)
38+
except KeyError as e:
39+
raise ConfigSyntaxError(str(e)) from e
40+
41+
42+
@dataclass
43+
class PlanRegulationGroupConfig:
44+
"""Describes a plan regulation group"""
45+
46+
name: str
47+
category: str
48+
geometry: str
49+
color_code: str | None
50+
letter_code: str | None
51+
plan_regulations: list[PlanRegulationConfig]
52+
53+
@classmethod
54+
def from_dict(cls, data: dict) -> PlanRegulationGroupConfig:
55+
return cls(
56+
name=data["name"],
57+
category=data["category"],
58+
geometry=data["geometry"],
59+
color_code=data.get("color_code"),
60+
letter_code=data.get("letter_code"),
61+
plan_regulations=[PlanRegulationConfig.from_dict(regulation) for regulation in data["plan_regulations"]],
62+
)
63+
64+
65+
@dataclass
66+
class PlanRegulationConfig:
67+
"""Describes a plan regulation"""
68+
69+
name: str
70+
code: str
71+
value: str | None
72+
unit: str | None
73+
additional_information: dict | None
74+
75+
@classmethod
76+
def from_dict(cls, data: dict) -> PlanRegulationConfig:
77+
return cls(
78+
name=data["name"],
79+
code=data["code"],
80+
value=data.get("value"),
81+
unit=data.get("unit"),
82+
additional_information=data.get("additional_information"),
83+
)
84+
85+
86+
def parse_group_regulation_library_config(fp: Path) -> PlanRegulationGroupLibraryConfig:
87+
with fp.open(encoding="utf-8") as f:
88+
data = yaml.safe_load(f)
89+
return PlanRegulationGroupLibraryConfig.from_dict(data)

0 commit comments

Comments
 (0)