The sushy.utils Module

The sushy.utils Module

sushy.utils.cache_clear(res_selfie, force_refresh, only_these=None)

Clear some or all cached values of the resource.

If the cache variable refers to a resource instance then the invalidate() method is called on that. Otherwise it is set to None. Should there be a need to force refresh the resource and its sub-resources, “cascading refresh”, force_refresh is to be set to True.

This is the complimentary method of cache_it decorator.

Parameters:
  • res_selfie – the resource instance.
  • force_refresh – force_refresh argument of invalidate() method.
  • only_these – expects a sequence of specific method names for which the cached value/s need to be cleared only. When None, all the cached values are cleared.
sushy.utils.cache_it(res_accessor_method)

Utility decorator to cache the return value of the decorated method.

This decorator is to be used with any Sushy resource class method. This will internally create an attribute on the resource namely _cache_<decorated_method_name>. This is referred to as the “caching attribute”. This attribute will eventually hold the resultant value from the method invocation (when method gets first time called) and for every subsequent calls to that method this cached value will get returned. It expects the decorated method to contain its own logic of evaluation.

This also assigns a variable named _cache_attr_names on the resource. This variable maintains a collection of all the existing “caching attribute” names.

To invalidate or clear the cache use cache_clear(). Usage:

class SomeResource(base.ResourceBase):
  ...
  @cache_it
  def get_summary(self):
    # do some calculation and return the result
    # and this result will be cached.
    return result
  ...
  def _do_refresh(self, force):
    cache_clear(self, force)

If the returned value is a Sushy resource instance or a sequence whose element is of type Sushy resource it handles the case of calling the refresh() method of that resource. This is done to avoid unnecessary recreation of a new resource instance which got already created at the first place in contrast to fresh retrieval of the resource json data. Again, the force argument is deliberately set to False to do only the “light refresh” of the resource (only the fresh retrieval of resource) instead of doing the complete exhaustive “cascading refresh” (resource with all its nested subresources recursively).

class SomeResource(base.ResourceBase):
  ...
  @property
  @cache_it
  def nested_resource(self):
    return NestedResource(
      self._conn, "Path/to/NestedResource",
      redfish_version=self.redfish_version)
  ...
  def _do_refresh(self, force):
    # selective attribute clearing
    cache_clear(self, force, only_these=['nested_resource'])

Do note that this is not thread safe. So guard your code to protect it from any kind of concurrency issues while using this decorator.

Parameters:res_accessor_method – the resource accessor decorated method.
sushy.utils.camelcase_to_underscore_joined(camelcase_str)

Convert camelCase string to underscore_joined string

Parameters:camelcase_str – The camelCase string
Returns:the equivalent underscore_joined string
sushy.utils.get_members_identities(members)

Extract and return a tuple of members identities

Parameters:members – A list of members in JSON format
Returns:A tuple containing the members paths
sushy.utils.get_sub_resource_path_by(resource, subresource_name, is_collection=False)

Helper function to find the subresource path

Parameters:
  • resource – ResourceBase instance on which the name gets queried upon.
  • subresource_name – name of the resource field to fetch the @odata.id’ from.
  • is_collection – if True, expect a list of resources to fetch the @odata.id’ from.
Returns:

Resource path (if is_collection is False) or a list of resource paths (if is_collection is True).

sushy.utils.int_or_none(x)

Given a value x it cast as int or None

Parameters:x – The value to transform and return
Returns:Either None or x cast to an int
sushy.utils.max_safe(iterable, default=0)

Helper wrapper over builtin max() function.

This function is just a wrapper over builtin max() w/o key argument. The default argument specifies an object to return if the provided iterable is empty. Also it filters out the None type values.

Parameters:
  • iterable – an iterable
  • default – 0 by default
sushy.utils.revert_dictionary(dictionary)

Given a dictionary revert it’s mapping

Parameters:dictionary – A dictionary to be reverted
Returns:A dictionary with the keys and values reverted
sushy.utils.sanitize(item)

Remove passwords from the item.

sushy.utils.setdefaultattr(obj, name, default)

Python’s dict.setdefault applied on Python objects.

If name is an attribute with obj, return its value. If not, set name attribute with a value of default and return default.

Parameters:
  • obj – a python object
  • name – name of attribute
  • default – default value to be set
sushy.utils.synchronized(wrapped)

Simple synchronization decorator.

Decorating a method like so:

@synchronized
def foo(self, *args):
  ...

ensures that only one thread will execute the foo method at a time.

Creative Commons Attribution 3.0 License

Except where otherwise noted, this document is licensed under Creative Commons Attribution 3.0 License. See all OpenStack Legal Documents.