General-purpose utilities package.


Archive utilities.

aria.utils.archive.tar(source, destination)
aria.utils.archive.untar(archive, destination=None)
aria.utils.archive.unzip(archive, destination=None), destination)


Enhancements to Python’s argparse module.

class aria.utils.argparse.ArgumentParser(prog=None, usage=None, description=None, epilog=None, version=None, parents=[], formatter_class=<class 'argparse.HelpFormatter'>, prefix_chars='-', fromfile_prefix_chars=None, argument_default=None, conflict_handler='error', add_help=True)

Bases: argparse.ArgumentParser

Enhanced argument parser.

Applied patch to fix this issue.

add_flag_argument(name, help_true=None, help_false=None, default=False)

Adds a flag argument as two arguments: --my-flag and --no-my-flag.


Caching utilities.

class aria.utils.caching.HasCachedMethods(method_cache=None)

Bases: object

Provides convenience methods for working with cachedmethod.

class aria.utils.caching.cachedmethod(func)

Bases: object

Decorator for caching method return values.

The implementation is thread-safe.

Supports cache_info to be compatible with Python 3’s functools.lru_cache. Note that the statistics are combined for all instances of the class.

Won’t use the cache if not called when bound to an object, allowing you to override the cache.

Adapted from this solution.



Additional collection classes and collection utilities.

class aria.utils.collections.FrozenDict(*args, **kwargs)

Bases: collections.OrderedDict

An immutable ordered dict.

After initialization it will raise TypeError exceptions if modification is attempted.

Note that objects stored in the dict may not be immutable.

class aria.utils.collections.FrozenList(*args, **kwargs)

Bases: list

An immutable list.

After initialization it will raise TypeError exceptions if modification is attempted.

Note that objects stored in the list may not be immutable.

insert(index, value)
class aria.utils.collections.StrictDict(items=None, key_class=None, value_class=None, wrapper_function=None, unwrapper_function=None)

Bases: collections.OrderedDict

An ordered dict that raises TypeError exceptions when keys or values of the wrong type are used.

class aria.utils.collections.StrictList(items=None, value_class=None, wrapper_function=None, unwrapper_function=None)

Bases: list

A list that raises TypeError exceptions when objects of the wrong type are inserted.

insert(index, value)
aria.utils.collections.copy_locators(target, source)

Copies over _locator for all elements, recursively.

Assumes that target and source have exactly the same list/dict structure.


Like deepcopy(), but also copies over locators.

aria.utils.collections.is_removable(_container, _key, v)
aria.utils.collections.merge(dict_a, dict_b, path=None, strict=False)

Merges dicts, recursively.

aria.utils.collections.prune(value, is_removable_function=<function is_removable>)

Deletes None and empty lists and dicts, recursively.


Abstraction API above terminal color libraries.

class aria.utils.console.Colored

Bases: object

static black(string, always=False, bold=False)
static blue(string, always=False, bold=False)
static cyan(string, always=False, bold=False)
static green(string, always=False, bold=False)
static magenta(string, always=False, bold=False)
static red(string, always=False, bold=False)
static white(string, always=False, bold=False)
static yellow(string, always=False, bold=False)
class aria.utils.console.ColoredString(color, str_, always_color=False, bold=False)

Bases: clint.textui.colored.ColoredString


write(str) -> None. Write string str to file.

Note that due to buffering, flush() or close() may be needed before the file on disk reflects the data written.

aria.utils.console.puts(string='', newline=True, stream=<built-in method write of file object>)


Utilities for extracting and formatting Python exceptions.

aria.utils.exceptions.get_exception_as_string(exc_type, exc_val, traceback)
aria.utils.exceptions.print_exception(e, full=True, cause=False, traceback=None)

Prints the exception with nice colors and such.

aria.utils.exceptions.print_traceback(traceback=None, print_last_stack=False)

Prints the traceback with nice colors and such.



File utilities.


Enhancement of os.makedirs() that doesn’t fail if the directory already exists.



String formatting and string-based format utilities.

class aria.utils.formatting.JsonAsRawEncoder(*args, **kwargs)

Bases: json.encoder.JSONEncoder

A JSONEncoder that will use the as_raw property of objects if available.

