General Utiltities

workspace.utils.background_processes()

List of background processes from run_in_background

workspace.utils.log_exception(title=None, exit=False, call=None, stack=False)

Context generator that logs exceptions as error

Parameters:
  • title (str) – Title to log before the exception
  • exit (bool) – Do sys.exit when exception occurs
  • call (callable) – Call to make before exit
  • stack (bool) – Log stacktrace
workspace.utils.parallel_call(call, args, callback=None, workers=10, show_progress=None, progress_title='Progress')

Call a callable in parallel for each arg

Parameters:
  • call (callable) – Callable to call
  • args (list(iterable|non-iterable)) – List of args to call. One call per args.
  • callback (callable) – Callable to call for each result.
  • workers (int) – Number of workers to use.
  • bool/str/callable – Show progress. If callable, it should accept two lists: completed args and all args and return progress string.
Return dict:

Map of args to their results on completion

workspace.utils.parent_path_with(check, path=None)

Find parent that satisfies check with content.

Parameters:
  • check (str) – Callable that accepts current path returns True if path should be returned
  • path (str) – Initial path to look from. Defaults to current working directory.
Returns:

Parent path that contains the directory

Return type:

str on success or False on failure

workspace.utils.parent_path_with_dir(directory, path=None)

Find parent that contains the given directory.

Parameters:
  • directory (str) – Directory to look for
  • path (str) – Initial path to look from. Defaults to current working directory.
Returns:

Parent path that contains the directory

Return type:

str on success or False on failure

workspace.utils.parent_path_with_file(name, path=None)

Find parent that contains the given directory.

Parameters:
  • name (str) – File name to look for
  • path (str) – Initial path to look from. Defaults to current working directory.
Returns:

Parent path that contains the file name

Return type:

str on success or False on failure

workspace.utils.prompt_with_editor(instruction)

Prompt user with instruction in $EDITOR and return the response

workspace.utils.run(cmd, cwd=None, silent=None, return_output=False, raises=True, **subprocess_args)

Runs a CLI command.

Parameters:
  • cmd (list/str) – Command with args to run.
  • cwd (str) – Change directory to cwd before running
  • silent (bool/int) – Suppress stdout/stderr. If True, completely silent. If 2, print cmd output on error.
  • return_output (bool) – Return the command output. Defaults silent=True. Set silent=False to see output. If True, always return output. If set to 2, return a tuple of (output, success) where output is the output of the command and success is exit code 0. When used, it is guaranteed to always return output / other options are ignored (like raises).
  • raises (bool) – Raise an exception if command exits with an error code.
  • subprocess_args (dict) – Additional args to pass to subprocess
Returns:

Output or boolean of success depending on option selected

Raises:

RunError – if the command exits with an error code and raises=True

workspace.utils.run_in_background(title, repo=None, info_suffix='[To check, run: {prog} wait]', log_file=None)

Run any code after this point in the background. This call is idempotent as subsequent calls won’t do anything except change the title.

Parameters:
  • title (str) – Title to set the running process. This should be informative to the user on what is being run in the background and will happen.
  • repo (str) – Name of repo the task is being acted on. Defaults to os.getcwd()
  • info_suffix (str) – Informational suffix to append when showing the title before forking. {prog} will be replaced by the running program’s name.
  • log_file (str) – Full path to log file to save output/error. Saves to temp dir by default. Set to “/dev/null” to discard output.
workspace.utils.shortest_id(name, names)

Return shortest name that isn’t a duplicate in names

workspace.utils.show_status(message)
Parameters:message (str) – Status message to show. If not, then status bar will be cleared.
workspace.utils.silent_run(*args, **kwargs)

Same as run with slient=True