-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheb_update.py
60 lines (45 loc) · 1.94 KB
/
eb_update.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"Makes it possible to update code without restarting."
import imp, sys
class Update(object):
def __init__(self):
self.modules = {}
self.objects = {}
def request_obj(self, module_path, class_name, args=None):
"Load and return requested object, if not already cached."
# Remove trailing .py, if present
if module_path[-3:] == ".py":
module_path = module_path[:-3]
# Check if the module has been loaded
if module_path not in self.modules:
# todo: handle exceptions here
# The module hasn't been loaded yet. Load it now.
mod = __import__(module_path)
self.modules[module_path] = mod
self.objects[module_path] = {}
else:
# Module already loaded. Retrieve it from cache.
mod = self.modules[module_path]
# Check if the object is loaded.
if class_name not in self.objects[module_path]:
# The object is not loaded. Load it now.
if args:
class_path = "mod.%s.%s(args)" % (module_path, class_name)
print("1")
else:
class_path = "mod.%s.%s()" % (module_path, class_name)
print("module_path: %s" % module_path)
print("class_name : %s" % class_name)
print("Evaluating: '%s'" % class_path)
self.objects[module_path][class_name] = eval(class_path)
return self.objects[module_path][class_name]
def reload_module(self, module_path):
"Reload a module that was changed since last time it was loaded."
print("[Update] Reloading %s." % module_path)
if not module_path in self.modules:
print("[Update] Module not loaded.")
print("Modules: %s" % self.modules)
return False
imp.reload(sys.modules[module_path])
self.objects[module_path] = { }
return True
update = Update()