def test_allow_java(self): assert_equals(is_dict_like({}, allow_java=True), True) assert_equals(is_dict_like([], allow_java=True), False) if sys.platform.startswith('java'): assert_equals(is_dict_like(HashMap()), False) assert_equals(is_dict_like(HashMap(), allow_java=True), True) assert_equals(is_dict_like([], allow_java=True), False)
def test_allow_java(self): assert_equals(is_dict_like({}, allow_java=True), True) assert_equals(is_dict_like([], allow_java=True), False) if sys.platform.startswith('java'): assert_equals(is_dict_like(HashMap()), sys.version_info >= (2, 7, 0, 'beta', 4)) assert_equals(is_dict_like(HashMap(), allow_java=True), True) assert_equals(is_dict_like([], allow_java=True), False)
def validate(self, types): if is_dict_like(types): return self.validate_type_dict(types) if is_list_like(types): return self.convert_type_list_to_dict(types) raise DataError('Type information must be given as a dictionary or ' 'a list, got %s.' % type_name(types))
def _validate(self, name, value): if name[0] == '@' and not is_list_like(value): raise DataError("Invalid variable '%s': Expected list-like value, " "got %s." % (name, type_name(value))) if name[0] == '&' and not is_dict_like(value): raise DataError("Invalid variable '%s': Expected dict-like value, " "got %s." % (name, type_name(value)))
def _import(self, path): with open(path) as stream: variables = yaml.load(stream) if not is_dict_like(variables): raise DataError('YAML variable file must be a mapping, got %s.' % type_name(variables)) return variables.items()
def _import(self, path): with io.open(path, encoding='UTF-8') as stream: variables = self._load_yaml(stream) if not is_dict_like(variables): raise DataError('YAML variable file must be a mapping, got %s.' % type_name(variables)) return variables.items()
def _get_variable_item(self, name, variable, item): if is_dict_like(variable): return self._get_dict_variable_item(name, variable, item) if is_list_like(variable): return self._get_list_variable_item(name, variable, item) raise VariableError("Variable '%s' is %s, not list or dictionary, " "and thus accessing item '%s' from it is not " "possible." % (name, type_name(variable), item))
def _handle_binary(self, value): if isinstance(value, xmlrpclib.Binary): return str(value) if is_list_like(value): return [self._handle_binary(v) for v in value] if is_dict_like(value): return dict((k, self._handle_binary(v)) for k, v in value.items()) return value
def _decorate(self, name, value): if is_dict_like(value): name = '&{%s}' % name elif is_list_like(value): name = '@{%s}' % name else: name = '${%s}' % name return name, value
def _handle_binary(self, value): if isinstance(value, xmlrpclib.Binary): return value.data if is_list_like(value): return [self._handle_binary(v) for v in value] if is_dict_like(value): return dict((k, self._handle_binary(v)) for k, v in value.items()) return value
def _convert(self, value): if isinstance(value, xmlrpclib.Binary): return bytes(value.data) if is_dict_like(value): return DotDict((k, self._convert(v)) for k, v in value.items()) if is_list_like(value): return [self._convert(v) for v in value] return value
def _get_dynamic(self, var_file, args): get_variables = (getattr(var_file, 'get_variables', None) or getattr(var_file, 'getVariables')) variables = get_variables(*args) if is_dict_like(variables): return variables.items() raise DataError("Expected '%s' to return dict-like value, got %s." % (get_variables.__name__, type_name(variables)))
def validate(self, types): if not types: return {} if is_dict_like(types): return self.validate_type_dict(types) if is_list_like(types): return self.convert_type_list_to_dict(types) raise DataError('Type information must be given as a dictionary or ' 'a list, got %s.' % type_name(types))
def __init__(self, result): if not (is_dict_like(result) and 'status' in result): raise RuntimeError('Invalid remote result dictionary: %s' % result) self.status = result['status'] self.output = unic(self._get(result, 'output')) self.return_ = self._get(result, 'return') self.error = unic(self._get(result, 'error')) self.traceback = unic(self._get(result, 'traceback')) self.fatal = bool(self._get(result, 'fatal', False)) self.continuable = bool(self._get(result, 'continuable', False))
def _get_converters(self, libcode): converters = getattr(libcode, 'ROBOT_LIBRARY_CONVERTERS', None) if not converters: return None if not is_dict_like(converters): self.report_error( f'Argument converters must be given as a dictionary, ' f'got {type_name(converters)}.') return None return CustomArgumentConverters.from_dict(converters, self.report_error)
def _undecorate(self, name, value): validate_var(name) if name[0] == '@': if not is_list_like(value): self._raise_cannot_set_type(name, value, 'list') value = list(value) if name[0] == '&': if not is_dict_like(value): self._raise_cannot_set_type(name, value, 'dictionary') value = DotDict(value) return name[2:-1], value
def _validate_value(self, value, identifier, name): if identifier == '@': if not is_list_like(value): raise VariableError("Value of variable '%s' is not list or " "list-like." % name) return list(value) if identifier == '&': if not is_dict_like(value): raise VariableError("Value of variable '%s' is not dictionary " "or dictionary-like." % name) return DotDict(value) return value
def _get_dynamical_variables(self, var_file, args): get_variables = getattr(var_file, "get_variables", None) if not get_variables: get_variables = getattr(var_file, "getVariables", None) if not get_variables: return None variables = get_variables(*args) if utils.is_dict_like(variables): return variables.items() if isinstance(variables, Map): return [(entry.key, entry.value) for entry in variables.entrySet()] raise DataError("Expected mapping but %s returned %s." % (get_variables.__name__, type(variables).__name__))
def _undecorate(self, name, value): if not is_assign(name): raise DataError("Invalid variable name '%s'." % name) if name[0] == '@': if not is_list_like(value): self._raise_cannot_set_type(name, value, 'list') value = list(value) if name[0] == '&': if not is_dict_like(value): self._raise_cannot_set_type(name, value, 'dictionary') value = DotDict(value) return name[2:-1], value
def _validate_value(self, value, identifier, name): if identifier == '@': if not is_list_like(value): raise DataError("Value of variable '%s' is not list or " "list-like." % name) return list(value) if identifier == '&': if not is_dict_like(value): raise DataError("Value of variable '%s' is not dictionary " "or dictionary-like." % name) return DotDict(value) return value
def _get_dynamical_variables(self, var_file, args): get_variables = getattr(var_file, 'get_variables', None) if not get_variables: get_variables = getattr(var_file, 'getVariables', None) if not get_variables: return None variables = get_variables(*args) if utils.is_dict_like(variables): return variables.items() if isinstance(variables, Map): return [(entry.key, entry.value) for entry in variables.entrySet()] raise DataError("Expected mapping but %s returned %s." % (get_variables.__name__, type(variables).__name__))
def _get_dynamical_variables(self, var_file, args): get_variables = getattr(var_file, 'get_variables', None) if not get_variables: get_variables = getattr(var_file, 'getVariables', None) if not get_variables: return None variables = get_variables(*args) if is_dict_like(variables): return variables.items() # TODO: This shouldn't be needed after Jython 2.7 beta 4 if isinstance(variables, Map): return [(entry.key, entry.value) for entry in variables.entrySet()] raise DataError("Expected mapping but %s returned %s." % (get_variables.__name__, type(variables).__name__))
def _validate_value(self, value, identifier, name): if identifier == '@': if not is_list_like(value): raise VariableError("Value of variable '%s' is not list or " "list-like." % name) # TODO: Is converting to list needed or would checking be enough? # TODO: Check this and DotDict usage below in RF 3.1. return list(value) if identifier == '&': if not is_dict_like(value): raise VariableError("Value of variable '%s' is not dictionary " "or dictionary-like." % name) # TODO: Is converting to DotDict needed? Check in RF 3.1. return DotDict(value) return value
def _get_variable_item(self, match, value): name = match.name for item in match.items: if is_dict_like(value): value = self._get_dict_variable_item(name, value, item) elif hasattr(value, '__getitem__'): value = self._get_sequence_variable_item(name, value, item) else: raise VariableError( "Variable '%s' is %s, which is not subscriptable, and " "thus accessing item '%s' from it is not possible. To use " "'[%s]' as a literal value, it needs to be escaped like " "'\\[%s]'." % (name, type_name(value), item, item, item)) name = '%s[%s]' % (name, item) return value
def _get_variable_item(self, name, value, match): if match.identifier in '@&': var = '%s[%s]' % (name, match.items[0]) logger.warn("Accessing variable items using '%s' syntax " "is deprecated. Use '$%s' instead." % (var, var[1:])) for item in match.items: if is_dict_like(value): value = self._get_dict_variable_item(name, value, item) elif is_list_like(value): value = self._get_list_variable_item(name, value, item) else: raise VariableError( "Variable '%s' is %s, not list or dictionary, and thus " "accessing item '%s' from it is not possible." % (name, type_name(value), item)) name = '%s[%s]' % (name, item) return value
def _get_variable_item(self, match, value): name = match.name if match.identifier in '@&': var = '%s[%s]' % (name, match.items[0]) logger.warn("Accessing variable items using '%s' syntax " "is deprecated. Use '$%s' instead." % (var, var[1:])) for item in match.items: if is_dict_like(value): value = self._get_dict_variable_item(name, value, item) elif hasattr(value, '__getitem__'): value = self._get_sequence_variable_item(name, value, item) else: raise VariableError( "Variable '%s' is %s, which is not subscriptable, and " "thus accessing item '%s' from it is not possible. To use " "'[%s]' as a literal value, it needs to be escaped like " "'\\[%s]'." % (name, type_name(value), item, item, item)) name = '%s[%s]' % (name, item) return value
def _take_copy_of_mutable_value(self, value): if is_dict_like(value): return dict(value) if is_list_like(value): return list(value) return value
def _dot_dict(self, value): if is_dict_like(value): value = DotDict((n, self._dot_dict(v)) for n, v in value.items()) return value
def _validate_dictionary(self, dictionary, position=1): if not is_dict_like(dictionary): raise TypeError( f"Expected argument {position} to be a dictionary or " f"dictionary-like, got {type_name(dictionary)} instead.")
def test_dict_likes(self): for thing in [dict(), UserDict(), MyMapping()]: assert_equal(is_dict_like(thing), True, thing)
def _extra_arg_has_kwargs(self, positional, named): if named or len(positional) != self._maxargs + 1: return False return is_dict_like(positional[-1])
def test_others(self): for thing in ['', u'', 1, None, True, object(), [], (), set()]: assert_equal(is_dict_like(thing), False, thing)
def test_java_maps(self): assert_equal(is_dict_like(HashMap()), True) assert_equal(is_dict_like(Hashtable()), True)
def _get_list_index_name_mapping(self, names, list_length): if not names: return {} if is_dict_like(names): return dict((int(index), names[index]) for index in names) return dict(zip(range(list_length), names))