Revision b9426097
b/snf-common/synnefo/lib/commissioning/specificator.py | ||
---|---|---|
129 | 129 |
return argmap[None] |
130 | 130 |
return argmap |
131 | 131 |
|
132 |
def parse(self, item): |
|
133 |
opts = self.opts |
|
134 |
if item is None and 'default' in opts: |
|
135 |
item = opts['default'] |
|
136 |
|
|
137 |
can_be_null = opts.get('null', False) |
|
138 |
if item is None and can_be_null: |
|
139 |
return None |
|
140 |
|
|
141 |
return self._parse(item) |
|
142 |
|
|
143 |
def _parse(self, item): |
|
144 |
raise NotImplementedError |
|
145 |
|
|
146 | 132 |
def create(self): |
147 | 133 |
return None |
148 | 134 |
|
... | ... | |
244 | 230 |
|
245 | 231 |
return num |
246 | 232 |
|
247 |
def _parse(self, item): |
|
248 |
return self.check(item) |
|
249 |
|
|
250 | 233 |
def random_integer(self, kw): |
251 | 234 |
optget = self.opts.get |
252 | 235 |
kwget = kw.get |
... | ... | |
343 | 326 |
|
344 | 327 |
return item |
345 | 328 |
|
346 |
def _parse(self, item): |
|
347 |
return self.check(item) |
|
348 |
|
|
349 | 329 |
default_alphabet = '0123456789αβγδεζ'.decode('utf8') |
350 | 330 |
|
351 | 331 |
def random_string(self, kw): |
... | ... | |
518 | 498 |
|
519 | 499 |
return canonified |
520 | 500 |
|
521 |
def _parse(self, item): |
|
522 |
if item is None: |
|
523 |
item = () |
|
524 |
|
|
525 |
try: |
|
526 |
items = iter(item) |
|
527 |
except TypeError, e: |
|
528 |
m = "%s: %s is not iterable" % (self, shorts(item)) |
|
529 |
raise CanonifyException(m) |
|
530 |
|
|
531 |
canonical = self.canonical |
|
532 |
canonified = [] |
|
533 |
append = canonified.append |
|
534 |
|
|
535 |
for k, v in items: |
|
536 |
item = canonical.parse(v) |
|
537 |
append(item) |
|
538 |
|
|
539 |
if not canonified and self.opts.get('nonempty', False): |
|
540 |
m = "%s: must be nonempty" % (self,) |
|
541 |
raise CanonifyException(m) |
|
542 |
|
|
543 |
return canonified |
|
544 |
|
|
545 | 501 |
def random_listof(self, kw): |
546 | 502 |
z = randint(1, 4) |
547 | 503 |
get_random = self.canonical.random |
... | ... | |
632 | 588 |
|
633 | 589 |
return tuple(g) |
634 | 590 |
|
635 |
def _parse(self, item): |
|
636 |
try: |
|
637 |
items = list(item) |
|
638 |
except TypeError, e: |
|
639 |
m = "%s: %s is not iterable" % (self, shorts(item)) |
|
640 |
raise CanonifyException(m) |
|
641 |
|
|
642 |
canonicals = self.args |
|
643 |
zi = len(items) |
|
644 |
zc = len(canonicals) |
|
645 |
|
|
646 |
if zi != zc: |
|
647 |
m = "%s: expecting %d elements, not %d (%s)" % (self, zc, zi, str(items)) |
|
648 |
raise CanonifyException(m) |
|
649 |
|
|
650 |
g = (canonical.parse(element) |
|
651 |
for canonical, (k, element) in zip(self.args, item)) |
|
652 |
return tuple(g) |
|
653 |
|
|
654 | 591 |
def __add__(self, other): |
655 | 592 |
oargs = other.args if isinstance(other, Tuple) else (other,) |
656 | 593 |
args = self.args + oargs |
... | ... | |
696 | 633 |
|
697 | 634 |
return canonified |
698 | 635 |
|
699 |
def _parse(self, item): |
|
700 |
|
|
701 |
try: |
|
702 |
item = dict(item) |
|
703 |
except TypeError: |
|
704 |
m = "%s: '%s' is not dict-able" % (self, shorts(item)) |
|
705 |
raise CanonifyException(m) |
|
706 |
|
|
707 |
canonified = {} |
|
708 |
canonical = self.kw |
|
709 |
|
|
710 |
for n, c in canonical.items(): |
|
711 |
if n not in item: |
|
712 |
m = "%s: key '%s' not found" % (self, shorts(n)) |
|
713 |
raise CanonifyException(m) |
|
714 |
canonified[n] = c(item[n]) |
|
715 |
|
|
716 |
strict = self.opts.get('strict', True) |
|
717 |
if strict and len(item) != len(canonical): |
|
718 |
for k in sorted(item.keys()): |
|
719 |
if k not in canonical: |
|
720 |
break |
|
721 |
|
|
722 |
m = "%s: unexpected key '%s' (strict mode)" % (self, shorts(k)) |
|
723 |
raise CanonifyException(m) |
|
724 |
|
|
725 |
return canonified |
|
726 |
|
|
727 | 636 |
def random_dict(self, kw): |
728 | 637 |
item = {} |
729 | 638 |
for n, c in self.canonical.items(): |
Also available in: Unified diff