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 import \
25 LOGGER = logging.getLogger(__name__)
27 class Firecrest(object):
28 """Gather information about older firecrest runs
33 FIRECREST = 'Firecrest'
34 SOFTWARE_VERSION = 'version'
41 def __init__(self, xml=None):
42 """Initialize a Firecrest object
44 consider using factory :function:firecrest
46 :param xml: xml serialzation element to initialze from [optional]
51 self.date = date.today()
56 self.set_elements(xml)
58 def _get_software(self):
60 software = property(_get_software)
63 return time.mktime(self.date.timetuple())
64 time = property(_get_time, doc='return run time as seconds since epoch')
67 """Report debugginf information
69 print "Starting cycle:", self.start
70 print "Ending cycle:", self.stop
71 print "Firecrest version:", self.version
72 print "Run date:", self.date
73 print "user:", self.user
75 def get_elements(self):
76 """Return XML serialization structure.
78 attribs = {'version': str(Firecrest.XML_VERSION) }
79 root = ElementTree.Element(Firecrest.FIRECREST, attrib=attribs)
80 version = ElementTree.SubElement(root, Firecrest.SOFTWARE_VERSION)
81 version.text = self.version
82 start_cycle = ElementTree.SubElement(root, Firecrest.START)
83 start_cycle.text = str(self.start)
84 stop_cycle = ElementTree.SubElement(root, Firecrest.STOP)
85 stop_cycle.text = str(self.stop)
86 run_date = ElementTree.SubElement(root, Firecrest.DATE)
87 run_date.text = str(self.time)
88 user = ElementTree.SubElement(root, Firecrest.USER)
90 if self.matrix is not None:
91 matrix = ElementTree.SubElement(root, Firecrest.MATRIX)
92 matrix.text = self.matrix
95 def set_elements(self, tree):
96 if tree.tag != Firecrest.FIRECREST:
97 raise ValueError('Expected "Firecrest" SubElements')
98 xml_version = int(tree.attrib.get('version', 0))
99 if xml_version > Firecrest.XML_VERSION:
100 LOGGER.warn('Firecrest XML tree is a higher version than this class')
101 for element in list(tree):
102 if element.tag == Firecrest.SOFTWARE_VERSION:
103 self.version = element.text
104 elif element.tag == Firecrest.START:
105 self.start = int(element.text)
106 elif element.tag == Firecrest.STOP:
107 self.stop = int(element.text)
108 elif element.tag == Firecrest.DATE:
109 self.date = date.fromtimestamp(float(element.text))
110 elif element.tag == Firecrest.USER:
111 self.user = element.text
112 elif element.tag == Firecrest.MATRIX:
113 self.matrix = element.text
115 raise ValueError("Unrecognized tag: %s" % (element.tag,))
117 def firecrest(pathname):
119 Examine the directory at pathname and initalize a Firecrest object
122 f.pathname = pathname
124 # parse firecrest directory name
125 path, name = os.path.split(pathname)
126 groups = name.split('_')
127 # grab the start/stop cycle information
128 cycle = re.match("C([0-9]+)-([0-9]+)", groups[0])
129 f.start = int(cycle.group(1))
130 f.stop = int(cycle.group(2))
132 version = re.search(VERSION_RE, groups[1])
133 f.version = (version.group(1))
135 t = time.strptime(groups[2], EUROPEAN_STRPTIME)
136 f.date = date(*t[0:3])
140 bustard_pattern = os.path.join(pathname, 'Bustard*')
141 # should I parse this deeper than just stashing the
142 # contents of the matrix file?
143 matrix_pathname = os.path.join(pathname, 'Matrix', 's_matrix.txt')
144 if os.path.exists(matrix_pathname):
145 # this is for firecrest < 1.3.2
146 f.matrix = open(matrix_pathname, 'r').read()
147 elif glob(bustard_pattern) > 0:
149 # there are runs here. Bustard should save the matrix.
157 Initialize a Firecrest object from an element tree node