1
2
3
4 """
5 A class for writting a Python IceTrayConfig object to XML
6
7 copyright (c) 2005 the icecube collaboration
8
9 @version: $Revision: 1.0 $
10 @date: $Date: $
11 @author: Juan Carlos Diaz Velez <juancarlos@icecube.wisc.edu>
12
13 @todo: Many of the same todo items on xmlparser.py
14 """
15
16 import sys,os
17 import string
18 import re
19 import types
20 import logging
21 import logger
22 from dataclasses import *
23
24 from os.path import expandvars
25 from iceprod.core import ipxml
26 from xml.sax._exceptions import SAXParseException
27
28 logger = logging.getLogger('XMLParamDB')
29
30 SupportedTypes = ['bool', 'int', 'long', 'float', 'double', 'string','OMKey']
31 VectorTypes = ['boolv', 'intv', 'longv', 'floatv', 'doublev', 'stringv','OMKeyv']
32
34
35 - def __init__(self,metaprojects,uri='http://x2100.icecube.wisc.edu/dtd/i3paramdb.dtd'):
36 """
37 Initialize and build DOM tree
38 from IceTrayConfig object
39 """
40 self.metaprojects = metaprojects
41 dom = ipxml.getDOMImplementation()
42
43 doctype = dom.createDocumentType("i3paramdb",
44 "-//W3C//DTD XML 1.0 Strict//EN", uri)
45
46 self.doc = dom.createDocument( None, "i3paramdb", doctype )
47 config = self.doc.documentElement
48 config.setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance')
49 config.setAttribute('xsi:noNamespaceSchemaLocation','config.xsd')
50
51 self.AddMetaProjects()
52
53
116
117
119 """
120 Create a value element depending on the type
121 """
122 if ptype.startswith("OMKey"):
123 pValueElement = self.doc.createElement( 'map' )
124 pEntryElement0 = self.doc.createElement( 'cvalue' )
125 pEntryElement0.appendChild(self.doc.createTextNode(str(pvalue.stringid)))
126 pEntryElement0.setAttribute('label', 'stringid' )
127 pValueElement.appendChild(pEntryElement0)
128 pEntryElement1 = self.doc.createElement( 'cvalue' )
129 pEntryElement1.appendChild(self.doc.createTextNode(str(pvalue.omid)))
130 pEntryElement1.setAttribute('label', 'omid' )
131 pValueElement.appendChild(pEntryElement1)
132 else:
133 pValueElement = self.doc.createElement( 'value' )
134 pValueElement.appendChild(self.doc.createTextNode(str(pvalue.value)))
135 if pvalue.unit:
136 pValueElement.setAttribute('unit', pvalue.unit )
137 return pValueElement
138
140 """
141 Load services from IceTrayConfig and add the to DOM tree
142 """
143
144 services = project.GetServices()
145
146 for s in services:
147
148 servElement = self.doc.createElement( 'service' )
149 servElement.setAttribute('class',s.GetClass())
150 servElement.setAttribute('id',str(s.GetId()))
151 projects = ','.join( [p.GetName() for p in s.GetProjectList()] )
152 servElement.setAttribute('projects',projects)
153 docElement.appendChild(servElement)
154
155 nameElement = self.doc.createElement( 'name' )
156 nameNode = self.doc.createTextNode( s.GetName() )
157 nameElement.appendChild(nameNode)
158 servElement.appendChild(nameElement)
159
160 paramsElement = self.doc.createElement( 'parameters' )
161 servElement.appendChild(paramsElement)
162
163 for param in s.GetParameters():
164
165 pNameElement = self.doc.createElement( 'name' )
166 pNameNode = self.doc.createTextNode( param.GetName() )
167 pNameElement.appendChild(pNameNode)
168 pTypeElement = self.doc.createElement( param.GetType() )
169 pTypeElement.setAttribute('id',str(param.GetId()))
170 pTypeElement.appendChild(pNameElement)
171 paramsElement.appendChild(pTypeElement)
172 ptype = param.GetType()
173
174 if ptype in VectorTypes:
175 for pvalue in param.GetValue():
176 pValueElement = self.mkValueElement(ptype,pvalue,self.doc)
177 pTypeElement.appendChild(pValueElement)
178 else:
179 pValueElement = self.mkValueElement(ptype,param.GetValue(),self.doc)
180 pTypeElement.appendChild(pValueElement)
181
182
183 - def AddPres(self,docElement,project):
184 """
185 Load modules from IceTrayConfig and add the to DOM tree
186 """
187
188 modules = project.GetIceProdPres()
189
190 for m in modules:
191
192 modElement = self.doc.createElement( 'iceprodpre' )
193 modElement.setAttribute('class',m.GetClass())
194 modElement.setAttribute('id',str(m.GetId()))
195 projects = ','.join( [p.GetName() for p in m.GetProjectList()] )
196 modElement.setAttribute('projects',projects)
197 docElement.appendChild(modElement)
198
199 nameElement = self.doc.createElement( 'name' )
200 nameNode = self.doc.createTextNode( m.GetName() )
201 nameElement.appendChild(nameNode)
202 modElement.appendChild(nameElement)
203
204 paramsElement = self.doc.createElement( 'parameters' )
205 modElement.appendChild(paramsElement)
206
207 for param in m.GetParameters():
208
209 pNameElement = self.doc.createElement( 'name' )
210 pNameNode = self.doc.createTextNode( param.GetName() )
211 pNameElement.appendChild(pNameNode)
212 pTypeElement = self.doc.createElement( param.GetType() )
213 pTypeElement.setAttribute('id',str(param.GetId()))
214 pTypeElement.appendChild(pNameElement)
215 paramsElement.appendChild(pTypeElement)
216 ptype = param.GetType()
217
218 if ptype in VectorTypes:
219 for pvalue in param.GetValue():
220 pValueElement = self.mkValueElement(ptype,pvalue,self.doc)
221 pTypeElement.appendChild(pValueElement)
222 else:
223 pValueElement = self.mkValueElement(ptype,param.GetValue(),self.doc)
224 pTypeElement.appendChild(pValueElement)
225
227 """
228 Load modules from IceTrayConfig and add the to DOM tree
229 """
230
231 modules = project.GetModules()
232
233 for m in modules:
234
235 modElement = self.doc.createElement( 'module' )
236 modElement.setAttribute('class',m.GetClass())
237 modElement.setAttribute('id',str(m.GetId()))
238 projects = ','.join( [p.GetName() for p in m.GetProjectList()] )
239 modElement.setAttribute('projects',projects)
240 docElement.appendChild(modElement)
241
242 nameElement = self.doc.createElement( 'name' )
243 nameNode = self.doc.createTextNode( m.GetName() )
244 nameElement.appendChild(nameNode)
245 modElement.appendChild(nameElement)
246
247 paramsElement = self.doc.createElement( 'parameters' )
248 modElement.appendChild(paramsElement)
249
250 for param in m.GetParameters():
251
252 pNameElement = self.doc.createElement( 'name' )
253 pNameNode = self.doc.createTextNode( param.GetName() )
254 pNameElement.appendChild(pNameNode)
255 pTypeElement = self.doc.createElement( param.GetType() )
256 pTypeElement.setAttribute('id',str(param.GetId()))
257 pTypeElement.appendChild(pNameElement)
258 paramsElement.appendChild(pTypeElement)
259 ptype = param.GetType()
260
261 if ptype in VectorTypes:
262 for pvalue in param.GetValue():
263 pValueElement = self.mkValueElement(ptype,pvalue,self.doc)
264 pTypeElement.appendChild(pValueElement)
265 else:
266 pValueElement = self.mkValueElement(ptype,param.GetValue(),self.doc)
267 pTypeElement.appendChild(pValueElement)
268
269
270
272 return self.doc
273
275 """
276 Write DOM tree as human readable xml to a file
277 """
278 if fname:
279 file_object = open(fname, "w")
280 ipxml.PrettyPrint(self.doc, file_object)
281 file_object.close()
282 else:
283 ipxml.PrettyPrint(self.doc)
284
286
290
354
355
357 return self.projects.values()
358
359
361 """
362 Load services from icetray configuration file
363 """
364 logger.debug('ADD SERVICES')
365
366 service = projectElement.getElementsByTagName('service')
367
368 for s in service:
369 service = Service()
370
371 sclass = s.getAttribute('class')
372 id = int(s.getAttribute('id'))
373
374
375 service.AddProject(project.GetName(),project)
376
377 sname = ipxml.Evaluate('name', s)
378 sname = sname[0].firstChild.data
379
380
381 service.SetName(sname)
382 service.SetClass(sclass)
383 service.SetId(id)
384 project.AddService(service)
385
386 parameters = s.getElementsByTagName('parameters')
387
388
389 if len(parameters) != 0:
390 for pmtr in parameters:
391 for stype in SupportedTypes + VectorTypes:
392 ps = pmtr.getElementsByTagName(stype)
393 if len(ps) != 0:
394 for p in ps:
395 sparameter = Parameter()
396
397 pname = p.getElementsByTagName('name')
398 pname = pname[0].firstChild.data
399 pvalues = p.getElementsByTagName('value')
400 pmaps = p.getElementsByTagName('map')
401
402
403 sparameter.SetType(stype)
404 sparameter.SetName(pname)
405
406 value = []
407
408 for pvalue in pvalues:
409 val = Value(pvalue.firstChild.data)
410 if pvalue.getAttribute('unit'):
411 punit = pvalue.getAttribute('unit')
412 val.unit = punit
413 if pvalue.getAttribute('format'):
414 pformat = pvalue.getAttribute('format')
415 val.format = pformat
416 value.append( val )
417
418 for pmap in pmaps:
419 pcvalues = pmap.getElementsByTagName('cvalue')
420 val = pyOMKey("0","0")
421 for pcval in pcvalues:
422 label = pcval.getAttribute('label')
423 if label == 'stringid':
424 val.stringid = pcval.firstChild.data
425 elif label == 'omid':
426 val.omid = pcval.firstChild.data
427 value.append( val )
428
429 if stype in VectorTypes: sparameter.SetValue( value )
430 else: sparameter.SetValue( value[0] )
431
432 service.AddParameter(sparameter)
433 logger.debug("%s %s %s %s" % (sname,stype,pname,sparameter.GetValue()))
434 project.AddService(service)
435
436
437
439 """
440 Load modules from icetray configuration file
441 """
442
443 logger.debug('ADD MODULES')
444
445 modules = projectElement.getElementsByTagName('module')
446
447 for m in modules:
448 module = Module()
449
450 mclass = m.getAttribute('class')
451 module.AddProject(project.GetName(),project)
452
453 mname = ipxml.Evaluate('name', m)
454 mname = mname[0].firstChild.data
455 id = int(m.getAttribute('id'))
456
457
458 module.SetName(mname)
459 module.SetClass(mclass)
460 module.SetId(id)
461 project.AddModule(module)
462
463 parameters = m.getElementsByTagName('parameters')
464
465
466 if len(parameters) != 0:
467 for pmtr in parameters:
468
469
470
471
472 for stype in SupportedTypes + VectorTypes:
473
474
475 ps = pmtr.getElementsByTagName(stype)
476
477
478 if len(ps) != 0:
479 for p in ps:
480 mparameter = Parameter()
481
482 pname = p.getElementsByTagName('name')
483 pname = pname[0].firstChild.data
484 pvalues = p.getElementsByTagName('value')
485 pmaps = p.getElementsByTagName('map')
486 id = int(p.getAttribute('id'))
487
488 mparameter.SetType(stype)
489 mparameter.SetName(pname)
490 mparameter.SetId(id)
491 value = []
492
493
494 for pvalue in pvalues:
495 val = Value(pvalue.firstChild.data)
496 if pvalue.getAttribute('unit'):
497 punit = pvalue.getAttribute('unit')
498 val.unit = punit
499 if pvalue.getAttribute('format'):
500 pformat = pvalue.getAttribute('format')
501 val.format = pformat
502 value.append( val )
503
504 for pmap in pmaps:
505 pcvalues = pmap.getElementsByTagName('cvalue')
506 val = pyOMKey("0","0")
507 for pcval in pcvalues:
508 label = pcval.getAttribute('label')
509 if label == 'stringid':
510 val.stringid = pcval.firstChild.data
511 elif label == 'omid':
512 val.omid = pcval.firstChild.data
513 value.append( val )
514
515 if stype in VectorTypes: mparameter.SetValue( value )
516 else: mparameter.SetValue( value[0] )
517
518 module.AddParameter(mparameter)
519 logger.debug("%s %s %s %s" %(mname,stype,pname,mparameter.GetValue()))
520 project.AddModule(module)
521
522
523
524 - def AddPres(self,project,projectElement):
525 """
526 Load modules from icetray configuration file
527 """
528
529 logger.debug('ADD PRES')
530 modules = projectElement.getElementsByTagName('iceprodpre')
531
532 for m in modules:
533 module = IceProdPre()
534
535 mclass = m.getAttribute('class')
536 module.AddProject(project.GetName(),project)
537
538 mname = ipxml.Evaluate('name', m)
539 mname = mname[0].firstChild.data
540 id = int(m.getAttribute('id'))
541
542
543 module.SetName(mname)
544 module.SetClass(mclass)
545 module.SetId(id)
546 project.AddPre(module)
547
548 parameters = m.getElementsByTagName('parameters')
549
550
551 if len(parameters) != 0:
552 for pmtr in parameters:
553
554
555
556
557 for stype in SupportedTypes + VectorTypes:
558
559
560 ps = pmtr.getElementsByTagName(stype)
561
562
563 if len(ps) != 0:
564 for p in ps:
565 mparameter = Parameter()
566
567 pname = p.getElementsByTagName('name')
568 pname = pname[0].firstChild.data
569 pvalues = p.getElementsByTagName('value')
570 pmaps = p.getElementsByTagName('map')
571 id = int(p.getAttribute('id'))
572
573 mparameter.SetType(stype)
574 mparameter.SetName(pname)
575 mparameter.SetId(id)
576 value = []
577
578
579 for pvalue in pvalues:
580 val = Value(pvalue.firstChild.data)
581 if pvalue.getAttribute('unit'):
582 punit = pvalue.getAttribute('unit')
583 val.unit = punit
584 if pvalue.getAttribute('format'):
585 pformat = pvalue.getAttribute('format')
586 val.format = pformat
587 value.append( val )
588
589 for pmap in pmaps:
590 pcvalues = pmap.getElementsByTagName('cvalue')
591 val = pyOMKey("0","0")
592 for pcval in pcvalues:
593 label = pcval.getAttribute('label')
594 if label == 'stringid':
595 val.stringid = pcval.firstChild.data
596 elif label == 'omid':
597 val.omid = pcval.firstChild.data
598 value.append( val )
599
600 if stype in VectorTypes: mparameter.SetValue( value )
601 else: mparameter.SetValue( value[0] )
602
603 module.AddParameter(mparameter)
604 logger.debug("%s %s %s %s" %(mname,stype,pname,mparameter.GetValue()))
605
606
607
608 - def ParseFile(self,config_file_name,validate=0):
609
610
611 doc = None
612 try:
613
614
615 xmlin = open(config_file_name, 'r')
616 logger.debug("opened %s" % config_file_name)
617
618 sys.stdout.write('loading pdb ')
619 sys.stdout.write('...')
620 doc = ipxml.Parse(xmlin, validate=validate)
621 logger.debug("doc = Sax2.FromXmlStream(xmlin, validate=%s)" % validate)
622
623 except SAXParseException, xe:
624 logger.error("Exception: " + str(xe) )
625 return
626
627
628 logger.debug("self.ParseMetaProjects(doc)")
629 self.ParseMetaProjects(doc)
630
631 xmlin.close()
632 return self.metaprojects
633
634 if __name__ == '__main__':
635
636 if len(sys.argv) != 2:
637 sys.exit()
638
639 parser = XMLParamDBParser()
640 metaprojects = parser.ParseFile(sys.argv[1],validate=1)
641
642 if metaprojects:
643 writer = XMLParamDB(metaprojects)
644 writer.write_to_file()
645