Skip to content

Commit

Permalink
Fixed array command generation
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjoes authored and ei-grad committed Nov 4, 2012
1 parent 0e6253e commit ec1010c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,5 @@ TOREDIS

TODO:
* Write README.md
* Implement PubSub
* Rewrite `get_commands.py` in declarative style
* Add arguments description in docstrings
* Add result parsing wrappers for callbacks?
* Add retry to `Redis.send_message` for callback=None
24 changes: 22 additions & 2 deletions gen_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,30 @@ def parse_arguments(command, arguments):
args = ['self']
doc = []
code = ['args = ["%s"]' % 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:
Expand All @@ -65,7 +73,9 @@ def parse_arguments(command, arguments):
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']]),
Expand All @@ -79,6 +89,7 @@ def parse_arguments(command, arguments):
args.append('%s=%s' % (cmd, cmd_default))
else:
args.append(cmd)
# Special case for numkeys parameter
elif arg['name'] == 'numkeys':
# do not adding arg for numkeys argument
assert arguments[arguments.index(arg) + 1] == {
Expand All @@ -87,24 +98,33 @@ def parse_arguments(command, arguments):
"multiple": True
}
code.append('args.append(len(keys))')
# If name is list
elif isinstance(arg['name'], list):
assert arg.get('multiple') # makes no sense for single pairs
# 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)')
# 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)')
# if length is 1, use parameter
elif len(arg['name']) == 1:
name = '%ss' % argname(arg['name'][0])
args.append(name)
code.append('args.extend(%s)' % name)
code.append('if isinstance(%s, basestring):' % name)
code.append(' args.append(%s)' % name)
code.append('else:')
code.append(' args.extend(%s)' % name)
else:
raise Exception('Unknown list name group in argument '
'specification: %s' % arg['name'])
Expand Down
10 changes: 8 additions & 2 deletions toredis/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,10 @@ def psubscribe(self, patterns, callback=None):
subscribed to.
"""
args = ["PSUBSCRIBE"]
args.extend(patterns)
if isinstance(patterns, basestring):
args.append(patterns)
else:
args.extend(patterns)
self.send_message(args, callback)

def pttl(self, key, callback=None):
Expand Down Expand Up @@ -1521,7 +1524,10 @@ def subscribe(self, channels, callback=None):
O(N) where N is the number of channels to subscribe to.
"""
args = ["SUBSCRIBE"]
args.extend(channels)
if isinstance(channels, basestring):
args.append(channels)
else:
args.extend(channels)
self.send_message(args, callback)

def sunion(self, keys, callback=None):
Expand Down

0 comments on commit ec1010c

Please sign in to comment.