Skip to content

Commit 497d374

Browse files
committed
recursively reloading package
1 parent debe469 commit 497d374

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

app.py

+28-1
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,40 @@ def jsonify(data, status_code=200):
3737
status=status_code)
3838

3939

40+
import os
41+
import types
42+
import importlib
43+
44+
45+
def reload_package(package):
46+
assert(hasattr(package, "__package__"))
47+
fn = package.__file__
48+
fn_dir = os.path.dirname(fn) + os.sep
49+
module_visit = {fn}
50+
del fn
51+
52+
def reload_recursive_ex(module):
53+
importlib.reload(module)
54+
55+
for module_child in vars(module).values():
56+
if isinstance(module_child, types.ModuleType):
57+
fn_child = getattr(module_child, "__file__", None)
58+
if (fn_child is not None) and fn_child.startswith(fn_dir):
59+
if fn_child not in module_visit:
60+
# print("reloading:", fn_child, "from", module)
61+
module_visit.add(fn_child)
62+
reload_recursive_ex(module_child)
63+
64+
return reload_recursive_ex(package)
65+
66+
4067
def load(directory, module, handler_path):
4168
file_path = path.join(directory, module)
4269
file_directory = path.dirname(file_path)
4370
sys.path.append(file_directory)
4471
function_file, function_name = path.splitext(handler_path)
4572
mod = importlib.import_module(function_file)
46-
importlib.reload(mod)
73+
reload_package(mod)
4774
func = getattr(mod, function_name.strip('.'))
4875
return func
4976

0 commit comments

Comments
 (0)