def run(self):
        dateToday = datetime.date.today().strftime("%Y%m%d")
        converter = Bovada()
        handInput = ''
        processedHandsTotal = 0
        origPht = 0
        numFilesWithHands = 0
        processedFiles = 0

        txtFiles = [f for f in os.listdir(self.inputDir)
                    if (os.path.isfile(os.path.join(self.inputDir, f))
                        and f.endswith('.txt'))]

        totalFiles = len(txtFiles)

        errorsOutPath = os.path.join(self.outputDir, dateToday + '_errors.txt')

        if self.saveErrors:
            errorsOut = open(errorsOutPath, 'a')

        for fileName in txtFiles:
            fileInPath = os.path.join(self.inputDir, fileName)
            fileOutPath = os.path.join(self.outputDir, fileName)
            fileOutPath = fileOutPath.replace('.txt', '_' + dateToday + '.txt')
            converter.in_path = fileName
            origPht = processedHandsTotal
            hhStr = ''

            fileIn = codecs.open(fileInPath, 'r', 'utf-8-sig')

            for chunk in self.readFile(fileIn):
                hhStr += chunk

            fileIn.close()

            hhStr = hhStr.replace('Bodog.eu Hand #', '\n\nBodog.eu Hand #')
            hhStr = hhStr.replace('Bovada Hand #', '\n\nBovada Hand #')
            hhStr = hhStr.replace('Ignition Hand #', '\n\nIgnition Hand #')
            hhStr += '\n\n'
            lines = hhStr.splitlines(True)

            fileOut = open(fileOutPath, 'a')

            for line in lines:
                handInput += line
                if line.strip() == '':
                    if handInput.strip() != '':
                        hand = converter.processHand(handInput,
                                                     self.showKnown,
                                                     self.fastFold,
                                                     self.separateTablesByMaxSeats)
                        if hand:
                            processedHandsTotal += 1
                            handText = StringIO()
                            hand.writeHand(handText)
                            handOutput = handText.getvalue()
                            #fileOut = open(fileOutPath, 'a')
                            fileOut.write(handOutput)
                            #fileOut.close()
                        else:
                            if self.saveErrors:
                                #errorsOut = open(errorsOutPath, 'a')
                                errorsOut.write(handInput + '\n\n')
                                #errorsOut.close()
                    handInput = ''
                    handOutput = ''

            fileOut.close()

            processedFiles += 1
            self.upbtrigger.emit(processedFiles, totalFiles)

            if processedHandsTotal > origPht:
                numFilesWithHands += 1
                if self.moveFiles:
                    fileMovePath = os.path.join(self.moveDir, fileName)
                    if not os.path.exists(fileMovePath):
                        os.rename(fileInPath, fileMovePath)

        if self.saveErrors:
            errorsOut.close()

        self.rftrigger.emit(processedHandsTotal, numFilesWithHands)
예제 #2
0
    def run(self):
        dateToday = datetime.date.today().strftime("%Y%m%d")
        converter = Bovada()
        handInput = ''
        processedHandsTotal = 0
        origPht = 0
        numFilesWithHands = 0
        processedFiles = 0

        txtFiles = [
            f for f in os.listdir(self.inputDir)
            if (os.path.isfile(os.path.join(self.inputDir, f))
                and f.endswith('.txt'))
        ]

        totalFiles = len(txtFiles)

        errorsOutPath = os.path.join(self.outputDir, dateToday + '_errors.txt')

        if self.saveErrors:
            errorsOut = open(errorsOutPath, 'a')

        for fileName in txtFiles:
            fileInPath = os.path.join(self.inputDir, fileName)
            fileOutPath = os.path.join(self.outputDir, fileName)
            fileOutPath = fileOutPath.replace('.txt', '_' + dateToday + '.txt')
            converter.in_path = fileName
            origPht = processedHandsTotal
            hhStr = ''

            fileIn = codecs.open(fileInPath, 'r', 'utf-8-sig')

            for chunk in self.readFile(fileIn):
                hhStr += chunk

            fileIn.close()

            hhStr = hhStr.replace('Bodog.eu Hand #', '\n\nBodog.eu Hand #')
            hhStr = hhStr.replace('Bovada Hand #', '\n\nBovada Hand #')
            hhStr = hhStr.replace('Ignition Hand #', '\n\nIgnition Hand #')
            hhStr += '\n\n'
            lines = hhStr.splitlines(True)

            fileOut = open(fileOutPath, 'a')

            for line in lines:
                handInput += line
                if line.strip() == '':
                    if handInput.strip() != '':
                        hand = converter.processHand(
                            handInput, self.showKnown, self.fastFold,
                            self.separateTablesByMaxSeats)
                        if hand:
                            processedHandsTotal += 1
                            handText = StringIO()
                            hand.writeHand(handText)
                            handOutput = handText.getvalue()
                            #fileOut = open(fileOutPath, 'a')
                            fileOut.write(handOutput)
                            #fileOut.close()
                        else:
                            if self.saveErrors:
                                #errorsOut = open(errorsOutPath, 'a')
                                errorsOut.write(handInput + '\n\n')
                                #errorsOut.close()
                    handInput = ''
                    handOutput = ''

            fileOut.close()

            processedFiles += 1
            self.emit(QtCore.SIGNAL('updateProgressBar'), processedFiles,
                      totalFiles)

            if processedHandsTotal > origPht:
                numFilesWithHands += 1
                if self.moveFiles:
                    fileMovePath = os.path.join(self.moveDir, fileName)
                    if not os.path.exists(fileMovePath):
                        os.rename(fileInPath, fileMovePath)

        if self.saveErrors:
            errorsOut.close()

        self.emit(QtCore.SIGNAL('runFinished'), processedHandsTotal,
                  numFilesWithHands)