switch __init__.py to 4 spaces
parent
74713798f9
commit
1f9a35b699
84
__init__.py
84
__init__.py
|
@ -10,58 +10,58 @@ http://inamidst.com/phenny/
|
||||||
import sys, os, time, threading, signal
|
import sys, os, time, threading, signal
|
||||||
|
|
||||||
class Watcher(object):
|
class Watcher(object):
|
||||||
# Cf. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496735
|
# Cf. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496735
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.child = os.fork()
|
self.child = os.fork()
|
||||||
if self.child != 0:
|
if self.child != 0:
|
||||||
signal.signal(signal.SIGTERM, self.sig_term)
|
signal.signal(signal.SIGTERM, self.sig_term)
|
||||||
self.watch()
|
self.watch()
|
||||||
|
|
||||||
def watch(self):
|
def watch(self):
|
||||||
try: os.wait()
|
try: os.wait()
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
self.kill()
|
self.kill()
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
def kill(self):
|
def kill(self):
|
||||||
try: os.kill(self.child, signal.SIGKILL)
|
try: os.kill(self.child, signal.SIGKILL)
|
||||||
except OSError: pass
|
except OSError: pass
|
||||||
|
|
||||||
def sig_term(self, signum, frame):
|
def sig_term(self, signum, frame):
|
||||||
self.kill()
|
self.kill()
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
def run_phenny(config):
|
def run_phenny(config):
|
||||||
if hasattr(config, 'delay'):
|
if hasattr(config, 'delay'):
|
||||||
delay = config.delay
|
delay = config.delay
|
||||||
else: delay = 20
|
else: delay = 20
|
||||||
|
|
||||||
def connect(config):
|
def connect(config):
|
||||||
import bot
|
import bot
|
||||||
p = bot.Phenny(config)
|
p = bot.Phenny(config)
|
||||||
p.run(config.host, config.port, config.ssl, config.ipv6)
|
p.run(config.host, config.port, config.ssl, config.ipv6)
|
||||||
|
|
||||||
try: Watcher()
|
try: Watcher()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print('Warning:', e, '(in __init__.py)', file=sys.stderr)
|
print('Warning:', e, '(in __init__.py)', file=sys.stderr)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
try: connect(config)
|
try: connect(config)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
if not isinstance(delay, int):
|
if not isinstance(delay, int):
|
||||||
break
|
break
|
||||||
|
|
||||||
warning = 'Warning: Disconnected. Reconnecting in %s seconds...' % delay
|
warning = 'Warning: Disconnected. Reconnecting in %s seconds...' % delay
|
||||||
print(warning, file=sys.stderr)
|
print(warning, file=sys.stderr)
|
||||||
time.sleep(delay)
|
time.sleep(delay)
|
||||||
|
|
||||||
def run(config):
|
def run(config):
|
||||||
t = threading.Thread(target=run_phenny, args=(config,))
|
t = threading.Thread(target=run_phenny, args=(config,))
|
||||||
if hasattr(t, 'run'):
|
if hasattr(t, 'run'):
|
||||||
t.run()
|
t.run()
|
||||||
else: t.start()
|
else: t.start()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
print(__doc__)
|
print(__doc__)
|
||||||
|
|
Loading…
Reference in New Issue