def _update_keywords(self, **update_props):
        """ Update operation for ISO type-specific Keywords metadata: Theme or Place """

        tree_to_update = update_props['tree_to_update']
        prop = update_props['prop']
        values = update_props['values']

        keywords = []

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

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

            # Remove descriptiveKeyword nodes according to type
            for element in get_elements(tree_to_update, xpath_root):
                if get_element_text(element, xtype).lower() == ktype.lower():
                    remove_element(tree_to_update, xpath_root)

            element = insert_element(tree_to_update, 0, xpath_root)
            insert_element(element, 0, xtype, ktype)  # Add the type node

            keywords.extend(update_property(element, xroot, xpath, prop, values))

        return keywords
    def _update_keywords(self, **update_props):
        """ Update operation for ISO type-specific Keywords metadata: Theme or Place """

        tree_to_update = update_props['tree_to_update']
        prop = update_props['prop']
        values = update_props['values']

        keywords = []

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

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

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

            # Remove descriptiveKeyword nodes according to type
            for element in get_elements(tree_to_update, xpath_root):
                if get_element_text(element, xtype).lower() == ktype.lower():
                    remove_element(tree_to_update, xpath_root)

            element = insert_element(tree_to_update, 0, xpath_root)
            insert_element(element, 0, xtype, ktype)  # Add the type node

            keywords.extend(update_property(element, xroot, xpath, prop, values))

        return keywords
    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
Example #4
0
    def test_iso_parser(self):
        """ Tests behavior unique to the ISO parser """

        # Remove the attribute details href attribute
        iso_element = get_remote_element(self.iso_file)
        for citation_element in get_elements(iso_element, _iso_tag_formats["_attr_citation"]):
            removed = remove_element_attributes(citation_element, "href")

        # Assert that the href attribute was removed and a different one was read in
        iso_parser = IsoParser(element_to_string(iso_element))
        attribute_href = iso_parser._attr_details_file_url

        self.assertIsNotNone(removed, "ISO file URL was not removed")
        self.assertIsNotNone(attribute_href, "ISO href attribute was not read in")
        self.assertNotEqual(attribute_href, removed, "ISO href attribute is the same as the one removed")

        # Remove the attribute details linkage attribute
        iso_element = get_remote_element(self.iso_file)
        for linkage_element in get_elements(iso_element, _iso_tag_formats["_attr_contact_url"]):
            removed = get_element_text(linkage_element)
            clear_element(linkage_element)

        # Assert that the linkage URL was removed and a different one was read in
        iso_parser = IsoParser(element_to_string(iso_element))
        linkage_url = iso_parser._attr_details_file_url

        self.assertIsNotNone(removed, "ISO linkage URL was not removed")
        self.assertIsNotNone(linkage_url, "ISO linkage URL was not read in")
        self.assertNotEqual(linkage_url, removed, "ISO file URL is the same as the one removed")

        # Change the href attribute so that it is invalid
        for citation_element in get_elements(iso_element, _iso_tag_formats["_attr_citation"]):
            removed = set_element_attributes(citation_element, href="neither url nor file")

        # Assert that the href attribute was removed and a different one was read in
        iso_parser = IsoParser(element_to_string(iso_element))
        attributes = iso_parser.attributes
        self.assertIsNone(iso_parser._attr_details_file_url, "Invalid URL stored with parser")
        self.assertEqual(
            attributes, TEST_METADATA_VALUES[ATTRIBUTES], "Invalid parsed attributes: {0}".format(attributes)
        )
    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