wrap bot.py in a main() function to be import-safe
This commit is contained in:
parent
62fbe6e237
commit
921b986683
66
bot.py
66
bot.py
|
@ -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,37 +14,45 @@ 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(),
|
|
||||||
os.path.join('core', 'reload.py'), 'exec'))
|
|
||||||
reload(init=True)
|
|
||||||
|
|
||||||
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:
|
print 'Connecting to IRC'
|
||||||
config()
|
|
||||||
if not hasattr(bot, 'config'):
|
|
||||||
exit()
|
|
||||||
except Exception, e:
|
|
||||||
print 'ERROR: malformed config file:', e
|
|
||||||
traceback.print_exc()
|
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
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:
|
print 'Running main loop'
|
||||||
reload() # these functions only do things
|
|
||||||
config() # if changes have occured
|
|
||||||
|
|
||||||
for conn in bot.conns.itervalues():
|
while True:
|
||||||
try:
|
reload() # these functions only do things
|
||||||
out = conn.out.get_nowait()
|
config() # if changes have occured
|
||||||
main(conn, out)
|
|
||||||
except Queue.Empty:
|
for conn in bot.conns.itervalues():
|
||||||
pass
|
try:
|
||||||
while all(conn.out.empty() for conn in bot.conns.itervalues()):
|
out = conn.out.get_nowait()
|
||||||
time.sleep(.1)
|
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()
|
||||||
|
|
Loading…
Reference in New Issue