class aria.utils.formatting.YamlAsRawDumper(stream, default_style=None, default_flow_style=None, canonical=None, indent=None, width=None, allow_unicode=None, line_break=None, encoding=None, explicit_start=None, explicit_end=None, version=None, tags=None, block_seq_indent=None, top_level_colon_align=None, prefix_colon=None)

Bases: ruamel.yaml.dumper.RoundTripDumper

A RoundTripDumper that will use the as_raw property of objects if available.


Converts subclasses of list and dict to standard lists and dicts, and Unicode strings to non-Unicode if possible, recursively.

Useful for creating human-readable output of structures.


Converts values using their as_raw property, if it exists, recursively.


Assuming value is a dict, converts its values using as_raw(). The keys are left as is.


Assuming value is a list, converts its values using as_raw().

aria.utils.formatting.json_dumps(value, indent=2)

JSON dumps that supports Unicode and the as_raw property of objects if available.


Like repr(), but calls as_raw() and as_agnostic() first.


Like str coercion, but makes sure that Unicode strings are properly encoded, and will never return None.


Nice representation of a list of strings.

aria.utils.formatting.yaml_dumps(value, indent=2)

YAML dumps that supports Unicode and the as_raw property of objects if available.



HTTP utilities.

aria.utils.http.download_file(url, destination=None, logger=None, progress_handler=None)

Download file.

  • url (basestring) – URL from which to download
  • destination (basestring) – path where the file should be saved or None to auto-generate

path where the file was saved

Return type:




Utilities for dynamically loading Python code.

aria.utils.imports.import_fullname(name, paths=None)

Imports a variable or class based on a full name, optionally searching for it in the paths.


Imports a module and all its sub-modules, recursively. Relies on modules defining a MODULES attribute listing their sub-module names.


Dynamically load an attribute based on the path to it. E.g. some_package.some_module.some_attribute, will load some_attribute from the some_package.some_module module.


Utilities for working with open/close patterns.

class aria.utils.openclose.OpenClose(wrapped)

Bases: object

Wraps an object that has open() and close() methods to support the with keyword.


Plugin utilities.

aria.utils.plugin.create(source, destination_dir)


Process utilities.

aria.utils.process.append_to_path(*args, **kwargs)

Appends one or more paths to the system path of an environment. The environment will be that of the current process unless another is passed using the ‘env’ keyword argument. :param args: paths to append :param kwargs: ‘env’ may be used to pass a custom environment to use

aria.utils.process.append_to_pythonpath(*args, **kwargs)

Appends one or more paths to the python path of an environment. The environment will be that of the current process unless another is passed using the ‘env’ keyword argument. :param args: paths to append :param kwargs: ‘env’ may be used to pass a custom environment to use


Utilities for cross-referencing code with specification documents.

aria.utils.specification.implements_specification(section, spec)

Decorator for specification implementations.

Used for documentation and standards compliance.


Threading utilities.

class aria.utils.threading.DaemonThread(*args, **kwargs)

Bases: threading.Thread


We’re overriding in order to avoid annoying (but harmless) error messages during shutdown. The problem is that CPython nullifies the global state _before_ shutting down daemon threads, so that exceptions might happen, and then Thread.__bootstrap_inner prints them out.

Our solution is to swallow these exceptions here.

The side effect is that uncaught exceptions in our own thread code will _not_ be printed out as usual, so it’s our responsibility to catch them in our code.

class aria.utils.threading.ExceptionThread(*args, **kwargs)

Bases: threading.Thread

A thread from which top level exceptions can be retrieved or re-raised.

exception aria.utils.threading.ExecutorException

Bases: exceptions.Exception

class aria.utils.threading.FixedThreadPoolExecutor(size=None, timeout=None, print_exceptions=False)

Bases: object

Executes tasks in a fixed thread pool.

Makes sure to gather all returned results and thrown exceptions in one place, in order of task submission.


def sum(arg1, arg2):
    return arg1 + arg2

executor = FixedThreadPoolExecutor(10)
    for value in range(100):
        executor.submit(sum, value, value)
print executor.returns

You can also use it with the Python with keyword, in which case you don’t need to call close explicitly:

with FixedThreadPoolExecutor(10) as executor:
    for value in range(100):
        executor.submit(sum, value, value)
    print executor.returns
  • size – number of threads in the pool; if None will use an optimal number for the platform
  • timeout – timeout in seconds for all blocking operations (None means no timeout)
  • print_exceptions – set to True in order to print exceptions from tasks

