Better reload function.

master
Sean B. Palmer 2011-02-24 18:57:21 +00:00
parent 22dfed082c
commit ecb8af1bbe
1 changed files with 15 additions and 9 deletions

View File

@ -7,6 +7,7 @@ Licensed under the Eiffel Forum License 2.
http://inamidst.com/phenny/
"""
import sys, os.path, time, imp
import irc
def f_reload(phenny, input):
@ -21,18 +22,23 @@ def f_reload(phenny, input):
phenny.setup()
return phenny.reply('done')
try: module = getattr(__import__('modules.' + name), name)
except ImportError:
module = getattr(__import__('opt.' + name), name)
reload(module)
if not sys.modules.has_key(name):
return phenny.reply('%s: no such module!' % name)
# Thanks to moot for prodding me on this
path = sys.modules[name].__file__
if path.endswith('.pyc') or path.endswith('.pyo'):
path = path[:-1]
if not os.path.isfile(path):
return phenny.reply('Found %s, but not the source file' % name)
module = imp.load_source(name, path)
sys.modules[name] = module
if hasattr(module, 'setup'):
module.setup(phenny)
if hasattr(module, '__file__'):
import os.path, time
mtime = os.path.getmtime(module.__file__)
modified = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(mtime))
else: modified = 'unknown'
mtime = os.path.getmtime(module.__file__)
modified = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(mtime))
phenny.register(vars(module))
phenny.bind_commands()