def write_keywords_for_uri(self, uri, keywords): """Write keywords for a URI into the keywords database. All the keywords for the uri should be written in a single operation. A hash will be constructed from the supplied uri and a lookup made in a local SQLite database for the keywords. If there is an existing record it will be updated, if not, a new one will be created. .. seealso:: read_keyword_from_uri, delete_keywords_for_uri :param uri: A layer uri. e.g. ```dbname=\'osm\' host=localhost port=5432 user=\'foo\' password=\'bar\' sslmode=disable key=\'id\' srid=4326``` :type uri: str :param keywords: The metadata keywords to write (which should be provided as a dict of key value pairs). :type keywords: dict :returns: The XML written to the DB :raises: KeywordNotFoundError if the keyword is not recognised. """ hash_value = self.hash_for_datasource(uri) try: cursor = self.get_cursor() # now see if we have any data for our hash sql = ('select dict from keyword where hash = \'%s\';' % hash_value) cursor.execute(sql) data = cursor.fetchone() metadata_xml = generate_iso_metadata(keywords) pickle_dump = pickle.dumps(metadata_xml, pickle.HIGHEST_PROTOCOL) if data is None: # insert a new rec # cursor.execute('insert into keyword(hash) values(:hash);', # {'hash': hash_value}) cursor.execute( 'insert into keyword(hash, dict) values(:hash, :dict);', { 'hash': hash_value, 'dict': sqlite.Binary(pickle_dump) }) self.connection.commit() else: # update existing rec cursor.execute('update keyword set dict=? where hash = ?;', (sqlite.Binary(pickle_dump), hash_value)) self.connection.commit() except sqlite.Error: LOGGER.exception('Error writing keywords to SQLite db %s' % self.keyword_db_path) # See if we can roll back. if self.connection is not None: self.connection.rollback() raise finally: self.close_connection() return metadata_xml
def write_keywords_for_uri(self, uri, keywords): """Write keywords for a URI into the keywords database. All the keywords for the uri should be written in a single operation. A hash will be constructed from the supplied uri and a lookup made in a local SQLite database for the keywords. If there is an existing record it will be updated, if not, a new one will be created. .. seealso:: read_keyword_from_uri, delete_keywords_for_uri :param uri: A layer uri. e.g. ```dbname=\'osm\' host=localhost port=5432 user=\'foo\' password=\'bar\' sslmode=disable key=\'id\' srid=4326``` :type uri: str :param keywords: The metadata keywords to write (which should be provided as a dict of key value pairs). :type keywords: dict :returns: The XML written to the DB :raises: KeywordNotFoundError if the keyword is not recognised. """ hash_value = self.hash_for_datasource(uri) try: cursor = self.get_cursor() # now see if we have any data for our hash sql = ( 'select dict from keyword where hash = \'%s\';' % hash_value) cursor.execute(sql) data = cursor.fetchone() metadata_xml = generate_iso_metadata(keywords) pickle_dump = pickle.dumps(metadata_xml, pickle.HIGHEST_PROTOCOL) if data is None: # insert a new rec # cursor.execute('insert into keyword(hash) values(:hash);', # {'hash': hash_value}) cursor.execute( 'insert into keyword(hash, dict) values(:hash, :dict);', {'hash': hash_value, 'dict': sqlite.Binary(pickle_dump)}) self.connection.commit() else: # update existing rec cursor.execute( 'update keyword set dict=? where hash = ?;', (sqlite.Binary(pickle_dump), hash_value)) self.connection.commit() except sqlite.Error: LOGGER.exception('Error writing keywords to SQLite db %s' % self.keyword_db_path) # See if we can roll back. if self.connection is not None: self.connection.rollback() raise finally: self.close_connection() return metadata_xml
def test_generate_iso_metadata(self): today = time.strftime("%Y-%m-%d") keywords = { 'category': 'exposure', 'datatype': 'itb', 'subcategory': 'building', 'title': 'Test TITLE' } metadata_xml = generate_iso_metadata(keywords) # lets see if the title substitution went well self.assertIn('<gco:CharacterString>Test TITLE', metadata_xml, 'XML should include %s' % today) # lets check if the date generation worked self.assertIn(today, metadata_xml, 'XML should include today\'s date (%s)' % today)
def test_generate_iso_metadata(self): today = time.strftime("%Y-%m-%d") keywords = { 'category': 'exposure', 'datatype': 'itb', 'subcategory': 'building', 'title': 'Test TITLE'} metadata_xml = generate_iso_metadata(keywords) # lets see if the title substitution went well self.assertIn( '<gco:CharacterString>Test TITLE', metadata_xml, 'XML should include %s' % today) # lets check if the date generation worked self.assertIn( today, metadata_xml, 'XML should include today\'s date (%s)' % today)