Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer newest, released release for miottemplate #1540

Merged
merged 1 commit into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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