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
20 from htsworkflow.pipelines.runfolder import \
25 LOGGER = logging.getLogger(__name__)
27 __docformat__ = "restructuredtext en"
29 class Firecrest(object):
33 FIRECREST = 'Firecrest'
34 SOFTWARE_VERSION = 'version'
41 def __init__(self, xml=None):
45 self.date = date.today()
50 self.set_elements(xml)
52 def _get_software(self):
54 software = property(_get_software)
57 return time.mktime(self.date.timetuple())
58 time = property(_get_time, doc='return run time as seconds since epoch')
61 print "Starting cycle:", self.start
62 print "Ending cycle:", self.stop
63 print "Firecrest version:", self.version
64 print "Run date:", self.date
65 print "user:", self.user
67 def get_elements(self):
68 attribs = {'version': str(Firecrest.XML_VERSION) }
69 root = ElementTree.Element(Firecrest.FIRECREST, attrib=attribs)
70 version = ElementTree.SubElement(root, Firecrest.SOFTWARE_VERSION)
71 version.text = self.version
72 start_cycle = ElementTree.SubElement(root, Firecrest.START)
73 start_cycle.text = str(self.start)
74 stop_cycle = ElementTree.SubElement(root, Firecrest.STOP)
75 stop_cycle.text = str(self.stop)
76 run_date = ElementTree.SubElement(root, Firecrest.DATE)
77 run_date.text = str(self.time)
78 user = ElementTree.SubElement(root, Firecrest.USER)
80 if self.matrix is not None:
81 matrix = ElementTree.SubElement(root, Firecrest.MATRIX)
82 matrix.text = self.matrix
85 def set_elements(self, tree):
86 if tree.tag != Firecrest.FIRECREST:
87 raise ValueError('Expected "Firecrest" SubElements')
88 xml_version = int(tree.attrib.get('version', 0))
89 if xml_version > Firecrest.XML_VERSION:
90 LOGGER.warn('Firecrest XML tree is a higher version than this class')
91 for element in list(tree):
92 if element.tag == Firecrest.SOFTWARE_VERSION:
93 self.version = element.text
94 elif element.tag == Firecrest.START:
95 self.start = int(element.text)
96 elif element.tag == Firecrest.STOP:
97 self.stop = int(element.text)
98 elif element.tag == Firecrest.DATE:
99 self.date = date.fromtimestamp(float(element.text))
100 elif element.tag == Firecrest.USER:
101 self.user = element.text
102 elif element.tag == Firecrest.MATRIX:
103 self.matrix = element.text
105 raise ValueError("Unrecognized tag: %s" % (element.tag,))
107 def firecrest(pathname):
109 Examine the directory at pathname and initalize a Firecrest object
112 f.pathname = pathname
114 # parse firecrest directory name
115 path, name = os.path.split(pathname)
116 groups = name.split('_')
117 # grab the start/stop cycle information
118 cycle = re.match("C([0-9]+)-([0-9]+)", groups[0])
119 f.start = int(cycle.group(1))
120 f.stop = int(cycle.group(2))
122 version = re.search(VERSION_RE, groups[1])
123 f.version = (version.group(1))
125 t = time.strptime(groups[2], EUROPEAN_STRPTIME)
126 f.date = date(*t[0:3])
130 bustard_pattern = os.path.join(pathname, 'Bustard*')
131 # should I parse this deeper than just stashing the
132 # contents of the matrix file?
133 matrix_pathname = os.path.join(pathname, 'Matrix', 's_matrix.txt')
134 if os.path.exists(matrix_pathname):
135 # this is for firecrest < 1.3.2
136 f.matrix = open(matrix_pathname, 'r').read()
137 elif glob(bustard_pattern) > 0:
139 # there are runs here. Bustard should save the matrix.
147 Initialize a Firecrest object from an element tree node