-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkbase.py
69 lines (59 loc) · 1.68 KB
/
kbase.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
import etcd
import json
import re
import slackbot_settings as ss
import traceback
client = etcd.Client(host=ss.ETCDHOST, port=ss.ETCDPORT,
username=ss.ETCDUSER, password=ss.ETCDPASSWD, protocol=ss.ETCDPROTOCOL)
mapping = {}
matches = {}
kbname = '/{}/popsbot-kb'.format(ss.ETCDUSER)
def init():
global mapping
global matches
global kbname
errored_key = ''
try:
f = client.read(kbname).value
if len(f) == 0:
return
mapping = json.loads(f)
for k, v in mapping.iteritems():
value = [v, [], None]
matches[k] = value
try:
value[1] = k.split('.*')
value[2] = re.compile('.*' + k + '.*', re.IGNORECASE)
except:
print 'canot compile {} as regex'.format(k)
except Exception as e:
print errored_key
traceback.print_exc()
print 'error while loading kb from etcd: {}'.format(e)
return
def clear(k):
global mapping
global matches
global kbname
mapping.pop(k, None)
matches.pop(k, None)
try:
client.write(kbname, json.dumps(mapping))
except Exception as e:
print 'error while saving kb to etcd: {}'.format(e)
def update(k, v):
global mapping
global matches
global kbname
mapping[k] = v
value = [v, [], None]
matches[k] = value
try:
value[1] = k.split('.*')
value[2] = re.compile('.*' + k + '.*', re.IGNORECASE)
except:
print 'cannot compile {} as regex'.format(k)
try:
client.write(kbname, json.dumps(mapping))
except Exception as e:
print 'error while saving kb to etcd: {}'.format(e)