def _update_report_item(self, **update_props): """ Update the text for each element at the configured path if attribute matches """ tree_to_update = update_props['tree_to_update'] prop = update_props['prop'] values = wrap_value(update_props['values']) xroot = self._get_xroot_for(prop) attr_key = 'type' attr_val = u'' if prop == 'attribute_accuracy': attr_val = 'DQQuanAttAcc' elif prop == 'dataset_completeness': attr_val = 'DQCompOm' # Clear (make empty) all elements of the appropriate type for elem in get_elements(tree_to_update, xroot): if get_element_attributes(elem).get(attr_key) == attr_val: clear_element(elem) # Remove all empty elements, including those previously cleared remove_empty_element(tree_to_update, xroot) # Insert elements with correct attributes for each new value attrs = {attr_key: attr_val} updated = [] for idx, value in enumerate(values): elem = insert_element(tree_to_update, idx, xroot, **attrs) updated.append(insert_element(elem, idx, 'measDesc', value)) return updated
def has_property(elem_to_parse, xpath): """ Parse xpath for any attribute reference "path/@attr" and check for root and presence of attribute. :return: True if xpath is present in the element along with any attribute referenced, otherwise False """ xroot, attr = get_xpath_tuple(xpath) if not xroot: return False elif not attr: return bool(get_elements_text(elem_to_parse, xroot)) else: return bool(get_element_attributes(elem_to_parse, xroot).get(attr))
def has_property(elem_to_parse, xpath): """ Parse xpath for any attribute reference "path/@attr" and check for root and presence of attribute. :return: True if xpath is present in the element along with any attribute referenced, otherwise False """ xroot, attr = get_xpath_tuple(xpath) if not xroot and not attr: return False elif not attr: return bool(get_elements_text(elem_to_parse, xroot)) else: return bool(get_element_attributes(elem_to_parse, xroot).get(attr))
def parse_property(tree_to_parse, xpath_root, xpath_map, prop): """ Defines the default parsing behavior for metadata values. :param tree_to_parse: the XML tree compatible with element_utils to be parsed :param xpath_root: used to determine the relative XPATH location within the parent element :param xpath_map: a dict of XPATHs that may contain alternate locations for a property :param prop: the property to parse: corresponds to a key in xpath_map """ xpath = xpath_map[prop] if isinstance(xpath, ParserProperty): if xpath.xpath is None: return xpath.get_prop(prop) xpath = xpath.xpath if xpath_root: xpath = get_xpath_branch(xpath_root, xpath) parsed = None if not has_property(tree_to_parse, xpath): # Element has no text: try next alternate location alternate = '_' + prop if alternate in xpath_map: return parse_property(tree_to_parse, xpath_root, xpath_map, alternate) elif '@' not in xpath: parsed = get_elements_text(tree_to_parse, xpath) else: xroot, xattr = get_xpath_tuple(xpath) parsed = get_element_attributes(tree_to_parse, xroot).get(xattr) return get_default_for(prop, parsed)