From ecb8af1bbea0dee31903b1e962c0b731f2d36318 Mon Sep 17 00:00:00 2001 From: "Sean B. Palmer" Date: Thu, 24 Feb 2011 18:57:21 +0000 Subject: [PATCH] Better reload function. --- modules/reload.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/modules/reload.py b/modules/reload.py index fc508a6..50e1a56 100755 --- a/modules/reload.py +++ b/modules/reload.py @@ -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()