def find_element_by_tag_and_id(tag, context_element, element_id, none_if_id_unused=False, id_types=None): """ Return element with given tag and element_id under context_element. When element does not exists raises LibraryError or return None if specified in none_if_id_unused. etree.Element(Tree) context_element is part of tree for element scan string|list tag is expected tag (or list of tags) of search element string element_id is id of search element bool none_if_id_unused if the element is not found then return None if True or raise a LibraryError if False list id_types optional list of descriptions for id / expected types of id """ tag_list = [tag] if is_string(tag) else tag if id_types is None: id_type_list = tag_list elif is_string(id_types): id_type_list = [id_types] else: id_type_list = id_types element_list = context_element.xpath('.//*[({0}) and @id="{1}"]'.format( " or ".join(["self::{0}".format(one_tag) for one_tag in tag_list]), element_id)) if element_list: return element_list[0] element = get_root(context_element).find( './/*[@id="{0}"]'.format(element_id)) if element is not None: raise LibraryError( reports.id_belongs_to_unexpected_type( element_id, expected_types=tag_list, current_type=element.tag ) if element.tag not in tag_list else reports.object_with_id_in_unexpected_context( element.tag, element_id, context_element. tag, context_element.attrib.get("id", ""))) if none_if_id_unused: return None raise LibraryError( reports.id_not_found(element_id, id_type_list, context_element.tag, context_element.attrib.get("id", "")))
def is_empty_string(value): """ Check if the specified value is an empty string mixed value -- value to check """ return is_string(value) and not value
def remove(cib_tree): xpath_list = ( [element_xpath] if is_string(element_xpath) else element_xpath ) for xpath in xpath_list: element_to_remove = _find_in(cib_tree, xpath) element_to_remove.getparent().remove(element_to_remove)
def find_element_by_tag_and_id( tag, context_element, element_id, none_if_id_unused=False, id_description="" ): """ Return element with given tag and element_id under context_element. When element does not exists raises LibraryError or return None if specified in none_if_id_unused. etree.Element(Tree) context_element is part of tree for element scan string|list tag is expected tag (or list of tags) of search element string element_id is id of search element bool none_if_id_unused is flag, when is True and element with element_id does not exists function returns None string id_description optional description for id """ tag_list = [tag] if is_string(tag) else tag element_list = context_element.xpath( './/*[({0}) and @id="{1}"]'.format( " or ".join(["self::{0}".format(one_tag) for one_tag in tag_list]), element_id ) ) if element_list: return element_list[0] element = get_root(context_element).find( './/*[@id="{0}"]'.format(element_id) ) if element is not None: raise LibraryError( reports.id_belongs_to_unexpected_type( element_id, expected_types=tag_list, current_type=element.tag ) if element.tag not in tag_list else reports.object_with_id_in_unexpected_context( element.tag, element_id, context_element.tag, context_element.attrib.get("id", "") ) ) if none_if_id_unused: return None raise LibraryError( reports.id_not_found( element_id, id_description if id_description else "/".join(tag_list), context_element.tag, context_element.attrib.get("id", "") ) )
def build_node_description(node_types): if not node_types: return "Node" label = "{0} node".format if is_string(node_types): return label(node_types) if len(node_types) == 1: return label(node_types[0]) return "nor " + " or ".join([label(ntype) for ntype in node_types])
def create_patcher(target_prefix_or_module): """ Return function for patching tests with preconfigured target prefix string|module target_prefix_or_module could be: * a prefix for patched names. Typicaly tested module: "pcs.lib.commands.booth" * a (imported) module: pcs.lib.cib Between prefix and target is "." (dot) """ prefix = target_prefix_or_module if not is_string(target_prefix_or_module): prefix = target_prefix_or_module.__name__ def patch(target, *args, **kwargs): return mock.patch("{0}.{1}".format(prefix, target), *args, **kwargs) return patch
def test_recognize_unicode_string(self): #in python3 this is str type self.assertTrue(is_string(u""))
def test_recognize_plain_string(self): self.assertTrue(is_string(""))
def test_list_of_string_is_not_string(self): self.assertFalse(is_string(["a", "b"]))
def test_rcognize_bytes(self): #in python3 this is str type self.assertTrue(is_string(b""))
" allowed option{are} {allowed_values}").format( desc=format_optional(info["option_type"], "{0} "), allowed_values=", ".join(sorted(info["allowed"])), option_names_list=", ".join( sorted(["'{0}'".format(name) for name in info["option_names"]])), s=("s:" if len(info["option_names"]) > 1 else ""), are=("s are:" if len(info["allowed"]) > 1 else " is"), **info), codes.INVALID_OPTION_VALUE: lambda info: #value on key "allowed_values" is overloaded: # * it can be a list - then it express possible option values # * it can be a string - then it is verbal description of value "'{option_value}' is not a valid {option_name} value, use {hint}".format( hint=(", ".join(sorted(info["allowed_values"])) if (isinstance(info["allowed_values"], Iterable) and not is_string( info["allowed_values"])) else info["allowed_values"]), **info), codes.INVALID_OPTION_TYPE: lambda info: #value on key "allowed_types" is overloaded: # * it can be a list - then it express possible option types # * it can be a string - then it is verbal description of the type "specified {option_name} is not valid, use {hint}".format(hint=( ", ".join(sorted(info["allowed_types"])) if (isinstance(info["allowed_types"], Iterable) and not is_string(info[ "allowed_types"])) else info["allowed_types"]), **info), codes.EMPTY_ID: lambda info: "{id_description} cannot be empty".format(**info), codes.INVALID_ID: lambda info: ("invalid {id_description} '{id}', '{invalid_character}' "
are=("s are:" if len(info["allowed"]) > 1 else " is"), **info ) , codes.INVALID_OPTION_VALUE: lambda info: #value on key "allowed_values" is overloaded: # * it can be a list - then it express possible option values # * it can be a string - then it is verbal description of value "'{option_value}' is not a valid {option_name} value, use {hint}" .format( hint=( ", ".join(sorted(info["allowed_values"])) if ( isinstance(info["allowed_values"], Iterable) and not is_string(info["allowed_values"]) ) else info["allowed_values"] ), **info ) , codes.INVALID_OPTION_TYPE: lambda info: #value on key "allowed_types" is overloaded: # * it can be a list - then it express possible option types # * it can be a string - then it is verbal description of the type "specified {option_name} is not valid, use {hint}" .format( hint=( ", ".join(sorted(info["allowed_types"])) if ( isinstance(info["allowed_types"], Iterable)