phenny-1/__init__.py

69 lines
1.6 KiB
Python
Raw Normal View History

2012-12-16 17:09:43 -05:00
#!/usr/bin/env python3
"""
__init__.py - Phenny Init Module
Copyright 2008, Sean B. Palmer, inamidst.com
Licensed under the Eiffel Forum License 2.
http://inamidst.com/phenny/
"""
import sys, os, time, threading, signal
class Watcher(object):
2012-05-31 02:24:21 -04:00
# Cf. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496735
def __init__(self):
self.child = os.fork()
if self.child != 0:
signal.signal(signal.SIGTERM, self.sig_term)
self.watch()
2012-05-31 02:24:21 -04:00
def watch(self):
try: os.wait()
except KeyboardInterrupt:
self.kill()
sys.exit()
2012-05-31 02:24:21 -04:00
def kill(self):
try: os.kill(self.child, signal.SIGKILL)
except OSError: pass
2012-05-31 02:24:21 -04:00
def sig_term(self, signum, frame):
self.kill()
sys.exit()
def run_phenny(config):
2012-05-31 02:24:21 -04:00
if hasattr(config, 'delay'):
delay = config.delay
else: delay = 20
2012-05-31 02:24:21 -04:00
def connect(config):
import bot
p = bot.Phenny(config)
2014-06-01 21:52:05 -04:00
p.run(config.host, config.port, config.ssl, config.ipv6,
config.ca_certs)
2012-05-31 02:24:21 -04:00
try: Watcher()
except Exception as e:
print('Warning:', e, '(in __init__.py)', file=sys.stderr)
2012-05-31 02:24:21 -04:00
while True:
try: connect(config)
except KeyboardInterrupt:
sys.exit()
2008-03-02 10:50:05 -05:00
2012-05-31 02:24:21 -04:00
if not isinstance(delay, int):
break
2012-05-31 02:24:21 -04:00
warning = 'Warning: Disconnected. Reconnecting in %s seconds...' % delay
print(warning, file=sys.stderr)
time.sleep(delay)
def run(config):
2012-05-31 02:24:21 -04:00
t = threading.Thread(target=run_phenny, args=(config,))
if hasattr(t, 'run'):
t.run()
else: t.start()
2012-05-31 02:24:21 -04:00
if __name__ == '__main__':
print(__doc__)