Permalink
Browse files

Added option to equip impact functions with a title as per issue #131

  • Loading branch information...
1 parent 05b2ae7 commit 95e49bf2512b671abb3050c0f46c1e660ba50537 @uniomni uniomni committed Jun 27, 2012
Showing with 71 additions and 4 deletions.
  1. +14 −2 gui/is_dock.py
  2. +22 −0 gui/is_safe_interface.py
  3. +1 −0 impact_functions/__init__.py
  4. +17 −0 impact_functions/core.py
  5. +16 −2 impact_functions/test_plugin_core.py
  6. +1 −0 safe_api.py
View
@@ -34,6 +34,7 @@
QgsCoordinateTransform)
from is_impact_calculator import ISImpactCalculator
from is_safe_interface import (availableFunctions,
+ getFunctionTitle,
getOptimalExtent,
getBufferedExtent,
internationalisedNames)
@@ -569,8 +570,19 @@ def getFunctions(self):
try:
myDict = availableFunctions(myList)
# Populate the hazard combo with the available functions
- for myFunction in myDict: # Use only key
- self.addComboItemInOrder(self.cboFunction, myFunction)
+ for myFunctionID in myDict:
+ myFunction = myDict[myFunctionID]
+ myTitle = getFunctionTitle(myFunction)
+
+ # KEEPING THESE STATEMENTS FOR DEBUGGING UNTIL SETTLED
+ #print
+ #print 'myFunction (ID)', myFunctionID
+ #print 'myFunction', myFunction
+ #print 'Function title:', myTitle
+
+ # FIXME (Ole): How do we show the title and and the same
+ # time keep myFunction as the canonical function identifier
+ self.addComboItemInOrder(self.cboFunction, myFunctionID)
except Exception, e:
raise e
View
@@ -28,6 +28,7 @@
# SAFE functionality
from safe_api import get_admissible_plugins
+from safe_api import get_function_title
from safe_api import get_plugins as safe_get_plugins
from safe_api import read_keywords, bbox_intersection
from safe_api import write_keywords as safe_write_keywords
@@ -405,6 +406,25 @@ def getSafeImpactFunctions(theFunction=None):
raise
+def getFunctionTitle(theFunction):
+ """Thin wrapper around the safe get_function_title.
+
+ Args:
+ * theFunction - SAFE impact function instance to be used
+ Returns:
+ The title of a safe impact function is returned
+ Raises:
+ Any exceptions are propogated
+ """
+
+ # FIXME (Ole): I don't think we have to do try-except-raise. It would be the same just
+ # to call the function and let Python's normal exception handling
+ # propagate exceptions (just saving some lines and complexity)
+ try:
+ return get_function_title(theFunction)
+ except:
+ raise
+
def calculateSafeImpact(theLayers, theFunction):
"""Thin wrapper around the safe calculate_impact function.
@@ -421,3 +441,5 @@ def calculateSafeImpact(theLayers, theFunction):
return safe_calculate_impact(theLayers, theFunction)
except:
raise
+
+
@@ -27,3 +27,4 @@
from impact_functions.core import get_plugins # FIXME: Deprecate
from impact_functions.core import get_admissible_plugins
from impact_functions.core import compatible_layers
+from impact_functions.core import get_function_title
View
@@ -46,6 +46,23 @@ class FunctionProvider:
symbol_field = 'USE_MAJOR'
+def get_function_title(func):
+ """Get title for impact function
+
+ Input
+ func: Impact function class
+
+ Output
+ it's title if available as an attribute in the class description,
+ otherwise what is returned by the function pretty_function_name.
+ """
+
+ if hasattr(func, 'title'):
+ return func.title
+ else:
+ return pretty_function_name(func)
+
+
def get_plugins(name=None):
"""Retrieve a list of plugins that match the name you pass
@@ -15,6 +15,7 @@
from core import requirement_check
from core import requirements_met
from core import get_admissible_plugins
+from core import get_function_title
class BasicFunction(FunctionProvider):
@@ -48,6 +49,8 @@ class F1(FunctionProvider):
"""
+ title = 'Title for F1'
+
@staticmethod
def run():
return None
@@ -65,6 +68,8 @@ class F2(FunctionProvider):
subcategory.startswith('building')
"""
+ title = 'Title for F2'
+
@staticmethod
def run():
return None
@@ -147,7 +152,7 @@ def test_filtering_of_impact_functions(self):
# Keywords matching F1 and F3
haz_keywords1 = dict(category='hazard', subcategory='flood',
- layertype='raster', unit='m')
+ layertype='raster', unit='m')
exp_keywords1 = dict(category='exposure', subcategory='population',
layertype='raster', datatype='population')
@@ -156,15 +161,24 @@ def test_filtering_of_impact_functions(self):
layertype='raster', unit='m')
exp_keywords2 = dict(category='exposure', subcategory='building')
- # Check correct matching
+ # Check correct matching of keyword set 1
P = get_admissible_plugins([haz_keywords1, exp_keywords1])
msg = 'Expected impact functions F1 and F3 in %s' % str(P.keys())
assert 'F1' in P and 'F3' in P, msg
+ # Check correctness of title attribute
+ assert get_function_title(P['F1']) == 'Title for F1'
+ assert get_function_title(P['F3']) == 'F3'
+
+ # Check correct matching of keyword set 2
P = get_admissible_plugins([haz_keywords2, exp_keywords2])
msg = 'Expected impact functions F2 and F3 in %s' % str(P.keys())
assert 'F2' in P and 'F3' in P, msg
+ # Check correctness of title attribute
+ assert get_function_title(P['F2']) == 'Title for F2'
+ assert get_function_title(P['F3']) == 'F3'
+
# Check empty call returns all
P = get_admissible_plugins([])
msg = ('Expected at least impact functions F1, F2 and F3 in %s'
View
@@ -27,6 +27,7 @@
from storage.core import read_layer
from impact_functions import get_plugins
+from impact_functions import get_function_title
from impact_functions import get_admissible_plugins
from engine.core import calculate_impact

0 comments on commit 95e49bf

Please sign in to comment.