Gaudi Framework, version v23r5

Home   Generated: Wed Nov 28 2012
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Public Member Functions | Public Attributes | Private Member Functions | List of all members
EnvConfig.xmlModule.XMLOperations Class Reference

Public Member Functions

def __init__
 
def errors
 
def warnings
 
def check
 
def merge
 

Public Attributes

 posActions
 
 separator
 
 report
 
 varNames
 
 realVariables
 
 variables
 
 file
 
 output
 

Private Member Functions

def _processVars
 
def _checkVariable
 
def _loadVariables
 

Detailed Description

This class is for checking and merging XML files.

Variables are stored in a double dictionary with keys of names and then actions.

Definition at line 213 of file xmlModule.py.

Constructor & Destructor Documentation

def EnvConfig.xmlModule.XMLOperations.__init__ (   self,
  separator = ':',
  reportLevel = 0,
  reportOutput = None 
)

Definition at line 218 of file xmlModule.py.

219  def __init__(self, separator=':', reportLevel=0, reportOutput=None):
220  self.posActions = ['append','prepend','set','unset', 'remove', 'remove-regexp', 'declare']
221  self.separator = separator
222  self.report = Report(reportLevel, reportOutput=reportOutput)
223  self.varNames = []
224  self.realVariables = {}
225  self.variables = {}
226  self.file = None
227  self.output = None

Member Function Documentation

def EnvConfig.xmlModule.XMLOperations._checkVariable (   self,
  varName,
  action,
  local,
  value,
  nodeNum 
)
private
Tries to add to variables dict, checks for errors during process

Definition at line 298 of file xmlModule.py.

299  def _checkVariable(self, varName, action, local, value, nodeNum):# pylint: disable=W0613
300  '''Tries to add to variables dict, checks for errors during process'''
301 
302  if varName not in self.variables:
303  self.variables[varName] = []
304  self.variables[varName].append(action)
305 
306  # If variable is in dict, check if this is not an unset command
307  elif action == 'unset':
308  if 'unset' in self.variables[varName]:
309  self.report.addWarn('Multiple "unset" actions found for variable: "'+varName+'".', varName, 'multiple unset','', 'checkVariable')
310  if not('unset' in self.variables[varName] and len(self.variables[varName]) == 1):
311  self.report.addError('Node '+str(nodeNum)+': "unset" action found for variable "'+varName+'" after previous command(s). Any previous commands are overridden.', varName, 'unset overwrite')
312 
313  # or set command
314  elif action == 'set':
315  if len(self.variables[varName]) == 1 and 'unset' in self.variables[varName]:
316  self.report.addWarn('Node '+str(nodeNum)+': "set" action found for variable "'+varName+'" after unset. Can be merged to one set only.')
317  else:
318  self.report.addError('Node '+str(nodeNum)+': "set" action found for variable "'+varName+'" after previous command(s). Any previous commands are overridden.', varName, 'set overwrite')
319  if 'set' in self.variables[varName]:
320  self.report.addWarn('Multiple "set" actions found for variable: "'+varName+'".', varName, 'multiple set','', 'checkVariable')
321 
322  if action not in self.variables[varName]:
323  self.variables[varName].append(action)
324 
325  try:
326  if action == 'remove-regexp':
327  action = 'remove_regexp'
328  eval('(self.realVariables[varName]).'+action+'(value)')
329  except Variable.EnvError as e:
330  if e.code == 'undefined':
331  self.report.addWarn('Referenced variable "' +e.val+ '" is not defined.')
332  elif e.code == 'ref2var':
333  self.report.addError('Reference to list from the middle of string.')
334  elif e.code == 'redeclaration':
335  self.report.addError('Redeclaration of variable "'+e.val+'".')
336  else:
337  self.report.addError('Unknown environment error occured.')
338 
def EnvConfig.xmlModule.XMLOperations._loadVariables (   self,
  fileName 
)
private
loads XML file for input variables

Definition at line 339 of file xmlModule.py.

340  def _loadVariables(self, fileName):
341  '''loads XML file for input variables'''
342  XMLfile = XMLFile()
343  variables = XMLfile.variable(fileName)
344  for i, (action, (arg1, arg2, arg3)) in enumerate(variables):
345  undeclared = False
346  if arg1 == '':
347  raise RuntimeError('Empty variable or local name is not allowed.')
348 
349  if arg1 not in self.realVariables.keys():
350  if action != 'declare':
351  self.report.addInfo('Node '+str(i)+': Variable '+arg1+' is used before declaration. Treated as an unlocal list furthermore.')
352  undeclared = True
353  self.realVariables[arg1] = Variable.List(arg1, False, report=self.report)
354  else:
355  self.varNames.append(arg1)
356  if arg2 == 'list':
357  self.realVariables[arg1] = Variable.List(arg1, arg3, report=self.report)
358  else:
359  self.realVariables[arg1] = Variable.Scalar(arg1, arg3, report=self.report)
360  if not undeclared:
361  continue
362 
363  if action not in self.posActions:
364  self.report.addError('Node '+str(i)+': Action "'+action+'" which is not implemented found. Variable "'+arg1+'".', arg1, action, arg2)
365  continue
366 
367  else:
368  if action == 'declare':
369  self.report.addError('Node '+str(i)+': Variable '+arg1+' is redeclared.')
370  else:
371  self._checkVariable(arg1, action, self.realVariables[arg1].local, str(arg2), i)
def EnvConfig.xmlModule.XMLOperations._processVars (   self,
  variables 
)
private

