Skip to content

Commit

Permalink
Add get_name to get the pretty name easily.
Browse files Browse the repository at this point in the history
  • Loading branch information
ismailsunni committed Jul 26, 2017
1 parent a4408a3 commit 4871612
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 13 deletions.
12 changes: 11 additions & 1 deletion safe/definitions/test/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
default_classification_value_maps,
fields_in_field_groups,
get_field_groups,
map_report_component
map_report_component,
get_name
)

from safe.common.utilities import safe_dir
Expand Down Expand Up @@ -95,6 +96,15 @@ def test_definition(self):
keyword_definition = definition(keyword)
self.assertTrue('description' in keyword_definition)

def test_get_name(self):
"""Test get_name method."""
flood_name = get_name(hazard_flood['key'])
self.assertEqual(flood_name, hazard_flood['name'])

not_exist_key = 'Mega flux capacitor'
not_found_name = get_name(not_exist_key)
self.assertEqual(not_exist_key, not_found_name)

def test_layer_purpose_for_layer(self):
"""Test for purpose_for_layer method."""
expected = ['aggregation', 'exposure', 'hazard']
Expand Down
26 changes: 26 additions & 0 deletions safe/definitions/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,32 @@ def definition(keyword):
return None


def get_name(keyword):
"""Given a keyword, try to get the name of it.
.. versionadded:: 4.2
Definition dicts are defined in keywords.py. We try to return
the name if present, otherwise we return none.
keyword = 'layer_purpose'
kio = safe.utilities.keyword_io.Keyword_IO()
name = kio.get_name(keyword)
print name
:param keyword: A keyword key.
:type keyword: str
:returns: The name of the keyword
:rtype: str
"""
definition_dict = definition(keyword)
if definition_dict:
return definition_dict.get('name', keyword)
# Else, return the keyword
return keyword


def get_allowed_geometries(layer_purpose_key):
"""Helper function to get all possible geometry
Expand Down
10 changes: 8 additions & 2 deletions safe/gui/widgets/dock.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
PREPARE_SUCCESS,
)
from safe.definitions.provenance import provenance_list
from safe.definitions.utilities import map_report_component
from safe.definitions.utilities import map_report_component, get_name
from safe.defaults import supporters_logo_path
from safe.definitions.reports import (
final_product_tag,
Expand Down Expand Up @@ -1435,7 +1435,13 @@ def write_project_variable(key, value):
write_project_variable(
'%s__%s' % (key, dict_key), dict_value)
elif isinstance(value, (bool, str, unicode, Number)):
QgsExpressionContextUtils.setProjectVariable(key, value)
# Don't use get_name for field
if 'field' in key:
pretty_value = get_name(value)
QgsExpressionContextUtils.setProjectVariable(
key, pretty_value)
else:
QgsExpressionContextUtils.setProjectVariable(key, value)
elif isinstance(value, type(None)):
QgsExpressionContextUtils.setProjectVariable(key, '')
elif isinstance(value, datetime):
Expand Down
22 changes: 12 additions & 10 deletions safe/impact_function/impact_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@
from safe.gis.raster.polygonize import polygonize
from safe.gis.raster.zonal_statistics import zonal_stats
from safe.definitions.analysis_steps import analysis_steps
from safe.definitions.utilities import definition, get_non_compulsory_fields
from safe.definitions.utilities import (
definition,
get_non_compulsory_fields,
get_name
)
from safe.definitions.exposure import indivisible_exposure
from safe.definitions.fields import (
size_field,
Expand Down Expand Up @@ -929,22 +933,20 @@ def prepare(self):
return status, message

# Set the name
hazard_name = definition(
self.hazard.keywords.get('hazard'))['name']
exposure_name = definition(
self.exposure.keywords.get('exposure'))['name']
hazard_geometry_name = definition(
self.hazard.keywords.get('layer_geometry'))['name']
exposure_geometry_name = definition(
self.exposure.keywords.get('layer_geometry'))['name']
hazard_name = get_name(self.hazard.keywords.get('hazard'))
exposure_name = get_name(self.exposure.keywords.get('exposure'))
hazard_geometry_name = get_name(
self.hazard.keywords.get('layer_geometry'))
exposure_geometry_name = get_name(
self.exposure.keywords.get('layer_geometry'))
self._name = tr(
'{hazard_type} {hazard_geometry} On {exposure_type} '
'{exposure_geometry}').format(
hazard_type=hazard_name,
hazard_geometry=hazard_geometry_name,
exposure_type=exposure_name,
exposure_geometry=exposure_geometry_name
)
).title()

# Set the title
if self.exposure.keywords.get('exposure') == 'population':
Expand Down

0 comments on commit 4871612

Please sign in to comment.