-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
643 additions
and
402 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from typing import Any | ||
|
||
import yaml | ||
|
||
from .planner import ( | ||
ACTION_CLASSES, | ||
TRIGGER_CLASSES, | ||
PlanAction, | ||
PlanActionSequence, | ||
PlanEntry, | ||
PlanTrigger, | ||
) | ||
|
||
|
||
def load_plan(path: str, encoding: str | None = None) -> list[PlanEntry]: | ||
with open(path, "r", encoding=encoding) as f: | ||
master_config = yaml.safe_load(f) | ||
plan_entries = master_config.get("plan", []) | ||
plan = [load_plan_entry(entry) for entry in plan_entries] | ||
return plan | ||
|
||
|
||
def generate_mappings(classes, prefix): | ||
return {cls.__name__.replace(prefix, ""): cls for cls in classes} | ||
|
||
|
||
action_map = generate_mappings(ACTION_CLASSES, "PlanAction") | ||
trigger_map = generate_mappings(TRIGGER_CLASSES, "PlanTrigger") | ||
|
||
|
||
def _load_action(action_data: dict[str, Any]) -> PlanAction: | ||
type, args = action_data["type"], action_data["args"] | ||
action_class = action_map[type] | ||
if action_class is PlanActionSequence: | ||
action = _load_sequence(args) | ||
else: | ||
action = action_class.loads(args) | ||
return action | ||
|
||
|
||
def _load_trigger(trigger_data: dict[str, Any]) -> PlanTrigger: | ||
type, args = trigger_data["type"], trigger_data["args"] | ||
trigger_class = trigger_map[type] | ||
return trigger_class.loads(args) | ||
|
||
|
||
def _load_sequence(entries: list[dict[str, Any]]) -> PlanActionSequence: | ||
items: list[PlanAction] = [] | ||
for entry in entries: | ||
items.append(_load_action(entry)) | ||
return PlanActionSequence(*items) | ||
|
||
|
||
def load_plan_entry(entry: dict[str, Any]) -> PlanEntry: | ||
action = _load_action(entry["action"]) | ||
trigger = _load_trigger(entry["trigger"]) | ||
return PlanEntry(trigger=trigger, action=action) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
from __future__ import annotations | ||
|
||
import json | ||
|
||
import aiohttp | ||
import structlog | ||
import yarl | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from typing import Any, Protocol | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class Serializable(Protocol): | ||
@property | ||
def _Model(self) -> BaseModel: ... | ||
|
||
@classmethod | ||
def loads(cls, data: dict[str, Any]): | ||
return cls(**cls._Model.model_validate(data).model_dump()) # type: ignore | ||
|
||
def dumps(self) -> dict[str, Any]: | ||
return self._Model.model_validate(self, from_attributes=True).model_dump() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.