wrap bot.py in a main() function to be import-safe

This commit is contained in:
Ryan Hitchman 2015-01-21 23:16:27 -08:00
parent 62fbe6e237
commit 921b986683
1 changed files with 35 additions and 31 deletions

32
bot.py
View File

@ -6,10 +6,6 @@ import sys
import traceback import traceback
import time import time
sys.path += ['plugins'] # so 'import hook' works without duplication
sys.path += ['lib']
os.chdir(sys.path[0] or '.') # do stuff relative to the install directory
class Bot(object): class Bot(object):
def __init__(self): def __init__(self):
@ -18,29 +14,34 @@ class Bot(object):
if not os.path.exists(self.persist_dir): if not os.path.exists(self.persist_dir):
os.mkdir(self.persist_dir) os.mkdir(self.persist_dir)
bot = Bot() def main():
sys.path += ['plugins'] # so 'import hook' works without duplication
sys.path += ['lib']
os.chdir(sys.path[0] or '.') # do stuff relative to the install directory
print 'Loading plugins' bot = Bot()
# bootstrap the reloader print 'Loading plugins'
eval(compile(open(os.path.join('core', 'reload.py'), 'U').read(),
# bootstrap the reloader
eval(compile(open(os.path.join('core', 'reload.py'), 'U').read(),
os.path.join('core', 'reload.py'), 'exec')) os.path.join('core', 'reload.py'), 'exec'))
reload(init=True) reload(init=True)
print 'Connecting to IRC' print 'Connecting to IRC'
try: try:
config() config()
if not hasattr(bot, 'config'): if not hasattr(bot, 'config'):
exit() exit()
except Exception, e: except Exception, e:
print 'ERROR: malformed config file:', e print 'ERROR: malformed config file:', e
traceback.print_exc() traceback.print_exc()
sys.exit() sys.exit()
print 'Running main loop' print 'Running main loop'
while True: while True:
reload() # these functions only do things reload() # these functions only do things
config() # if changes have occured config() # if changes have occured
@ -52,3 +53,6 @@ while True:
pass pass
while all(conn.out.empty() for conn in bot.conns.itervalues()): while all(conn.out.empty() for conn in bot.conns.itervalues()):
time.sleep(.1) time.sleep(.1)
if __name__ == '__main__':
main()