Пример #1
0
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))
Пример #2
0
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_keywords(self, prop):
        """ Parse type-specific keywords from the metadata: Theme or Place """

        keywords = []

        if prop in KEYWORD_PROPS:
            xpath_root = self._data_map['_keywords_root']
            xpath_map = self._data_structures[prop]

            xtype = xpath_map['keyword_type']
            xpath = xpath_map['keyword']
            ktype = KEYWORD_TYPES[prop]

            for element in get_elements(self._xml_tree, xpath_root):
                if get_element_text(element, xtype).lower() == ktype.lower():
                    keywords.extend(get_elements_text(element, xpath))

        return keywords
    def _parse_keywords(self, prop):
        """ Parse type-specific keywords from the metadata: Theme or Place """

        keywords = []

        if prop in [KEYWORDS_PLACE, KEYWORDS_THEME]:
            xpath_root = self._data_map['_keywords_root']
            xpath_map = self._data_structures[prop]

            xtype = xpath_map['keyword_type']
            xpath = xpath_map['keyword']

            if prop == KEYWORDS_PLACE:
                ktype = KEYWORD_TYPE_PLACE
            elif prop == KEYWORDS_THEME:
                ktype = KEYWORD_TYPE_THEME

            for element in get_elements(self._xml_tree, xpath_root):
                if get_element_text(element, xtype).lower() == ktype.lower():
                    keywords.extend(get_elements_text(element, xpath))

        return keywords
Пример #5
0
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)
Пример #6
0
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)