This repository has been archived by the owner on Jun 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy path__init__.py
executable file
·73 lines (57 loc) · 1.62 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python
"""
honbot - A Heroes Of Newerth chatserver Bot
Copyright 2011, Anton Romanov
Heavily inspired by phenny:
__init__.py - Phenny Init Module
Copyright 2008, Sean B. Palmer, inamidst.com
Licensed under the Eiffel Forum License 2.
"""
import sys, os, time, threading, signal,traceback
import bot
class Watcher(object):
# Cf. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496735
def __init__(self):
self.child = os.fork()
if self.child != 0:
self.watch()
def watch(self):
try: os.wait()
except KeyboardInterrupt:
self.kill()
sys.exit()
def kill(self):
try: os.kill(self.child, signal.SIGKILL)
except OSError: pass
def run_honbot(config):
if hasattr(config, 'delay'):
delay = config.delay
else: delay = 20
def connect(config):
p = bot.Bot(config)
p.run()
try: Watcher()
except Exception, e:
print >> sys.stderr, 'Warning:', e, '(in __init__.py)'
while True:
try: connect(config)
except KeyboardInterrupt:
sys.exit()
except:
print(sys.exc_type,sys.exc_value)
print(sys.exc_traceback)
print(sys.exc_info())
traceback.print_exc(file=sys.stdout)
pass
if not isinstance(delay, int):
break
warning = 'Warning: Disconnected. Reconnecting in %s seconds...' % delay
print >> sys.stderr, warning
time.sleep(delay)
def run(config):
t = threading.Thread(target=run_honbot, args=(config,))
if hasattr(t, 'run'):
t.run()
else: t.start()
if __name__ == '__main__':
print __doc__