@@ -29,24 +29,30 @@ def __init__(self, app, root_path):
2929 self .add_blueprint (root_path )
3030
3131 def directory_path (self , path ):
32-
3332 """ get all the list of files and directories """
34- for file in os .listdir (path ):
33+ self .scan_directory_for_module (root = path , files = os .listdir (path ))
34+
35+ def scan_directory_for_module (self , root , files ):
36+ if isinstance (files , list ):
37+ if len (files ):
38+ file = files .pop (0 )
39+
40+ """ prevent __pycache__ directory or any directory that has __ """
41+ if "__" not in file :
42+ """ get the full path directory """
43+ dir_file = root + '/' + file
3544
36- """ prevent __pycache__ directory or any directory that has __ """
37- if "__" not in file :
38- """ get the full path directory """
39- dir_file = path + '/' + file
45+ """ check is the path is a directory
46+ only directories are picked
47+ """
48+ if os .path .isdir (dir_file ):
49+ """ register blueprint on the directory """
50+ self .add_blueprint (dir_file )
4051
41- """ check is the path is a directory
42- only directories are picked
43- """
44- if os .path .isdir (dir_file ):
45- """ register blueprint on the directory """
46- self .add_blueprint (dir_file )
52+ """ find sub directories on each directory found """
53+ self .directory_path (path = dir_file )
4754
48- """ find sub directories on each directory found """
49- self .directory_path (path = dir_file )
55+ self .scan_directory_for_module (root , files )
5056
5157 @staticmethod
5258 def blueprint_name (name ):
@@ -63,7 +69,6 @@ def blueprint_name(name):
6369 if name [- 1 :] == "." :
6470 name = name [:- 1 ]
6571 http_name = str (name ).replace ("." , "/" )
66- print (http_name )
6772 return http_name
6873
6974 @staticmethod
@@ -88,23 +93,16 @@ def get_http_methods(names):
8893 else :
8994 raise TypeError ("names must be a list" )
9095
91- def model_add_router (self , mod ):
96+ def model_add_router (self , mod ): # mod --> module
9297 if hasattr (mod , '__routes__' ):
93- for route in mod .__routes__ :
98+ if len (mod .__routes__ ):
99+ route = mod .__routes__ .pop (0 )
94100 if inspect .isclass (route [2 ]):
95101 """ If it's a class it needs to extract the methods by function names
96102 magic functions are excluded
97103 """
98104 route_name , slug , cls = route
99- for (fn_name , fn_object ) in self .get_cls_fn_members (cls ):
100- if inspect .isfunction (fn_object ):
101- mod .__method__ .add_url_rule (
102- rule = slug ,
103- endpoint = fn_name ,
104- view_func = fn_object ,
105- methods = self .get_http_methods ([fn_name ]))
106- else :
107- raise KeyError ("Member is not a function." )
105+ self .class_member_add_router (mod .__method__ , route , members = self .get_cls_fn_members (cls ))
108106
109107 elif inspect .isfunction (route [2 ]):
110108 route_name , slug , fn , methods = route
@@ -114,6 +112,24 @@ def model_add_router(self, mod):
114112 endpoint = fn .__name__ ,
115113 view_func = fn ,
116114 methods = methods )
115+ self .model_add_router (mod )
116+
117+ def class_member_add_router (self , method , route , members ):
118+ if isinstance (members , (list , set )):
119+ if len (members ):
120+ (fn_name , fn_object ) = members .pop (0 )
121+ route_name , slug , cls = route
122+ if inspect .isfunction (fn_object ):
123+ method .add_url_rule (
124+ rule = slug ,
125+ endpoint = fn_name ,
126+ view_func = fn_object ,
127+ methods = self .get_http_methods ([fn_name ]))
128+ else :
129+ raise KeyError ("Member is not a function." )
130+ self .class_member_add_router (method , route , members )
131+ else :
132+ raise TypeError ("members must be a list." )
117133
118134 @staticmethod
119135 def get_cls_fn_members (cls ):
@@ -122,19 +138,30 @@ def get_cls_fn_members(cls):
122138 def add_blueprint (self , path ):
123139
124140 """ find all packages in the current path """
125- for loader , name , is_pkg in pkgutil .walk_packages (path , prefix = "" , onerror = None ):
126- """ if module found load module and save all attributes in the module found """
127- mod = loader .find_module (name ).load_module (name )
128-
129- """ find the attribute method on each module """
130- if hasattr (mod , '__method__' ):
131- self .model_add_router (mod )
132- root_module = self .root_path .replace ("." , "" )
133- url_prefix_name = str (name ).replace (root_module , "" )
134- """ register to the blueprint if method attribute found """
135- self .__app .register_blueprint (mod .__method__ , url_prefix = self .blueprint_name (url_prefix_name ))
136-
137- else :
138- """ prompt not found notification """
139- # print('{} has no module attribute method'.format(mod))
140- pass
141+ self .extract_packages (packages = pkgutil .walk_packages (path , prefix = "" , onerror = None ))
142+
143+ def extract_packages (self , packages ):
144+ if inspect .isgenerator (packages ):
145+ packages = [package for package in packages ]
146+
147+ if isinstance (packages , (list , set )):
148+ if len (packages ):
149+ loader , name , is_pkg = packages .pop (0 )
150+ """ if module found load module and save all attributes in the module found """
151+ mod = loader .find_module (name ).load_module (name )
152+
153+ """ find the attribute method on each module """
154+ if hasattr (mod , '__method__' ):
155+ self .model_add_router (mod )
156+ root_module = self .root_path .replace ("." , "" )
157+ url_prefix_name = str (name ).replace (root_module , "" )
158+ """ register to the blueprint if method attribute found """
159+ self .__app .register_blueprint (mod .__method__ , url_prefix = self .blueprint_name (url_prefix_name ))
160+
161+ else :
162+ """ prompt not found notification """
163+ # print('{} has no module attribute method'.format(mod))
164+ pass
165+ self .extract_packages (packages )
166+ else :
167+ raise TypeError ("Packages must be a list" )
0 commit comments