| The Gaudi Framework v32r1 (f65d50dc)

Public Member Functions | Public Attributes | Private Attributes | List of all members
GaudiTest.TemporaryEnvironment Class Reference

Utility Classes. More...

## Public Member Functions
def | __init__ (self, orig=os.environ, keep_same=False)
def | __setitem__ (self, key, value)
def | __getitem__ (self, key)
def | __delitem__ (self, key)
def | keys (self)
def | items (self)
def | __contains__ (self, key)
def | restore (self)
def | __del__ (self)
def | gen_script (self, shell_type)

## Public Attributes
| old_values
| env

## Private Attributes
| _keep_same

## Detailed Description

Utility Classes.

    Class to changes the environment temporarily.

Definition at line 77 of file GaudiTest.py.

## Constructor & Destructor Documentation

## ◆ __init__()

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

82 def __init__(self, orig=os.environ, keep_same=False):
83 """
84 Create a temporary environment on top of the one specified
85 (it can be another TemporaryEnvironment instance).
86 """
87 # print "New environment"
88 self.old_values = {}
89 self.env = orig
90 self._keep_same = keep_same
91

## ◆ __del__()

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

    Revert the changes on destruction.

Definition at line 153 of file GaudiTest.py.

153 def __del__(self):
154 """
155 Revert the changes on destruction.
156 """
157 # print "Restoring the environment"
158 self.restore()
159

## Member Function Documentation

## ◆ __contains__()

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

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

Definition at line 135 of file GaudiTest.py.

135 def __contains__(self, key):
136 """
137 Operator 'in'.
138 Needed to provide the same interface as os.environ.
139 """
140 return key in self.env
141

## ◆ __delitem__()

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

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

Definition at line 111 of file GaudiTest.py.

111 def __delitem__(self, key):
112 """
113 Unset an environment variable.
114 Needed to provide the same interface as os.environ.
115 """
116 if key not in self.env:
117 raise KeyError(key)
118 self.old_values[key] = self.env[key]
119 del self.env[key]
120

## ◆ __getitem__()

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

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

Definition at line 104 of file GaudiTest.py.

104 def __getitem__(self, key):
105 """
106 Get an environment variable.
107 Needed to provide the same interface as os.environ.
108 """
109 return self.env[key]
110
GaudiPython.Pythonizations.__getitem__
__getitem__
Definition: Pythonizations.py:115

## ◆ __setitem__()

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

    Set an environment variable recording the previous value.

Definition at line 92 of file GaudiTest.py.

92 def __setitem__(self, key, value):
93 """
94 Set an environment variable recording the previous value.
95 """
96 if key not in self.old_values:
97 if key in self.env:
98 if not self._keep_same or self.env[key] != value:
99 self.old_values[key] = self.env[key]
100 else:
101 self.old_values[key] = None
102 self.env[key] = value
103

## ◆ gen_script()

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

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

Definition at line 160 of file GaudiTest.py.

160 def gen_script(self, shell_type):
161 """
162 Generate a shell script to reproduce the changes in the environment.
163 """
164 shells = ['csh', 'sh', 'bat']
165 if shell_type not in shells:
166 raise RuntimeError("Shell type '%s' unknown. Available: %s" %
167 (shell_type, shells))
168 out = ""
169 for key, value in self.old_values.items():
170 if key not in self.env:
171 # unset variable
172 if shell_type == 'csh':
173 out += 'unsetenv %s\n' % key
174 elif shell_type == 'sh':
175 out += 'unset %s\n' % key
176 elif shell_type == 'bat':
177 out += 'set %s=\n' % key
178 else:
179 # set variable
180 if shell_type == 'csh':
181 out += 'setenv %s "%s"\n' % (key, self.env[key])
182 elif shell_type == 'sh':
183 out += 'export %s="%s"\n' % (key, self.env[key])
184 elif shell_type == 'bat':
185 out += 'set %s=%s\n' % (key, self.env[key])
186 return out
187
188

## ◆ items()

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

128 def items(self):
129 """
130 Return the list of (name,value) pairs for the defined environment variables.
131 Needed to provide the same interface as os.environ.
132 """
133 return self.env.items()
134

## ◆ keys()

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

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

Definition at line 121 of file GaudiTest.py.

121 def keys(self):
122 """
123 Return the list of defined environment variables.
124 Needed to provide the same interface as os.environ.
125 """
126 return self.env.keys()
127

## ◆ restore()

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

    Revert all the changes done to the original environment.

Definition at line 142 of file GaudiTest.py.

142 def restore(self):
143 """
144 Revert all the changes done to the original environment.
145 """
146 for key, value in self.old_values.items():
147 if value is None:
148 del self.env[key]
149 else:
150 self.env[key] = value
151 self.old_values = {}
152

## Member Data Documentation

## ◆ _keep_same

GaudiTest.TemporaryEnvironment._keep_same
| private

Definition at line 90 of file GaudiTest.py.

## ◆ env

GaudiTest.TemporaryEnvironment.env

Definition at line 89 of file GaudiTest.py.

## ◆ old_values

GaudiTest.TemporaryEnvironment.old_values

Definition at line 88 of file GaudiTest.py.

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

- GaudiPolicy/qmtest_classes/GaudiTest.py