safe.metadata.base_metadata module

InaSAFE Disaster risk assessment tool developed by AusAid - metadata module.

Contact : ole.moller.nielsen@gmail.com

Note

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

class safe.metadata.base_metadata.BaseMetadata(layer_uri, xml_uri=None, json_uri=None)[source]

Bases: object

Abstract Metadata class, this has to be subclassed.

if you need to add a standard XML property add it to _standard_properties @property and @propname.setter will be generated automatically Standard properties are the ones that we try to read from an xml file when instantiating a new metadata object. Reading from json metadata files is easier because we have an ordered structure.

The class will try to read all she can without throwing errors because the more we can read from malformed input the better.

New in version 3.2.

dict[source]

dictionary representation of the metadata.

Returns:dictionary representation of the metadata
Return type:dict
get_property(name)[source]

get a property.

Parameters:name (str) – the name of the property
Returns:the property
Return type:BaseProperty
get_value(name)[source]

get the typed value of a property.

The type is the original python type used when the value was set

Parameters:name (str) – the name of the property
Returns:the value of the property
get_writable_metadata(file_format)[source]

Convert the metadata to a writable form.

Parameters:file_format (str) – the needed format can be json or xml
Returns:the dupled metadata
Return type:str
get_xml_value(name)[source]

get the xml value of a property.

Parameters:name (str) – the name of the property
Returns:the value of the property
Return type:str
json[source]

json representation of the metadata.

Returns:json representation of the metadata
Return type:str
json_uri[source]

the json file URI if it is None than the json is coming from a DB.

Returns:the json URI
Return type:str, None
last_update[source]

time of the last update of the metadata in memory.

Returns:time of the last update
Return type:datetime
layer_is_file_based[source]

flag if the layer is file based.

Returns:flag if the layer is file based
Return type:bool
layer_uri[source]

the layer URI.

Returns:the layer URI
Return type:str
properties[source]

get all properties.

Returns:the properties
Return type:dict
read_from_ancillary_file(custom_xml=None)[source]

try to read xml and json from existing files or db.

This is used when instantiating a new metadata object. We explicitly check if a custom XML was passed so we give it priority on the JSON. If no custom XML is passed, JSON has priority

Parameters:custom_xml (str) – the path to a custom xml file
read_json()[source]

read metadata from json and set all the found properties.

when overriding remember to wrap your calls in reading_ancillary_files

Returns:the read metadata
Return type:dict
read_xml()[source]

read metadata from xml and set all the found properties.

Returns:the root element of the xml
Return type:ElementTree.Element
save(save_json=True, save_xml=True)[source]

Saves the metadata json and/or xml to a file or DB.

Parameters:
  • save_json (bool) – flag to save json
  • save_xml (bool) – flag to save xml
set(name, value, xml_path)[source]

Create a new metadata property.

The accepted type depends on the property type which is determined by the xml_path

Parameters:
  • name (str) – the name of the property
  • value – the value of the property
  • xml_path – the xml path where the property should be stored.

This is split on / and the last element is used to determine the property type :type xml_path: str

set_last_update_to_now()[source]

set time of the last update of the metadata in memory to now.

update(name, value)[source]

update a property value.

The accepted type depends on the property type

Parameters:
  • name (str) – the name of the property
  • value – the new value
write_to_db(save_json=True, save_xml=True)[source]

Stores the metadata json and/or xml in a DB.

The returned tuple can contain None.

Parameters:
  • save_json (bool) – flag to save json
  • save_xml (bool) – flag to save xml
Returns:

the stored metadata

Return type:

(str, str)

write_to_file(destination_path)[source]

Writes the metadata json or xml to a file.

Parameters:destination_path – the file path the file format is inferred

from the destination_path extension. :type destination_path: str :return: the written metadata :rtype: str

xml[source]

xml representation of the metadata.

Returns:xml representation of the metadata
Return type:ElementTree.Element
xml_uri[source]

the xml file URI if it is None than the xml is coming from a DB.

Returns:the xml URI
Return type:str, None