tacker.common.utils module

Utilities and helper functions.

class tacker.common.utils.CooperativeReader(fd)

Bases: object

An eventlet thread friendly class for reading in image data.

When accessing data either through the iterator or the read method we perform a sleep to allow a co-operative yield. When there is more than one image being uploaded/downloaded this prevents eventlet thread starvation, ie allows all threads to be scheduled periodically rather than having the same thread be continuously active.

read(length=None)

Return the requested amount of bytes.

Fetching the next chunk of the underlying iterator when needed. This is replaced with cooperative_read in __init__ if the underlying fd already supports read().

class tacker.common.utils.LimitingReader(data, limit, exception_class=<class 'tacker.common.exceptions.CSARFileSizeLimitExceeded'>)

Bases: object

Limit Reader to read data past to configured allowed amount.

Reader designed to fail when reading image data past the configured allowable amount.

read(i)
class tacker.common.utils.MemoryUnit

Bases: object

UNIT_SIZE_DEFAULT = 'B'
UNIT_SIZE_DICT = {'B': 1, 'GB': 1000000000, 'GiB': 1073741824, 'KiB': 1024, 'MB': 1000000, 'MiB': 1048576, 'TB': 1000000000000, 'TiB': 1099511627776, 'kB': 1000}
static convert_unit_size_to_num(size, unit=None)

Convert given size to a number representing given unit.

If unit is None, convert to a number representing UNIT_SIZE_DEFAULT :param size: unit size e.g. 1 TB :param unit: unit to be converted to e.g GB :return: converted number e.g. 1000 for 1 TB size and unit GB

static validate_unit(unit)
tacker.common.utils.change_memory_unit(mem, to)

Change the memory value(mem) based on the unit(‘to’) specified.

If the unit is not specified in ‘mem’, by default, it is considered as “MB”. And this method returns only integer.

tacker.common.utils.chunkiter(fp, chunk_size=65536)

Convert iterator to a file-like object.

Return an iterator to a file-like obj which yields fixed size chunks

Parameters:
  • fp – a file-like object

  • chunk_size – maximum size of chunk

tacker.common.utils.chunkreadable(iter, chunk_size=65536)

Wrap a readable iterator.

Wrap a readable iterator with a reader yielding chunks of a preferred size, otherwise leave iterator unchanged.

Parameters:
  • iter – an iter which may also be readable

  • chunk_size – maximum size of chunk

tacker.common.utils.convert_camelcase_to_snakecase(request_data)

Converts dict keys or list of dict keys from camelCase to snake_case.

Returns a dict with keys or list with dict keys, in snake_case. This method takes care only keys in a dict or dicts in a list. For simple list with string items, the elements which are actual values are ignored during conversion. And property value is not converted when key is additionalParams.

Parameters:

request_data – dict with keys or list with items, in camelCase.

tacker.common.utils.convert_snakecase_to_camelcase(request_data)

Converts dict keys or list of dict keys from snake_case to camelCase.

Returns a dict with keys or list with dict key, in camelCase. This method takes care only keys in a dict or dicts in a list. For simple list with string items, the elements which are actual values are ignored during conversion. Also, Snake case is a notation method that uses underscores to connect words. For that reason, if the initial word of the key in dict starts with ‘_’, this function ignore to convert the key. And property value is not converted when key is additional_params.

Parameters:

request_data – dict with keys or list with items, in snake_case.

Example:

Before:

[
    {"vnf_lcm_op_occ_id" : "uuid"},
    {
        "_links" : {
            "vnf_lcm_op_occ": {"href": "resource_link"}
        }
    }
]

After:

[
    {"vnfLcmOpOccId": "uuid"},
    {
        "_links": {
            "vnfLcmOpOcc": {"href": "resource_link"}
        }
    }
]
tacker.common.utils.convert_string_to_snakecase(name)

Converts a string from camelCase to snake_case.

tacker.common.utils.cooperative_iter(iter)

Prevent eventlet thread starvation during iteration

Return an iterator which schedules after each iteration. This can prevent eventlet thread starvation.

Parameters:

iter – an iterator to wrap

tacker.common.utils.cooperative_read(fd)

Prevent eventlet thread starvationafter each read operation.

Wrap a file descriptor’s read with a partial function which schedules after each read. This can prevent eventlet thread starvation.

Parameters:

fd – a file descriptor to wrap

tacker.common.utils.deep_update(orig_dict, new_dict)
tacker.common.utils.deepgetattr(obj, attr)

Recurses through an attribute chain to get the ultimate value.

tacker.common.utils.dict2tuple(d)
tacker.common.utils.expects_func_args(*args)
tacker.common.utils.find_config_file(options, config_file)

Return the first config file found.

We search for the paste config file in the following order: * If –config-file option is used, use that * Search for the configuration files via common cfg directories :retval Full path to config file, or None if no config file found

tacker.common.utils.flatten_dict(data, prefix='')
tacker.common.utils.get_auth_url_v3(auth_url)
tacker.common.utils.get_hostname()
tacker.common.utils.is_url(url)
tacker.common.utils.is_valid_area(area)

Verify that the area attribute is valid.

Area attribute is an area-region pair. The value of this attribute should be a string in the format of “area@region”.

tacker.common.utils.is_valid_ipv4(address)

Verify that address represents a valid IPv4 address.

tacker.common.utils.is_valid_url(url)
tacker.common.utils.is_valid_vlan_tag(vlan)
tacker.common.utils.json_merge_patch(target, patch)
tacker.common.utils.load_class_by_alias_or_classname(namespace, name)

Load class using stevedore alias or the class name.

Load class using the stevedore driver manager :param namespace: namespace where the alias is defined :param name: alias or class name of the class to be loaded :returns: class if calls can be loaded :raises ImportError: if class cannot be loaded

tacker.common.utils.log_opt_values(log)
tacker.common.utils.none_from_string(orig_str)
tacker.common.utils.proj_root()

Return tacker’s project root dir.

It’s assumed abspath(__file__) is equals to ${PROJ_ROOT}/tacker/common. The result is like as “/opt/stack/tacker”.

tacker.common.utils.str_to_bool(value)

Convert string to Bool

tacker.common.utils.str_to_bytes(value)

Convert string to bytes

tacker.common.utils.str_to_num(value)

Convert a string representation of a number into a numeric type.

tacker.common.utils.subprocess_popen(args, stdin=None, stdout=None, stderr=None, shell=False, env=None)