2020-01-26 00:04:29 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
from glob import glob
|
2020-01-26 01:45:20 +01:00
|
|
|
from os import path
|
2020-01-26 00:04:29 +01:00
|
|
|
import itertools
|
2020-01-26 15:09:22 +01:00
|
|
|
from typing import Iterable, List, Union, Tuple
|
2020-01-26 01:45:20 +01:00
|
|
|
from tatsu import parse as tatsu_parse
|
2020-01-26 15:09:22 +01:00
|
|
|
from tatsu.ast import AST
|
2020-01-26 01:29:19 +01:00
|
|
|
from .models import Location, Choice, Option, Dish
|
2020-01-26 00:04:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
# TODO Use proper way to get resources, see https://stackoverflow.com/a/10935674
|
|
|
|
with open(path.join(path.dirname(__file__), "hlds.tatsu")) as fh:
|
|
|
|
GRAMMAR = fh.read()
|
|
|
|
|
|
|
|
|
2020-01-26 01:29:19 +01:00
|
|
|
def filter_instance(cls, iterable):
|
|
|
|
return [item for item in iterable if isinstance(item, cls)]
|
|
|
|
|
|
|
|
|
2020-01-26 01:45:20 +01:00
|
|
|
# pylint: disable=no-self-use
|
2020-01-26 01:29:19 +01:00
|
|
|
class HldsSemanticActions:
|
2020-01-26 15:09:22 +01:00
|
|
|
def location(self, ast) -> Location:
|
2020-01-26 01:40:50 +01:00
|
|
|
choices = {choice.id: choice for choice in filter_instance(Choice, ast["items_"])}
|
|
|
|
dishes = filter_instance(Dish, ast["items_"])
|
|
|
|
for dish in dishes:
|
|
|
|
for i, choice in enumerate(dish.choices):
|
2020-01-26 02:09:53 +01:00
|
|
|
if not isinstance(choice[1], Choice):
|
|
|
|
dish.choices[i] = (dish.choices[i][0], choices[choice[1]])
|
2020-01-26 01:40:50 +01:00
|
|
|
|
2020-01-26 01:29:19 +01:00
|
|
|
return Location(
|
|
|
|
ast["id"],
|
|
|
|
name=ast["name"],
|
|
|
|
attributes={att["key"]: att["value"] for att in ast["attributes"]},
|
2020-01-26 01:40:50 +01:00
|
|
|
dishes=dishes,
|
2020-01-26 01:29:19 +01:00
|
|
|
)
|
|
|
|
|
2020-01-26 15:09:22 +01:00
|
|
|
def base_block(self, ast) -> Dish:
|
2020-01-26 01:29:19 +01:00
|
|
|
return Dish(
|
|
|
|
ast["id"],
|
|
|
|
name=ast["name"],
|
|
|
|
description=ast["description"],
|
|
|
|
price=ast["price"],
|
|
|
|
tags=ast["tags"] if ast["tags"] else [],
|
|
|
|
choices=ast["choices"],
|
|
|
|
)
|
|
|
|
|
2020-01-26 15:09:22 +01:00
|
|
|
def choice_block(self, ast) -> Choice:
|
2020-01-26 01:29:19 +01:00
|
|
|
return Choice(
|
|
|
|
ast["id"],
|
|
|
|
name=ast["name"],
|
|
|
|
description=ast["description"],
|
|
|
|
options=ast["entries"],
|
|
|
|
)
|
|
|
|
|
2020-01-26 15:09:22 +01:00
|
|
|
def indent_choice_block(self, ast) -> Tuple[str, Union[Choice, AST]]:
|
2020-01-26 02:09:53 +01:00
|
|
|
return (
|
|
|
|
(ast["type"], self.choice_block(ast))
|
|
|
|
if ast["kind"] == "declaration" else
|
|
|
|
(ast["type"], ast["id"])
|
|
|
|
)
|
2020-01-26 01:29:19 +01:00
|
|
|
|
2020-01-26 15:09:22 +01:00
|
|
|
def indent_choice_entry(self, ast) -> Option:
|
2020-01-26 01:29:19 +01:00
|
|
|
return Option(
|
|
|
|
ast["id"],
|
|
|
|
name=ast["name"],
|
|
|
|
description=ast["description"],
|
|
|
|
price=ast["price"],
|
|
|
|
tags=ast["tags"],
|
|
|
|
)
|
|
|
|
|
2020-01-26 01:40:50 +01:00
|
|
|
noindent_choice_entry = indent_choice_entry
|
|
|
|
|
2020-01-26 15:09:22 +01:00
|
|
|
def price(self, ast) -> int:
|
|
|
|
return (
|
|
|
|
100 * int(ast["value_unit"]) +
|
|
|
|
(
|
|
|
|
0 if not ast["value_cents"] else
|
|
|
|
10 * int(ast["value_cents"]) if len(ast["value_cents"]) == 1 else
|
|
|
|
int(ast["value_cents"])
|
|
|
|
)
|
|
|
|
)
|
2020-01-26 01:29:19 +01:00
|
|
|
|
|
|
|
def _default(self, ast):
|
|
|
|
return ast
|
|
|
|
|
|
|
|
SEMANTICS = HldsSemanticActions()
|
2020-01-26 00:04:29 +01:00
|
|
|
|
|
|
|
|
2020-01-26 15:09:22 +01:00
|
|
|
def parse(menu: str) -> List[Location]:
|
2020-01-26 01:29:19 +01:00
|
|
|
parsed = tatsu_parse(GRAMMAR, menu, semantics=SEMANTICS)
|
2020-01-26 00:04:29 +01:00
|
|
|
return parsed
|
|
|
|
|
|
|
|
|
2020-01-26 15:09:22 +01:00
|
|
|
def parse_file(filename: str) -> List[Location]:
|
2020-01-26 01:45:20 +01:00
|
|
|
with open(filename, "r") as file_handle:
|
|
|
|
return parse(file_handle.read())
|
2020-01-26 00:04:29 +01:00
|
|
|
|
|
|
|
|
2020-01-26 15:09:22 +01:00
|
|
|
def parse_files(files: Iterable[str]) -> List[Location]:
|
2020-01-26 00:04:29 +01:00
|
|
|
menus = map(parse_file, files)
|
|
|
|
return list(itertools.chain.from_iterable(menus))
|
|
|
|
|
|
|
|
|
2020-01-26 15:09:22 +01:00
|
|
|
def parse_all_directory(directory: str) -> List[Location]:
|
2020-01-26 00:04:29 +01:00
|
|
|
# TODO Use proper way to get resources, see https://stackoverflow.com/a/10935674
|
|
|
|
files = glob(path.join(directory, "**.hlds"), recursive=True)
|
|
|
|
return parse_files(files)
|