/
psi_example.py
143 lines (111 loc) · 4.35 KB
/
psi_example.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import argparse
import redis
import json
import os
from pprint import pprint
def Populate(f):
with open(f) as json_data:
configs = json.load(json_data)
print configs
for config_name in configs:
config = configs[config_name]
for key, value in config['user'].items():
r.hset(config_name+':user', key, value)
for key, value in config['experiment'].items():
r.hset(config_name+':experiment', key, value)
for source, source_data in config['sources'].items():
r.sadd(config_name+':sources', source)
for key, value in source_data.items():
r.hset('source:'+config_name+':'+source, key, value)
def Query(config=None):
d = dict()
lst = dict()
for key in r.scan_iter():
key_type = r.type(key)
if key_type == "hash" and key.startswith('source'):
d[key] = r.hgetall(key)
if key_type == "set":
lst[key] = r.smembers(key)
return d,lst
def Update(key,item,val,redis):
d,lst = Query()
if key in d.keys() and redis.hexists(key,item):
old_val = redis.hget(key,item)
redis.hset(key,item,val)
r.publish(src[0],src[1])
def Fetch(config_name,redis):
config = dict()
for key in 'user','experiment','sources':
if redis.type(config_name+':'+key) == 'hash':
config[key] = redis.hgetall(config_name+':'+key)
if redis.type(config_name+':'+key) == 'set':
config[key] = dict()
for item in redis.smembers(config_name+':'+key):
config[key][item] = redis.hgetall('source:'+config_name+':'+item)
return config
def config_change_handler(message):
print 'Change on hash '+message['channel']+' key: '+message['data']
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Redis client test")
parser.add_argument("-p","--populate",
action="store",
nargs='?',
type=str,
help="populate redis with file content")
parser.add_argument("-q","--query",
action="store_true",
help="query redis dictionary")
parser.add_argument("-k","--key",
action="store",
nargs='?',
type=str,
help="dictionary keys to look for")
parser.add_argument("-l","--listen",
action="store",
nargs='?',
type=str,
help="sources to listen")
parser.add_argument("-u","--update",
action="store",
nargs='?',
type=str,
help="key-value to update")
parser.add_argument("-f","--fetch",
action="store",
nargs='?',
type=str,
help="fetch configuration information")
args = parser.parse_args()
r = redis.StrictRedis('pc11997',port='63791')
if args.populate != None:
Populate(args.populate)
if (args.query and args.key==None):
sources_list = Query()
pprint(sources_list)
if args.listen != None:
p = r.pubsub(ignore_subscribe_messages=True)
# Split argument input into channels to wich subscribe
for src in args.listen.split(','):
p.subscribe(**{src:config_change_handler})
thread = p.run_in_thread(sleep_time=0.001)
print ''
print 'Press any key to stop'
print ''
os.system('read')
thread.stop()
if args.update != None:
src=args.update.split(',')
Update(src[0],src[1],src[2],r)
if args.fetch != None:
pprint(Fetch(args.fetch,r))
# eventually open a new channel?
if args.key!=None:
for key in r.scan_iter():
if key == args.key:
key_type = r.type(key)
if key_type == "hash":
if key==args.key:
pprint(r.hgetall(key))
if key_type == "set":
if key==args.key:
pprint(r.smembers(key))