2020-01-25 01:33:23 +01:00
|
|
|
#!/usr/bin/env python3
|
2020-01-26 01:45:20 +01:00
|
|
|
# pylint: disable=too-few-public-methods
|
2020-01-25 01:33:23 +01:00
|
|
|
|
2022-04-19 22:03:00 +02:00
|
|
|
from typing import Any, Iterable, List, Mapping, Optional, Tuple
|
|
|
|
|
2020-02-21 18:38:30 +01:00
|
|
|
from utils import euro_string, first
|
2020-01-25 01:33:23 +01:00
|
|
|
|
|
|
|
|
2020-01-26 15:13:48 +01:00
|
|
|
def _format_tags(tags: Iterable[str]) -> str:
|
2022-04-19 22:03:00 +02:00
|
|
|
# pylint: disable=consider-using-f-string
|
|
|
|
return " :: {}".format(" ".join(["{" + tag + "}"
|
|
|
|
for tag in tags])) if tags else ""
|
2020-01-26 01:29:19 +01:00
|
|
|
|
|
|
|
|
2020-01-26 15:13:48 +01:00
|
|
|
def _format_price(price: int) -> str:
|
2022-04-19 22:03:00 +02:00
|
|
|
return f" {euro_string(price)}" if price else ""
|
2020-01-26 15:13:48 +01:00
|
|
|
|
|
|
|
|
2020-01-26 02:09:53 +01:00
|
|
|
def _format_type_and_choice(type_and_choice):
|
|
|
|
type_, choice = type_and_choice
|
2022-04-19 22:03:00 +02:00
|
|
|
return f"{type_} {choice}"
|
2020-01-26 02:09:53 +01:00
|
|
|
|
|
|
|
|
2020-01-25 01:33:23 +01:00
|
|
|
class Option:
|
2022-04-19 22:03:00 +02:00
|
|
|
|
2020-01-26 01:29:19 +01:00
|
|
|
def __init__(self, id_, *, name, description, price, tags):
|
2020-01-26 15:09:22 +01:00
|
|
|
self.id: str = id_
|
|
|
|
self.name: str = name
|
|
|
|
self.description: str = description
|
|
|
|
self.price: int = price
|
|
|
|
self.tags: List[str] = tags
|
2020-01-26 01:29:19 +01:00
|
|
|
|
|
|
|
def __str__(self):
|
2022-04-19 22:03:00 +02:00
|
|
|
# pylint: disable=consider-using-f-string
|
2020-01-26 01:29:19 +01:00
|
|
|
return "{0.id}: {0.name}{1}{2}{3}".format(
|
|
|
|
self,
|
2022-04-19 22:03:00 +02:00
|
|
|
f" -- {self.description}" if self.description else "",
|
2020-01-26 01:29:19 +01:00
|
|
|
_format_tags(self.tags),
|
2020-07-17 11:40:15 +02:00
|
|
|
_format_price(self.price),
|
2020-01-26 01:29:19 +01:00
|
|
|
)
|
2020-01-25 01:33:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Choice:
|
2022-04-19 22:03:00 +02:00
|
|
|
|
2020-01-26 01:29:19 +01:00
|
|
|
def __init__(self, id_, *, name, description, options):
|
2020-01-26 15:09:22 +01:00
|
|
|
self.id: str = id_
|
|
|
|
self.name: str = name
|
|
|
|
self.description: str = description
|
2020-01-25 01:33:23 +01:00
|
|
|
|
|
|
|
self.options: List[Option] = options
|
|
|
|
|
2020-01-26 01:29:19 +01:00
|
|
|
def __str__(self):
|
|
|
|
return "{0.id}: {0.name}{1}\n\t\t{2}".format(
|
|
|
|
self,
|
2022-04-19 22:03:00 +02:00
|
|
|
f" -- {self.description}" if self.description else "",
|
2020-07-17 11:40:15 +02:00
|
|
|
"\n\t\t".join(map(str, self.options)),
|
2020-01-26 01:29:19 +01:00
|
|
|
)
|
2020-01-25 01:33:23 +01:00
|
|
|
|
2020-02-23 23:29:39 +01:00
|
|
|
def option_by_id(self, option_id: str) -> Optional[Option]:
|
|
|
|
return first(filter(lambda o: o.id == option_id, self.options))
|
|
|
|
|
2020-01-26 01:29:19 +01:00
|
|
|
|
|
|
|
class Dish:
|
2022-04-19 22:03:00 +02:00
|
|
|
|
2020-01-26 01:29:19 +01:00
|
|
|
def __init__(self, id_, *, name, description, price, tags, choices):
|
2020-01-26 15:09:22 +01:00
|
|
|
self.id: str = id_
|
|
|
|
self.name: str = name
|
|
|
|
self.description: str = description
|
|
|
|
self.price: int = price
|
|
|
|
self.tags: List[str] = tags
|
2020-01-25 01:33:23 +01:00
|
|
|
|
2020-02-21 18:38:30 +01:00
|
|
|
# The str in (str, Choice) is the type of choice: single_choice or multi_choice
|
2020-01-26 02:09:53 +01:00
|
|
|
self.choices: List[(str, Choice)] = choices
|
2020-01-26 01:29:19 +01:00
|
|
|
|
|
|
|
def __str__(self):
|
2020-01-26 23:51:29 +01:00
|
|
|
return "dish {0.id}: {0.name}{1}{2}{3}\n\t{4}".format(
|
2020-01-26 01:29:19 +01:00
|
|
|
self,
|
2022-04-19 22:03:00 +02:00
|
|
|
f" -- {self.description}" if self.description else "",
|
2020-01-26 01:29:19 +01:00
|
|
|
_format_tags(self.tags),
|
2020-01-26 15:13:48 +01:00
|
|
|
_format_price(self.price),
|
2020-07-17 11:40:15 +02:00
|
|
|
"\n\t".join(map(_format_type_and_choice, self.choices)),
|
2020-01-26 01:29:19 +01:00
|
|
|
)
|
|
|
|
|
2020-02-24 00:31:14 +01:00
|
|
|
def price_range(self) -> Tuple[int, int]:
|
2020-07-17 11:40:15 +02:00
|
|
|
return (
|
|
|
|
self.price + self._sum_f_option_prices(min),
|
|
|
|
self.price + self._sum_f_option_prices(max),
|
|
|
|
)
|
2020-02-24 00:31:14 +01:00
|
|
|
|
|
|
|
def _sum_f_option_prices(self, f):
|
2022-05-15 00:04:50 +02:00
|
|
|
for (_, choice) in self.choices:
|
|
|
|
if len(choice.options) == 0:
|
|
|
|
print((f"[PARSE ERROR] At least 1 option expected in dish choice.\n"
|
|
|
|
f"\tDish:\t'{self.name}'\n"
|
|
|
|
f"\tChoice:\t'{choice.name}'\n"))
|
|
|
|
exit(1)
|
2020-02-24 00:31:14 +01:00
|
|
|
return sum(
|
|
|
|
f(option.price for option in choice.options)
|
|
|
|
for (choice_type, choice) in self.choices
|
2022-04-19 22:03:00 +02:00
|
|
|
if choice_type == "single_choice")
|
2020-02-24 00:31:14 +01:00
|
|
|
|
2020-01-26 01:29:19 +01:00
|
|
|
|
|
|
|
class Location:
|
2022-04-19 22:03:00 +02:00
|
|
|
|
|
|
|
def __init__(self,
|
|
|
|
id_,
|
|
|
|
*,
|
|
|
|
name,
|
|
|
|
dishes,
|
|
|
|
osm=None,
|
|
|
|
address=None,
|
|
|
|
telephone=None,
|
|
|
|
website=None):
|
2020-01-26 15:09:22 +01:00
|
|
|
self.id: str = id_
|
|
|
|
self.name: str = name
|
2020-01-27 00:46:29 +01:00
|
|
|
self.osm: Optional[str] = osm
|
|
|
|
self.address: Optional[str] = address
|
|
|
|
self.telephone: Optional[str] = telephone
|
|
|
|
self.website: Optional[str] = website
|
2020-01-26 01:29:19 +01:00
|
|
|
|
|
|
|
self.dishes: List[Dish] = dishes
|
|
|
|
|
2020-02-21 18:38:30 +01:00
|
|
|
def dish_by_id(self, dish_id: str) -> Optional[Dish]:
|
|
|
|
return first(filter(lambda d: d.id == dish_id, self.dishes))
|
|
|
|
|
2020-01-26 01:29:19 +01:00
|
|
|
def __str__(self):
|
2022-04-19 22:03:00 +02:00
|
|
|
return ("============================\n"
|
|
|
|
"{0.id}: {0.name}"
|
|
|
|
"{1}\n"
|
|
|
|
"============================\n"
|
|
|
|
"\n"
|
|
|
|
"{2}").format(
|
|
|
|
self,
|
|
|
|
"".join(f"\n\t{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)),
|
2020-07-17 11:40:15 +02:00
|
|
|
)
|