safe.utilities.utilities module

InaSAFE Disaster risk assessment tool developed by AusAid -
IS Utilities implementation.

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.

safe.utilities.utilities.add_ordered_combo_item(combo, text, data=None)[source]

Add a combo item ensuring that all items are listed alphabetically.

Although QComboBox allows you to set an InsertAlphabetically enum this only has effect when a user interactively adds combo items to an editable combo. This we have this little function to ensure that combos are always sorted alphabetically.

Parameters:
  • combo (QComboBox) – Combo box receiving the new item.
  • text (str) – Display text for the combo.
  • data (QVariant, str) – Optional UserRole data to be associated with the item.
safe.utilities.utilities.compare_version(version1, version2)[source]

Compare between InaSAFE version.

Adapted from http://stackoverflow.com/a/1714190/1198772

Parameters:
  • version1 (str) – String representation of version 1
  • version2 (str) – String representation of version 1
Returns:

-1, 0, 1 if less, same, and more respectively.

Return type:

int

safe.utilities.utilities.get_error_message(exception, context=None, suggestion=None)[source]

Convert exception into an ErrorMessage containing a stack trace.

Parameters:
  • exception (Exception) – Exception object.
  • context (str) – Optional context message.
  • suggestion (str) – Optional suggestion.
Returns:An error message with stack trace info suitable for display.
Return type:ErrorMessage
safe.utilities.utilities.html_to_file(html, file_path=None, open_browser=False)[source]

Save the html to an html file adapting the paths to the filesystem.

if a file_path is passed, it is used, if not a unique_filename is generated.

Parameters:
  • html (str) – the html for the output file.
  • file_path (str) – the path for the html output file.
  • open_browser (bool) – if true open the generated html in an external browser
safe.utilities.utilities.humanise_seconds(seconds)[source]

Utility function to humanise seconds value into e.g. 10 seconds ago.

The function will try to make a nice phrase of the seconds count provided.

Note

Currently seconds that amount to days are not supported.

Parameters:seconds (int) – Mandatory seconds value e.g. 1100.
Returns:A humanised version of the seconds count.
Return type:str
safe.utilities.utilities.impact_attribution(keywords, inasafe_flag=False)[source]

Make a little table for attribution of data sources used in impact.

Parameters:
  • keywords (dict) – A keywords dict for an impact layer.
  • inasafe_flag – bool - whether to show a little InaSAFE promotional text in the attribution output. Defaults to False.
Returns:

An html snippet containing attribution information for the impact layer. If no keywords are present or no appropriate keywords are present, None is returned.

Return type:

safe.messaging.Message

safe.utilities.utilities.open_in_browser(file_path)[source]

Open a file in the default web browser.

Parameters:file_path (str) – Path to the file that should be opened.
safe.utilities.utilities.read_file_keywords(layer_path, keyword=None)[source]
Get metadata from the keywords file associated with a local
file in the file system.

Note

Requires a str representing a file path instance as parameter As opposed to read_keywords_from_layer which takes a inasafe file object as parameter.

See also

read_keywords_from_layer

Param:layer_path: a string representing a path to a layer (e.g. ‘/tmp/foo.shp’, ‘/tmp/foo.tif’)
Parameters:keyword (str) – optional - the metadata keyword to retrieve e.g. ‘title’
Returns:A string containing the retrieved value for the keyword if the keyword argument is specified, otherwise the complete keywords dictionary is returned.
Raises:KeywordNotFoundError, NoKeywordsFoundError, InvalidParameterError
Note:
  • KeywordNotFoundError - occurs when the keyword is not recognised.
  • NoKeywordsFoundError - occurs when no keyword file exists.
  • InvalidParameterError - occurs when the layer does not exist.
safe.utilities.utilities.replace_accentuated_characters(message)[source]

Normalize unicode data in Python to remove umlauts, accents etc.

Parameters:message (str, unicode) – The string where to delete accentuated characters.
Returns:A string without umlauts, accents etc.
Return type:str
safe.utilities.utilities.write_keywords_to_file(filename, keywords)[source]

Thin wrapper around the safe write_keywords function.

Parameters:
  • filename (str) – Path to layer that must be written. If the file does not end in .keywords, its extension will be stripped off and the basename + .keywords will be used as the file.
  • keywords (dict) – A dictionary of keywords to be written