Load HLDS locations in app

This commit is contained in:
Midgard 2020-01-24 15:47:29 +01:00
parent e6b707e3d7
commit 303349e376
Signed by: midgard
GPG key ID: 511C112F1331BBB4
3 changed files with 37 additions and 18 deletions

View file

@ -15,6 +15,7 @@ from flask_migrate import Migrate, MigrateCommand
from flask_oauthlib.client import OAuth, OAuthException from flask_oauthlib.client import OAuth, OAuthException
from flask_script import Manager, Server from flask_script import Manager, Server
import hlds
from admin import init_admin from admin import init_admin
from login import init_login from login import init_login
from models import db from models import db
@ -70,6 +71,9 @@ def register_plugins(app: Flask) -> Manager:
# Initialize SQLAlchemy # Initialize SQLAlchemy
db.init_app(app) db.init_app(app)
# Load locations
locations = hlds.load_all()
# Initialize Flask-Migrate # Initialize Flask-Migrate
migrate = Migrate(app, db) migrate = Migrate(app, db)
app_manager = Manager(app) app_manager = Manager(app)

View file

@ -1,38 +1,53 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from os import path from glob import glob
from os import path, walk
from tatsu import parse as tatsu_parse from tatsu import parse as tatsu_parse
import itertools
# TODO Use proper way to get resources, see https://stackoverflow.com/a/10935674 # TODO Use proper way to get resources, see https://stackoverflow.com/a/10935674
with open(path.join(path.dirname(__file__), "hlds.tatsu")) as fh: with open(path.join(path.dirname(__file__), "hlds.tatsu")) as fh:
GRAMMAR = fh.read() GRAMMAR = fh.read()
def kind_comparer(compare_to): def kind_comparer(compare_to):
return lambda item: item["kind"] == compare_to return lambda item: item["kind"] == compare_to
def parse(menu): def parse(menu):
parsed = tatsu_parse(GRAMMAR, menu) parsed = tatsu_parse(GRAMMAR, menu)
return dict(( return parsed
*((att["key"], att["value"]) for att in parsed["attributes"]), return dict((
("id", parsed["id"]), *((att["key"], att["value"]) for att in parsed["attributes"]),
("name", parsed["name"]), ("id", parsed["id"]),
("choices", filter(kind_comparer("choice_declaration"), parsed["items_"])), ("name", parsed["name"]),
("bases", filter(kind_comparer("base"), parsed["items_"])), ("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): def main(filename):
import json import json
from tatsu.util import asjson from tatsu.util import asjson
with open(filename) as fh: ast = parse_file(filename)
ast = parse(fh.read()) print(json.dumps(asjson(ast), indent="\t"))
print(json.dumps(asjson(ast), indent="\t"))
if __name__ == "__main__": if __name__ == "__main__":
import sys import sys
main(*sys.argv[1:]) main(*sys.argv[1:])