Skip to content

Commit

Permalink
added: raise type error if not a package, added 2 methods that iterat…
Browse files Browse the repository at this point in the history
…es sub directories inside the module
  • Loading branch information
aiscenblue committed Oct 19, 2018
1 parent 764440c commit bf80eb8
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions flask_blueprint/package_extractor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import inspect
import pkgutil
from os import listdir

from .module_router import ModuleRouter

Expand Down Expand Up @@ -29,16 +30,26 @@ class PackageExtractor:
def __init__(self, application, paths):
self.path = paths
self.application = application
self.__extract_packages(packages=pkgutil.walk_packages(paths, prefix="", onerror=None))
self.__iter_paths()

def __iter_paths(self):
for path in self.path:
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))

def __extract_packages(self, packages):
if inspect.isgenerator(packages):
try:
loader, name, is_pkg = next(packages)
self.__extract_modules(loader, name, is_pkg)
self.__extract_packages(packages)
self.__extract_modules(loader, name, is_pkg)
except StopIteration:
pass
else:
raise TypeError("package should be a generator type")

""" extract modules from the package"""

Expand All @@ -52,6 +63,7 @@ def __extract_modules(self, loader, name, is_pkg):

""" register to the blueprint if method attribute found """
module_router = ModuleRouter(mod).register_route(app=self.application, name=name)

self.__routers.extend(module_router.routers)
self.__modules.append(mod)

Expand Down

0 comments on commit bf80eb8

Please sign in to comment.