Revision f78e128d

b/snf-common/synnefo/lib/commissioning/specificator.py
35 35
from random import random, choice, randint
36 36
from math import log
37 37
from inspect import isclass
38
from .utils.argmap import argmap_decode
38
from .utils.argmap import argmap_decode, argmap_check, argmap_dict_to_list
39 39

  
40 40
try:
41 41
    from collections import OrderedDict
......
538 538

  
539 539
class Args(Canonical):
540 540

  
541
    def _parse(self, arglist):
542
        formalslen = len(self.kw)
543
        arglen = len(arglist)
544
        if arglen != formalslen:
545
            raise Exception('param inconsistent')
541
    def _check(self, item):
542
        if argmap_check(item):
543
            if hasattr(item, 'keys') and callable(item.keys):
544
                arglist = argmap_dict_to_list(item)[:-1]
545
            else:
546
                arglist = [(None, item[i]) for i in xrange(0, len(item)-1)]
547
        else:
548
            try:
549
                arglist = OrderedDict(item).items()
550
            except (TypeError, ValueError), e:
551
                m = "%s: %s is not dict-able" % (self, shorts(item))
552
                raise CanonifyException(m)
546 553

  
547
        parsed = OrderedDict()
548 554
        keys = self.kw.keys()
555
        kw = self.kw
556
        arglen = len(arglist)
557
        if arglen != len(keys):
558
            m = "inconsistent number of parameters: %s != %s" % (
559
                arglen, len(keys))
560
            raise CanonifyException(m)
561

  
562
        canonified = OrderedDict()
549 563
        position = 0
550 564

  
551 565
        for k, v in arglist:
552 566
            if k:
553
                parsed[k] = self.kw[k].parse(v)
567
                canonified[k] = kw[k].check(v)
554 568
            else:
555 569
                # find the right position
556 570
                for i in range(position, arglen):
557 571
                    key = keys[i]
558
                    if not key in parsed.keys():
572
                    if not key in canonified.keys():
559 573
                        position = i + 1
560 574
                        break
561 575
                else: # exhausted
562 576
                    raise Exception("shouldn't happen")
563
                parsed[key] = self.kw[key].parse(v)
564

  
565
        return parsed
566

  
567
    def _check(self, item):
568
        try:
569
            item = OrderedDict(item)
570
        except TypeError, e:
571
            m = "%s: %s is not dict-able" % (self, shorts(item))
572
            raise CanonifyException(m)
573

  
574
        canonified = OrderedDict()
575

  
576
        try:
577
            for n, c in self.kw.items():
578
                t = item[n] if n in item else None
579
                canonified[n] = c(t)
580
        except KeyError:
581
            m = ("%s: Argument '%s' not found in '%s'"
582
                        % (self, shorts(n), shorts(item)))
583
            raise CanonifyException(m)
577
                canonified[key] = kw[key].check(v)
584 578

  
585 579
        return canonified
586 580

  
b/snf-common/synnefo/lib/commissioning/utils/argmap.py
203 203

  
204 204

  
205 205
def argmap_decode_args(inputf):
206
    args = [ARGMAP_MAGIC]
207 206
    append = args.append
208 207
    s = inputf(1)
209 208
    key = None
......
215 214
        if s == ']':
216 215
            if key is not None:
217 216
                append((None, key))
217
	    args.append(ARGMAP_MAGIC)
218 218
            return args, None
219 219

  
220 220
        if s == '=':
......
243 243
    if hasattr(obj, 'keys'):
244 244
        # this could cover both cases
245 245
        return ARGMAP_MAGIC in obj
246
    return hasattr(obj, '__len__') and len(obj) and obj[0] == ARGMAP_MAGIC
246
    return hasattr(obj, '__len__') and len(obj) and obj[-1] == ARGMAP_MAGIC
247 247

  
248 248
def argmap_unzip_dict(argmap):
249 249
    if not hasattr(argmap, 'keys'):
......
258 258
    return args, kw
259 259

  
260 260
def argmap_unzip_list(argmap):
261
    if not argmap or argmap[0] != ARGMAP_MAGIC:
261
    if not argmap or argmap[-1] != ARGMAP_MAGIC:
262 262
        m = "argmap unzip list: magic not found"
263 263
        raise ValueError(m)
264 264

  
......
287 287
        raise ValueError(m)
288 288

  
289 289
def argmap_zip_list(args, kw):
290
    return [ARGMAP_MAGIC] + [(None, a) for a in args] + kw.items()
290
    return [(None, a) for a in args] + kw.items() + [ARGMAP_MAGIC]
291 291

  
292 292
def argmap_zip_dict(args, kw):
293 293
    argmap = OrderedDict()
......
298 298

  
299 299
argmap_zip = argmap_zip_list
300 300

  
301
def argmap_list_to_dict(argmap):
302
    args, kw = argmap_unzip_list(argmap)
303
    kw[ARGMAP_MAGIC] = ARGMAP_MAGIC
304
    kw[None] = args
305
    return kw
306

  
307
def argmap_dict_to_list(argmap):
308
    args, kw = argmap_unzip_dict(argmap)
309
    return args + kw.items() + [ARGMAP_MAGIC]
310

  

Also available in: Unified diff