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
.
-
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
_
.
-
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.
-
property
-
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
-
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.
-
runway.util.
change_dir
(newdir)[source]¶ Change directory.
Adapted from http://stackoverflow.com/a/24176022
-
runway.util.
environ
(env=None, **kwargs)[source]¶ Context manager for temporarily changing os.environ.
The original value of os.environ is restored upon exit.
-
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.
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.
get_hash_for_filename
(filename, hashfile_path)[source]¶ Return hash for filename in the hashfile.
-
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.
strip_leading_option_delim
(args)[source]¶ Remove leading – if present.
Using the “–” end of options syntax bypasses docopt’s parsing of options.