diff --git a/app/app.py b/app/app.py index 573bdde..3ca6f5e 100755 --- a/app/app.py +++ b/app/app.py @@ -15,6 +15,7 @@ from flask_migrate import Migrate, MigrateCommand from flask_oauthlib.client import OAuth, OAuthException from flask_script import Manager, Server +import hlds from admin import init_admin from login import init_login from models import db @@ -70,6 +71,9 @@ def register_plugins(app: Flask) -> Manager: # Initialize SQLAlchemy db.init_app(app) + # Load locations + locations = hlds.load_all() + # Initialize Flask-Migrate migrate = Migrate(app, db) app_manager = Manager(app) diff --git a/app/hlds/__init__.py b/app/hlds/__init__.py index fb0b307..08d9e21 100755 --- a/app/hlds/__init__.py +++ b/app/hlds/__init__.py @@ -1,38 +1,53 @@ #!/usr/bin/env python3 -from os import path +from glob import glob +from os import path, walk from tatsu import parse as tatsu_parse +import itertools # 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() + GRAMMAR = fh.read() def kind_comparer(compare_to): - return lambda item: item["kind"] == compare_to + return lambda item: item["kind"] == compare_to def parse(menu): - parsed = tatsu_parse(GRAMMAR, menu) - return dict(( - *((att["key"], att["value"]) for att in parsed["attributes"]), - ("id", parsed["id"]), - ("name", parsed["name"]), - ("choices", filter(kind_comparer("choice_declaration"), parsed["items_"])), - ("bases", filter(kind_comparer("base"), parsed["items_"])), - )) + parsed = tatsu_parse(GRAMMAR, menu) + return parsed + return dict(( + *((att["key"], att["value"]) for att in parsed["attributes"]), + ("id", parsed["id"]), + ("name", parsed["name"]), + ("choices", filter(kind_comparer("choice_declaration"), parsed["items_"])), + ("bases", filter(kind_comparer("base"), parsed["items_"])), + )) + + +def parse_file(filename): + with open(filename, "r") as fh: + return parse(fh.read()) + + +def load_all(): + # TODO Use proper way to get resources, see https://stackoverflow.com/a/10935674 + data_dir = path.join(path.dirname(__file__), "..", "..", "data") + files = glob(path.join(data_dir, "**.hlds")) + menus = map(parse_file, files) + return list(itertools.chain.from_iterable(menus)) def main(filename): - import json - from tatsu.util import asjson + import json + from tatsu.util import asjson - with open(filename) as fh: - ast = parse(fh.read()) - print(json.dumps(asjson(ast), indent="\t")) + ast = parse_file(filename) + print(json.dumps(asjson(ast), indent="\t")) if __name__ == "__main__": - import sys - main(*sys.argv[1:]) + import sys + main(*sys.argv[1:]) diff --git a/data/oceangarden_future_syntax.hlds b/data/oceangarden_future_syntax.hlds-disabled similarity index 100% rename from data/oceangarden_future_syntax.hlds rename to data/oceangarden_future_syntax.hlds-disabled