1
- import importlib ,pdb ,sys ,traceback
1
+ import importlib ,pdb ,sys ,traceback , inspect
2
2
3
3
class LiveObject :
4
4
instances = []
@@ -27,35 +27,55 @@ def live_classes(self):
27
27
28
28
def store_old_live_classes (self ):
29
29
live_classes = {}
30
+ print ("start storing old live classes............" )
30
31
for module in self .live_modules :
31
- live_module_name_list = [mod for mod in sys .modules .keys () if module in mod ]
32
+ live_module_name_list = [mod for mod in sys .modules .keys () if mod . endswith ( module ) ]
32
33
live_module_name = live_module_name_list [0 ]
33
34
live_classes [module ] = []
34
- if hasattr (sys .modules [live_module_name ],"live_environment" ):
35
- sys .modules [live_module_name ].live_environment .update ()
35
+ print ("\n checking module: " , live_module_name )
36
+ for mod in live_module_name_list :
37
+
38
+ if hasattr (sys .modules [mod ],"live_environment" ):
39
+ #print("updating module",mod)
40
+ sys .modules [mod ].live_environment .update ()
36
41
for old_obj in sys .modules [live_module_name ].__dict__ .values ():
37
- if old_obj .__class__ .__name__ == 'type' and hasattr (old_obj .__class__ ,"instances" ):
42
+ #print("checking {} for class definitions using value ".format(live_module_name,old_obj))
43
+ if inspect .isclass (old_obj ) and hasattr (old_obj ,"instances" ):
38
44
live_classes [module ].append (old_obj )
45
+ print ("appended {} to the list with type {}" .format (old_obj , type (old_obj )))
39
46
self ._live_classes = live_classes
47
+ print ("\n live_classes : {} \n " .format (self ._live_classes ))
40
48
return live_classes
41
49
42
50
def update (self ):
51
+ print ("\n ***** START CODE RELOAD ******\n " )
43
52
self .store_old_live_classes ()
44
53
for live_module in self .live_classes : #the name of the module is used as a key for the dict that stores all
54
+ print ("iterating through " ,live_module )
45
55
# live classes
46
- live_module_name_list = [mod for mod in sys .modules .keys () if live_module in mod ]
56
+
57
+ live_module_name_list = [mod for mod in sys .modules .keys () if mod .endswith (live_module )]
47
58
live_module_name = live_module_name_list [0 ]
48
- #pdb.set_trace( )
49
- new_live_class = 0
59
+ #print("code reload for module: ", live_module_name )
60
+
50
61
live_classes_ids = [id (cl ) for cl in self .live_classes [live_module ]]
62
+ #print("code reload for module: ", live_module_name)
63
+
51
64
importlib .reload (sys .modules [live_module_name ])
52
65
live_classes_ids = [id (cl ) for cl in self .live_classes [live_module ]]
53
66
67
+ #if live_module == "cyclops.morpheas":
68
+ #pdb.set_trace()
54
69
for live_class in self .live_classes [live_module ]:
70
+ print ("iterate class : {} with instances: {}" .format (live_class ,live_class .instances ))
55
71
new_live_class = eval ("sys.modules[live_module_name]." + live_class .__name__ )
56
72
57
73
for live_instance in live_class .instances :
74
+ print ("iterate instance :" ,live_instance )
58
75
backup_instances = live_instance .__class__ .instances
59
76
live_instance .__class__ = new_live_class
77
+ print ("reloading instance \n instance : {} id: {} \n old class: {} id: {} \n new class : {} id: {"
78
+ "} \n "
79
+ "" .format (live_instance ,id (live_instance ),live_class ,id (live_class ),new_live_class ,id (new_live_class )))
60
80
live_instance .__class__ .instances = backup_instances
61
- live_instance = 0
81
+ print ( " \n **********END CODE RELOAD************ \n " )
0 commit comments