* Migrated commandline options to Options.py

* Added a EOF exception
* Fixed a few error handling issues
This commit is contained in:
Chaz 2010-10-13 01:51:57 -04:00
parent f97c1da3b9
commit fa3f878c3a

View File

@ -24,17 +24,20 @@ import os
import sys import sys
import re import re
import codecs import codecs
from optparse import OptionParser import Options
import Configuration import Configuration
from Exceptions import *
from cStringIO import StringIO from cStringIO import StringIO
(options, argv) = Options.fpdb_options()
__ARCHIVE_PRE_HEADER_REGEX='^Hand #(\d+)\s*$|\*{20}\s#\s\d+\s\*+\s+' __ARCHIVE_PRE_HEADER_REGEX='^Hand #(\d+)\s*$|\*{20}\s#\s\d+\s\*+\s+'
re_SplitArchive = re.compile(__ARCHIVE_PRE_HEADER_REGEX) re_SplitArchive = re.compile(__ARCHIVE_PRE_HEADER_REGEX)
codepage = ["utf-16", "utf-8", "cp1252"] codepage = ["utf-16", "utf-8", "cp1252"]
class SplitHandHistory: class SplitHandHistory:
def __init__(self, config, in_path = '-', out_path = None, hands = 100, site = "PokerStars", archive = False): def __init__(self, config, in_path = '-', out_path = None, hands = 100, filter = "PokerStarsToFpdb", archive = False):
self.config = config self.config = config
self.in_path = in_path self.in_path = in_path
self.out_path = out_path self.out_path = out_path
@ -48,7 +51,6 @@ class SplitHandHistory:
self.filedone = False self.filedone = False
#Acquire re_SplitHands for this hh #Acquire re_SplitHands for this hh
filter = self.config.hhcs[site].converter
filter_name = filter.replace("ToFpdb", "") filter_name = filter.replace("ToFpdb", "")
mod = __import__(filter) mod = __import__(filter)
obj = getattr(mod, filter_name, None) obj = getattr(mod, filter_name, None)
@ -61,16 +63,12 @@ class SplitHandHistory:
self.line_delimiter = '\n\n\n' self.line_delimiter = '\n\n\n'
#Add new line addendum for sites which match SplitHand to next line as well #Add new line addendum for sites which match SplitHand to next line as well
if site == 'OnGame': if filter_name == 'OnGame':
self.line_addendum = '*' self.line_addendum = '*'
if site == 'Carbon': if filter_name == 'Carbon':
self.line_addendum = '<game' self.line_addendum = '<game'
#Open the gargantuan file #Open the gargantuan file
try:
infile = file(self.in_path, 'r')
except:
print 'File is sophisticated'
for kodec in self.__listof(codepage): for kodec in self.__listof(codepage):
try: try:
infile = codecs.open(self.in_path, 'r', kodec) infile = codecs.open(self.in_path, 'r', kodec)
@ -104,7 +102,7 @@ class SplitHandHistory:
def new_file(self, fileno=-1): def new_file(self, fileno=-1):
if fileno < 1: if fileno < 1:
print 'Nope, won\'t work (fileno=%d)' % fileno print 'Nope, will not work (fileno=%d)' % fileno
sys.exit(2) sys.exit(2)
basename = os.path.splitext(os.path.basename(self.in_path))[0] basename = os.path.splitext(os.path.basename(self.in_path))[0]
name = os.path.join(self.out_path, basename+'-%06d.txt' % fileno) name = os.path.join(self.out_path, basename+'-%06d.txt' % fileno)
@ -121,9 +119,12 @@ class SplitHandHistory:
try: try:
infile = self.next_hand(infile) infile = self.next_hand(infile)
infile = self.process_hand(infile, outfile) infile = self.process_hand(infile, outfile)
except: except FpdbEndOfFile:
done = True done = True
break break
except:
print "Unexpected error processing file"
sys.exit(2)
n += 1 n += 1
outfile.close() outfile.close()
if not done: if not done:
@ -161,7 +162,7 @@ class SplitHandHistory:
#print l, len(l) #print l, len(l)
# Catch EOF # Catch EOF
if len(l) == 0: if len(l) == 0:
raise IOError raise FpdbEndOfFile("End of file reached")
m = re_SplitArchive.search(l) m = re_SplitArchive.search(l)
# There is an empty line after pre-hand header and actual HH entry # There is an empty line after pre-hand header and actual HH entry
l = infile.readline() l = infile.readline()
@ -195,26 +196,12 @@ def main(argv=None):
if argv is None: if argv is None:
argv = sys.argv[1:] argv = sys.argv[1:]
parser = OptionParser() if not options.config:
parser.add_option("-f", "--file", dest="filename", metavar="FILE", default=None, options.config = Configuration.Config(file = "HUD_config.test.xml")
help=_("Input file in quiet mode"))
parser.add_option("-o", "--outpath", dest="outpath", metavar="FILE", default=None,
help=_("Input out path in quiet mode"))
parser.add_option("-c", "--convert", dest="filtername", default="PokerStars", metavar="FILTER",
help=_("Conversion filter (*Full Tilt Poker, PokerStars, Everleaf, Absolute)"))
parser.add_option("-a", "--archive", action="store_true", dest="archive", default=False,
help=_("File to be split is a PokerStars or Full Tilt Poker archive file"))
parser.add_option("-d", "--hands", dest="hands", default="100", type="int",
help=_("How many hands do you want saved to each file. Default is 100"))
(options, argv) = parser.parse_args(args = argv)
config = Configuration.Config(file = "HUD_config.test.xml") if options.filename:
SplitHH = SplitHandHistory(options.config, options.filename, options.outpath, options.hands,
if not options.filename: options.hhc, options.archive)
pass
else:
SplitHH = SplitHandHistory(config, options.filename, options.outpath, options.hands,
options.filtername, options.archive)
if __name__ == '__main__': if __name__ == '__main__':
sys.exit(main()) sys.exit(main())