1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
10 _DEFAULTS = "vstf check key defaults".encode()
13 def check(key, choices=[], defaults=_DEFAULTS):
15 def __deco(*args, **kwargs):
17 if defaults != _DEFAULTS:
18 kwargs[key] = defaults
20 raise Exception("Error: '%s' is needed in %s" % (key, func))
22 if choices and kwargs[key] not in choices:
23 raise Exception("Error: %s :%s" % (key, kwargs[key]))
24 ret = func(*args, **kwargs)
32 def dcheck(key, choices=[]):
41 if isinstance(values, dict):
43 raise Exception("Error: '%s' is needed in %s" % (key, func))
44 if choices and values[key] not in choices:
45 raise Exception("Error: %s :%s" % (key, values[key]))
54 def vstf_input(key, types=str, choices=[], default=None):
58 if not isinstance(ret, dict):
60 in_str = "----> %s : " % key
62 in_str = "---- %s\n" % (str(choices)) + in_str
64 if types == list or types == dict:
67 value = raw_input(in_str)
71 if not choices or value in choices:
73 ret.update({key: value})
83 def __deco(*args, **kwargs):
84 ret = func(*args, **kwargs)
85 nspace = kwargs.get("namespace", None)
87 ret = "ip netns exec %(namespace)s " % {"namespace": nspace} + ret