diff --git a/flask_blueprint/__init__.py b/flask_blueprint/__init__.py index 2545c4c..6b7e126 100644 --- a/flask_blueprint/__init__.py +++ b/flask_blueprint/__init__.py @@ -14,7 +14,7 @@ """ __all__ = ['Core', '__version__'] -__version__ = '1.2.7' +__version__ = '1.2.8' """ diff --git a/flask_blueprint/module_router.py b/flask_blueprint/module_router.py index 89c3ed6..d7e30d5 100644 --- a/flask_blueprint/module_router.py +++ b/flask_blueprint/module_router.py @@ -9,24 +9,6 @@ class ErrorMessage(Enum): MEMBER_NOT_A_FUNCTION = 'Member is not a function.' -class RoutingType(Enum): - CLS = 'cls' - FN = 'fn' - - -class MethodType(Enum): - GET = 'GET' - POST = 'POST' - PUT = 'PUT' - DELETE = 'DELETE' - PATCH = 'PATCH' - - -class ModuleType(Enum): - ROUTES = '__routes__' - METHOD = '__method__' - - class PreDefinedHttpMethod(Enum): INDEX = 'index' CREATE = 'create' @@ -47,16 +29,16 @@ def __init__(self, mod, **kwargs): # module self.model_add_router() def model_add_router(self): - if hasattr(self._module, '__routes__') and len(self._module.__routes__): + if hasattr(self._module, "__routes__") and len(self._module.__routes__): route_type, route_data = self._routing_type(route=self._module.__routes__.pop(0)) - if route_type == RoutingType.CLS: + if route_type == 'cls': """ If it's a class it needs to extract the methods by function names magic functions are excluded """ route_name, slug, cls = route_data self.class_member_route(route=route_data, members=self.get_cls_fn_members(cls)) - elif route_type == RoutingType.FN: + elif route_type == 'fn': route_name, slug, fn, methods = route_data self.__routers.append(route_data) self._module.__method__.add_url_rule( @@ -67,17 +49,17 @@ def model_add_router(self): self.model_add_router() def _is_valid_module(self): - return hasattr(self._module, str(ModuleType.ROUTES)) or hasattr(self._module, str(ModuleType.METHOD)) + return hasattr(self._module, "__routes__") or hasattr(self._module, "__method__") @staticmethod def _routing_type(route): __type = None if isinstance(route, tuple): if len(route) == 3 and inspect.isclass(route[2]): - __type = RoutingType.CLS + __type = 'cls' elif len(route) == 4 and inspect.isfunction(route[2]): if isinstance(route[3], (list, tuple, set)): - __type = RoutingType.FN + __type = 'fn' else: raise TypeError(ErrorMessage.METHOD_NOT_A_LIST) else: @@ -92,15 +74,15 @@ def get_http_methods(names): for name in names: if "__" not in name: if name == PreDefinedHttpMethod.INDEX: - methods.append(MethodType.GET) + methods.append('GET') elif name == PreDefinedHttpMethod.CREATE: - methods.append(MethodType.POST) + methods.append('POST') elif name == PreDefinedHttpMethod.UPDATE: - methods.append(MethodType.PUT) + methods.append('PUT') elif name == PreDefinedHttpMethod.DESTROY: - methods.append(MethodType.DELETE) + methods.append('DELETE') else: - methods.append(MethodType.GET) + methods.append('GET') return methods else: @@ -153,6 +135,8 @@ def blueprint_name_to_url(name): """ if name[-1:] == ".": name = name[:-1] + if name[-1:] is not "/": + name = "/" + name name = str(name).replace(".", "/") return name diff --git a/flask_blueprint/package_extractor.py b/flask_blueprint/package_extractor.py index f05acfd..1d7e729 100644 --- a/flask_blueprint/package_extractor.py +++ b/flask_blueprint/package_extractor.py @@ -1,16 +1,13 @@ import inspect import pkgutil -from os import listdir from .module_router import ModuleRouter """ PACKAGE EXTRACTOR - It's purpose is to extract all the modules based on the paths of the parameter given it extracts valid packages within the root module path and register the valid modules to the flask blueprint - :param application flask application object example: flask_app = Flask(import_name, instance_relative_config=True) @@ -33,19 +30,14 @@ def __init__(self, application, paths): self.__iter_paths() def __iter_paths(self): - for path in self.paths: - self.__iter_path_sub_path(sub_paths=listdir(path)) - - def __iter_path_sub_path(self, sub_paths): - for path_directories in sub_paths: - self.__extract_packages(packages=pkgutil.walk_packages(path_directories, prefix='', onerror=None)) + self.__extract_packages(packages=pkgutil.walk_packages(self.paths, prefix='', onerror=None)) def __extract_packages(self, packages): if inspect.isgenerator(packages): try: loader, name, is_pkg = next(packages) - self.__extract_packages(packages) self.__extract_modules(loader, name, is_pkg) + self.__extract_packages(packages) except StopIteration: pass else: