| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2020-11-02 03:38:38 +00:00
										 |  |  | import pkg_resources | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class InterfaceGenerator: | 
					
						
							|  |  |  |     known = [] | 
					
						
							|  |  |  |     classes = {} | 
					
						
							|  |  |  |     mapper = {"str": "string", "int": "number", "float": "number", "datetime": "Date"} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __init__(self, namespace, filename): | 
					
						
							|  |  |  |         self.basename = "" | 
					
						
							|  |  |  |         self.namespace = namespace | 
					
						
							|  |  |  |         self.filename = filename | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def pytype(self, cls): | 
					
						
							|  |  |  |         if isinstance(cls, list): | 
					
						
							|  |  |  |             return "", "Array<{}>".format(self.pytype(cls[0])[1]) | 
					
						
							|  |  |  |         # if typing.get_origin(cls) is typing.Optional: | 
					
						
							|  |  |  |         #    return "?", pytype(typing.get_args(cls)[1]) | 
					
						
							|  |  |  |         if hasattr(cls, "__name__"): | 
					
						
							|  |  |  |             if cls.__name__ in self.mapper: | 
					
						
							|  |  |  |                 return "", self.mapper[cls.__name__] | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 return "", cls.__name__ | 
					
						
							|  |  |  |         return "?", "any" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def walker(self, module): | 
					
						
							|  |  |  |         if inspect.ismodule(module[1]) and module[1].__name__.startswith(self.basename) and module[1].__name__ not in self.known: | 
					
						
							|  |  |  |             self.known.append(module[1].__name__) | 
					
						
							|  |  |  |             for cls in inspect.getmembers(module[1], lambda x: inspect.isclass(x) or inspect.ismodule(x)): | 
					
						
							|  |  |  |                 self.walker(cls) | 
					
						
							|  |  |  |         elif ( | 
					
						
							|  |  |  |                 inspect.isclass(module[1]) | 
					
						
							|  |  |  |                 and module[1].__module__.startswith(self.basename) | 
					
						
							|  |  |  |                 and module[0] not in self.classes | 
					
						
							|  |  |  |                 and not module[0].startswith("_") | 
					
						
							|  |  |  |                 and hasattr(module[1], "__annotations__") | 
					
						
							|  |  |  |         ): | 
					
						
							|  |  |  |             d = { | 
					
						
							|  |  |  |                 param: self.pytype(ptype) | 
					
						
							|  |  |  |                 for param, ptype in module[1].__annotations__.items() | 
					
						
							|  |  |  |                 if not param.startswith("_") and not param.endswith("_") | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if len(d) == 1: | 
					
						
							|  |  |  |                 key, value = d.popitem() | 
					
						
							|  |  |  |                 self.classes[module[0]] = value[1] | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 self.classes[module[0]] = d | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def run(self, models): | 
					
						
							|  |  |  |         self.basename = models.__name__ | 
					
						
							|  |  |  |         self.walker(("models", models)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def write(self): | 
					
						
							|  |  |  |         with open(self.filename, "w") as file: | 
					
						
							|  |  |  |             file.write("declare namespace {} {{\n".format(self.namespace)) | 
					
						
							|  |  |  |             for cls, params in self.classes.items(): | 
					
						
							|  |  |  |                 if isinstance(params, str): | 
					
						
							|  |  |  |                     file.write("\ttype {} = {};\n".format(cls, params)) | 
					
						
							|  |  |  |                 else: | 
					
						
							|  |  |  |                     file.write("\tinterface {} {{\n".format(cls)) | 
					
						
							|  |  |  |                     for name in params: | 
					
						
							|  |  |  |                         file.write("\t\t{}{}: {};\n".format(name, *params[name])) | 
					
						
							|  |  |  |                     file.write("\t}\n") | 
					
						
							|  |  |  |             file.write("}\n") | 
					
						
							| 
									
										
										
										
											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-18 23:42:54 +00:00
										 |  |  |     from flaschengeist.app import create_app | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     app = create_app() | 
					
						
							|  |  |  |     with app.app_context(): | 
					
						
							| 
									
										
										
										
											2020-11-02 03:38:38 +00:00
										 |  |  |         gen = InterfaceGenerator(arguments.namespace, arguments.file) | 
					
						
							|  |  |  |         gen.run(models) | 
					
						
							|  |  |  |         if arguments.plugins: | 
					
						
							|  |  |  |             for entry_point in pkg_resources.iter_entry_points("flaschengeist.plugin"): | 
					
						
							|  |  |  |                 plg = entry_point.load() | 
					
						
							|  |  |  |                 if hasattr(plg, "models"): | 
					
						
							|  |  |  |                     gen.run(plg.models) | 
					
						
							|  |  |  |         gen.write() | 
					
						
							| 
									
										
										
										
											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-11-02 03:38:38 +00:00
										 |  |  |     parser_export.add_argument("--plugins", help="Also export plugins", action="store_true") | 
					
						
							| 
									
										
										
										
											2020-10-18 23:42:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-15 16:11:27 +00:00
										 |  |  |     args = parser.parse_args() | 
					
						
							|  |  |  |     args.func(args) |