#!/usr/bin/env python # Copyright 2010, Chaz Littlejohn # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ######################################################################## import L10n _ = L10n.get_translation() # This code is based heavily on stars-support-hh-split.py by Mika Boström import os import sys import re import codecs import Options import Configuration from Exceptions import * from cStringIO import StringIO import time (options, argv) = Options.fpdb_options() __ARCHIVE_PRE_HEADER_REGEX='^Hand #(\d+)\s*$|\*{20}\s#\s\d+\s\*+\s+' re_SplitArchive = re.compile(__ARCHIVE_PRE_HEADER_REGEX) codepage = ["utf-16", "utf-8", "cp1252"] class SplitHandHistory: def __init__(self, config, in_path = '-', out_path = None, hands = 100, filter = "PokerStarsToFpdb", archive = False, workerid = 0): self.config = config self.in_path = in_path self.out_path = out_path if not self.out_path: self.out_path = os.path.dirname(self.in_path) self.hands = hands self.archive = archive self.re_SplitHands = None self.line_delimiter = None self.line_addendum = None self.filedone = False self.timestamp = str(time.time()) self.workerid = '%02d' % workerid #Acquire re_SplitHands for this hh self.filter_name = filter.replace("ToFpdb", "") mod = __import__(filter) obj = getattr(mod, self.filter_name, None) self.re_SplitHands = obj.re_SplitHands #Determine line delimiter type if any if self.re_SplitHands.match('\n\n\n'): self.line_delimiter = '\n\n\n' if self.re_SplitHands.match('\n\n'): self.line_delimiter = '\n\n' #Add new line addendum for sites which match SplitHand to next line as well if self.filter_name == 'OnGame': self.line_addendum = '*' if self.filter_name == 'Carbon': self.line_addendum = '= self.hands: break outfile.close() def new_file(self, fileno=-1): if fileno < 1: print _('Nope, will not work (fileno=%d)' % fileno) sys.exit(2) basename = os.path.splitext(os.path.basename(self.in_path))[0] name = os.path.join(self.out_path, self.filter_name+'-'+basename+'_'+self.workerid+'_'+self.timestamp+'_%06d.txt' % fileno) print '-> %s' % name newfile = file(name, 'w') os.chmod(name, 0775) return newfile #Archive Hand Splitter def do_hands_per_file(self, infile, num=-1): done = False n = 0 outfile = self.new_file(num) while n < self.hands: try: infile = self.next_hand(infile) infile = self.process_hand(infile, outfile) except FpdbEndOfFile: done = True break except UnicodeEncodeError: print _('Absurd character done messed you up') sys.exit(2) except: print _('Unexpected error processing file') sys.exit(2) n += 1 outfile.close() if not done: return infile else: return None #Non-Archive Hand Splitter def paragraphs(self, file, separator=None, addendum=None): if not callable(separator) and self.line_delimiter: def separator(line): return line == '\n' else: def separator(line): return self.re_SplitHands.search(line) file_str = StringIO() print file_str.getvalue() for line in file: if separator(line+addendum): if file_str.getvalue(): if not self.line_delimiter: file_str.write(line) yield file_str.getvalue() file_str = None file_str = StringIO() else: file_str.write(line) if file_str.getvalue(): yield file_str.getvalue() self.filedone = True # Finds pre-hand header (Hand #) def next_hand(self, infile): m = None while not m: l = infile.readline() #print l, len(l) # Catch EOF if len(l) == 0: raise FpdbEndOfFile(_("End of file reached")) m = re_SplitArchive.search(l) # There is an empty line after pre-hand header and actual HH entry l = infile.readline() return infile # Each individual hand is written separately def process_hand(self, infile=None, outfile=None): l = infile.readline() l = l.replace('\r\n', '\n') outfile.write(l) l = infile.readline().encode(self.kodec) while len(l) < 3: l = infile.readline() while len(l) > 2: l = l.replace('\r\n', '\n') outfile.write(l) l = infile.readline().encode(self.kodec) outfile.write(self.line_delimiter) return infile def __listof(self, x): if isinstance(x, list) or isinstance(x, tuple): return x else: return [x] def main(argv=None): if argv is None: argv = sys.argv[1:] if not options.filename: options.filename = sys.argv[1] if not options.config: options.config = sys.argv[2] if sys.argv[3] == "True": options.archive = True if options.filename: SplitHH = SplitHandHistory(options.config, options.filename, options.outpath, options.hands, options.hhc, options.archive, options.workerid) if __name__ == '__main__': sys.exit(main())