Skip to content

Commit

Permalink
Prefer newest, released release for miottemplate (#1540)
Browse files Browse the repository at this point in the history
  • Loading branch information
rytilahti authored Sep 26, 2022
1 parent 639a008 commit 6d841e0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
30 changes: 25 additions & 5 deletions devtools/containers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import logging
from dataclasses import dataclass, field
from operator import attrgetter
from typing import Any, Dict, List, Optional

from dataclasses_json import DataClassJsonMixin, config

_LOGGER = logging.getLogger(__name__)


def pretty_name(name):
return name.replace(" ", "_").replace("-", "_")
Expand Down Expand Up @@ -34,20 +38,36 @@ class InstanceInfo:
type: str
version: int

@property
def filename(self) -> str:
return f"{self.model}_{self.status}_{self.version}.json"


@dataclass
class ModelMapping(DataClassJsonMixin):
instances: List[InstanceInfo]

def urn_for_model(self, model: str):
def info_for_model(self, model: str, *, status_filter="released") -> InstanceInfo:
matches = [inst for inst in self.instances if inst.model == model]

if len(matches) > 1:
print( # noqa: T201
"WARNING more than a single match for model %s, using the first one: %s"
% (model, matches)
_LOGGER.warning(
"more than a single match for model %s: %s, filtering with status=%s",
model,
matches,
status_filter,
)

return matches[0]
released_versions = [inst for inst in matches if inst.status == status_filter]
if not released_versions:
raise Exception(f"No releases for {model}, adjust status_filter if you ")

_LOGGER.debug("Got %s releases, picking the newest one", released_versions)

match = max(released_versions, key=attrgetter("version"))
_LOGGER.debug("Using %s", match)

return match


@dataclass
Expand Down
7 changes: 4 additions & 3 deletions devtools/miottemplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,12 @@ def download(ctx, urn, model):
ctx.invoke(download_mapping)

mapping = get_mapping()
urn = mapping.urn_for_model(model)
model = mapping.info_for_model(model)

url = f"https://miot-spec.org/miot-spec-v2/instance?type={urn}"
url = f"https://miot-spec.org/miot-spec-v2/instance?type={model.type}"
click.echo("Going to download %s" % url)
content = requests.get(url)
save_to = f"{urn}.json"
save_to = model.filename
click.echo(f"Saving data to {save_to}")
with open(save_to, "w") as f:
f.write(content.text)
Expand Down

0 comments on commit 6d841e0

Please sign in to comment.