Public » MiniRedis Read More
Clone URL:  
Pushed to one repository · View In Graph Contained in tip

clean up how errors are returned

Changeset 80ff910c8ff5

Parent 4e30762c5ca3

by Profile picture of Benjamin PollackBenjamin Pollack

Changes to one file · Browse files at 80ff910c8ff5 Showing diff from parent 4e30762c5ca3 Diff from another changeset...

Change 1 of 6 Show Changes Only miniredis.py Stacked
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
 
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
 
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
 #!/usr/bin/env python    import getopt  import os  import re  import pickle  import select  import socket  import sys  import threading    class RedisError(object):   def __init__(self, message):   self.message = message     def __repr__(self):   return '<RedisError(%s)>' % self.message   -class RedisBadValueError(RedisError): - def __init__(self): - super(RedisBadValueError, self).__init__('Operation against a key holding the wrong kind of value') -  class RedisConstant(object):   def __init__(self, type):   self.type = type     def __repr__(self):   return '<RedisConstant(%s)>' % self.type    EMPTY_SCALAR = RedisConstant('EmptyScalar') -EMPTY_ARRAY = RedisConstant('EmptyArray') +EMPTY_LIST = RedisConstant('EmptyList') +BAD_VALUE = RedisError('Operation against a key holding the wrong kind of value')      class RedisClient(object):   def __init__(self, socket):   self.socket = socket   self.wfile = socket.makefile('wb')   self.rfile = socket.makefile('rb')   self.db = None   self.table = None    class MiniRedis(threading.Thread):   def __init__(self, host='127.0.0.1', port=56784, logging=False, db_file=None):   super(MiniRedis, self).__init__()   self.host = host   self.port = port   self.logging = logging   self.halt = True   self.tables = {}   self.clients = {}   self.db_file = db_file     self.load()     def log(self, client, s):   if self.logging:   who = '%s:%s' % client.socket.getpeername() if client else 'SERVER'   print '%s: %s' % (who, s)     def select(self, client, db):   if db not in self.tables:   self.tables[db] = {}   client.db = db   client.table = self.tables[db]     def save(self):   if self.db_file:   with open(self.db_file, 'wb') as f:   pickle.dump(self.tables, f, pickle.HIGHEST_PROTOCOL)   self.log(None, 'saved database to "%s"' % self.db_file)     def load(self):   if self.db_file and os.path.lexists(self.db_file):   with open(self.db_file, 'rb') as f:   self.tables = pickle.load(f)   self.log(None, 'loaded database from file "%s"' % self.db_file)     def handle_set(self, client, key, data):   client.table[key] = data   self.log(client, 'SET %s -> %s' % (key, data))   return True     def handle_get(self, client, key):   data = client.table.get(key, None)   if isinstance(data, list): - return RedisBadValueError() + return BAD_VALUE   if data != None:   data = str(data)   else:   data = EMPTY_SCALAR   self.log(client, 'GET %s -> %s' % (key, data))   return data     def handle_del(self, client, key):   self.log(client, 'DEL %s' % key)   if key not in client.table:   return 0   del client.table[key]   return 1     def handle_lpush(self, client, key, data):   if key not in client.table:   client.table[key] = []   elif not isinstance(client.table[key], list): - return RedisBadValueError() + return BAD_VALUE   client.table[key].insert(0, data)   self.log(client, 'LPUSH %s %s' % (key, data))   return True     def handle_rpop(self, client, key):   if key not in client.table:   return EMPTY_SCALAR   if not isinstance(client.table[key], list): - return RedisBadValueError() + return BAD_VALUE   if len(client.table[key]) > 0:   data = client.table[key].pop()   else:   data = EMPTY_SCALAR   self.log(client, 'LPOP %s -> %s' % (key, data))   return data     def handle_keys(self, client, pattern):   r = re.compile(pattern.replace('*', '.*'))   self.log(client, 'KEYS %s' % pattern)   return ' '.join(k for k in client.table.keys() if r.search(k))     def handle_incr(self, client, key):   return self.handle_incrby(client, key, 1)     def handle_incrby(self, client, key, by):   try:   client.table[key] = int(client.table[key])   client.table[key] += int(by)   except (KeyError, TypeError, ValueError):   client.table[key] = 1   self.log(client, 'INCRBY %s %s -> %s' % (key, by, client.table[key]))   return client.table[key]     def handle_flushdb(self, client):   self.log(client, 'FLUSHDB')   client.table.clear()   return True     def handle_select(self, client, db):   db = int(db)   self.select(client, db)   self.log(client, 'SELECT %s' % db)   return True     def handle_quit(self, client):   self.log(client, 'QUIT')   client.socket.shutdown(socket.SHUT_RDWR)   client.socket.close()   del self.clients[client.socket]   return False     def handle_shutdown(self, client):   self.log(client, 'SHUTDOWN')   self.halt = True   return True     def handle_save(self, client):   self.save()   self.log(client, 'SAVE')   return True     def unwrap_set(self, client, line):   key, length = line.split()   data = client.rfile.read(int(length))   client.rfile.read(2) # throw out newline   return self.handle_set(key, client, data)     def unwrap_get(self, client, line):   key = line.strip()   return self.handle_get(client, key)     def unwrap_del(self, client, line):   key = line.strip()   return self.handle_del(client, key)     def unwrap_lpush(self, client, line):   key, length = line.split()   data = client.rfile.read(int(length))   client.rfile.read(2) # throw out newline   return self.handle_lpush(client, key, data)     def unwrap_rpop(self, client, line):   key = line.strip()   return self.handle_rpop(client, key)     def unwrap_keys(self, client, line):   pattern = line.strip()   return self.handle_keys(client, pattern)     def unwrap_incrby(self, client, line):   key, by = line.split()   return self.handle_incrby(client, key, by)     def unwrap_flushdb(self, client, line):   return self.handle_flushdb(client)     def unwrap_select(self, client, line):   db = line.strip()   return self.handle_select(client, db)     def unwrap_quit(self, client, line):   return self.handle_quit(client)     def unwrap_shutdown(self, client, line):   return self.handle_shutdown(client)     def handle_normal(self, client, line):   command, _, rest = line.partition(' ')   return getattr(self, 'unwrap_' + command.strip().lower())(client, rest)     def handle_multibulk(self, client, line):   items = int(line[1:].strip())   args = []   for x in xrange(0, items):   length = int(client.rfile.readline().strip()[1:])   args.append(client.rfile.read(length))   client.rfile.read(2) # throw out newline   command = args[0].lower()   return getattr(self, 'handle_' + command)(client, *args[1:])     def handle(self, client):   line = client.rfile.readline()   if not line:   self.log(client, 'client disconnected')   del self.clients[client.socket]   client.socket.close()   return   if line[0] == '*':   o = self.handle_multibulk(client, line)   else:   o = self.handle_normal(client, line)   self.dump(client, o)     def dump(self, client, o):   nl = '\r\n'   if isinstance(o, bool):   if o:   client.wfile.write('+OK\r\n')   # Show nothing for a false return; that means be quiet   elif o == EMPTY_SCALAR:   client.wfile.write('$-1\r\n') - elif o == EMPTY_ARRAY: + elif o == EMPTY_LIST:   client.wfile.write('*-1\r\n')   elif isinstance(o, int):   client.wfile.write(':' + str(o) + nl)   elif isinstance(o, str):   client.wfile.write('$' + str(len(o)) + nl)   client.wfile.write(o + nl)   elif isinstance(o, list):   client.wfile.write('*' + len(o))   for val in o:   self.dump(client, val)   elif isinstance(o, RedisError):   client.wfile.write('-ERR %s\r\n' % o.message)   else:   client.wfile.write('return type not yet implemented\r\n')   client.wfile.flush()     def stop(self):   self.halt = True   self.join()     def run(self):   self.halt = False   server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)   server.bind((self.host, self.port))   server.listen(5)   while not self.halt:   readable, _, _ = select.select([server] + self.clients.keys(), [], [], 1.0)   for sock in readable:   if sock == server:   (client_socket, address) = server.accept()   client = RedisClient(client_socket)   self.clients[client_socket] = client   self.log(client, 'client connected')   self.select(client, 0)   else:   try:   self.handle(self.clients[sock])   except Exception, e:   self.log(client, 'exception: %s' % e)   self.handle_quit(client)   for client_socket in self.clients.iterkeys():   client_socket.close()   self.clients.clear()   server.close()   server = None    def main(args):   host, port, logging = '127.0.0.1', 56784, True   opts, args = getopt.getopt(args, 'h:p:l')   for o, a in opts:   if o == '-h':   host = a   elif o == '-p':   port = int(a)   elif o == '-l':   logging = True   print 'Launching MiniRedis on %s:%s' % (host, port)   m = MiniRedis(host=host, port=port, logging=logging, db_file='miniredis.db')   m.start()   m.join()   print 'Stopped'    if __name__ == '__main__':   main(sys.argv[1:])