Previous topic

util.gio

Next topic

util.gplot

This Page

util.gos

Functions for operating system and filesystem interactions.

class glimpse.util.gos.Cmd(*args)

A builder syntax for shell commands.

Note

This class is currently incomplete. Specifically, the PipeFrom() method is not implemented.

The following table shows some example Bash commands, with the equivalent Cmd usage. In this table, C stands for an arbitrary command name and P is the currently executing Python script.

Bash Shell Syntax Python Builder Syntax
C Cmd(“C”).Run()
C | P Cmd(“C”).ReturnStdout()
C 1>F1 Cmd(“C”).Redirect(“F”)
C 1>F1 2>F2 Cmd(“C”).Redirect(“F1”, “F2”)
C1 | C2 1>F Cmd(“C2”).PipeFrom(Cmd(“C1”)).Redirect(“F”)
C 1>F 2>&1 Cmd(“C”).TieStderrToStdout().Redirect(“F”)
C 2>&1 | P Cmd(“C”).TieStderrToStdout().ReturnStdout()
C 2>&1 1>/dev/null | P Cmd(“C”).TieStderrToStdout().ReturnStderr()
Redirect(fout=None, ferr=None, fout_append=None, ferr_append=None)

Run command, redirecting stdout and/or stderr to a file.

Only one of fout and fout_append should be specified (similarly for ferr and ferr_append).

Parameters:
  • fout – Path of file to which output is written (clobbering existing data).
  • ferr – Path of file to which errors are written (clobbering existing data).
  • fout_append – Path of file to which output is appended.
  • ferr_append – Path of file to which errors are appended.
ReturnStderr()

Run command, capturing and returning stdout.

Returns:Data written to standard error by command.
Return type:str
ReturnStdout()

Run command, capturing and returning stdout.

Returns:Data written to standard out by command.
Return type:str
verbose = None

Flag indicating that issued commands should be logged.

glimpse.util.gos.IsImageFile(fname)

Determine if a file contains an image.

The decision is based on the extension portion of the filename.

Parameters:fname (str) – Path to file.
glimpse.util.gos.RunCommand(cmd, verbose=False, catch_retval=True)

Execute a command on the shell, and check the results.

See the Cmd class for a more flexibile solution.

Parameters:
  • cmd (str) – Command to run.
  • verbose (bool) – Whether to log the command.
  • catch_retval (bool) – Whether to throw an exception if the command’s return value is non-zero.
class glimpse.util.gos.TempDir

A temporary directory.

The directory is deleted when this object is destroyed. For a temporary file, see the tempfile.TemporaryFile class.

CopyAllTo(destPath)

Copy all files in temporary directory to another location.

CopyFrom(srcPath, destRelPath)

Copy a single file into the temporary directory.

Remove(relPath)

Remove a single file from the temporary directory.

glimpse.util.gos.TouchFile(fname)

Update a file’s access time, creating the file if it does not exist.

Parameters:fname (str) – Path to file.