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_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)

View file

@ -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:])