| # Gaudi Framework, version v23r0 |

Home |  | Generated: Mon Jan 30 2012

Public Member Functions | Public Attributes | Private Attributes

# GaudiTest::TemporaryEnvironment Class Reference

Utility Classes. More...

List of all members.

## 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

## Detailed Description

Utility Classes.

    Class to changes the environment temporarily.

Definition at line 46 of file GaudiTest.py.

## Constructor & Destructor 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 50 of file GaudiTest.py.

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

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

    Revert the changes on destruction.

Definition at line 121 of file GaudiTest.py.

    00122                      :
    00123         """
    00124         Revert the changes on destruction.
    00125         """
    00126         #print "Restoring the environment"
    00127         self.restore()

## Member Function Documentation

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

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

Definition at line 103 of file GaudiTest.py.

    00104                               :
    00105         """
    00106         Operator 'in'.
    00107         Needed to provide the same interface as os.environ.
    00108         """
    00109         return key in self.env

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

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

Definition at line 79 of file GaudiTest.py.

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

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

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

Definition at line 72 of file GaudiTest.py.

    00073                              :
    00074         """
    00075         Get an environment variable.
    00076         Needed to provide the same interface as os.environ.
    00077         """
    00078         return self.env[key]

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

    Set an environment variable recording the previous value.

Definition at line 60 of file GaudiTest.py.

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

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

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

Definition at line 128 of file GaudiTest.py.

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

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 96 of file GaudiTest.py.

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

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

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

Definition at line 89 of file GaudiTest.py.

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

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

    Revert all the changes done to the orignal environment.

Definition at line 110 of file GaudiTest.py.

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

## Member Data Documentation

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

Definition at line 53 of file GaudiTest.py.

GaudiTest::TemporaryEnvironment::env

Definition at line 53 of file GaudiTest.py.

GaudiTest::TemporaryEnvironment::old_values

Definition at line 53 of file GaudiTest.py.

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

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

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines