Config implementation
Implementation of a JSON file that save all the Signal descriptor
This commit is contained in:
1 parent
fdd4597233
commit
6c0da0a22f
2 files changed
+58
-3
No files matched your search
@@ -0,0 +1,45 @@
|
||||
from pydantic import BaseModel, Field
|
||||
from dynalab_core.protocols.packets.handshake import JsonSignalDescriptor
|
||||
from pathlib import Path
|
||||
from typing import Self
|
||||
from uuid import uuid4
|
||||
|
||||
|
||||
|
||||
class Config(BaseModel):
|
||||
rpmhz_sig: JsonSignalDescriptor | None = None
|
||||
lambda_sig: JsonSignalDescriptor | None = None
|
||||
comments: str = ""
|
||||
|
||||
def write(self, file: str | Path) -> None:
|
||||
output_path = Path(file).expanduser()
|
||||
|
||||
output_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
output_path.write_text(
|
||||
self.model_dump_json(indent=4),
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
def read_config(file: str | Path) -> Config:
|
||||
input_path = Path(file).expanduser()
|
||||
if input_path.exists():
|
||||
return Config.model_validate_json(input_path.read_text(encoding="utf-8"))
|
||||
raise FileNotFoundError
|
||||
|
||||
|
||||
DEFAULT_CONFIG = Config(
|
||||
rpmhz_sig=JsonSignalDescriptor(
|
||||
id=uuid4(),
|
||||
name="Rpmhz",
|
||||
type="number",
|
||||
timeout_ms=5000,
|
||||
),
|
||||
lambda_sig=JsonSignalDescriptor(
|
||||
id=uuid4(),
|
||||
name="Lambda",
|
||||
type="number",
|
||||
timeout_ms=5000,
|
||||
),
|
||||
comments="Mon test",
|
||||
)
|
||||
Reference in new issue
Block a user