| # Gaudi Framework, version v25r0 |

Home |  | Generated: Mon Feb 17 2014

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Public Member Functions | Public Attributes | Private Attributes | List of all members
GaudiTest.TemporaryEnvironment Class Reference

Utility Classes. More...

## 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 69 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 73 of file GaudiTest.py.

73
74 def __init__(self, orig = os.environ, keep_same = False):
75 """
76 Create a temporary environment on top of the one specified
77 (it can be another TemporaryEnvironment instance).
78 """
79 #print "New environment"
80 self.old_values = {}
81 self.env = orig
82 self._keep_same = keep_same

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

    Revert the changes on destruction.

Definition at line 144 of file GaudiTest.py.

144
145 def __del__(self):
146 """
147 Revert the changes on destruction.
148 """
149 #print "Restoring the environment"
150 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 126 of file GaudiTest.py.

126
127 def __contains__(self,key):
128 """
129 Operator 'in'.
130 Needed to provide the same interface as os.environ.
131 """
132 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 102 of file GaudiTest.py.

102
103 def __delitem__(self,key):
104 """
105 Unset an environment variable.
106 Needed to provide the same interface as os.environ.
107 """
108 if key not in self.env :
109 raise KeyError(key)
110 self.old_values[key] = self.env[key]
111 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 95 of file GaudiTest.py.

95
96 def __getitem__(self,key):
97 """
98 Get an environment variable.
99 Needed to provide the same interface as os.environ.
100 """
101 return self.env[key]

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

    Set an environment variable recording the previous value.

Definition at line 83 of file GaudiTest.py.

83
84 def __setitem__(self,key,value):
85 """
86 Set an environment variable recording the previous value.
87 """
88 if key not in self.old_values :
89 if key in self.env :
90 if not self._keep_same or self.env[key] != value:
91 self.old_values[key] = self.env[key]
92 else:
93 self.old_values[key] = None
94 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 151 of file GaudiTest.py.

151
152 def gen_script(self,shell_type):
153 """
154 Generate a shell script to reproduce the changes in the environment.
155 """
156 shells = [ 'csh', 'sh', 'bat' ]
157 if shell_type not in shells:
158 raise RuntimeError("Shell type '%s' unknown. Available: %s"%(shell_type,shells))
159 out = ""
160 for key,value in self.old_values.items():
161 if key not in self.env:
162 # unset variable
163 if shell_type == 'csh':
164 out += 'unsetenv %s\n'%key
165 elif shell_type == 'sh':
166 out += 'unset %s\n'%key
167 elif shell_type == 'bat':
168 out += 'set %s=\n'%key
169 else:
170 # set variable
171 if shell_type == 'csh':
172 out += 'setenv %s "%s"\n'%(key,self.env[key])
173 elif shell_type == 'sh':
174 out += 'export %s="%s"\n'%(key,self.env[key])
175 elif shell_type == 'bat':
176 out += 'set %s=%s\n'%(key,self.env[key])
177 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 119 of file GaudiTest.py.

119
120 def items(self):
121 """
122 Return the list of (name,value) pairs for the defined environment variables.
123 Needed to provide the same interface as os.environ.
124 """
125 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 112 of file GaudiTest.py.

112
113 def keys(self):
114 """
115 Return the list of defined environment variables.
116 Needed to provide the same interface as os.environ.
117 """
118 return self.env.keys()

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

    Revert all the changes done to the orignal environment.

Definition at line 133 of file GaudiTest.py.

133
134 def restore(self):
135 """
136 Revert all the changes done to the orignal environment.
137 """
138 for key,value in self.old_values.items():
139 if value is None:
140 del self.env[key]
141 else:
142 self.env[key] = value
143 self.old_values = {}

## Member Data Documentation

GaudiTest.TemporaryEnvironment._keep_same
| private

Definition at line 81 of file GaudiTest.py.

GaudiTest.TemporaryEnvironment.env

Definition at line 80 of file GaudiTest.py.

GaudiTest.TemporaryEnvironment.old_values

Definition at line 79 of file GaudiTest.py.

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

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