From 3380d790bc584abe0d0b4322ece802a37fa81632 Mon Sep 17 00:00:00 2001 From: Midgard Date: Mon, 27 Jan 2020 00:46:29 +0100 Subject: [PATCH] Store attributes directly on model instead of in map --- app/hlds/models.py | 16 ++++++++++++---- app/hlds/parser.py | 7 ++++++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/app/hlds/models.py b/app/hlds/models.py index 0f95936..dc1ec81 100644 --- a/app/hlds/models.py +++ b/app/hlds/models.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # pylint: disable=too-few-public-methods -from typing import Iterable, List, Mapping, Any +from typing import Iterable, List, Mapping, Any, Optional def _format_tags(tags: Iterable[str]) -> str: @@ -75,10 +75,13 @@ class Dish: class Location: - def __init__(self, id_, *, name, attributes, dishes): + def __init__(self, id_, *, name, dishes, osm=None, address=None, telephone=None, website=None): self.id: str = id_ self.name: str = name - self.attributes: Mapping[str, Any] = attributes + self.osm: Optional[str] = osm + self.address: Optional[str] = address + self.telephone: Optional[str] = telephone + self.website: Optional[str] = website self.dishes: List[Dish] = dishes @@ -92,6 +95,11 @@ class Location: "{2}" ).format( self, - "".join("\n\t{} {}".format(k, v) for k, v in self.attributes.items()), + "".join("\n\t{} {}".format(k, v) for k, v in ( + ("osm", self.osm), + ("address", self.address), + ("telephone", self.telephone), + ("website", self.website), + ) if v is not None), "\n".join(map(str, self.dishes)) ) diff --git a/app/hlds/parser.py b/app/hlds/parser.py index 076f2b0..5bd8036 100644 --- a/app/hlds/parser.py +++ b/app/hlds/parser.py @@ -28,11 +28,16 @@ class HldsSemanticActions: if not isinstance(choice[1], Choice): dish.choices[i] = (dish.choices[i][0], choices[choice[1]]) + attributes = {att["key"]: att["value"] for att in ast["attributes"]} + return Location( ast["id"], name=ast["name"], - attributes={att["key"]: att["value"] for att in ast["attributes"]}, dishes=dishes, + osm=attributes.get("osm"), + address=attributes.get("address"), + telephone=attributes.get("telephone"), + website=attributes.get("website"), ) def dish_block(self, ast) -> Dish: