safe.util

No project is complete without a utility module.

author:Joe Joyce <joe@decafjoe.com>
copyright:Copyright (c) Joe Joyce and contributors, 2016-2019.
license:BSD
safe.util.expand_path(path)[source]

Return absolute path, with variables and ~ expanded.

Parameters:path (str) – Path, possibly with variables and ~
Returns:Absolute path with special sequences expanded
Return type:str
safe.util.get_executable(name)[source]

Return the full path to executable named name, if it exists.

Parameters:name (str) – Name of the executable to find
Returns:Full path to the executable or None
Return type:str or None
safe.util.prompt_bool(prompt, default=False)[source]

Prompt user for a yes or no answer and return the result as a boolean.

Parameters:
  • prompt (str) – Prompt. ' [y/n] ' will be appended to this value
  • default (bool) – Default value if user enters nothing
Returns:

Boolean indicating user’s choice

Return type:

bool

safe.util.temporary_directory(*args, **kwds)[source]

Context manager that creates a temporary directory for use in the body.

Example:

with temporary_directory() as tmp:
    # do stuff with tmp

The temporary directory permissions are set to 0700 before handing control over to the body.

class safe.util.Subprocess(<subprocess.Popen arguments>)[source]

Bases: subprocess.Popen

Subclass whose communicate() method turns bytes into strings.

communicate(stdin=None)[source]

Override parent to make sure bytes are decoded into strings.

Parameters:stdin (str or None) – Data to send to stdin
Returns:2-tuple, (stdout, stderr)
Return type:2-tuple()