Rename trunk from gaworkflow to htsworkflow (and update all of the imports)
[htsworkflow.git] / htsworkflow / pipeline / recipe_parser.py
1 from xml import sax
2
3
4 def get_cycles(recipe_xml_filepath):
5   """
6   returns the number of cycles found in Recipe*.xml
7   """
8   handler = CycleXmlHandler()
9   sax.parse(recipe_xml_filepath, handler)
10   return handler.cycle_count
11
12
13
14 class CycleXmlHandler(sax.ContentHandler):
15
16   def __init__(self):
17     self.cycle_count = 0
18     self.in_protocol = False
19     sax.ContentHandler.__init__(self)
20
21
22   def startDocument(self):
23     self.cycle_count = 0
24     self.in_protocol = False
25
26
27   def startElement(self, name, attrs):
28
29     #Only count Incorporations as cycles if within
30     # the protocol section of the xml document.
31     if name == "Incorporation" and self.in_protocol:
32       #print 'Found a cycle!'
33       self.cycle_count += 1
34       return
35     
36     elif name == 'Protocol':
37       #print 'In protocol'
38       self.in_protocol = True
39       return
40
41     #print 'Skipping: %s' % (name)
42     
43
44   def endElement(self, name):
45     
46     if name == 'Protocol':
47       #print 'End protocol'
48       self.in_protocol = False