''' Created on 28 Nov 2013 @author: jmht ''' import os import unittest class MolrepLogParser(object): """ Class to mine information from a """ def __init__(self,logfile): self.logfile = logfile self.score=None self.tfScore=None self.time = None self.wrfac=None self.version = None self.parse() return def parse(self): """This just drops through reading each summary and so we are left with the last one""" self.score=None self.tfScore=None self.time = None self.wrfac=None self.version = None with open(self.logfile) as fh: line=fh.readline() while line: if not self.version and line.startswith(" ### CCP4") and "version" in line: self.version=line.strip().split()[5] # Not sure what the below code is about... # if "--- Summary ---" in line: # # really scrappy - just skip 3 and take whatever comes next # fh.readline() # fh.readline() # fh.readline() # line=fh.readline() # fields = line.split() # if len(fields) != 14: # raise RuntimeError,"Error reading summary for line: {0}".format( line ) # # self.tfScore = float( fields[10] ) # self.wrfac = float( fields[11] ) # self.score= float( fields[12] ) if "Nmon RF TF theta phi chi tx ty tz TF/sg wRfac Score" in line: line=fh.readline() fields=line.strip().split() self.tfScore = float( fields[9] ) self.wrfac = float( fields[10] ) self.score= float( fields[11] ) if line.startswith( "Times: User:" ): fields = line.split() time = fields[6] m,s = time.split(":") self.time = int(m)*60 + int(s) line=fh.readline() return class Test(unittest.TestCase): @classmethod def setUpClass(cls): """ Set up paths. Need to do this with setUpClass, as otherwise the __file__ variable is updated whenever the cwd is changed in a test and the next test gets the wrong paths. """ cls.thisd = os.path.abspath( os.path.dirname( __file__ ) ) paths = cls.thisd.split( os.sep ) cls.mrbump_include = os.sep.join( paths[ : -1 ] ) cls.tests_dir=os.path.join(cls.mrbump_include,"tests") return def testParse1(self): """parse log""" logFile = os.path.join(self.tests_dir,"molrep.log") mp = MolrepLogParser( logFile ) self.assertEqual( mp.score, 0.405) self.assertEqual( mp.tfScore, 7.49) self.assertEqual( mp.time, 19) self.assertEqual( mp.wrfac, 0.587) self.assertEqual( mp.version, "11.2.08") return def testSuite(): suite = unittest.TestSuite() suite.addTest(Test('testParse1')) return suite # # Run unit tests if __name__ == "__main__": unittest.TextTestRunner(verbosity=2).run(testSuite())