safe.metadata.metadata_db_io module

metadata DB IO implementation.

class safe.metadata.metadata_db_io.MetadataDbIO[source]

Bases: PyQt4.QtCore.QObject

Class for doing metadata read/write operations on the local DB

The local sqlite db is used for supporting metadata for remote datasources.

New in version 3.2.

static are_metadata_file_based(layer)[source]

Check if metadata should be read/written to file or our metadata db.

Determine which metadata lookup system to use (file base or cache db) based on the layer’s provider type. True indicates we should use the datasource as a file and look for a metadata file, False and we look in the metadata db.

Parameters:layer (QgsMapLayer) – The layer which want to know how the metadata are stored.
Returns:True if metadata are stored in a file next to the dataset, else False if the dataset is remove e.g. a database.
Return type:bool
Raises:UnsupportedProviderError
close_connection()[source]

Close the active sqlite3 connection.

static default_metadata_db_path()[source]

Helper to get the default path for the metadata file.

Returns:The path to where the default location of the metadata database is. Maps to which is ~/.inasafe/metadata.db
Return type:str
delete_metadata_for_uri(uri)[source]

Delete metadata for a URI in the metadata database.

A hash will be constructed from the supplied uri and a lookup made in a local SQLITE database for the metadata. If there is an existing record for the hash, the entire record will be erased.

See also

write_metadata_for_uri, read_metadata_for_uri

Parameters:uri (str) – A layer uri. e.g. `dbname='osm' host=localhost port=5432 user='foo'password='bar' sslmode=disable key='id' srid=4326`
get_cursor()[source]

Get a cursor for the active connection.

The cursor can be used to execute arbitrary queries against the database. This method also checks that the metadata table exists in the schema, and if not, it creates it.

Returns:A valid cursor opened against the connection.
Return type:sqlite.
Raises:An sqlite.Error will be raised if anything goes wrong.
static hash_for_datasource(data_source)[source]

Given a data_source, return its hash.

Parameters:data_source (str) – The data_source name from a layer.
Returns:An md5 hash for the data source name.
Return type:str
open_connection()[source]

Open an sqlite connection to the metadata database.

By default the metadata database will be used in the plugin dir, unless an explicit path has been set using setmetadataDbPath, or overridden in QSettings. If the db does not exist it will be created.

Raises:An sqlite.Error is raised if anything goes wrong
read_metadata_from_uri(uri, metadata_format)[source]

Try to get metadata from the DB entry associated with a URI.

This is used for layers that are non local layer (e.g. postgresql connection) and so we need to retrieve the metadata from the sqlite metadata db.

A hash will be constructed from the supplied uri and a lookup made in a local SQLITE database for the metadata. If there is an existing record it will be returned, if not and error will be thrown.

See also

write_metadata_for_uri, delete_metadata_for_uri

Parameters:
  • uri (str) – A layer uri. e.g. `dbname='osm' host=localhost port=5432 user='foo' password='bar' sslmode=disable key='id' srid=4326`
  • metadata_format (str) – The format of the metadata to retrieve. Valid types are: ‘json’, ‘xml’
Returns:

A string containing the retrieved metadata

Raises:

metadataNotFoundError if the metadata is not found.

set_metadata_db_path(path)[source]

Set the path for the metadata database (sqlite).

The file will be used to search for metadata for non local datasets.

Parameters:path (str) – A valid path to a sqlite database. The database does not need to exist already, but the user should be able to write to the path provided.
setup_metadata_db_path()[source]

Helper to set the active path for the metadata.

Called at init time, you can override this path by calling set_metadata_db_path.setmetadataDbPath.

Returns:The path to where the metadata file is. If the user has never specified what this path is, the defaultmetadataDbPath is returned.
Return type:str
write_metadata_for_uri(uri, json=None, xml=None)[source]

Write metadata for a URI into the metadata database. All the metadata 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 metadata. If there is an existing record it will be updated, if not, a new one will be created.

See also

read_metadata_from_uri, delete_metadata_for_uri

Parameters:
  • uri (str) – A layer uri. e.g. `dbname='osm' host=localhost port=5432 user='foo' password='bar' sslmode=disable key='id' srid=4326`
  • json – The metadata to write (which should be provided as a

JSON str). :type json: str

Parameters:xml – The metadata to write (which should be provided as a

XML str). :type xml: str