2020-08-13 17:48:26 +00:00
|
|
|
#!/usr/bin/python3
|
2020-10-18 23:42:54 +00:00
|
|
|
import inspect
|
2020-09-03 15:56:42 +00:00
|
|
|
import argparse
|
2019-01-13 19:07:46 +00:00
|
|
|
|
|
|
|
|
2020-10-15 16:11:27 +00:00
|
|
|
def install(arguments):
|
|
|
|
from flaschengeist.app import create_app, install_all
|
2020-11-01 17:37:08 +00:00
|
|
|
|
2020-10-15 16:11:27 +00:00
|
|
|
app = create_app()
|
|
|
|
with app.app_context():
|
|
|
|
install_all()
|
|
|
|
|
2020-09-03 15:56:42 +00:00
|
|
|
|
2020-10-15 16:11:27 +00:00
|
|
|
def run(arguments):
|
|
|
|
from flaschengeist.app import create_app
|
2020-10-18 23:42:54 +00:00
|
|
|
|
2020-09-03 15:56:42 +00:00
|
|
|
app = create_app()
|
2020-10-15 16:11:27 +00:00
|
|
|
with app.app_context():
|
|
|
|
if arguments.debug:
|
|
|
|
app.run(arguments.host, arguments.port, debug=True)
|
2020-10-03 23:29:49 +00:00
|
|
|
else:
|
2020-10-28 13:21:54 +00:00
|
|
|
try:
|
|
|
|
import bjoern
|
2020-11-01 17:37:08 +00:00
|
|
|
|
2020-10-28 13:21:54 +00:00
|
|
|
bjoern.run(app, arguments.host, arguments.port, reuse_port=True)
|
|
|
|
except ImportError:
|
|
|
|
app.run(arguments.host, arguments.port, debug=False)
|
2020-10-15 16:11:27 +00:00
|
|
|
|
|
|
|
|
2020-10-18 23:42:54 +00:00
|
|
|
def export(arguments):
|
2020-10-30 02:30:46 +00:00
|
|
|
import flaschengeist.models as models
|
2020-10-19 14:48:34 +00:00
|
|
|
|
2020-10-18 23:42:54 +00:00
|
|
|
known = []
|
2020-10-19 14:48:34 +00:00
|
|
|
classes = {}
|
|
|
|
|
|
|
|
def pytype(cls):
|
|
|
|
if isinstance(cls, list):
|
2020-10-23 00:03:06 +00:00
|
|
|
return "", "Array<{}>".format(pytype(cls[0])[1])
|
2020-11-01 17:37:08 +00:00
|
|
|
# if typing.get_origin(cls) is typing.Optional:
|
2020-10-23 00:03:06 +00:00
|
|
|
# return "?", pytype(typing.get_args(cls)[1])
|
|
|
|
mapper = {"str": "string", "int": "number", "float": "number", "datetime": "Date"}
|
|
|
|
if hasattr(cls, "__name__"):
|
|
|
|
if cls.__name__ in mapper:
|
|
|
|
return "", mapper[cls.__name__]
|
|
|
|
else:
|
|
|
|
return "", cls.__name__
|
|
|
|
return "?", "any"
|
2020-10-19 14:48:34 +00:00
|
|
|
|
|
|
|
def walker(mod):
|
|
|
|
if inspect.ismodule(mod[1]) and mod[1].__name__.startswith(models.__name__) and mod[1].__name__ not in known:
|
2020-10-18 23:42:54 +00:00
|
|
|
known.append(mod[1].__name__)
|
|
|
|
for cls in inspect.getmembers(mod[1], lambda x: inspect.isclass(x) or inspect.ismodule(x)):
|
2020-10-19 14:48:34 +00:00
|
|
|
walker(cls)
|
|
|
|
elif (
|
|
|
|
inspect.isclass(mod[1])
|
|
|
|
and mod[1].__module__.startswith(models.__name__)
|
|
|
|
and mod[0] not in classes
|
|
|
|
and not mod[0].startswith("_")
|
|
|
|
and hasattr(mod[1], "__annotations__")
|
|
|
|
):
|
2020-11-01 17:37:08 +00:00
|
|
|
d = {
|
|
|
|
param: pytype(ptype)
|
|
|
|
for param, ptype in mod[1].__annotations__.items()
|
|
|
|
if not param.startswith("_") and not param.endswith("_")
|
|
|
|
}
|
2020-10-19 14:48:34 +00:00
|
|
|
if len(d) == 1:
|
|
|
|
key, value = d.popitem()
|
2020-10-23 00:03:06 +00:00
|
|
|
classes[mod[0]] = value[1]
|
2020-10-19 14:48:34 +00:00
|
|
|
else:
|
|
|
|
classes[mod[0]] = d
|
2020-10-18 23:42:54 +00:00
|
|
|
|
|
|
|
from flaschengeist.app import create_app
|
|
|
|
|
|
|
|
app = create_app()
|
|
|
|
with app.app_context():
|
2020-10-19 14:48:34 +00:00
|
|
|
walker(("models", models))
|
2020-10-18 23:42:54 +00:00
|
|
|
with open(arguments.file, "w") as file:
|
2020-10-24 18:11:50 +00:00
|
|
|
file.write("declare namespace {} \{\n".format(arguments.namespace))
|
2020-10-19 14:48:34 +00:00
|
|
|
for cls, params in classes.items():
|
|
|
|
if isinstance(params, str):
|
2020-10-23 00:03:06 +00:00
|
|
|
file.write("\ttype {} = {};\n".format(cls, params))
|
2020-10-19 14:48:34 +00:00
|
|
|
else:
|
2020-10-23 00:03:06 +00:00
|
|
|
file.write("\tinterface {} {{\n".format(cls))
|
2020-10-19 14:48:34 +00:00
|
|
|
for name in params:
|
2020-10-23 00:03:06 +00:00
|
|
|
file.write("\t\t{}{}: {};\n".format(name, *params[name]))
|
|
|
|
file.write("\t}\n")
|
|
|
|
file.write("}\n")
|
2020-10-18 23:42:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2020-10-15 16:11:27 +00:00
|
|
|
# create the top-level parser
|
|
|
|
parser = argparse.ArgumentParser()
|
2020-10-18 23:42:54 +00:00
|
|
|
subparsers = parser.add_subparsers(help="sub-command help", dest="sub_command")
|
2020-10-15 16:11:27 +00:00
|
|
|
subparsers.required = True
|
2020-10-18 23:42:54 +00:00
|
|
|
parser_run = subparsers.add_parser("run", help="run flaschengeist")
|
2020-10-15 16:11:27 +00:00
|
|
|
parser_run.set_defaults(func=run)
|
|
|
|
parser_run.add_argument("--host", help="set hostname to listen on", default="127.0.0.1")
|
|
|
|
parser_run.add_argument("--port", help="set port to listen on", type=int, default=5000)
|
|
|
|
parser_run.add_argument("--debug", help="run in debug mode", action="store_true")
|
2020-10-18 23:42:54 +00:00
|
|
|
parser_install = subparsers.add_parser(
|
|
|
|
"install", help="run database setup for flaschengeist and all installed plugins"
|
|
|
|
)
|
2020-10-15 16:11:27 +00:00
|
|
|
parser_install.set_defaults(func=install)
|
2020-10-18 23:42:54 +00:00
|
|
|
parser_export = subparsers.add_parser("export", help="export models to typescript interfaces")
|
|
|
|
parser_export.set_defaults(func=export)
|
|
|
|
parser_export.add_argument("--file", help="Filename where to save", default="flaschengeist.d.ts")
|
2020-10-24 18:11:50 +00:00
|
|
|
parser_export.add_argument("--namespace", help="Namespace of declarations", default="FG")
|
2020-10-18 23:42:54 +00:00
|
|
|
|
2020-10-15 16:11:27 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
args.func(args)
|