Skip to content

Commit

Permalink
Unregister and unbind modules from willie.bot when they are reloaded.
Browse files Browse the repository at this point in the history
-Adds methods unregister and is_callable to willie.bot.
-Adds code required to remove references to willie callables, in modules
 themselves and willie, to willie.modules.reload.
  • Loading branch information
ari-koivula committed May 19, 2013
1 parent e9d9ccc commit 36ca482
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion reload.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,22 @@ def f_reload(willie, trigger):
if not name in sys.modules:
return willie.reply('%s: no such module!' % name)

old_module = sys.modules[name]

old_callables = {}
for obj_name, obj in vars(old_module).iteritems():
if willie.is_callable(obj):
old_callables[obj_name] = obj

willie.unregister(old_callables)
# Also remove all references to willie callables from top level of the
# module, so that they will not get loaded again if reloading the
# module does not override them.
for obj_name in old_callables.keys():
delattr(old_module, obj_name)

# Thanks to moot for prodding me on this
path = sys.modules[name].__file__
path = old_module.__file__
if path.endswith('.pyc') or path.endswith('.pyo'):
path = path[:-1]
if not os.path.isfile(path):
Expand Down

0 comments on commit 36ca482

Please sign in to comment.