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

66
bot.py
View File

@ -6,10 +6,6 @@ import sys
import traceback
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):
def __init__(self):
@ -18,37 +14,45 @@ class Bot(object):
if not os.path.exists(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
eval(compile(open(os.path.join('core', 'reload.py'), 'U').read(),
os.path.join('core', 'reload.py'), 'exec'))
reload(init=True)
print 'Loading plugins'
print 'Connecting to IRC'
# bootstrap the reloader
eval(compile(open(os.path.join('core', 'reload.py'), 'U').read(),
os.path.join('core', 'reload.py'), 'exec'))
reload(init=True)
try:
config()
if not hasattr(bot, 'config'):
exit()
except Exception, e:
print 'ERROR: malformed config file:', e
traceback.print_exc()
sys.exit()
print 'Connecting to IRC'
print 'Running main loop'
try:
config()
if not hasattr(bot, 'config'):
exit()
except Exception, e:
print 'ERROR: malformed config file:', e
traceback.print_exc()
sys.exit()
while True:
reload() # these functions only do things
config() # if changes have occured
print 'Running main loop'
for conn in bot.conns.itervalues():
try:
out = conn.out.get_nowait()
main(conn, out)
except Queue.Empty:
pass
while all(conn.out.empty() for conn in bot.conns.itervalues()):
time.sleep(.1)
while True:
reload() # these functions only do things
config() # if changes have occured
for conn in bot.conns.itervalues():
try:
out = conn.out.get_nowait()
main(conn, out)
except Queue.Empty:
pass
while all(conn.out.empty() for conn in bot.conns.itervalues()):
time.sleep(.1)
if __name__ == '__main__':
main()