Skip to content

Commit

Permalink
Refactor selecting values from an attribute
Browse files Browse the repository at this point in the history
Move the code to select particular keys/indices from an attribute value out
of the get_attr intrinsic functions and into the attributes module as a
separate function.

Change-Id: I16135159027cc52e8da74a4b5863fc8a8595d582
  • Loading branch information
zaneb committed Jul 15, 2014
1 parent ba09b20 commit cacdb47
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
24 changes: 24 additions & 0 deletions heat/engine/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,27 @@ def __iter__(self):
def __repr__(self):
return ("Attributes for %s:\n\t" % self._resource_name +
'\n\t'.join(self._attributes.values()))


def select_from_attribute(attribute_value, path):
'''
Select an element from an attribute value.
:param attribute_value: the attribute value.
:param path: a list of path components to select from the attribute.
:returns: the selected attribute component value.
'''
def get_path_component(collection, key):
if not isinstance(collection, (collections.Mapping,
collections.Sequence)):
raise TypeError(_("Can't traverse attribute path"))

if not isinstance(key, (basestring, int)):
raise TypeError(_('Path components in attributes must be strings'))

return collection[key]

try:
return reduce(get_path_component, path, attribute_value)
except (KeyError, IndexError, TypeError):
return None
18 changes: 2 additions & 16 deletions heat/engine/hot/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import collections

from heat.common import exception
from heat.engine import attributes
from heat.engine.cfn import functions as cfn_funcs
from heat.engine import function

Expand Down Expand Up @@ -116,22 +117,7 @@ def result(self):
return None

path_components = function.resolve(self._path_components)

def get_path_component(collection, key):
if not isinstance(collection, (collections.Mapping,
collections.Sequence)):
raise TypeError(_('"%s" can\'t traverse path') % self.fn_name)

if not isinstance(key, (basestring, int)):
raise TypeError(_('Path components in "%s" '
'must be strings') % self.fn_name)

return collection[key]

try:
return reduce(get_path_component, path_components, attribute)
except (KeyError, IndexError, TypeError):
return None
return attributes.select_from_attribute(attribute, path_components)


class Replace(cfn_funcs.Replace):
Expand Down

0 comments on commit cacdb47

Please sign in to comment.