HWRF  trunk@4391
Public Member Functions | List of all members
produtil.prog.ImmutableRunner Class Reference

An copy-on-write version of Runner. More...

Detailed Description

An copy-on-write version of Runner.

This subclass of Runner is unmodifiable. It is meant to be used for re-usable exe()-like objects. For example, if one wants an object lsl that runs exe('ls')['-l'] with optional extra arguments, one could do:

lsl=ImmutableRunner(Runner('ls')['-l'])

and then every time one does run(lsl[argument list]), it generates a new object without modifying the original lsl, ensuring later calls to lsl will have the same effect:

lsl['/'] lsl['~'] lsl['/'] # prints the same as the first

This is implemented by a copy-on-write method: if a modification is requested, a Runner is returned with the requested modifications.

Definition at line 884 of file prog.py.

Inheritance diagram for produtil.prog.ImmutableRunner:
produtil.prog.Runner

Public Member Functions

def __init__ (self, args, kwargs)
 Creates a new ImmutableRunner. More...
 
def copy
 Creates a deep copy of this runner, except if stream objects are connected to stdin, stdout or stderr. More...
 
def runner (self)
 Returns a modifiable version of this object (as a Runner). More...
 
def copyenv (self)
 Creates a new Runner that is like self in all ways except that it uses the parent process environment. More...
 
def clearenv (self)
 Creates a new Runner which is like self in all ways except that it uses an empty environment except for a few critical variables without which most programs cannot run. More...
 
def cd (self, cd)
 Returns a new Runner that is like self, except that it cd's to the target directory before running. More...
 
def env (self, kwargs)
 Returns a new Runner that is like self in all ways except that the specified environment variables are set. More...
 
def pipeto (self, other)
 Returns a new Runner that is like self in all ways, except that it has been piped into the other Runner. More...
 
def inp
 Returns a new Runner that is like self in all ways except that it has a different stdin. More...
 
def out
 Returns a new Runner that is like self in all ways except with a different stdout. More...
 
def err
 Returns a new Runner that is like self in all ways except with a different stderr. More...
 
def err2out (self)
 Returns a new Runner that is like self in all ways except that stderr is piped into stdout. More...
 
def prerun (self, arg)
 Returns a new Runner that is like self in all ways except that a new prerun function has been added. More...
 
def __getitem__ (self, args)
 Returns a new Runner that is like self in all ways except with new arguments. More...
 
def argins (self, index, arg)
 Returns a new Runner that is like self in all ways, except with the specified argument inserted. More...
 
def setthreads (self, nthreads)
 Sets the number of threads requested by this program. More...
 
def delthreads (self)
 Removes the request for threads. More...
 
- Public Member Functions inherited from produtil.prog.Runner
def __init__ (self, args, kwargs)
 Creates a new Runner. More...
 
def getthreads (self)
 Returns the number of threads requested by this program. More...
 
def setthreads (self, nthreads)
 Sets the number of threads requested by this program. More...
 
def delthreads (self)
 Removes the request for threads. More...
 
def first (self)
 Returns the first Runner in this pipeline. More...
 
def prerun (self, arg)
 Adds a function or callable object to be called before running the program. More...
 
def __getitem__ (self, args)
 Add one or more arguments to the executable. More...
 
def __str__ (self)
 Alias for repr()
 
def __repr__ (self)
 Attempts to produce valid Python code to represent this Runnable. More...
 
def __eq__ (self, other)
 Returns True if the other object is a Runner that is equal to this one, and False otherwise. More...
 
def isplainexe (self)
 Returns true if this is simply an executable with arguments (no redirection, no prerun objects, no environment modification, no piping), and False otherwise. More...
 
def cd (self, dirpath)
 Requests that this process run in the specified directory. More...
 
def __lt__ (self, stdin)
 Connects the given object to stdin, via inp(stdin,string=False). More...
 
def __gt__ (self, stdout)
 Connects the given object to stdout, truncating it if it is a file. More...
 
def __lshift__ (self, stdin)
 Sends the specified string into stdin. More...
 
def __rshift__ (self, stdout)
 Appends stdout to the specified file. More...
 
def __pos__ (self)
 Sends stderr to stdout. More...
 
def __ge__ (self, outerr)
 Redirects stderr and stdout to the specified file, truncating it. More...
 
def __or__ (self, other)
 Pipes this Runner to the other Runner. More...
 
def argins (self, index, arg)
 Inserts the specified argument before the given index. More...
 
def args (self)
 Iterates over the executable and arguments of this command.
 
def copy
 Returns a deep copy of this object, almost. More...
 
def copyenv (self)
 Instructs this command to duplicate the parent process environment (the default). More...
 
def clearenv (self)
 Instructs this command to start with an empty environment except for certain critical variables without which most programs cannot run. More...
 
def env (self, kwargs)
 Sets environment variables for this Runner. More...
 
def to_shell (self)
 Returns a string that expresses this object as a POSIX sh shell command if possible, or raises a subclass of NotValidPosixSh if not. More...
 
