runway.util module

Utility functions.

class runway.util.cached_property(func)[source]

Bases: object

Decorator for creating cached properties.

A property that is only computed once per instance and then replaces itself with an ordinary attribute. Deleting the attribute resets the property. Source: https://github.com/bottlepy/bottle/commit/fa7733e075da0d790d809aa3d2f53071897e6f76

Initialize class.

Parameters

func (Callable) – Method being decorated.

class runway.util.JsonEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]

Bases: json.encoder.JSONEncoder

Encode Python objects to JSON data.

This class can be used with json.dumps() to handle most data types that can occur in responses from AWS.

Usage:
>>> json.dumps(data, cls=JsonEncoder)

Constructor for JSONEncoder, with sensible defaults.

If skipkeys is false, then it is a TypeError to attempt encoding of keys that are not str, int, float or None. If skipkeys is True, such items are simply skipped.

If ensure_ascii is true, the output is guaranteed to be str objects with all incoming non-ASCII characters escaped. If ensure_ascii is false, the output can contain non-ASCII characters.

If check_circular is true, then lists, dicts, and custom encoded objects will be checked for circular references during encoding to prevent an infinite recursion (which would cause an OverflowError). Otherwise, no such check takes place.

If allow_nan is true, then NaN, Infinity, and -Infinity will be encoded as such. This behavior is not JSON specification compliant, but is consistent with most JavaScript based encoders and decoders. Otherwise, it will be a ValueError to encode such floats.

If sort_keys is true, then the output of dictionaries will be sorted by key; this is useful for regression tests to ensure that JSON serializations can be compared on a day-to-day basis.

If indent is a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines. None is the most compact representation.

If specified, separators should be an (item_separator, key_separator) tuple. The default is (‘, ‘, ‘: ‘) if indent is None and (‘,’, ‘: ‘) otherwise. To get the most compact JSON representation, you should specify (‘,’, ‘:’) to eliminate whitespace.

If specified, default is a function that gets called for objects that can’t otherwise be serialized. It should return a JSON encodable version of the object or raise a TypeError.

default(o)[source]

Encode types not supported by the default JSONEncoder.

Parameters

o – Object to encode.

Returns

JSON serializable data type.

Raises

TypeError – Object type could not be encoded.

class runway.util.MutableMap(**kwargs)[source]

Bases: collections.abc.MutableMapping

Base class for mutable map objects.

Initialize class.

Provided kwargs are added to the object as attributes.

Example

property data

Sanitized output of __dict__.

Removes anything that starts with _.

clear_found_cache()[source]

Clear _found_cache.

find(query, default=None, ignore_cache=False)[source]

Find a value in the map.

Previously found queries are cached to increase search speed. The cached value should only be used if values are not expected to change.

Parameters
  • query – A period delimited string that is split to search for nested values

  • default – The value to return if the query was unsuccessful.

  • ignore_cache – Ignore cached value.

get(key, default=None)[source]

Implement evaluation of self.get.

Parameters
  • key – Attribute name to return the value for.

  • default – Value to return if attribute is not found.

class runway.util.SafeHaven(argv=None, environ=None, sys_modules_exclude=None, sys_path=None)[source]

Bases: contextlib.AbstractContextManager

Context manager that caches and resets important values on exit.

Caches and resets os.environ, sys.argv, sys.modules, and sys.path.

Instantiate class.

Parameters
  • argv (Optional[List[str]]) – Override the value of sys.argv.

  • environ (Optional[Dict[str, str]]) – Update os.environ.

  • sys_modules_exclude (Optional[List[str]]) – A list of modules to exclude when reverting sys.modules to its previous state. These are checked as module.startswith(name).

  • sys_path (Optional[List[str]]) – Override the value of sys.path.

reset_all()[source]

Reset all values cached by this context manager.

reset_os_environ()[source]

Reset the value of os.environ.

reset_sys_argv()[source]

Reset the value of sys.argv.

reset_sys_modules()[source]

Reset the value of sys.modules.

reset_sys_path()[source]

Reset the value of sys.path.

class runway.util.YamlDumper(stream, default_style=None, default_flow_style=False, canonical=None, indent=None, width=None, allow_unicode=None, line_break=None, encoding=None, explicit_start=None, explicit_end=None, version=None, tags=None, sort_keys=True)[source]

Bases: yaml.dumper.Dumper

Custom YAML Dumper.

This Dumper allows for YAML to be output to follow YAML spec 1.2, example 2.3 of collections (2.1). This provides an output that is more humanreadable and complies with yamllint.

Example

>>> print(yaml.dump({'key': ['val1', 'val2']}, Dumper=YamlDumper))

Note

YAML 1.2 Specification: https://yaml.org/spec/1.2/spec.html used for reference.

increase_indent(flow=False, indentless=False)[source]

Override parent method.

runway.util.argv(*args)[source]

Context manager for temporarily changing sys.argv.

runway.util.change_dir(newdir)[source]

Change directory.

Adapted from http://stackoverflow.com/a/24176022

runway.util.ensure_file_is_executable(path)[source]

Exit if file is not executable.

runway.util.environ(env=None, **kwargs)[source]

Context manager for temporarily changing os.environ.

The original value of os.environ is restored upon exit.

Parameters

env (Dict[str, str]) – Dictionary to use when updating os.environ.

runway.util.json_serial(obj)[source]

JSON serializer for objects not serializable by default json code.

runway.util.load_object_from_string(fqcn, try_reload=False)[source]

Convert “.” delimited strings to a python object.

Parameters
  • fqcn (str) – A “.” delimited string representing the full path to an object (function, class, variable) inside a module

  • try_reload (bool) – Try to reload the module so any global variables set within the file during import are reloaded. This only applies to modules that are already imported and are not builtin.

Returns

Object being imported from the provided path.

Return type

Any

Example:

load_object_from_string("os.path.basename")
load_object_from_string("logging.Logger")
load_object_from_string("LocalClassName")
runway.util.merge_dicts(dict1, dict2, deep_merge=True)[source]

Merge dict2 into dict1.

runway.util.extract_boto_args_from_env(env_vars)[source]

Return boto3 client args dict with environment creds.

runway.util.flatten_path_lists(env_dict, env_root=None)[source]

Join paths in environment dict down to strings.

runway.util.merge_nested_environment_dicts(env_dicts, env_name=None, env_root=None)[source]

Return single-level dictionary from dictionary of dictionaries.

runway.util.find_cfn_output(key, outputs)[source]

Return CFN output value.

runway.util.get_embedded_lib_path()[source]

Return path of embedded libraries.

runway.util.get_hash_for_filename(filename, hashfile_path)[source]

Return hash for filename in the hashfile.

runway.util.ignore_exit_code_0()[source]

Capture exit calls and ignore those with exit code 0.

runway.util.fix_windows_command_list(commands)[source]

Return command list with working Windows commands.

npm on windows is npm.cmd, which will blow up subprocess.check_call([‘npm’, ‘…’])

Similar issues arise when calling python apps like pipenv that will have a windows-only suffix applied to them

runway.util.run_commands(commands, directory, env=None)[source]

Run list of commands.

runway.util.md5sum(filename)[source]

Return MD5 hash of file.

runway.util.sha256sum(filename)[source]

Return SHA256 hash of file.

runway.util.strip_leading_option_delim(args)[source]

Remove leading – if present.

Using the “–” end of options syntax bypasses docopt’s parsing of options.

runway.util.use_embedded_pkgs(embedded_lib_path=None)[source]

Temporarily prepend embedded packages to sys.path.

runway.util.which(program)[source]

Mimic ‘which’ command behavior.