EnvConfig.TestEnvOps.TemporaryDir Class Reference
Inheritance diagram for EnvConfig.TestEnvOps.TemporaryDir:
Collaboration diagram for EnvConfig.TestEnvOps.TemporaryDir:

Public Member Functions

def __init__
 
def join (self, args)
 
def __str__ (self)
 
def remove (self)
 
def __enter__ (self)
 
def __exit__ (self, exc_type, exc_val, exc_tb)
 

Public Attributes

 chdir
 
 keep
 
 path
 
 old_dir
 

Detailed Description

Helper class to create a temporary directory and manage its lifetime.

An instance of this class can be used inside the 'with' statement and
returns the path to the temporary directory.

Definition at line 69 of file TestEnvOps.py.

Constructor & Destructor Documentation

def EnvConfig.TestEnvOps.TemporaryDir.__init__ (   self,
  chdir = False,
  keep = False 
)
Constructor.

Definition at line 76 of file TestEnvOps.py.

76  def __init__(self, chdir=False, keep=False):
77  '''Constructor.'''
78  self.chdir = chdir
79  self.keep = keep
80  self.path = mkdtemp()
81  self.old_dir = None

Member Function Documentation

def EnvConfig.TestEnvOps.TemporaryDir.__enter__ (   self)
Context Manager protocol 'enter' function.

Definition at line 98 of file TestEnvOps.py.

98  def __enter__(self):
99  '''
100  Context Manager protocol 'enter' function.
101  '''
102  if self.chdir:
103  self.old_dir = os.getcwd()
104  os.chdir(self.path)
105  return self.path
def EnvConfig.TestEnvOps.TemporaryDir.__exit__ (   self,
  exc_type,
  exc_val,
  exc_tb 
)
Context Manager protocol 'exit' function.
Remove the temporary directory and let the exceptions propagate.

Definition at line 106 of file TestEnvOps.py.

106  def __exit__(self, exc_type, exc_val, exc_tb):
107  '''
108  Context Manager protocol 'exit' function.
109  Remove the temporary directory and let the exceptions propagate.
110  '''
111  if self.old_dir:
112  os.chdir(self.old_dir)
113  self.old_dir = None
114  if not self.keep:
115  self.remove()
116  else:
117  print "WARNING: not removing temporary directory", self.path
118  return False
119 
def __exit__(self, exc_type, exc_val, exc_tb)
Definition: TestEnvOps.py:106
def EnvConfig.TestEnvOps.TemporaryDir.__str__ (   self)
String representation (path to the temporary directory).

Definition at line 87 of file TestEnvOps.py.

87  def __str__(self):
88  '''String representation (path to the temporary directory).'''
89  return self.path
def EnvConfig.TestEnvOps.TemporaryDir.join (   self,
  args 
)
Equivalent to os.path.join(self.path, *args).

Definition at line 82 of file TestEnvOps.py.

82  def join(self, *args):
83  '''
84  Equivalent to os.path.join(self.path, *args).
85  '''
86  return os.path.join(self.path, *args)
def EnvConfig.TestEnvOps.TemporaryDir.remove (   self)
Remove the temporary directory.
After a call to this method, the object is not usable anymore.

Definition at line 90 of file TestEnvOps.py.

90  def remove(self):
91  '''
92  Remove the temporary directory.
93  After a call to this method, the object is not usable anymore.
94  '''
95  if self.path: # allow multiple calls to the remove method
96  shutil.rmtree(self.path, ignore_errors=True)
97  self.path = None

Member Data Documentation

EnvConfig.TestEnvOps.TemporaryDir.chdir

Definition at line 78 of file TestEnvOps.py.

EnvConfig.TestEnvOps.TemporaryDir.keep

Definition at line 79 of file TestEnvOps.py.

EnvConfig.TestEnvOps.TemporaryDir.old_dir

Definition at line 81 of file TestEnvOps.py.

EnvConfig.TestEnvOps.TemporaryDir.path

Definition at line 80 of file TestEnvOps.py.


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