rse.utils package — The Research Software Encyclopedia 11 documentation

# rse.utils package

## Submodules

## rse.utils.command module

Copyright (C) 2020 Vanessa Sochat.

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

class rse.utils.command.Capturing

  Bases: `object`

  capture output from stdout and stderr into capture object. This is based off of github.com/vsoch/gridtest but modified to write files. The stderr and stdout are set to temporary files at the init of the capture, and then they are closed when we exit. This means expected usage looks like:

  with Capturing() as capture:

    process = subprocess.Popen(…)

  And then the output and error are retrieved from reading the files: and exposed as properties to the client:

  > capture.out capture.err

  And cleanup means deleting these files, if they exist.

  cleanup()
  property err

    Return error stream. Returns empty string if empty or doesn’t exist. Returns (str) : error stream written to file

  property out

    Return output stream. Returns empty string if empty or doesn’t exist. Returns (str) : output stream written to file

  set_stderr()
  set_stdout()

class rse.utils.command.Command(cmd=None)

  Bases: `object`

  Class method to invoke shell commands and retrieve output and error. This class is inspired and derived from utils functions in https://github.com/vsoch/scif

  decode(line)

    Given a line of output (error or regular) decode using the system default, if appropriate

  execute()

    Execute a system command and return output and error. :param cmd: shell command to execute :type cmd: str, required :return: Output and Error from shell command :rtype: two str objects

  get_error()

    Returns the error from shell command :rtype: str

  get_output()

    Returns the output from shell command :rtype: str

  returnCode()

    Returns the return code from shell command :rtype: int

  set_command(cmd)

    parse is called when a new command is provided to ensure we have a list. We don’t check that the executable is on the path, as the initialization might not occur in the runtime environment.

rse.utils.command.get_github_username()

  Get the github username of the user from git config.

## rse.utils.file module

Copyright (C) 2020 Vanessa Sochat.

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

rse.utils.file.get_latest_modified(base, pattern='*.json')

  Given a folder, get the latest modified file

rse.utils.file.get_tmpdir(prefix='', create=True)

  get a temporary directory for an operation.

  Arguments:

    - prefix (str) : prefix with this string

    - create (bool) : create the folder (defaults to true)

rse.utils.file.get_tmpfile(prefix='')

  get a temporary file with an optional prefix. By default, the file is closed (and just a name returned).

  Arguments:

    - prefix (str) : prefix with this string

rse.utils.file.mkdir_p(path)

  mkdir_p attempts to get the same functionality as mkdir -p

  Arguments:

    - path (str) : the path to create

rse.utils.file.read_file(filename, readlines=True)

  write_file will open a file, “filename” and write content and properly close the file.

  Arguments:

    - filename (str) : the filename to read

    - readlines (bool) : read lines of the file (vs all raw)

rse.utils.file.read_json(input_file)

  Read json from an input file.

  Arguments:

    - input_file (str) : the filename to read

rse.utils.file.recursive_find(base, pattern='*.py')

  recursive find will yield python files in all directory levels below a base path.

  Arguments:

    - base (str) : the base directory to search

    - pattern: a pattern to match, defaults to *.py

rse.utils.file.save_pickle(obj, filename)

  Save a pickle to file

  Arguments:

    > - obj (any) : the object to pickle

    - filename (str) : the output file to write to

rse.utils.file.write_file(filename, content)

  Write some text content to a file

rse.utils.file.write_json(json_obj, filename, pretty=True)

  write_json will write a json object to file, pretty printed

  Arguents:

    - json_obj (dict) : the dict to print to json

    - filename (str) : the output file to write to

## rse.utils.prompt module

Copyright (C) 2020 Vanessa Sochat.

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

rse.utils.prompt.choice_prompt(prompt, choices, choice_prefix=None, multiple=False)

  Ask the user for a prompt, and only return when one of the requested options is provided.

  Parameters

    - prompt (the prompt to ask the user)

    - choices (a list of choices that are valid.)

    - multiple (allow multiple responses (separated by spaces))

rse.utils.prompt.confirm(prompt, response=False)

  Used to prompt the user for a yes or no response, and returns True/False. This means we can use it like: if confirm(“Would you like to do the thing?”):

## Module contents