From d8a9162e013960de7c5c1ea010f559b32f3b94f5 Mon Sep 17 00:00:00 2001 From: Brandon King Date: Thu, 17 Jan 2008 23:40:58 +0000 Subject: [PATCH] Added a new get_cycles(recipe_xml_filepath) function in gaworkflow.pipeline.recipe_parser. --- gaworkflow/automation/spoolwatcher.py | 39 ++-------------------- gaworkflow/pipeline/recipe_parser.py | 48 +++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 37 deletions(-) create mode 100644 gaworkflow/pipeline/recipe_parser.py diff --git a/gaworkflow/automation/spoolwatcher.py b/gaworkflow/automation/spoolwatcher.py index d50eefb..abd9709 100644 --- a/gaworkflow/automation/spoolwatcher.py +++ b/gaworkflow/automation/spoolwatcher.py @@ -4,9 +4,9 @@ import os import re import sys import time -import glob - +#import glob +#from gaworkflow.pipeline.recipe_parser import get_cycles # this uses pyinotify import pyinotify @@ -15,41 +15,6 @@ from pyinotify import EventsCodes from benderjab import rpc -s_cycles = re.compile('No. Cycles: (?P[0-9]+)') - -def get_cycles(run_dir="."): - """ - Find the number of cycles from the Recipe*.xml file found in run_dir. - """ - - file_path_list = glob.glob(os.path.join(run_dir, "Recipe*.xml")) - - # Error handling - if len(file_path_list) == 0: - msg = "Recipe xml file not found." - raise IOError, msg - - elif len(file_path_list) > 1: - msg = "%s Recipe files found, expected 1." % (len(file_path_list)) - raise ValueError, msg - - f = open(file_path_list[0], 'r') - - #Find the line of the file with the cycle number in it. - for line in f: - mo = s_cycles.search(line) - if mo: - break - - f.close() - - # Process the line with the cycle number in it. - cycle_num = int(mo.group('cycles')) - - return cycle_num - - - class WatcherEvents(object): # two events need to be tracked # one to send startCopy diff --git a/gaworkflow/pipeline/recipe_parser.py b/gaworkflow/pipeline/recipe_parser.py new file mode 100644 index 0000000..7f5ced6 --- /dev/null +++ b/gaworkflow/pipeline/recipe_parser.py @@ -0,0 +1,48 @@ +from xml import sax + + +def get_cycles(recipe_xml_filepath): + """ + returns the number of cycles found in Recipe*.xml + """ + handler = CycleXmlHandler() + sax.parse(recipe_xml_filepath, handler) + return handler.cycle_count + + + +class CycleXmlHandler(sax.ContentHandler): + + def __init__(self): + self.cycle_count = 0 + self.in_protocol = False + sax.ContentHandler.__init__(self) + + + def startDocument(self): + self.cycle_count = 0 + self.in_protocol = False + + + def startElement(self, name, attrs): + + #Only count Incorporations as cycles if within + # the protocol section of the xml document. + if name == "Incorporation" and self.in_protocol: + #print 'Found a cycle!' + self.cycle_count += 1 + return + + elif name == 'Protocol': + #print 'In protocol' + self.in_protocol = True + return + + #print 'Skipping: %s' % (name) + + + def endElement(self, name): + + if name == 'Protocol': + #print 'End protocol' + self.in_protocol = False -- 2.30.2