def runner (self)
 Returns self if self is modifiable, otherwise returns a modifiable copy of self. More...
 
def pipeto (self, other)
 Specifies that this Runner will send its stdout to the other runner's stdin. More...
 
def inp
 Specifies that the first Runner in this pipeline takes input from the given file or string specified by stdin. More...
 
def out
 Specifies that this process sends output from its stdout stream to the given file or stream. More...
 
def err2out (self)
 Sends stderr to stdout. More...
 
def err
 Specifies that this process sends output from its stderr stream to the given file or stream. More...
 

Constructor & Destructor Documentation

def produtil.prog.ImmutableRunner.__init__ (   self,
  args,
  kwargs 
)

Creates a new ImmutableRunner.

All arguments to this constructor have the same meanings as the Runner constructor.

Parameters
args,kwargspassed to Runner.__init__

Definition at line 905 of file prog.py.

Member Function Documentation

def produtil.prog.ImmutableRunner.__getitem__ (   self,
  args 
)

Returns a new Runner that is like self in all ways except with new arguments.

Parameters
argsthe new argument or arguments
See also
Runner.__getitem__

Definition at line 1001 of file prog.py.

def produtil.prog.ImmutableRunner.argins (   self,
  index,
  arg 
)

Returns a new Runner that is like self in all ways, except with the specified argument inserted.

Parameters
indexthe index to insert before
argthe argument to insert

Definition at line 1007 of file prog.py.

def produtil.prog.ImmutableRunner.cd (   self,
  cd 
)

Returns a new Runner that is like self, except that it cd's to the target directory before running.

The directory must already exist before the program starts.

Parameters
cdthe directory to cd into, which must already exist.
Returns
the new Runner

Definition at line 954 of file prog.py.

def produtil.prog.ImmutableRunner.clearenv (   self)

Creates a new Runner which is like self in all ways except that it uses an empty environment except for a few critical variables without which most programs cannot run.

(Retains PATH, USER, LOGNAME and HOME.)

Returns
a new Runner

Definition at line 947 of file prog.py.

def produtil.prog.ImmutableRunner.copy (   self,
  typeobj = None 
)

Creates a deep copy of this runner, except if stream objects are connected to stdin, stdout or stderr.

In that case, those same stream objects are still connected.

Parameters
typeobjthe type of the output object. Do not use this unless you know what you're doing
Returns
a copy of self

Definition at line 918 of file prog.py.

def produtil.prog.ImmutableRunner.copyenv (   self)

Creates a new Runner that is like self in all ways except that it uses the parent process environment.

Returns
the new Runner

Definition at line 942 of file prog.py.

def produtil.prog.ImmutableRunner.delthreads (   self)

Removes the request for threads.

Same as self.threads=1

Definition at line 1028 of file prog.py.

def produtil.prog.ImmutableRunner.env (   self,
  kwargs 
)

Returns a new Runner that is like self in all ways except that the specified environment variables are set.

Parameters
kwargsvarname=value arguments of environment variables to set
Returns
the new Runner

Definition at line 961 of file prog.py.

def produtil.prog.ImmutableRunner.err (   self,
  stderr,
  append = False 
)

Returns a new Runner that is like self in all ways except with a different stderr.

Parameters
stderrthe stderr filename
appendif True, append to the file, otherwise truncate

Definition at line 985 of file prog.py.

def produtil.prog.ImmutableRunner.err2out (   self)

Returns a new Runner that is like self in all ways except that stderr is piped into stdout.

Definition at line 991 of file prog.py.

def produtil.prog.ImmutableRunner.inp (   self,
  stdin,
  string = False 
)

Returns a new Runner that is like self in all ways except that it has a different stdin.

Parameters
stdinthe stdin string or filename
stringif True, stdin is a string

Definition at line 973 of file prog.py.

def produtil.prog.ImmutableRunner.out (   self,
  stdout,
  append = False 
)

Returns a new Runner that is like self in all ways except with a different stdout.

Parameters
stdoutthe stdout filename
appendif True, append to the file, otherwise truncate

Definition at line 979 of file prog.py.

def produtil.prog.ImmutableRunner.pipeto (   self,
  other 
)

Returns a new Runner that is like self in all ways, except that it has been piped into the other Runner.

Returns
the new Runner
Parameters
otherthe Runner to pipe into.

Definition at line 967 of file prog.py.

def produtil.prog.ImmutableRunner.prerun (   self,
  arg 
)

Returns a new Runner that is like self in all ways except that a new prerun function has been added.

Parameters
argthe new prerun function
See also
Runner.prerun()

Definition at line 995 of file prog.py.

def produtil.prog.ImmutableRunner.runner (   self)

Returns a modifiable version of this object (as a Runner).

Definition at line 928 of file prog.py.

def produtil.prog.ImmutableRunner.setthreads (   self,
  nthreads 
)

Sets the number of threads requested by this program.

Definition at line 1023 of file prog.py.


The documentation for this class was generated from the following file: