def __init__(self, TWResultPath, buildnumber, platform, logger):
        self.__twResultPath = TWResultPath
        self.__logger = logger
        # init PSF log
        self.__psfLog = PSFLog(logger)
        self.__psfLog.addOwner("chxyang", "NotifyErrorOnly")
        self.__psfLog.addOwner("chxyang", "NotifyWhenDone")
        self.__psfLog.setBuildVersion(buildnumber)
        self.__psfLog.setStatus("AUTOMATION_COMPLETE")
        if platform.lower() == "win":
            self.__psfLog.setSUT("Adobe Bridge", "4.0.0", "Windows", "en_US", "Release")
            self.__psfLog.setTestEnvironment("br-perf-win", "Windows XP 5.1 Service Pack 3", "en_US")
        else:
            self.__psfLog.setSUT("Adobe Bridge", "4.0.0", "Macintosh", "en_US", "Release")
            self.__psfLog.setTestEnvironment("br-perf-mac", "Macintosh OS 10.5.6", "en_US")

        return
class TWResultParser:
    def __init__(self, TWResultPath, buildnumber, platform, logger):
        self.__twResultPath = TWResultPath
        self.__logger = logger
        # init PSF log
        self.__psfLog = PSFLog(logger)
        self.__psfLog.addOwner("chxyang", "NotifyErrorOnly")
        self.__psfLog.addOwner("chxyang", "NotifyWhenDone")
        self.__psfLog.setBuildVersion(buildnumber)
        self.__psfLog.setStatus("AUTOMATION_COMPLETE")
        if platform.lower() == "win":
            self.__psfLog.setSUT("Adobe Bridge", "4.0.0", "Windows", "en_US", "Release")
            self.__psfLog.setTestEnvironment("br-perf-win", "Windows XP 5.1 Service Pack 3", "en_US")
        else:
            self.__psfLog.setSUT("Adobe Bridge", "4.0.0", "Macintosh", "en_US", "Release")
            self.__psfLog.setTestEnvironment("br-perf-mac", "Macintosh OS 10.5.6", "en_US")

        return

    def extractThumbnails(self, labelsFilePath):
        import csv

        resultFiles = os.listdir(self.__twResultPath)

        self.nameMap = {}
        for file in resultFiles:
            if os.path.isfile(os.path.join(self.__twResultPath, file)):
                name = file
                self.nameMap[name.replace("_", "").lower().replace(".txt", "")] = name

        labsFile = open(labelsFilePath, "rb")
        reader = csv.reader(labsFile)
        labs = reader.next()

        for l in labs:
            av = str(self.__getValue(l))
            fst = str(self.__getFirstValue(l))
            # Test case name should be "draft_model_first"
            # add result to PSF log
            self.__addResulttoPSFLog(self.__createThubnailTestName(l), int(float(av) * 1000), av != "0")
            self.__addResulttoPSFLog(self.__createThubnailTestName(l) + "_first", int(float(fst) * 1000), fst != "0")

        labsFile.close()
        return

    def extractLaunch(self):
        launchResultPath = os.path.join(self.__twResultPath, "Launch.txt")
        coldlaunch = self.__getTimeFromTWResult(launchResultPath, "Average Cold Launch Time")
        warmlaunch = self.__getTimeFromTWResult(launchResultPath, "Average Warm Launch Time")
        self.__addResulttoPSFLog(
            "Performance.Standalone Cold Launch.Standalone Cold Launch", int(coldlaunch * 1000), 0 != coldlaunch
        )
        self.__addResulttoPSFLog(
            "Performance.Standalone Warm Launch.Standalone Warm Launch", int(warmlaunch * 1000), 0 != warmlaunch
        )
        return

    def sendToPSLuna(self):
        xmlName = os.path.basename(self.__twResultPath)
        fullFilename = os.path.join(self.__twResultPath, xmlName + ".xml")
        self.__psfLog.sendToPSLuna("chxyang", fullFilename)
        return

    def WriteToXML(self):
        xmlName = os.path.basename(self.__twResultPath)
        self.__psfLog.writeResult(os.path.join(self.__twResultPath, xmlName + ".xml"))
        return

    def __getFirstValue(self, lab):
        name = "%s200first" % lab.replace("_", "").lower()
        name = name.replace(" ", "")
        if self.nameMap.has_key(name):
            fileName = self.nameMap[name]
        else:
            self.__logger.info("Did not find thumbnail result for: " + name)
            return 0
        rf = os.path.join(self.__twResultPath, fileName)
        return self.__getResult(rf)

    def __getValue(self, lab):
        name = "%s200" % lab.replace("_", "").lower()
        name = name.replace(" ", "")
        if self.nameMap.has_key(name):
            filename = self.nameMap[name.replace("_", "")]
        else:
            self.__logger.info("Did not find thumbnail result for: " + name)
            return 0
        rf = os.path.join(self.__twResultPath, filename)
        return self.__getResult(rf)

    def __getResult(self, filePath):
        time = self.__getTimeFromTWResult(filePath, "time")
        return time

    def __getTimeFromTWResult(self, filePath, id):
        time = 0
        try:
            file = open(filePath, "r")
            for line in file.readlines():
                if id in line:
                    strs = line.split("\t")
                    timestr = strs[3]
                    timestr = timestr.strip().replace("\n", "")
                    time = float(timestr)
                    break

            file.close()
        except Exception, e:
            # do nothing, just return 0
            self.__logger.error("Parse with error for:" + filePath)
            self.__logger.error(e)

        return time