def filter_kwargs(klass, kwargs): res = {} LOG.debug('args: %s', kwargs) dummy = klass(**{k: kwargs[k] for k in klass.identity_attributes}) for k, v in kwargs.iteritems(): if v is not ATTR_UNSPECIFIED: try: attr_type = klass.other_attributes.get(k) is_list_of_dicts = (attr_type and attr_type.get("type") == "array" and attr_type.get( "items", {}).get("type") == "object") is_boolean = (attr_type and attr_type.get("type") == "boolean") if k in DICT_LIST_ATTRS or is_list_of_dicts: res[k] = [{z.split('=')[0]: z.split('=')[1] for z in y} for y in [x.split(',') for x in v.split(' ')]] if v else [] elif k in BOOL_ATTRS or is_boolean: b = utils.stob(v) if b is None: raise click.BadParameter( 'Invalid value %s for boolean ' 'attribute %s' % (v, k), param_hint="--%s" % k) res[k] = utils.stob(v) elif isinstance(getattr(dummy, k), list): res[k] = v.split(',') if v else [] else: res[k] = v except AttributeError: # No default is specified for this attribute, assume no list res[k] = v LOG.debug('Sanitized args: %s', kwargs) return res
def filter_kwargs(klass, kwargs): res = {} LOG.debug('args: %s', kwargs) dummy = klass(**{k: kwargs[k] for k in klass.identity_attributes}) for k, v in kwargs.iteritems(): if v is not ATTR_UNSPECIFIED: try: attr_type = klass.other_attributes.get(k) is_list_of_dicts = ( attr_type and attr_type.get("type") == "array" and attr_type.get("items", {}).get("type") == "object") is_boolean = (attr_type and attr_type.get("type") == "boolean") if k in DICT_LIST_ATTRS or is_list_of_dicts: res[k] = [{z.split('=')[0]: z.split('=')[1] for z in y} for y in [x.split(',') for x in v.split(' ')]] if v else [] elif k in BOOL_ATTRS or is_boolean: b = utils.stob(v) if b is None: raise click.BadParameter( 'Invalid value %s for boolean ' 'attribute %s' % (v, k), param_hint="--%s" % k) res[k] = utils.stob(v) elif isinstance(getattr(dummy, k), list): res[k] = v.split(',') if v else [] else: res[k] = v except AttributeError: # No default is specified for this attribute, assume no list res[k] = v LOG.debug('Sanitized args: %s', kwargs) return res
def _parse(self, res, klass=None): if not res.output_bytes: return None res = [' '.join(x.split()) for x in res.output_bytes.split('\n')][1:] res = [[x[:x.find(' ')], x[x.find(' ') + 1:]] for x in res if x] if ['Property', 'Value'] in res: # Remove additional tables # TODO(ivar): test expected faults res = res[:res.index(['Property', 'Value'])] res_dict = {} klass = klass or self.resource_class klass_attributes = klass.attributes for item in res: if len(item) == 2 and item[0] in klass_attributes(): attr_type = klass.other_attributes.get(item[0]) is_boolean = (attr_type and attr_type.get("type") == "boolean") try: # Try to load lists loaded = ast.literal_eval(item[1]) if isinstance(loaded, list): res_dict[item[0]] = loaded continue except (SyntaxError, ValueError): pass if is_boolean: res_dict[item[0]] = utils.stob(item[1]) else: res_dict[item[0]] = item[1] return klass(**res_dict)
def _parse(self, res, klass=None): output_bytes = _get_output_bytes(res) if not output_bytes: return None res = [' '.join(x.split()) for x in output_bytes.split('\n')][1:] res = [[x[:x.find(' ')], x[x.find(' ') + 1:]] for x in res if x] if ['Property', 'Value'] in res: # Remove additional tables # TODO(ivar): test expected faults res = res[:res.index(['Property', 'Value'])] res_dict = {} klass = klass or self.resource_class klass_attributes = klass.attributes for item in res: if len(item) == 2 and item[0] in klass_attributes(): attr_type = klass.other_attributes.get(item[0]) is_boolean = (attr_type and attr_type.get("type") == "boolean") try: # Try to load lists loaded = ast.literal_eval(item[1]) if isinstance(loaded, list): res_dict[item[0]] = loaded continue except (SyntaxError, ValueError): pass if is_boolean: res_dict[item[0]] = utils.stob(item[1]) else: res_dict[item[0]] = item[1] return klass(**res_dict)