Skip to content

Commit

Permalink
channels as str
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterKraus committed Nov 13, 2024
1 parent 2d19721 commit 83522a6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
27 changes: 24 additions & 3 deletions src/tomato/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
Peter Kraus
"""

from pydantic import BaseModel, Field
from pydantic import BaseModel, Field, field_validator
from typing import Optional, Any, Mapping, Sequence, Literal
from pathlib import Path
import logging


logger = logging.getLogger(__name__)


Expand All @@ -26,19 +27,39 @@ class Device(BaseModel):
name: str
driver: str
address: str
channels: Sequence[int]
channels: Sequence[str]
pollrate: int = 1

@field_validator("channels", mode="before")
def coerce_channels(cls, v):
if any([isinstance(vv, int) for vv in v]):
logger.warning(
"Supplying 'channels' as a Sequence[int] is deprecated "
"and will stop working in tomato-2.0."
)
return [str(vv) for vv in v]
return v


class Component(BaseModel):
name: str
driver: str
device: str
address: str
channel: int
channel: str
role: str
capabilities: Optional[set[str]] = None

@field_validator("channel", mode="before")
def coerce_channel(cls, v):
if isinstance(v, int):
logger.warning(
"Supplying 'channel' as an int is deprecated "
"and will stop working in tomato-2.0."
)
return str(v)
return v


class Pipeline(BaseModel):
name: str
Expand Down
12 changes: 9 additions & 3 deletions src/tomato/tomato/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def get_pipelines(
dev = devs[comp["device"]]
for ch in dev.channels:
name = pip["name"].replace("*", f"{ch}")
h = "/".join((dev.driver, dev.address, str(ch)))
h = f"{dev.driver}:({dev.address},{ch})"
c = Component(
name=h,
driver=dev.driver,
Expand All @@ -97,14 +97,20 @@ def get_pipelines(
logger.error("device '%s' not found", comp["device"])
break
dev = devs[comp["device"]]
if isinstance(comp["channel"], int):
logger.warning(
"Supplying 'channel' as an int is deprecated "
"and will stop working in tomato-2.0."
)
comp["channel"] = str(comp["channel"])
if comp["channel"] not in dev.channels:
logger.error(
"channel %d not found on device '%s'",
"channel %s not found on device '%s'",
comp["channel"],
comp["device"],
)
break
h = "/".join((dev.driver, dev.address, str(comp["channel"])))
h = f"{dev.driver}:({dev.address},{comp['channel']})"
c = Component(
name=h,
driver=dev.driver,
Expand Down

0 comments on commit 83522a6

Please sign in to comment.