Package gavo :: Package utils :: Module ostricks
[frames] | no frames]

Module ostricks

source code

OS abstractions and related.

This module contains, in partiular, the interface for having "easy subcommands" using argparse. The idea is to use the exposedFunction decorator on functions that should be callable from the command line as subcommands; the functions must all have the same signature. For example, if they all took the stuff returned by argparse, you could say in the module containing them:

 args = makeCLIParser(globals()).parse_args()
 args.subAction(args)

To specify the command line arguments to the function, use Args. See admin.py for an example.

Classes
  HTTPSHandler
  Arg
an argument/option to a subcommand.
Functions
 
safeclose(f)
syncs and closes the python file f.
source code
 
safeReplaced(*args, **kwds)
opens fName for "safe replacement".
source code
 
urlopenRemote(url, data=None, creds=(None, None), timeout=100)
works like urllib2.urlopen, except only http, https, and ftp URLs are handled.
source code
 
fgetmtime(fileobj)
returns the mtime of the file below fileobj.
source code
 
cat(srcF, destF, chunkSize=1048576)
reads srcF into destF in chunks.
source code
 
ensureDir(dirPath, mode=None, setGroupTo=None)
makes sure that dirPath exists and is a directory.
source code
 
exposedFunction(argSpecs=(), help=None)
a decorator exposing a function to parseArgs.
source code
 
makeCLIParser(functions)
returns a command line parser parsing subcommands from functions.
source code
Variables
  __package__ = 'gavo.utils'
Function Details

safeclose(f)

source code 

syncs and closes the python file f.

You generally want to use this rather than a plain close() before overwriting a file with a new version.

safeReplaced(*args, **kwds)

source code 

opens fName for "safe replacement".

Safe replacement means that you can write to the object returned, and when everything works out all right, what you have written replaces the old content of fName, where the old mode is preserved if possible. When there are errors, however, the old content remains.

Decorators:
  • @contextlib.contextmanager

urlopenRemote(url, data=None, creds=(None, None), timeout=100)

source code 

works like urllib2.urlopen, except only http, https, and ftp URLs are handled.

The function also massages the error messages of urllib2 a bit. urllib2 errors always become IOErrors (which is more convenient within the DC).

creds may be a pair of username and password. Those credentials will be presented in http basic authentication to any server that cares to ask. For both reasons, don't use any valuable credentials here.

fgetmtime(fileobj)

source code 

returns the mtime of the file below fileobj.

This raises an os.error if that file cannot be fstated.

ensureDir(dirPath, mode=None, setGroupTo=None)

source code 

makes sure that dirPath exists and is a directory.

If dirPath does not exist, it is created, and its permissions are set to mode with group ownership setGroupTo if those are given.

setGroupTo must be a numerical gid if given.

This function may raise all kinds of os.errors if something goes wrong. These probably should be handed through all the way to the user since when something fails here, there's usually little the program can safely do to recover.

exposedFunction(argSpecs=(), help=None)

source code 

a decorator exposing a function to parseArgs.

argSpecs is a sequence of Arg objects. This defines the command line interface to the function.

The decorated function itself must accept a single argument, the args object returned by argparse's parse_args.

makeCLIParser(functions)

source code 

returns a command line parser parsing subcommands from functions.

functions is a dictionary (as returned from globals()). Subcommands will be generated from all objects that have a subparseArgs attribute; furnish them using the commandWithArgs decorator.

This attribute must contain a sequence of Arg items (see above).