forked from mrjoes/toredis
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gen_commands.py
271 lines (222 loc) · 9.09 KB
/
gen_commands.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
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
#!/usr/bin/env python
import json
import os
from textwrap import TextWrapper
wrapper = TextWrapper(subsequent_indent=' ')
wrap = wrapper.wrap
def get_commands():
return json.load(
open(os.path.join(os.path.dirname(__file__), 'commands.json'))
)
def argname(name):
return name.lower().replace('-', '_').replace(':', '_')
def parse_command(command):
return '"%s"' % command.replace(' ', '", "')
def parse_arguments(command, arguments):
args = ['self']
doc = []
code = ['{args} = [%s]' % parse_command(command)]
for arg in arguments:
# Sub-command parsing
if 'command' in arg:
cmd = argname(arg['command'])
if cmd in args:
raise Exception('Command %s is already in args!' % cmd)
cmd_default = 'None'
if arg.get('multiple'):
cmd_default = 'tuple()'
if isinstance(arg['name'], list):
code.append('for %s in %s:' % (
', '.join([argname(i) for i in arg['name']]),
cmd
))
code.append(
' {args}.append("%s")' % arg['command']
)
for i in arg['name']:
code.append(' {args}.append(%s)' % argname(i))
else:
code.append('for %s in %s:' % (argname(arg['name']), cmd))
code.append(' {args}.append("%s")' % arg['command'])
code.append(' {args}.append(%s)' % argname(arg['name']))
elif arg.get('variadic'):
cmd_default = 'tuple()'
code.append('if len(%s):' % cmd)
code.append(' {args}.append("%s")' % arg['command'])
if isinstance(arg['name'], list):
code.append(' for %s in %s:' % (
', '.join([argname(i) for i in arg['name']]),
cmd
))
for i in arg['name']:
code.append(' {args}.append(%s)' % argname(i))
else:
code.append(' {args}.extend(%s)' % cmd)
else:
if arg.get('optional'):
prefix = ' '
code.append('if %s:' % cmd)
else:
prefix = ''
code.append(prefix + '{args}.append("%s")' % arg['command'])
if isinstance(arg['name'], list):
code.append(prefix + '%s = %s' % (
', '.join([argname(i) for i in arg['name']]),
cmd
))
for i in arg['name']:
code.append(prefix + '{args}.append(%s)' % argname(i))
else:
code.append(prefix + '{args}.append(%s)' % cmd)
if 'optional' in arg:
args.append('%s=%s' % (cmd, cmd_default))
else:
args.append(cmd)
doc.append(':param %s:' % cmd)
# Special case for numkeys parameter
elif arg['name'] == 'numkeys':
# do not adding arg for numkeys argument
assert arguments[arguments.index(arg) + 1] == {
"name": "key",
"type": "key",
"multiple": True
}
code.append('{args}.append(len(keys))')
# If name is list
elif isinstance(arg['name'], list):
# makes no sense for single pairs
assert arg.get('multiple')
# special case for score and member
if arg['name'] == [u"score", u"member"]:
args.append('member_score_dict')
code.append('for member, score in member_score_dict.items():')
code.append(' {args}.append(score)')
code.append(' {args}.append(member)')
doc.append(':param member_score_dict:')
doc.append(' member score dictionary')
# value pairs
elif len(arg['name']) == 2 and arg['name'][1] == 'value':
arg_name = argname(arg['name'][0])
name = '%s_dict' % arg_name
args.append(name)
code.append('for %s, value in %s.items():' % (arg_name, name))
code.append(' {args}.append(%s)' % arg_name)
code.append(' {args}.append(value)')
doc.append(':param member_score_dict:')
doc.append(' key value dictionary')
# special case for length = 1
elif len(arg['name']) == 1:
name = '%ss' % argname(arg['name'][0])
args.append(name)
code.append('if isinstance(%s, string_types):' % name)
code.append(' {args}.append(%s)' % name)
code.append('else:')
code.append(' {args}.extend(%s)' % name)
doc.append(':param member_score_dict:')
doc.append(' string or list of strings')
else:
raise Exception('Unknown list name group in argument '
'specification: %s' % arg['name'])
elif argname(arg['name']) in args:
raise Exception(
'Argument %s is already in args!' % argname(arg['name'])
)
elif 'multiple' in arg:
# If it is last parameter, use *arg notation
name = '%ss' % argname(arg['name'])
if arg.get('optional'):
args.append('%s=[]' % name)
else:
args.append(name)
code.append('if isinstance(%s, string_types):' % name)
code.append(' {args}.append(%s)' % name)
code.append('else:')
code.append(' {args}.extend(%s)' % name)
doc.append(':param %s:' % name)
doc.append(' string or list of strings')
elif 'enum' in arg and len(arg['enum']) == 1 and arg.get('optional'):
name = argname(arg['name'])
args.append('%s=False' % name)
code.append('if %s:' % name)
code.append(' {args}.append("%s")' % arg['enum'][0])
doc.append(':param %s:' % name)
else:
name = argname(arg['name'])
if arg.get('optional') == True:
args.append('%s=None' % name)
code.append('if %s:' % name)
code.append(' {args}.append(%s)' % name)
else:
args.append(name)
code.append('{args}.append(%s)' % name)
doc.append(':param %s:' % name)
args.append('callback=None')
if len(code) > 1:
code.append('self.send_message({args}, callback)')
else:
code = ['self.send_message([%s], callback)' % parse_command(command)]
if 'args' in args or any(arg.startswith('args=') for arg in args):
code = [line.format(args='message_args') for line in code]
else:
code = [line.format(args='args') for line in code]
return args, doc, code
name_map = {
'del': 'delete',
'exec': 'execute'
}
def get_command_name(command):
name = command.lower().replace(' ', '_')
if name in name_map:
return name_map[name]
else:
return name
def get_command_code(func_name, cmd, params):
args, args_doc, args_code = parse_arguments(
cmd, params.get('arguments', [])
)
args_doc = [" %s" % i for i in args_doc]
args_code = [" %s" % i for i in args_code]
wrap = TextWrapper().wrap
lines = []
lines.append('def %s(%s):' % (func_name, ', '.join(args)))
lines.append(' """')
doc = []
if 'summary' in params:
doc.extend(wrap(params['summary']))
if args_doc:
doc.append('')
doc.extend(args_doc)
if 'complexity' in params:
doc.append('')
doc.append('Complexity')
doc.append('----------')
doc.extend(wrap(params['complexity']))
lines.extend([' %s' % line if line else line for line in doc])
lines.append(' """')
lines.extend(args_code)
return lines
def get_class_source(class_name):
lines = ['class %s(object):' % class_name, '']
for cmd, params in sorted(get_commands().items()):
for line in get_command_code(get_command_name(cmd), cmd, params):
lines.append(' %s' % line if line else line)
lines.append('')
return '\n'.join(lines)
def get_imports():
imports = 'from toredis._compat import string_types'
return imports + '\n' * 3
def compile_commands():
ret = {}
for cmd, params in sorted(get_commands().items()):
name = get_command_name(cmd)
lines = get_command_code(name, cmd, params)
code = compile('\n'.join(lines), "<string>", "exec")
ctx = {}
exec(code in ctx)
ret[name] = ctx[name]
return ret
if __name__ == "__main__":
with open(os.path.join(os.path.dirname(__file__), 'toredis/commands.py'), 'w') as f:
f.write(get_imports())
f.write(get_class_source('RedisCommandsMixin'))
print('Generated commands.py')