Gaudi Framework, version v23r4

Home   Generated: Mon Sep 17 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 unset
def remove
def remove_regexp
def searchFile
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
 actions
 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.actions = {}
00034         self.actions['append'] = lambda n, v, _: self.append(n, v)
00035         self.actions['prepend'] = lambda n, v, _: self.prepend(n, v)
00036         self.actions['set'] = lambda n, v, _: self.set(n, v)
00037         self.actions['unset'] = lambda n, v, _: self.unset(n, v)
00038         self.actions['remove'] = lambda n, v, _: self.remove(n, v)
00039         self.actions['remove-regexp'] = lambda n, v, _: self.remove_regexp(n, v)
00040         self.actions['declare'] = self.declare
00041 
00042         self.variables = {}
00043         self.loadFromSystem = loadFromSystem
00044         self.asWriter = useAsWriter
00045         if useAsWriter:
00046             self.writer = xmlModule.XMLFile()
00047             self.startXMLinput()
00048 


Member Function Documentation

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

Definition at line 268 of file Control.py.

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

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

Definition at line 261 of file Control.py.

00262                               :
00263         del self.variables[key]

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

Definition at line 252 of file Control.py.

00253                               :
00254         return self.variables[key]

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

Definition at line 264 of file Control.py.

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

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

Definition at line 271 of file Control.py.

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

Definition at line 255 of file Control.py.

00256                                      :
00257         if value in self.variables.keys():
00258             self.report.addWarn('Addition canceled because of duplicate entry. Var: "' + self.varName + '" value: "' + value + '".')
00259         else:
00260             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 236 of file Control.py.

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

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

Definition at line 245 of file Control.py.

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

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

Definition at line 74 of file Control.py.

00075                                  :
00076         '''Appends to an existing variable.'''
00077         if self.asWriter:
00078             self._writeVarToXML(name, 'append', value)
00079         else:
00080             if name in self.variables.keys():
00081                 self.variables[name].append(value, self.separator, self.variables)
00082             else:
00083                 self.declare(name, 'list', False)
00084                 self.append(name, value)
00085 

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 97 of file Control.py.

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

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

Definition at line 190 of file Control.py.

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

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

Definition at line 229 of file Control.py.

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

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

Definition at line 174 of file Control.py.

00175                                                                :
00176         '''Loads XML file for input variables.'''
00177         XMLfile = xmlModule.XMLFile()
00178         variables = XMLfile.variable(fileName, namespace = namespace)
00179         for i, (action, args) in enumerate(variables):
00180             if action not in self.actions:
00181                 self.report.addError('Node {0}: No action taken with var "{1}". Probably wrong action argument: "{2}".'.format(i, args[0], action))
00182             else:
00183                 self.actions[action](*args)
00184 

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

Definition at line 86 of file Control.py.

00087                                   :
00088         '''Prepends to an existing variable, or create a new one.'''
00089         if self.asWriter:
00090             self._writeVarToXML(name, 'prepend', value)
00091         else:
00092             if name in self.variables.keys():
00093                 self.variables[name].prepend(value, self.separator, self.variables)
00094             else:
00095                 self.declare(name, 'list', False)
00096                 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)
00167 

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

Definition at line 69 of file Control.py.

00070                                                    :
00071         '''Searches in a variable for a value.'''
00072         return self.variables[varName].search(expr, regExp)
00073 

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

Definition at line 168 of file Control.py.

00169                                        :
00170         '''Searches for appearance of variable in a file.'''
00171         XMLFile = xmlModule.XMLFile()
00172         variable = XMLFile.variable(file, name=varName)
00173         return variable

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

Definition at line 129 of file Control.py.

00130                               :
00131         '''Sets a single variable - overrides any previous value!'''
00132         name = str(name)
00133         if self.asWriter:
00134             self._writeVarToXML(name, 'set', value)
00135         else:
00136             if name in self.variables:
00137                 self.variables[name].set(value, self.separator, self.variables)
00138             else:
00139                 self.declare(name, 'list', False)
00140                 self.set(name, value)

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

Definition at line 185 of file Control.py.

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

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 60 of file Control.py.

00061                        :
00062         '''Gets a single variable. If not available then tries to load from system.'''
00063         if name in self.variables.keys():
00064             return self.variables[name]
00065         else:
00066             self._loadFromSystem(name, '', 'append')
00067             return self.variables[name]
00068 

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

Definition at line 49 of file Control.py.

00050                                 :
00051         '''returns dictionary of all variables optionally converted to string'''
00052         if strings:
00053             vars = self.variables.copy()
00054             for item in vars:
00055                 vars[item] = self.var(item).value(True)
00056             return vars
00057         else:
00058             return self.variables
00059 

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 195 of file Control.py.

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

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 218 of file Control.py.

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


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 Mon Sep 17 2012 13:49:53 for Gaudi Framework, version v23r4 by Doxygen version 1.7.2 written by Dimitri van Heesch, © 1997-2004