Skip to content

Commit

Permalink
bugfix: string literals
Browse files Browse the repository at this point in the history
  • Loading branch information
aiscenblue committed Jun 26, 2019
1 parent c558d9d commit d951ba8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 40 deletions.
2 changes: 1 addition & 1 deletion flask_blueprint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"""

__all__ = ['Core', '__version__']
__version__ = '1.2.7'
__version__ = '1.2.8'


"""
Expand Down
42 changes: 13 additions & 29 deletions flask_blueprint/module_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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(
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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

Expand Down
12 changes: 2 additions & 10 deletions flask_blueprint/package_extractor.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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:
Expand Down

0 comments on commit d951ba8

Please sign in to comment.