qme.utils package

Submodules

qme.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 qme.utils.command.Capturing[source]

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()[source]
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()[source]
set_stdout()[source]
class qme.utils.command.QueueMeCommand(cmd=None)[source]

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)[source]

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

execute()[source]

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()[source]

Returns the error from shell command :rtype: str

get_output()[source]

Returns the output from shell command :rtype: str

returnCode()[source]

Returns the return code from shell command :rtype: int

set_command(cmd)[source]

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.

qme.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/.

qme.utils.file.get_latest_modified(base, pattern='*.json')[source]

Given a folder, get the latest modified file

qme.utils.file.get_tmpdir(prefix='', create=True)[source]

get a temporary directory for an operation. If SREGISTRY_TMPDIR is set, return that. Otherwise, return the output of tempfile.mkdtemp

Arguments:
  • prefix (str) : prefix with this string

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

qme.utils.file.get_tmpfile(prefix='')[source]

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

qme.utils.file.get_user()[source]

Get the name of the user. We first try to import pwd, but fallback to extraction from the environment.

qme.utils.file.get_userhome()[source]

get the user home based on the effective uid. If import of pwd fails (not supported for Windows) then fall back to environment variable.

qme.utils.file.mkdir_p(path)[source]

mkdir_p attempts to get the same functionality as mkdir -p

Arguments:
  • path (str) : the path to create

qme.utils.file.read_file(filename, readlines=True)[source]

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)

qme.utils.file.read_json(input_file)[source]

Read json from an input file.

Arguments:
  • input_file (str) : the filename to read

qme.utils.file.recursive_find(base, pattern='*.py')[source]

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

qme.utils.file.save_pickle(obj, filename)[source]

Save a pickle to file

Arguments:
  • obj (any) : the object to pickle

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

qme.utils.file.write_json(json_obj, filename, pretty=True)[source]

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

qme.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/.

qme.utils.prompt.confirm(prompt, response=False)[source]

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?”):

qme.utils.regex 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/.

Module contents