2 Extract information about the Firecrest run
4 Firecrest - class holding the properties we found
5 firecrest - Firecrest factory function initalized from a directory name
6 fromxml - Firecrest factory function initalized from an xml dump from
10 from datetime import date
15 from htsworkflow.pipeline.runfolder import \
20 class Firecrest(object):
24 FIRECREST = 'Firecrest'
25 SOFTWARE_VERSION = 'version'
32 def __init__(self, xml=None):
36 self.date = date.today()
41 self.set_elements(xml)
44 return time.mktime(self.date.timetuple())
45 time = property(_get_time, doc='return run time as seconds since epoch')
48 print "Starting cycle:", self.start
49 print "Ending cycle:", self.stop
50 print "Firecrest version:", self.version
51 print "Run date:", self.date
52 print "user:", self.user
54 def get_elements(self):
55 attribs = {'version': str(Firecrest.XML_VERSION) }
56 root = ElementTree.Element(Firecrest.FIRECREST, attrib=attribs)
57 version = ElementTree.SubElement(root, Firecrest.SOFTWARE_VERSION)
58 version.text = self.version
59 start_cycle = ElementTree.SubElement(root, Firecrest.START)
60 start_cycle.text = str(self.start)
61 stop_cycle = ElementTree.SubElement(root, Firecrest.STOP)
62 stop_cycle.text = str(self.stop)
63 run_date = ElementTree.SubElement(root, Firecrest.DATE)
64 run_date.text = str(self.time)
65 user = ElementTree.SubElement(root, Firecrest.USER)
67 matrix = ElementTree.SubElement(root, Firecrest.MATRIX)
68 matrix.text = self.matrix
71 def set_elements(self, tree):
72 if tree.tag != Firecrest.FIRECREST:
73 raise ValueError('Expected "Firecrest" SubElements')
74 xml_version = int(tree.attrib.get('version', 0))
75 if xml_version > Firecrest.XML_VERSION:
76 logging.warn('Firecrest XML tree is a higher version than this class')
77 for element in list(tree):
78 if element.tag == Firecrest.SOFTWARE_VERSION:
79 self.version = element.text
80 elif element.tag == Firecrest.START:
81 self.start = int(element.text)
82 elif element.tag == Firecrest.STOP:
83 self.stop = int(element.text)
84 elif element.tag == Firecrest.DATE:
85 self.date = date.fromtimestamp(float(element.text))
86 elif element.tag == Firecrest.USER:
87 self.user = element.text
88 elif element.tag == Firecrest.MATRIX:
89 self.matrix = element.text
91 raise ValueError("Unrecognized tag: %s" % (element.tag,))
93 def firecrest(pathname):
95 Examine the directory at pathname and initalize a Firecrest object
99 # parse firecrest directory name
100 path, name = os.path.split(pathname)
101 groups = name.split('_')
102 # grab the start/stop cycle information
103 cycle = re.match("C([0-9]+)-([0-9]+)", groups[0])
104 f.start = int(cycle.group(1))
105 f.stop = int(cycle.group(2))
107 version = re.search(VERSION_RE, groups[1])
108 f.version = (version.group(1))
110 t = time.strptime(groups[2], EUROPEAN_STRPTIME)
111 f.date = date(*t[0:3])
115 # should I parse this deeper than just stashing the
116 # contents of the matrix file?
117 matrix_pathname = os.path.join(pathname, 'Matrix', 's_matrix.txt')
118 f.matrix = open(matrix_pathname, 'r').read()
123 Initialize a Firecrest object from an element tree node