Gaudi Framework, version v23r3

Home   Generated: Thu Jun 28 2012
Public Member Functions | Public Attributes | Private Member Functions

EnvConfig::Control::Environment Class Reference

List of all members.

Public Member Functions

def __init__
def vars
def var
def search
def append
def prepend
def declare
def set
def searchFile
def unset
def remove
def remove_regexp
def loadXML
def startXMLinput
def finishXMLinput
def writeToFile
def writeToXMLFile
def loadAllSystemVariables
def __getitem__
def __setitem__
def __delitem__
def __iter__
def __contains__
def __len__

Public Attributes

 report
 sysSeparator
 separator
 posActions
 variables
 loadFromSystem
 asWriter
 writer

Private Member Functions

def _concatenate
def _writeVarToXML

Detailed Description

object to hold settings of environment

Definition at line 12 of file Control.py.


Constructor & Destructor Documentation

def EnvConfig::Control::Environment::__init__ (   self,
  loadFromSystem = True,
  useAsWriter = False,
  reportLevel = 1 
)
Initial variables to be pushed and setup

append switch between append and prepend for initial variables.
loadFromSystem causes variable`s system value to be loaded on first encounter.
If useAsWriter == True than every change to variables is recorded to XML file.
reportLevel sets the level of messaging.
**kwargs contain initial variables.

Definition at line 15 of file Control.py.

00016                                                                                  :
00017         '''Initial variables to be pushed and setup
00018 
00019         append switch between append and prepend for initial variables.
00020         loadFromSystem causes variable`s system value to be loaded on first encounter.
00021         If useAsWriter == True than every change to variables is recorded to XML file.
00022         reportLevel sets the level of messaging.
00023         **kwargs contain initial variables.
00024         '''
00025         self.report = xmlModule.Report(reportLevel)
00026 
00027         if platform.system() != 'Linux':
00028             self.sysSeparator = ';'
00029         else:
00030             self.sysSeparator = ':'
00031         self.separator = ':'
00032 
00033         self.posActions = ['append','prepend','set','unset','remove', 'remove-regexp', 'declare']
00034         self.variables = {}
00035         self.loadFromSystem = loadFromSystem
00036         self.asWriter = useAsWriter
00037         if useAsWriter:
00038             self.writer = xmlModule.XMLFile()
00039             self.startXMLinput()
00040 


Member Function Documentation

def EnvConfig::Control::Environment::__contains__ (   self,
  item 
)

Definition at line 267 of file Control.py.

00268                                 :
00269         return item in self.variables.keys()

def EnvConfig::Control::Environment::__delitem__ (   self,
  key 
)

Definition at line 260 of file Control.py.

00261                               :
00262         del self.variables[key]

def EnvConfig::Control::Environment::__getitem__ (   self,
  key 
)

Definition at line 251 of file Control.py.

00252                               :
00253         return self.variables[key]

def EnvConfig::Control::Environment::__iter__ (   self )

Definition at line 263 of file Control.py.

00264                       :
00265         for i in self.variables:
00266             yield i

def EnvConfig::Control::Environment::__len__ (   self )

Definition at line 270 of file Control.py.

00271                      :
00272         return len(self.variables.keys())
00273 
def EnvConfig::Control::Environment::__setitem__ (   self,
  key,
  value 
)

Definition at line 254 of file Control.py.

00255                                      :
00256         if value in self.variables.keys():
00257             self.report.addWarn('Addition canceled because of duplicate entry. Var: "' + self.varName + '" value: "' + value + '".')
00258         else:
00259             self.append(key, value)

def EnvConfig::Control::Environment::_concatenate (   self,
  value 
) [private]
Returns a variable string with separator separator from the values list

Definition at line 235 of file Control.py.

00236                                  :
00237         '''Returns a variable string with separator separator from the values list'''
00238         stri = ""
00239         for it in value:
00240             stri += it + self.separator
00241         stri = stri[0:len(stri)-1]
00242         return stri
00243 

def EnvConfig::Control::Environment::_writeVarToXML (   self,
  name,
  action,
  value,
  type = 'list',
  local = 'false' 
) [private]
Writes single variable to XML file.

Definition at line 244 of file Control.py.

00245                                                                              :
00246         '''Writes single variable to XML file.'''
00247         if isinstance(value, list):
00248             value = self._concatenate(value)
00249         self.writer.writeVar(name, action, value, type, local)
00250 

def EnvConfig::Control::Environment::append (   self,
  name,
  value 
)
Appends to an existing variable.

Definition at line 66 of file Control.py.

00067                                  :
00068         '''Appends to an existing variable.'''
00069         if self.asWriter:
00070             self._writeVarToXML(name, 'append', value)
00071         else:
00072             if name in self.variables.keys():
00073                 self.variables[name].append(value, self.separator, self.variables)
00074             else:
00075                 self.declare(name, 'list', False)
00076                 self.append(name, value)
00077 

def EnvConfig::Control::Environment::declare (   self,
  name,
  type,
  local 
)
Creates an instance of new variable. It loads values from the OS if the variable is not local.

Definition at line 89 of file Control.py.

00090                                         :
00091         '''Creates an instance of new variable. It loads values from the OS if the variable is not local.'''
00092         if self.asWriter:
00093             self._writeVarToXML(name, 'declare', '', type, local)
00094 
00095         if not isinstance(local, bool):
00096             if str(local).lower() == 'true':
00097                 local = True
00098             else:
00099                 local = False
00100 
00101         if name in self.variables.keys():
00102             if self.variables[name].local != local:
00103                 raise Variable.EnvironmentError(name, 'redeclaration')
00104             else:
00105                 if type.lower() == "list":
00106                     if not isinstance(self.variables[name],Variable.List):
00107                         raise Variable.EnvironmentError(name, 'redeclaration')
00108                 else:
00109                     if not isinstance(self.variables[name],Variable.Scalar):
00110                         raise Variable.EnvironmentError(name, 'redeclaration')
00111 
00112         if type.lower() == "list":
00113             a = Variable.List(name, local, report=self.report)
00114         else:
00115             a = Variable.Scalar(name, local, report=self.report)
00116 
00117         if self.loadFromSystem and not local:
00118             if name in os.environ.keys():
00119                 a.set(os.environ[name], self.sysSeparator, environment=self.variables, resolve=False)
00120         self.variables[name] = a

def EnvConfig::Control::Environment::finishXMLinput (   self,
  outputFile = '' 
)
Finishes input of XML file and closes the file.

Definition at line 189 of file Control.py.

00190                                              :
00191         '''Finishes input of XML file and closes the file.'''
00192         self.writer.writeToFile(outputFile)
00193 

def EnvConfig::Control::Environment::loadAllSystemVariables (   self )
Loads all variables from the current system settings.

Definition at line 228 of file Control.py.

00229                                     :
00230         '''Loads all variables from the current system settings.'''
00231         for val in os.environ.keys():
00232             if not val in self.variables.keys():
00233                 self.declare(val, 'list', False)
00234 

def EnvConfig::Control::Environment::loadXML (   self,
  fileName = None,
  namespace = 'EnvSchema' 
)
Loads XML file for input variables.

Definition at line 167 of file Control.py.

00168                                                                :
00169         '''Loads XML file for input variables.'''
00170         XMLfile = xmlModule.XMLFile()
00171         variables = XMLfile.variable(fileName, namespace = namespace)
00172         i = 0
00173         for variable in variables:
00174             i += 1
00175             if variable[1] not in self.posActions:
00176                 self.report.addError('Node '+str(i)+': No action taken with var "' + variable[0] + '". Probably wrong action argument: "'+variable[1]+'".')
00177             elif variable[1] == 'declare':
00178                 self.declare(str(variable[0]), str(variable[2]), variable[3])
00179             else:
00180                 if variable[1] == 'remove-regexp':
00181                     variable[1] = 'remove_regexp'
00182                 eval('self.'+variable[1]+'(str(variable[0]), str(variable[2]))')
00183 

def EnvConfig::Control::Environment::prepend (   self,
  name,
  value 
)
Prepends to an existing variable, or create a new one.

Definition at line 78 of file Control.py.

00079                                   :
00080         '''Prepends to an existing variable, or create a new one.'''
00081         if self.asWriter:
00082             self._writeVarToXML(name, 'prepend', value)
00083         else:
00084             if name in self.variables.keys():
00085                 self.variables[name].prepend(value, self.separator, self.variables)
00086             else:
00087                 self.declare(name, 'list', False)
00088                 self.prepend(name, value)

def EnvConfig::Control::Environment::remove (   self,
  name,
  value,
  regexp = False 
)
Remove value from variable.

Definition at line 153 of file Control.py.

00154                                                :
00155         '''Remove value from variable.'''
00156         if self.asWriter:
00157             self._writeVarToXML(name, 'remove', value)
00158         else:
00159             if name in self.variables:
00160                 self.variables[name].remove(value, self.separator, regexp)
00161             elif self.loadFromSystem:
00162                 self.declare(name, 'list', False)
00163 

def EnvConfig::Control::Environment::remove_regexp (   self,
  name,
  value 
)

Definition at line 164 of file Control.py.

00165                                         :
00166         self.remove(name, value, True)

def EnvConfig::Control::Environment::search (   self,
  varName,
  expr,
  regExp = False 
)
Searches in a variable for a value.

Definition at line 61 of file Control.py.

00062                                                    :
00063         '''Searches in a variable for a value.'''
00064         return self.variables[varName].search(expr, regExp)
00065 

def EnvConfig::Control::Environment::searchFile (   self,
  file,
  varName 
)
Searches for appearance of variable in a file.

Definition at line 134 of file Control.py.

00135                                        :
00136         '''Searches for appearance of variable in a file.'''
00137         XMLFile = xmlModule.XMLFile()
00138         variable = XMLFile.variable(file, name=varName)
00139         return variable
00140 

def EnvConfig::Control::Environment::set (   self,
  name,
  value 
)
Sets a single variable - overrides any previous value!

Definition at line 121 of file Control.py.

00122                               :
00123         '''Sets a single variable - overrides any previous value!'''
00124         name = str(name)
00125         if self.asWriter:
00126             self._writeVarToXML(name, 'set', value)
00127         else:
00128             if name in self.variables:
00129                 self.variables[name].set(value, self.separator, self.variables)
00130             else:
00131                 self.declare(name, 'list', False)
00132                 self.set(name, value)
00133 

def EnvConfig::Control::Environment::startXMLinput (   self )
Renew writer for new input.

Definition at line 184 of file Control.py.

00185                            :
00186         '''Renew writer for new input.'''
00187         self.writer.resetWriter()
00188 

def EnvConfig::Control::Environment::unset (   self,
  name,
  value = None 
)
Unsets a single variable to an empty value - overrides any previous value!

Definition at line 141 of file Control.py.

00142                                      :
00143         '''Unsets a single variable to an empty value - overrides any previous value!'''
00144         if self.asWriter:
00145             self._writeVarToXML(name, 'unset', '')
00146         else:
00147             if name in self.variables:
00148                 self.variables[name].set([], self.separator)
00149             else:
00150                 a = Variable.List(name, report=self.report)
00151                 self.variables[name] = a
00152 

def EnvConfig::Control::Environment::var (   self,
  name 
)
Gets a single variable. If not available then tries to load from system.

Definition at line 52 of file Control.py.

00053                        :
00054         '''Gets a single variable. If not available then tries to load from system.'''
00055         if name in self.variables.keys():
00056             return self.variables[name]
00057         else:
00058             self._loadFromSystem(name, '', 'append')
00059             return self.variables[name]
00060 

def EnvConfig::Control::Environment::vars (   self,
  strings = True 
)
returns dictionary of all variables optionally converted to string

Definition at line 41 of file Control.py.

00042                                 :
00043         '''returns dictionary of all variables optionally converted to string'''
00044         if strings:
00045             vars = self.variables.copy()
00046             for item in vars:
00047                 vars[item] = self.var(item).value(True)
00048             return vars
00049         else:
00050             return self.variables
00051 

def EnvConfig::Control::Environment::writeToFile (   self,
  fileName,
  shell = 'sh' 
)
Creates an output file with a specified name to be used for setting variables by sourcing this file

Definition at line 194 of file Control.py.

00195                                                :
00196         '''Creates an output file with a specified name to be used for setting variables by sourcing this file'''
00197         f = open(fileName, 'w')
00198         if shell == 'sh':
00199             f.write('#!/bin/bash\n')
00200             for variable in self.variables:
00201                 if not self[variable].local:
00202                     f.write('export ' +variable+'='+self[variable].value(True, self.sysSeparator)+'\n')
00203         elif shell == 'csh':
00204             f.write('#!/bin/csh\n')
00205             for variable in self.variables:
00206                 if not self[variable].local:
00207                     f.write('setenv ' +variable+' '+self[variable].value(True, self.sysSeparator)+'\n')
00208         else:
00209             f.write('')
00210             f.write('REM This is an enviroment settings file generated on '+strftime("%a, %d %b %Y %H:%M:%S\n", gmtime()))
00211             for variable in self.variables:
00212                 if not self[variable].local:
00213                     f.write('set '+variable+'='+self[variable].value(True, self.sysSeparator)+'\n')
00214 
00215         f.close()
00216 

def EnvConfig::Control::Environment::writeToXMLFile (   self,
  fileName 
)
Writes the current state of environment to a XML file.

NOTE: There is no trace of actions taken, variables are written with a set action only.

Definition at line 217 of file Control.py.

00218                                       :
00219         '''Writes the current state of environment to a XML file.
00220 
00221         NOTE: There is no trace of actions taken, variables are written with a set action only.
00222         '''
00223         writer = xmlModule.XMLFile()
00224         for var in self.variables.keys():
00225             writer.writeVar(var, 'set', self.variables[var].value(True, self.separator))
00226         writer.writeToFile(fileName)
00227 


Member Data Documentation

Definition at line 22 of file Control.py.

Definition at line 22 of file Control.py.

Definition at line 22 of file Control.py.

Definition at line 22 of file Control.py.

Definition at line 22 of file Control.py.

Definition at line 22 of file Control.py.

Definition at line 22 of file Control.py.

Definition at line 22 of file Control.py.


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated at Thu Jun 28 2012 12:30:18 for Gaudi Framework, version v23r3 by Doxygen version 1.7.2 written by Dimitri van Heesch, © 1997-2004