Blocks until all current tasks finish execution and all worker threads are dead.

You cannot submit tasks anymore after calling this.

This is called automatically upon exit if you are using the with keyword.


Blocks until all current tasks finish execution, but leaves the worker threads alive.


The raised exceptions from all tasks, in order of submission.


True if any of the worker threads are alive.


If exceptions were thrown by any task, then the first one will be raised.

This is rather arbitrary: proper handling would involve iterating all the exceptions. However, if you want to use the “raise” mechanism, you are limited to raising only one of them.


The returned values from all tasks, in order of submission.

submit(func, *args, **kwargs)

Submit a task for execution.

The task will be called ASAP on the next available worker thread in the pool.

Raises:ExecutorException – if cannot be submitted
class aria.utils.threading.LockedList(*args, **kwargs)

Bases: list

A list that supports the with keyword with a built-in lock.

Though Python lists are thread-safe in that they will not raise exceptions during concurrent access, they do not guarantee atomicity. This class will let you gain atomicity when needed.


Type utilities.


Return the canonical type for any Python, YAML, or TOSCA type name or alias, or None if unsupported.

Parameters:type_name – Type name (case insensitive)

Returns the canonical TOSCA type name of a primitive value, or None if unknown.

For a list of TOSCA type names, see the TOSCA Simple Profile v1.0 cos01 specification

aria.utils.type.convert_value_to_type(str_value, python_type_name)

Converts a value to a specific Python primitive type.

Parameters:python_type_name – Python primitive type name (case insensitive)
Raises:ValueError – for unsupported types or conversion failure

The full class name of a type or instance.

aria.utils.type.validate_value_type(value, type_name)

Validate that a value is of a specific type. Supports Python, YAML, and TOSCA type names and aliases.

Parameters:type_name – type name (case insensitive)
Raises:ValueError – on type mismatch


URI utilities.


If the URI is a file (either the file scheme or no scheme), then returns the normalized path. Otherwise, returns None.


UUID generation utilities.

aria.utils.uuid.generate_uuid(length=None, variant='base57')

A random string with varying degrees of guarantee of universal uniqueness.

  • base57 (the default) uses a mix of upper and lowercase alphanumerics ensuring no visually ambiguous characters; default length 22
  • alphanumeric uses lowercase alphanumeric; default length 25
  • uuid uses lowercase hexadecimal in the classic UUID format, including dashes; length is always 36
  • hex uses lowercase hexadecimal characters but has no guarantee of uniqueness; default length of 5


Validation utilities.

class aria.utils.validation.ValidatorMixin

Bases: object

A mix0in that should be added to classes that require validating user input.

classmethod validate_callable(argument_name, argument)

Validate argument is callable

classmethod validate_in_choice(name, argument, choices)

Validate argument is in choices

classmethod validate_instance(argument_name, argument, expected_type)

Validate argument is a instance of expected_type

classmethod validate_type(argument_name, argument, expected_type)

Validate argument is a subclass of expected_type

aria.utils.validation.validate_function_arguments(func, func_kwargs)

Validates all required arguments are supplied to func and that no additional arguments are supplied.


Verion string utilities.

class aria.utils.versions.VersionString(value=None)

Bases: unicode

Version string that can be compared, sorted, made unique in a set, and used as a unique dict key.

The primary part of the string is one or more dot-separated natural numbers. Trailing zeroes are treated as redundant, e.g. “1.0.0” == “1.0” == “1”.

An optional qualifier can be added after a “-“. The qualifier can be a natural number or a specially treated prefixed natural number, e.g. “1.1-beta1” > “1.1-alpha2”. The case of the prefix is ignored.

Numeric qualifiers will always be greater than prefixed integer qualifiers, e.g. “1.1-1” > “1.1-beta1”.

Versions without a qualifier will always be greater than their equivalents with a qualifier, e.g. e.g. “1.1” > “1.1-1”.

Any value that does not conform to this format will be treated as a zero version, which would be lesser than any non-zero version.

For efficient list sorts use the key property, e.g.:

sorted(versions, key=lambda x: x.key)
NULL = u''

Parses a version string.

Parameters:version – version string
Returns:primary tuple and qualifier float
Return type:((int), float)