| # Gaudi Framework, version v20r4
Generated: 8 Jan 2009

# GaudiTest::TemporaryEnvironment Class Reference

List of all members.

## Detailed Description

    Class to changes the environment temporarily.

Definition at line 45 of file GaudiTest.py.

## Public Member Functions
def | __init__
def | __setitem__
def | __getitem__
def | __delitem__
def | keys
def | items
def | __contains__
def | restore
def | __del__
def | gen_script
## Public Attributes
| old_values
| env
## Private Attributes
| _keep_same

## Member Function Documentation

def GaudiTest::TemporaryEnvironment::__init__ | ( |  | self,
|  |  | orig = `os.environ`,
|  |  | keep_same = `False` |
| ) |  |  |

    Create a temporary environment on top of the one specified
    (it can be another TemporaryEnvironment instance).

Definition at line 49 of file GaudiTest.py.

    00049                                                             :
    00050         """
    00051         Create a temporary environment on top of the one specified
    00052         (it can be another TemporaryEnvironment instance).
    00053         """
    00054         #print "New environment"
    00055         self.old_values = {}
    00056         self.env = orig
    00057         self._keep_same = keep_same
    00058
        def __setitem__(self,key,value):

def GaudiTest::TemporaryEnvironment::__setitem__ | ( |  | self,
|  |  | key,
|  |  | value |
| ) |  |  |

    Set an environment variable recording the previous value.

Definition at line 59 of file GaudiTest.py.

    00059                                    :
    00060         """
    00061         Set an environment variable recording the previous value.
    00062         """
    00063         if key not in self.old_values :
    00064             if key in self.env :
    00065                 if not self._keep_same or self.env[key] != value:
    00066                     self.old_values[key] = self.env[key]
    00067             else:
    00068                 self.old_values[key] = None
    00069         self.env[key] = value
    00070
        def __getitem__(self,key):

def GaudiTest::TemporaryEnvironment::__getitem__ | ( |  | self,
|  |  | key |
| ) |  |  |

    Get an environment variable.
    Needed to provide the same interface as os.environ.

Definition at line 71 of file GaudiTest.py.

    00071                              :
    00072         """
    00073         Get an environment variable.
    00074         Needed to provide the same interface as os.environ.
    00075         """
    00076         return self.env[key]
    00077
        def __delitem__(self,key):

def GaudiTest::TemporaryEnvironment::__delitem__ | ( |  | self,
|  |  | key |
| ) |  |  |

    Unset an environment variable.
    Needed to provide the same interface as os.environ.

Definition at line 78 of file GaudiTest.py.

    00078                              :
    00079         """
    00080         Unset an environment variable.
    00081         Needed to provide the same interface as os.environ.
    00082         """
    00083         if key not in self.env :
    00084             raise KeyError(key)
    00085         self.old_values[key] = self.env[key]
    00086         del self.env[key]
    00087
        def keys(self):

def GaudiTest::TemporaryEnvironment::keys | ( |  | self | ) |

    Return the list of defined environment variables.
    Needed to provide the same interface as os.environ.

Definition at line 88 of file GaudiTest.py.

    00088                   :
    00089         """
    00090         Return the list of defined environment variables.
    00091         Needed to provide the same interface as os.environ.
    00092         """
    00093         return self.env.keys()
    00094
        def items(self):

def GaudiTest::TemporaryEnvironment::items | ( |  | self | ) |

    Return the list of (name,value) pairs for the defined environment variables.
    Needed to provide the same interface as os.environ.

Definition at line 95 of file GaudiTest.py.

    00095                    :
    00096         """
    00097         Return the list of (name,value) pairs for the defined environment variables.
    00098         Needed to provide the same interface as os.environ.
    00099         """
    00100         return self.env.items()
    00101
        def __contains__(self,key):

def GaudiTest::TemporaryEnvironment::__contains__ | ( |  | self,
|  |  | key |
| ) |  |  |

    Operator 'in'.
    Needed to provide the same interface as os.environ.

Definition at line 102 of file GaudiTest.py.

    00102                               :
    00103         """
    00104         Operator 'in'.
    00105         Needed to provide the same interface as os.environ.
    00106         """
    00107         return key in self.env
    00108
        def restore(self):

def GaudiTest::TemporaryEnvironment::restore | ( |  | self | ) |

    Revert all the changes done to the orignal environment.

Definition at line 109 of file GaudiTest.py.

    00109                      :
    00110         """
    00111         Revert all the changes done to the orignal environment.
    00112         """
    00113         for key,value in self.old_values.items():
    00114             if value is None:
    00115                 del self.env[key]
    00116             else:
    00117                 self.env[key] = value
    00118         self.old_values = {}
    00119
        def __del__(self):

def GaudiTest::TemporaryEnvironment::__del__ | ( |  | self | ) |

    Revert the changes on destruction.

Definition at line 120 of file GaudiTest.py.

    00120                      :
    00121         """
    00122         Revert the changes on destruction.
    00123         """
    00124         #print "Restoring the environment"
    00125         self.restore()
    00126
        def gen_script(self,shell_type):

def GaudiTest::TemporaryEnvironment::gen_script | ( |  | self,
|  |  | shell_type |
| ) |  |  |

    Generate a shell script to reproduce the changes in the environment.

Definition at line 127 of file GaudiTest.py.

    00127                                    :
    00128         """
    00129         Generate a shell script to reproduce the changes in the environment.
    00130         """
    00131         shells = [ 'csh', 'sh', 'bat' ]
    00132         if shell_type not in shells:
    00133             raise RuntimeError("Shell type '%s' unknown. Available: %s"%(shell_type,shells))
    00134         out = ""
    00135         for key,value in self.old_values.items():
    00136             if key not in self.env:
    00137                 # unset variable
    00138                 if shell_type == 'csh':
    00139                     out += 'unsetenv %s\n'%key
    00140                 elif shell_type == 'sh':
    00141                     out += 'unset %s\n'%key
    00142                 elif shell_type == 'bat':
    00143                     out += 'set %s=\n'%key
    00144             else:
    00145                 # set variable
    00146                 if shell_type == 'csh':
    00147                     out += 'setenv %s "%s"\n'%(key,self.env[key])
    00148                 elif shell_type == 'sh':
    00149                     out += 'export %s="%s"\n'%(key,self.env[key])
    00150                 elif shell_type == 'bat':
    00151                     out += 'set %s=%s\n'%(key,self.env[key])
    00152         return out
    00153
    class TempDir:

## Member Data Documentation

GaudiTest::TemporaryEnvironment::old_values

Definition at line 55 of file GaudiTest.py.

GaudiTest::TemporaryEnvironment::env

Definition at line 56 of file GaudiTest.py.

GaudiTest::TemporaryEnvironment::_keep_same`[private]`

Definition at line 57 of file GaudiTest.py.

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

- /afs/cern.ch/sw/Gaudi/releases/GAUDI/GAUDI_v20r4/GaudiPolicy/qmtest_classes/GaudiTest.py