Commit 30779db6 authored by Georgios D. Tsoukalas's avatar Georgios D. Tsoukalas
Browse files

integrate Args and argmap, change argmap magic pos

parent 414a78a3
...@@ -35,7 +35,7 @@ ...@@ -35,7 +35,7 @@
from random import random, choice, randint from random import random, choice, randint
from math import log from math import log
from inspect import isclass from inspect import isclass
from .utils.argmap import argmap_decode from .utils.argmap import argmap_decode, argmap_check, argmap_dict_to_list
try: try:
from collections import OrderedDict from collections import OrderedDict
...@@ -538,49 +538,43 @@ class ListOf(Canonical): ...@@ -538,49 +538,43 @@ class ListOf(Canonical):
class Args(Canonical): class Args(Canonical):
def _parse(self, arglist): def _check(self, item):
formalslen = len(self.kw) if argmap_check(item):
arglen = len(arglist) if hasattr(item, 'keys') and callable(item.keys):
if arglen != formalslen: arglist = argmap_dict_to_list(item)[:-1]
raise Exception('param inconsistent') else:
arglist = [(None, item[i]) for i in xrange(0, len(item)-1)]
else:
try:
arglist = OrderedDict(item).items()
except (TypeError, ValueError), e:
m = "%s: %s is not dict-able" % (self, shorts(item))
raise CanonifyException(m)
parsed = OrderedDict()
keys = self.kw.keys() keys = self.kw.keys()
kw = self.kw
arglen = len(arglist)
if arglen != len(keys):
m = "inconsistent number of parameters: %s != %s" % (
arglen, len(keys))
raise CanonifyException(m)
canonified = OrderedDict()
position = 0 position = 0
for k, v in arglist: for k, v in arglist:
if k: if k:
parsed[k] = self.kw[k].parse(v) canonified[k] = kw[k].check(v)
else: else:
# find the right position # find the right position
for i in range(position, arglen): for i in range(position, arglen):
key = keys[i] key = keys[i]
if not key in parsed.keys(): if not key in canonified.keys():
position = i + 1 position = i + 1
break break
else: # exhausted else: # exhausted
raise Exception("shouldn't happen") raise Exception("shouldn't happen")
parsed[key] = self.kw[key].parse(v) canonified[key] = kw[key].check(v)
return parsed
def _check(self, item):
try:
item = OrderedDict(item)
except TypeError, e:
m = "%s: %s is not dict-able" % (self, shorts(item))
raise CanonifyException(m)
canonified = OrderedDict()
try:
for n, c in self.kw.items():
t = item[n] if n in item else None
canonified[n] = c(t)
except KeyError:
m = ("%s: Argument '%s' not found in '%s'"
% (self, shorts(n), shorts(item)))
raise CanonifyException(m)
return canonified return canonified
......
...@@ -203,7 +203,6 @@ def argmap_decode_atom(inputf): ...@@ -203,7 +203,6 @@ def argmap_decode_atom(inputf):
def argmap_decode_args(inputf): def argmap_decode_args(inputf):
args = [ARGMAP_MAGIC]
append = args.append append = args.append
s = inputf(1) s = inputf(1)
key = None key = None
...@@ -215,6 +214,7 @@ def argmap_decode_args(inputf): ...@@ -215,6 +214,7 @@ def argmap_decode_args(inputf):
if s == ']': if s == ']':
if key is not None: if key is not None:
append((None, key)) append((None, key))
args.append(ARGMAP_MAGIC)
return args, None return args, None
if s == '=': if s == '=':
...@@ -243,7 +243,7 @@ def argmap_check(obj): ...@@ -243,7 +243,7 @@ def argmap_check(obj):
if hasattr(obj, 'keys'): if hasattr(obj, 'keys'):
# this could cover both cases # this could cover both cases
return ARGMAP_MAGIC in obj return ARGMAP_MAGIC in obj
return hasattr(obj, '__len__') and len(obj) and obj[0] == ARGMAP_MAGIC return hasattr(obj, '__len__') and len(obj) and obj[-1] == ARGMAP_MAGIC
def argmap_unzip_dict(argmap): def argmap_unzip_dict(argmap):
if not hasattr(argmap, 'keys'): if not hasattr(argmap, 'keys'):
...@@ -258,7 +258,7 @@ def argmap_unzip_dict(argmap): ...@@ -258,7 +258,7 @@ def argmap_unzip_dict(argmap):
return args, kw return args, kw
def argmap_unzip_list(argmap): def argmap_unzip_list(argmap):
if not argmap or argmap[0] != ARGMAP_MAGIC: if not argmap or argmap[-1] != ARGMAP_MAGIC:
m = "argmap unzip list: magic not found" m = "argmap unzip list: magic not found"
raise ValueError(m) raise ValueError(m)
...@@ -287,7 +287,7 @@ def argmap_unzip(argmap): ...@@ -287,7 +287,7 @@ def argmap_unzip(argmap):
raise ValueError(m) raise ValueError(m)
def argmap_zip_list(args, kw): def argmap_zip_list(args, kw):
return [ARGMAP_MAGIC] + [(None, a) for a in args] + kw.items() return [(None, a) for a in args] + kw.items() + [ARGMAP_MAGIC]
def argmap_zip_dict(args, kw): def argmap_zip_dict(args, kw):
argmap = OrderedDict() argmap = OrderedDict()
...@@ -298,3 +298,13 @@ def argmap_zip_dict(args, kw): ...@@ -298,3 +298,13 @@ def argmap_zip_dict(args, kw):
argmap_zip = argmap_zip_list argmap_zip = argmap_zip_list
def argmap_list_to_dict(argmap):
args, kw = argmap_unzip_list(argmap)
kw[ARGMAP_MAGIC] = ARGMAP_MAGIC
kw[None] = args
return kw
def argmap_dict_to_list(argmap):
args, kw = argmap_unzip_dict(argmap)
return args + kw.items() + [ARGMAP_MAGIC]
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