]> jfr.im git - irc/rizon/acid.git/blob - pyva/src/main/python/pseudoclient/sys_base.py
Import acidictive 4 and pyva plugin
[irc/rizon/acid.git] / pyva / src / main / python / pseudoclient / sys_base.py
1 import threading
2 import MySQLdb as db
3
4 class Subsystem(object):
5 #--------------------------------------------------------------#
6 # i'm not entirely sure if this functionality is still
7 # required, but it seems it was removed from this module
8 # but not from cmd_admin, so I can only assume the removal
9 # was not intentional.
10
11 db_up = True
12
13 @classmethod
14 def set_db_up(cls, up):
15 cls.db_up = up
16
17 @classmethod
18 def get_db_up(cls):
19 return cls.db_up
20
21 #--------------------------------------------------------------#
22
23 def __init__(self, module, options, name):
24 self.module = module
25 self.__options = options
26 self.__lock = threading.Lock()
27 self.name = name
28 self.__timer = None
29 self.conn = None
30 self.cursor = None
31 self.reload()
32
33 def db_open(self):
34 self.conn = db.connect(
35 host=self.module.config.get('database', 'host'),
36 user=self.module.config.get('database', 'user'),
37 passwd=self.module.config.get('database', 'passwd'),
38 db=self.module.config.get('database', 'db'),
39 unix_socket=self.module.config.get('database','sock')
40 )
41 self.conn.ping(True)
42 self.conn.autocommit(True)
43 self.cursor = self.conn.cursor()
44
45 def db_close(self):
46 if self.cursor != None:
47 self.cursor.close()
48 self.cursor = None
49
50 if self.conn != None:
51 self.conn.close()
52 self.conn = None
53
54 def reload(self):
55 self.__delay = self.get_option('update_period', int, 120)
56
57 if self.__timer != None:
58 self.stop()
59 self.on_reload()
60 self.start()
61 else:
62 self.on_reload()
63
64 def on_reload(self):
65 pass
66
67 def get_option(self, name, type, default):
68 return self.__options.get('%s_%s' % (self.name, name), type, default)
69
70 def set_option(self, name, value):
71 return self.__options.set('%s_%s' % (self.name, name), value)
72
73 def start(self):
74 self.__timer = threading.Timer(self.__delay, self.worker)
75 self.__timer.daemon = True
76 self.__timer.start()
77
78 def stop(self):
79 self.__lock.acquire()
80
81 try:
82 if self.__timer != None:
83 self.__timer.cancel()
84 self.__timer = None
85 finally:
86 self.__lock.release()
87
88 def update(self):
89 self.__lock.acquire()
90
91 try:
92 self.commit()
93 finally:
94 self.__lock.release()
95
96 def force(self):
97 self.update()
98
99 def commit(self):
100 pass
101
102 def worker(self):
103 try:
104 if self.conn != None:
105 self.conn.ping(True)
106 finally:
107 pass
108
109 if self.__class__.db_up:
110 self.update()
111
112 self.__timer = threading.Timer(self.__delay, self.worker)
113 self.__timer.daemon = True
114 self.__timer.start()
115