HWRF  trunk@4391
Public Member Functions | Public Attributes | List of all members
produtil.cd.TempDir Class Reference

This class is intended to be used with the Python "with TempDir() as t" syntax. More...

Detailed Description

This class is intended to be used with the Python "with TempDir() as t" syntax.

Example:

1 with TempDir() as t:
2  # we're now in the temporary directory
3  ...do things...
4 # the temporary directory has been deleted now

Definition at line 38 of file cd.py.

Inheritance diagram for produtil.cd.TempDir:
produtil.cd.NamedDir

Public Member Functions

def __init__
 Creates a TempDir. More...
 
def name_make_dir (self)
 Decide the name of the directory, and create the directory. More...
 
def mkdir_cd (self)
 Creates the temporary directory and chdirs the current process into that directory. More...
 
def cd_out (self)
 Exit the temporary directory created by mkdir_cd and return to the original directory, if possible. More...
 
def cd_rmdir (self)
 CD out and remove the old directory. More...
 
def exception_info (self)
 Called to dump information to a log, or failing that, the terminal if an unexpected exception is caught. More...
 
def __enter__ (self)
 This is a simple wrapper around mkdir_cd that is intended to be used with in a "with" block. More...
 
def __exit__ (self, etype, value, traceback)
 Exit the 'with' block. More...
 

Public Attributes

 dirname
 The name of the target directory. More...
 
 suffix
 Temporary directory name suffix.
 
 prefix
 Temporary directory name prefix.
 
 print_on_exception
 Should we print exceptions before exiting the directory?
 
 dir
 The directory object. More...
 
 olddir
 The name of the directory we came from. More...
 

Constructor & Destructor Documentation

def produtil.cd.TempDir.__init__ (   self,
  suffix = '.tmp',
  prefix = 'tempdir.',
  dir = None,
  keep = False,
  logger = None,
  print_on_exception = True,
  add_perms = perm_add,
  remove_perms = perm_remove,
  keep_on_error = True,
  cd = True 
)

Creates a TempDir.

Parameters
suffix,prefix,dirPassed to the tempfile.mkdtemp to generate the directory name. Meanings are the same as in that constructor. See the Python documentation for details.
keepControls directory deletion if the "with" block returns without an exception. If False, the directory is deleted. Default: keep=False
loggerA logging.logger for log messages
print_on_exceptionPrint exceptions before leaving the dir
add_permsPermissions to add to the directory. Default: 755.
remove_permsPermissions to remove from the directory. Default: setuid and world write.
keep_on_errorControls directory deletion if the "with" block raises an Exception or GeneratorExit, or subclass thereof. If False, the directory is deleted under those circumstances. Default: keep_on_error=True.
cdIf True (default), cd to the directory in the "with" block and cd back out afterwards. If False, then only directory creation and deletion happens.

Definition at line 50 of file cd.py.

Member Function Documentation

def produtil.cd.TempDir.__enter__ (   self)

This is a simple wrapper around mkdir_cd that is intended to be used with in a "with" block.

This subroutine is automatically called at the beginning of the block.

Definition at line 193 of file cd.py.

def produtil.cd.TempDir.__exit__ (   self,
  etype,
  value,
  traceback 
)

Exit the 'with' block.

This is a simple wrapper around cd_rmdir that is intended to be used with in a "with" block. This subroutine is automatically called at the end of the block. It will call cd_rmdir to delete the directory unless an exception is thrown that is NOT a subclass of Exception or GeneratorExit. The removal is skipped to allow the program to exit quickly in case of a fatal signal (ie.: SIGQUIT, SIGTERM, SIGINT, SIGHUP).

Parameters
etype,value,tracebackexception information

Definition at line 199 of file cd.py.

def produtil.cd.TempDir.cd_out (   self)

Exit the temporary directory created by mkdir_cd and return to the original directory, if possible.

Definition at line 135 of file cd.py.

Referenced by produtil.cd.TempDir.__exit__(), and produtil.cd.TempDir.cd_rmdir().

def produtil.cd.TempDir.cd_rmdir (   self)

CD out and remove the old directory.

This subroutine exits the temporary directory created by mkdir_cd, and then deletes that temporary directory. After this routine, the process will be in its original directory (from before the call to mkdir_cd) if possible, or otherwise it will be in the root directory (/).

It is the caller's responsibility to ensure this function is not called if keep_on_error=True and an error occurs.

Definition at line 141 of file cd.py.

Referenced by produtil.cd.TempDir.__exit__().

def produtil.cd.TempDir.exception_info (   self)

Called to dump information to a log, or failing that, the terminal if an unexpected exception is caught.

Definition at line 181 of file cd.py.

Referenced by produtil.cd.TempDir.__exit__().

def produtil.cd.TempDir.mkdir_cd (   self)

Creates the temporary directory and chdirs the current process into that directory.

It calls self.name_make_dir() to do the naming and directory creation.

Definition at line 126 of file cd.py.

Referenced by produtil.cd.TempDir.__enter__().

def produtil.cd.TempDir.name_make_dir (   self)

Decide the name of the directory, and create the directory.

Also create any path components leading up to the directory.

Definition at line 108 of file cd.py.

Referenced by produtil.cd.TempDir.mkdir_cd().

Member Data Documentation

produtil.cd.TempDir.dir

The directory object.

Definition at line 78 of file cd.py.

Referenced by produtil.cd.TempDir.name_make_dir().

produtil.cd.TempDir.dirname
produtil.cd.TempDir.olddir

The name of the directory we came from.

Definition at line 79 of file cd.py.

Referenced by produtil.cd.TempDir.cd_out(), and produtil.cd.TempDir.mkdir_cd().


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