Definition at line 282 of file xmlModule.py.

283  def _processVars(self, variables):
284  for action, (arg1, arg2, arg3) in variables:
285  if action == 'declare':
286  if arg1 in self.variables.keys():
287  if arg2.lower() != self.variables[arg1][0]:
288  raise Variable.EnvError(arg1, 'redeclaration')
289  else:
290  if arg3.lower() != self.variables[arg1][1]:
291  raise Variable.EnvError(arg1, 'redeclaration')
292  else:
293  self.file.writeVar(arg1, 'declare', '', arg2, arg3)
294  self.variables[arg1] = [arg2.lower(), arg3.lower()]
295  else:
296  self.file.writeVar(arg1, action, arg2)
297 
def EnvConfig.xmlModule.XMLOperations.check (   self,
  xmlFile 
)
Runs a check through file

First check is made on wrong action parameter.
All valid actions are checked after and duplicated variables as well.

Definition at line 234 of file xmlModule.py.

235  def check(self, xmlFile):
236  '''Runs a check through file
237 
238  First check is made on wrong action parameter.
239  All valid actions are checked after and duplicated variables as well.
240  '''
241  #self.local = Variable.Local()
242  # reset state
243  self.varNames = []
244  self.realVariables = {}
245  self.variables = {}
246 
247  # load variables and resolve references to locals and then variables
248  self._loadVariables(xmlFile)
249 
250  # report
251  if (self.warnings() > 0 or self.errors() > 0):
252  self.report.addInfo('Encountered '+ (str)(self.warnings()) +' warnings and ' + (str)(self.errors()) + ' errors.')
253  return [self.warnings(), self.errors()]
254  else:
255  return True
256 
257  self.report.closeFile()
258 
def EnvConfig.xmlModule.XMLOperations.errors (   self)

Definition at line 228 of file xmlModule.py.

229  def errors(self):
230  return self.report.numErrors()
def EnvConfig.xmlModule.XMLOperations.merge (   self,
  xmlDoc1,
  xmlDoc2,
  outputFile = '',
  reportCheck = False 
)
Merges two files together. Files are checked first during variables loading process.

Second file is processed first, then the first file and after that they are merged together.

Definition at line 259 of file xmlModule.py.

260  def merge(self, xmlDoc1, xmlDoc2, outputFile = '', reportCheck = False):
261  '''Merges two files together. Files are checked first during variables loading process.
262 
263  Second file is processed first, then the first file and after that they are merged together.
264  '''
265  self.output = outputFile
266  self.file = XMLFile()
267  self.variables = {}
268 
269  variables = self.file.variable(xmlDoc1)
270  self._processVars(variables)
271  variables = self.file.variable(xmlDoc2)
272  self._processVars(variables)
273 
274  if not reportCheck:
275  self.report.level = 5
276 
277  self.file.writeToFile(outputFile)
278 
279  self.report.addInfo('Files merged. Running check on the result.')
280  self.check(self.output)
281  self.report.closeFile()
def EnvConfig.xmlModule.XMLOperations.warnings (   self)

Definition at line 231 of file xmlModule.py.

232  def warnings(self):
233  return self.report.numWarnings()

Member Data Documentation

EnvConfig.xmlModule.XMLOperations.file

Definition at line 225 of file xmlModule.py.

EnvConfig.xmlModule.XMLOperations.output

Definition at line 226 of file xmlModule.py.

EnvConfig.xmlModule.XMLOperations.posActions

Definition at line 219 of file xmlModule.py.

EnvConfig.xmlModule.XMLOperations.realVariables

Definition at line 223 of file xmlModule.py.

EnvConfig.xmlModule.XMLOperations.report

Definition at line 221 of file xmlModule.py.

EnvConfig.xmlModule.XMLOperations.separator

Definition at line 220 of file xmlModule.py.

EnvConfig.xmlModule.XMLOperations.variables

Definition at line 224 of file xmlModule.py.

EnvConfig.xmlModule.XMLOperations.varNames

Definition at line 222 of file xmlModule.py.


The documentation for this class was generated from the following file:
Generated at Wed Nov 28 2012 12:17:35 for Gaudi Framework, version v23r5 by Doxygen version 1.8.2 written by Dimitri van Heesch, © 1997-2004