2 Extract information about the Firecrest run
5 class holding the properties we found
7 Firecrest factory function initalized from a directory name
9 Firecrest factory function initalized from an xml dump from
13 from datetime import date
18 from htsworkflow.pipelines.runfolder import \
23 __docformat__ = "restructuredtext en"
25 class Firecrest(object):
29 FIRECREST = 'Firecrest'
30 SOFTWARE_VERSION = 'version'
37 def __init__(self, xml=None):
41 self.date = date.today()
46 self.set_elements(xml)
49 return time.mktime(self.date.timetuple())
50 time = property(_get_time, doc='return run time as seconds since epoch')
53 print "Starting cycle:", self.start
54 print "Ending cycle:", self.stop
55 print "Firecrest version:", self.version
56 print "Run date:", self.date
57 print "user:", self.user
59 def get_elements(self):
60 attribs = {'version': str(Firecrest.XML_VERSION) }
61 root = ElementTree.Element(Firecrest.FIRECREST, attrib=attribs)
62 version = ElementTree.SubElement(root, Firecrest.SOFTWARE_VERSION)
63 version.text = self.version
64 start_cycle = ElementTree.SubElement(root, Firecrest.START)
65 start_cycle.text = str(self.start)
66 stop_cycle = ElementTree.SubElement(root, Firecrest.STOP)
67 stop_cycle.text = str(self.stop)
68 run_date = ElementTree.SubElement(root, Firecrest.DATE)
69 run_date.text = str(self.time)
70 user = ElementTree.SubElement(root, Firecrest.USER)
72 matrix = ElementTree.SubElement(root, Firecrest.MATRIX)
73 matrix.text = self.matrix
76 def set_elements(self, tree):
77 if tree.tag != Firecrest.FIRECREST:
78 raise ValueError('Expected "Firecrest" SubElements')
79 xml_version = int(tree.attrib.get('version', 0))
80 if xml_version > Firecrest.XML_VERSION:
81 logging.warn('Firecrest XML tree is a higher version than this class')
82 for element in list(tree):
83 if element.tag == Firecrest.SOFTWARE_VERSION:
84 self.version = element.text
85 elif element.tag == Firecrest.START:
86 self.start = int(element.text)
87 elif element.tag == Firecrest.STOP:
88 self.stop = int(element.text)
89 elif element.tag == Firecrest.DATE:
90 self.date = date.fromtimestamp(float(element.text))
91 elif element.tag == Firecrest.USER:
92 self.user = element.text
93 elif element.tag == Firecrest.MATRIX:
94 self.matrix = element.text
96 raise ValueError("Unrecognized tag: %s" % (element.tag,))
98 def firecrest(pathname):
100 Examine the directory at pathname and initalize a Firecrest object
104 # parse firecrest directory name
105 path, name = os.path.split(pathname)
106 groups = name.split('_')
107 # grab the start/stop cycle information
108 cycle = re.match("C([0-9]+)-([0-9]+)", groups[0])
109 f.start = int(cycle.group(1))
110 f.stop = int(cycle.group(2))
112 version = re.search(VERSION_RE, groups[1])
113 f.version = (version.group(1))
115 t = time.strptime(groups[2], EUROPEAN_STRPTIME)
116 f.date = date(*t[0:3])
120 # should I parse this deeper than just stashing the
121 # contents of the matrix file?
122 matrix_pathname = os.path.join(pathname, 'Matrix', 's_matrix.txt')
123 if not os.path.exists(matrix_pathname):
125 f.matrix = open(matrix_pathname, 'r').read()
130 Initialize a Firecrest object from an element tree node