Commit d213c31d authored by Giorgos Korfiatis's avatar Giorgos Korfiatis
Browse files

Fixes in unpack

parent 3d08a070
......@@ -115,11 +115,20 @@ class Canonical(object):
if item is None and can_be_null:
return None
if argmap_check(item):
item = self._unpack(item)
return self._check(item)
def _check(self, item):
return item
def _unpack(self, item):
argmap = argmap_list_to_dict(item)
if len(argmap) == 2:
return argmap[None]
return argmap
def parse(self, item):
opts = self.opts
if item is None and 'default' in opts:
......@@ -546,12 +555,15 @@ class ListOf(Canonical):
class Args(Canonical):
def _unpack(self, arglist):
return arglist
def _check(self, item):
if argmap_check(item):
if hasattr(item, 'keys') and callable(item.keys):
arglist = argmap_dict_to_list(item)[:-1]
else:
arglist = [(None, item[i]) for i in xrange(0, len(item)-1)]
arglist = item[:-1]
else:
try:
arglist = OrderedDict(item).items()
......@@ -571,7 +583,7 @@ class Args(Canonical):
position = 0
for k, v in arglist:
if k:
if k is not None:
canonified[k] = kw[k].check(v)
else:
# find the right position
......@@ -774,8 +786,7 @@ class Canonifier(object):
def parse(self, method, arglist):
args, rest = argmap_decode(arglist)
argdict = self.input_canonical(method).parse(args)
return argdict
return self.input_canonical(method).check(args)
class Specificator(object):
......
......@@ -146,7 +146,7 @@ def argmap_encode(obj, output):
output('=')
argmap_encode(v)
output(']')
if hasattr(obj, '__iter__'):
output('[')
once = 1
......@@ -203,6 +203,7 @@ def argmap_decode_atom(inputf):
def argmap_decode_args(inputf):
args = []
append = args.append
s = inputf(1)
key = None
......@@ -240,7 +241,7 @@ def argmap_decode_args(inputf):
def argmap_check(obj):
if hasattr(obj, 'keys'):
if hasattr(obj, 'keys') and callable(obj.keys):
# this could cover both cases
return ARGMAP_MAGIC in obj
return hasattr(obj, '__len__') and len(obj) and obj[-1] == ARGMAP_MAGIC
......@@ -258,23 +259,18 @@ def argmap_unzip_dict(argmap):
return args, kw
def argmap_unzip_list(argmap):
if not argmap or argmap[-1] != ARGMAP_MAGIC:
if not argmap or argmap.pop() != ARGMAP_MAGIC:
m = "argmap unzip list: magic not found"
raise ValueError(m)
iter_argmap = iter(argmap)
for magic in iter_argmap:
break
args = []
append = args.append
kw = OrderedDict()
for k, v in iter_argmap:
for k, v in argmap:
if k is None:
append(v)
else:
kw[k] = v
return args, kw
def argmap_unzip(argmap):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment