|
| 1 | +import inspect |
| 2 | + |
| 3 | + |
| 4 | +class ModuleRouter: |
| 5 | + def __init__(self, mod): # module |
| 6 | + self._module = mod |
| 7 | + if self._is_valid_module(): |
| 8 | + self.model_add_router() |
| 9 | + |
| 10 | + def model_add_router(self): |
| 11 | + if hasattr(self._module, '__routes__') and len(self._module.__routes__): |
| 12 | + route_type, route_data = self._routing_type(route=self._module.__routes__.pop(0)) |
| 13 | + if route_type == 'cls': |
| 14 | + """ If it's a class it needs to extract the methods by function names |
| 15 | + magic functions are excluded |
| 16 | + """ |
| 17 | + route_name, slug, cls = route_data |
| 18 | + self.class_member_route(route=route_data, members=self.get_cls_fn_members(cls)) |
| 19 | + |
| 20 | + elif route_type == 'fn': |
| 21 | + route_name, slug, fn, methods = route_data |
| 22 | + |
| 23 | + self._module.__method__.add_url_rule( |
| 24 | + rule=slug, |
| 25 | + endpoint=fn.__name__, |
| 26 | + view_func=fn, |
| 27 | + methods=methods) |
| 28 | + self.model_add_router() |
| 29 | + |
| 30 | + def _is_valid_module(self): |
| 31 | + return hasattr(self._module, '__routes__') or hasattr(self._module, '__method__') |
| 32 | + |
| 33 | + @staticmethod |
| 34 | + def _routing_type(route): |
| 35 | + __type = None |
| 36 | + if isinstance(route, tuple): |
| 37 | + if len(route) == 3 and inspect.isclass(route[2]): |
| 38 | + __type = 'cls' |
| 39 | + elif len(route) == 4 and inspect.isfunction(route[2]): |
| 40 | + if isinstance(route[3], (list, tuple, set)): |
| 41 | + __type = 'fn' |
| 42 | + else: |
| 43 | + raise TypeError("methods must be a list.") |
| 44 | + else: |
| 45 | + raise TypeError("Invalid route syntax.") |
| 46 | + return __type, route |
| 47 | + |
| 48 | + @staticmethod |
| 49 | + def get_http_methods(names): |
| 50 | + if isinstance(names, list): |
| 51 | + methods = [] |
| 52 | + |
| 53 | + for name in names: |
| 54 | + if "__" not in name: |
| 55 | + if name == "index": |
| 56 | + methods.append('GET') |
| 57 | + elif name == "create": |
| 58 | + methods.append('POST') |
| 59 | + elif name == "update": |
| 60 | + methods.append('PUT') |
| 61 | + elif name == "destroy": |
| 62 | + methods.append('DELETE') |
| 63 | + else: |
| 64 | + methods.append('GET') |
| 65 | + |
| 66 | + return methods |
| 67 | + else: |
| 68 | + raise TypeError("names must be a list") |
| 69 | + |
| 70 | + @staticmethod |
| 71 | + def get_cls_fn_members(cls): |
| 72 | + return [member for member in inspect.getmembers(cls, predicate=inspect.isfunction)] |
| 73 | + |
| 74 | + def class_member_route(self, route, members): |
| 75 | + if isinstance(members, (list, set)): |
| 76 | + if len(members): |
| 77 | + (fn_name, fn_object) = members.pop(0) |
| 78 | + route_name, slug, cls = route |
| 79 | + if inspect.isfunction(fn_object): |
| 80 | + self._module.__method__.add_url_rule( |
| 81 | + rule=slug, |
| 82 | + endpoint=fn_name, |
| 83 | + view_func=fn_object, |
| 84 | + methods=self.get_http_methods([fn_name])) |
| 85 | + else: |
| 86 | + raise KeyError("Member is not a function.") |
| 87 | + self.class_member_route(route, members) |
| 88 | + else: |
| 89 | + raise TypeError("members must be a list.") |
| 90 | + |
| 91 | + def register_route(self, app, name): |
| 92 | + app.register_blueprint(self._module.__method__, url_prefix=self.blueprint_name(name)) |
| 93 | + |
| 94 | + @staticmethod |
| 95 | + def blueprint_name(name): |
| 96 | + |
| 97 | + root_module = name.replace(".", "") |
| 98 | + |
| 99 | + name = str(name).replace(root_module, "") |
| 100 | + """ set index automatically as home page """ |
| 101 | + if "index" in name: |
| 102 | + name = str(name).replace("index", "") |
| 103 | + if "routes" in name: |
| 104 | + name = str(name).replace("routes", "") |
| 105 | + if "module" in name: |
| 106 | + name = str(name).replace("module", "") |
| 107 | + |
| 108 | + """ remove the last . in the string it it ends with a . |
| 109 | + for the url structure must follow the flask routing format |
| 110 | + it should be /model/method instead of /model/method/ |
| 111 | + """ |
| 112 | + if name[-1:] == ".": |
| 113 | + name = name[:-1] |
| 114 | + http_name = str(name).replace(".", "/") |
| 115 | + |
| 116 | + return http_name |
| 117 | + |
0 commit comments