runway.utils package¶
Utility functions.
- pydantic model runway.utils.BaseModel[source]¶
Bases:
BaseModel
Base class for Runway models.
Show JSON schema
{ "title": "BaseModel", "description": "Base class for Runway models.", "type": "object", "properties": {} }
- class runway.utils.MutableMap[source]¶
Bases:
MutableMapping
[str
,Any
]Base class for mutable map objects.
- __delitem__(key: str) None [source]¶
Implement deletion of self[key].
- Parameters:
key – Attribute name to remove from the object.
Example
- __getitem__(key: str) Any [source]¶
Implement evaluation of self[key].
- Parameters:
key – Attribute name to return the value for.
- Returns:
The value associated with the provided key/attribute name.
- Raises:
AttributeError – If attribute does not exist on this object.
Example
- __init__(**kwargs: Any) None [source]¶
Initialize class.
Provided
kwargs
are added to the object as attributes.Example
- __iter__() Iterator[Any] [source]¶
Return iterator object that can iterate over all attributes.
Example
- __setitem__(key: str, value: Any) None [source]¶
Implement assignment to self[key].
- Parameters:
key – Attribute name to associate with a value.
value – Value of a key/attribute.
Example
- find(query: str, default: Any = None, ignore_cache: bool = False) Any [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.
- class runway.utils.SafeHaven[source]¶
Bases:
AbstractContextManager
[SafeHaven]Context manager that caches and resets important values on exit.
Caches and resets os.environ, sys.argv, sys.modules, and sys.path.
- __enter__() Self [source]¶
Enter the context manager.
- Returns:
Instance of the context manager.
- Return type:
- __exit__(exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None) None [source]¶
Exit the context manager.
- __init__(argv: Iterable[str] | None = None, environ: dict[str, str] | None = None, sys_modules_exclude: Iterable[str] | None = None, sys_path: Iterable[str] | None = None) None [source]¶
Instantiate class.
- Parameters:
argv – Override the value of sys.argv.
environ – Update os.environ.
sys_modules_exclude – A list of modules to exclude when reverting sys.modules to its previous state. These are checked as
module.startswith(name)
.sys_path – Override the value of sys.path.
- class runway.utils.YamlDumper[source]¶
Bases:
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.utils.argv(*args: str) Iterator[None] [source]¶
Context manager for temporarily changing sys.argv.
- runway.utils.change_dir(newdir: Path | str) Iterator[None] [source]¶
Change directory.
Adapted from http://stackoverflow.com/a/24176022
- Parameters:
newdir – Path to change directory into.
- runway.utils.ensure_file_is_executable(path: str) None [source]¶
Exit if file is not executable.
- Parameters:
path – Path to file.
- Raises:
SystemExit – file is not executable.
- runway.utils.environ(env: dict[str, str] | None = None, **kwargs: str) Iterator[None] [source]¶
Context manager for temporarily changing os.environ.
The original value of os.environ is restored upon exit.
- Parameters:
env – Dictionary to use when updating os.environ.
**kwargs – Arbitrary keyword arguments.
- runway.utils.extract_boto_args_from_env(env_vars: dict[str, str]) dict[str, str] [source]¶
Return boto3 client args dict with environment creds.
- runway.utils.find_cfn_output(key: str, outputs: list[OutputTypeDef]) str | None [source]¶
Return CFN output value.
- Parameters:
key – Name of the output.
outputs – List of Stack outputs.
- runway.utils.fix_windows_command_list(commands: list[str]) list[str] [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 that will have a windows-only suffix applied to them.
- runway.utils.flatten_path_lists(env_dict: dict[str, Any], env_root: str | None = None) dict[str, Any] [source]¶
Join paths in environment dict down to strings.
- runway.utils.get_file_hash(filename: str, algorithm: Literal['blake2b', 'md5', 'sha1', 'sha224', 'sha256', 'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512', 'sha384', 'sha512', 'shake_128', 'shake_256']) str [source]¶
Return cryptographic hash of file.
Deprecated since version 2.4.0: Use
runway.utils.FileHash
instead.
- runway.utils.get_hash_for_filename(filename: str, hashfile_path: str) str [source]¶
Return hash for filename in the hashfile.
- runway.utils.ignore_exit_code_0() Iterator[None] [source]¶
Capture exit calls and ignore those with exit code 0.
- runway.utils.json_serial(obj: Any) Any [source]¶
JSON serializer for objects not serializable by default json code.
- runway.utils.load_object_from_string(fqcn: str, try_reload: bool = False) type | Callable[[...], Any] [source]¶
Convert “.” delimited strings to a python object.
- Parameters:
fqcn – A “.” delimited string representing the full path to an object (function, class, variable) inside a module
try_reload – 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.
Example:
load_object_from_string("os.path.basename") load_object_from_string("logging.Logger") load_object_from_string("LocalClassName")
- runway.utils.md5sum(filename: str) str [source]¶
Return MD5 hash of file.
Deprecated since version 2.4.0: Use
runway.utils.FileHash
instead.
- runway.utils.merge_dicts(dict1: dict[Any, Any], dict2: dict[Any, Any], deep_merge: bool = True) dict[str, Any] [source]¶
- runway.utils.merge_dicts(dict1: list[Any], dict2: list[Any], deep_merge: bool = True) list[Any]
Merge dict2 into dict1.
- runway.utils.merge_nested_environment_dicts(env_dicts: dict[str, Any], env_name: str | None = None, env_root: str | None = None) dict[str, Any] [source]¶
Return single-level dictionary from dictionary of dictionaries.
- runway.utils.run_commands(commands: list[dict[str, list[str] | str] | list[str] | str], directory: Path | str, env: dict[str, str] | None = None) None [source]¶
Run list of commands.
- runway.utils.sha256sum(filename: str) str [source]¶
Return SHA256 hash of file.
Deprecated since version 2.4.0: Use
runway.utils.FileHash
instead.
- runway.utils.snake_case_to_kebab_case(value: str) str [source]¶
Convert snake_case to kebab-case.
- Parameters:
value – The string value to convert.
- runway.utils.use_embedded_pkgs(embedded_lib_path: str | None = None) Iterator[None] [source]¶
Temporarily prepend embedded packages to sys.path.