1
- import pkgutil
2
- import os
3
- import inspect
1
+ from package_extractor import PackageExtractor
4
2
5
3
"""
6
4
Description:: Initialize the blueprints inside in the root folder
@@ -26,115 +24,4 @@ def __init__(self, app, root_path):
26
24
self .root_path = root_path
27
25
28
26
""" register blueprint to the current path """
29
- self .add_blueprint (root_path )
30
-
31
- def directory_path (self , path ):
32
-
33
- """ get all the list of files and directories """
34
- for file in os .listdir (path ):
35
-
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
40
-
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 )
47
-
48
- """ find sub directories on each directory found """
49
- self .directory_path (path = dir_file )
50
-
51
- @staticmethod
52
- def blueprint_name (name ):
53
- """ set index automatically as home page """
54
- if "index" in name :
55
- name = str (name ).replace ("index" , "" )
56
- if "routes" in name :
57
- name = str (name ).replace ("routes" , "" )
58
-
59
- """ remove the last . in the string it it ends with a .
60
- for the url structure must follow the flask routing format
61
- it should be /model/method instead of /model/method/
62
- """
63
- if name [- 1 :] == "." :
64
- name = name [:- 1 ]
65
- http_name = str (name ).replace ("." , "/" )
66
- print (http_name )
67
- return http_name
68
-
69
- @staticmethod
70
- def get_http_methods (names ):
71
- if isinstance (names , list ):
72
- methods = []
73
-
74
- for name in names :
75
- if "__" not in name :
76
- if name == "index" :
77
- methods .append ('GET' )
78
- elif name == "create" :
79
- methods .append ('POST' )
80
- elif name == "update" :
81
- methods .append ('PUT' )
82
- elif name == "destroy" :
83
- methods .append ('DELETE' )
84
- else :
85
- methods .append ('GET' )
86
-
87
- return methods
88
- else :
89
- raise TypeError ("names must be a list" )
90
-
91
- def model_add_router (self , mod ):
92
- if hasattr (mod , '__routes__' ):
93
- for route in mod .__routes__ :
94
- if inspect .isclass (route [2 ]):
95
- """ If it's a class it needs to extract the methods by function names
96
- magic functions are excluded
97
- """
98
- 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." )
108
-
109
- elif inspect .isfunction (route [2 ]):
110
- route_name , slug , fn , methods = route
111
-
112
- mod .__method__ .add_url_rule (
113
- rule = slug ,
114
- endpoint = fn .__name__ ,
115
- view_func = fn ,
116
- methods = methods )
117
-
118
- @staticmethod
119
- def get_cls_fn_members (cls ):
120
- return [member for member in inspect .getmembers (cls , predicate = inspect .isfunction )]
121
-
122
- def add_blueprint (self , path ):
123
-
124
- """ 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
27
+ PackageExtractor (application = app , path = root_path )
0 commit comments