Merge branch 'chaz' of git://github.com/ChazDazzle/fpdb-chaz
This commit is contained in:
commit
921249af43
|
@ -73,3 +73,6 @@ class FpdbHandDuplicate(FpdbHandError):
|
|||
|
||||
class FpdbHandPartial(FpdbHandError):
|
||||
pass
|
||||
|
||||
class FpdbEndOfFile(FpdbHandError):
|
||||
pass
|
||||
|
|
|
@ -574,7 +574,6 @@ Left-Drag to Move"
|
|||
<hhc site="PartyPoker" converter="PartyPokerToFpdb"/>
|
||||
<hhc site="Betfair" converter="BetfairToFpdb"/>
|
||||
<hhc site="OnGame" converter="OnGameToFpdb"/>
|
||||
<hhc site="Partouche" converter="PartoucheToFpdb"/>
|
||||
<hhc site="Carbon" converter="CarbonToFpdb"/>
|
||||
<hhc site="PKR" converter="PkrToFpdb"/>
|
||||
<hhc site="iPoker" converter="iPokerToFpdb"/>
|
||||
|
|
|
@ -772,7 +772,6 @@ Left-Drag to Move"
|
|||
<hhc site="Absolute" converter="AbsoluteToFpdb"/>
|
||||
<hhc site="PartyPoker" converter="PartyPokerToFpdb"/>
|
||||
<hhc site="Betfair" converter="BetfairToFpdb"/>
|
||||
<hhc site="Partouche" converter="PartoucheToFpdb"/>
|
||||
<hhc site="Carbon" converter="CarbonToFpdb"/>
|
||||
<hhc site="OnGame" converter="OnGameToFpdb"/>
|
||||
<hhc site="PKR" converter="PkrToFpdb"/>
|
||||
|
|
122
pyfpdb/IdentifySite.py
Normal file
122
pyfpdb/IdentifySite.py
Normal file
|
@ -0,0 +1,122 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
#Copyright 2010 Chaz Littlejohn
|
||||
#This program is free software: you can redistribute it and/or modify
|
||||
#it under the terms of the GNU Affero General Public License as published by
|
||||
#the Free Software Foundation, version 3 of the License.
|
||||
#
|
||||
#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 Affero General Public License
|
||||
#along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#In the "official" distribution you can find the license in agpl-3.0.txt.
|
||||
|
||||
import L10n
|
||||
_ = L10n.get_translation()
|
||||
|
||||
import re
|
||||
import sys
|
||||
import os
|
||||
import os.path
|
||||
from optparse import OptionParser
|
||||
import codecs
|
||||
import Configuration
|
||||
import Database
|
||||
|
||||
__ARCHIVE_PRE_HEADER_REGEX='^Hand #(\d+)\s*$|\*{20}\s#\s\d+\s\*+\s+'
|
||||
re_SplitArchive = re.compile(__ARCHIVE_PRE_HEADER_REGEX)
|
||||
|
||||
|
||||
class IdentifySite:
|
||||
def __init__(self, config, in_path = '-'):
|
||||
self.in_path = in_path
|
||||
self.config = config
|
||||
self.db = Database.Database(config)
|
||||
self.sitelist = {}
|
||||
self.filelist = {}
|
||||
self.generateSiteList()
|
||||
self.walkDirectory(self.in_path, self.sitelist)
|
||||
|
||||
def generateSiteList(self):
|
||||
"""Generates a ordered dictionary of site, filter and filter name for each site in hhcs"""
|
||||
for site, hhc in self.config.hhcs.iteritems():
|
||||
filter = hhc.converter
|
||||
filter_name = filter.replace("ToFpdb", "")
|
||||
result = self.db.get_site_id(site)
|
||||
if len(result) == 1:
|
||||
self.sitelist[result[0][0]] = (site, filter, filter_name)
|
||||
else:
|
||||
pass
|
||||
|
||||
def walkDirectory(self, dir, sitelist):
|
||||
"""Walks a directory, and executes a callback on each file"""
|
||||
dir = os.path.abspath(dir)
|
||||
for file in [file for file in os.listdir(dir) if not file in [".",".."]]:
|
||||
nfile = os.path.join(dir,file)
|
||||
if os.path.isdir(nfile):
|
||||
self.walkDirectory(nfile, sitelist)
|
||||
else:
|
||||
self.idSite(nfile, sitelist)
|
||||
|
||||
def __listof(self, x):
|
||||
if isinstance(x, list) or isinstance(x, tuple):
|
||||
return x
|
||||
else:
|
||||
return [x]
|
||||
|
||||
def idSite(self, file, sitelist):
|
||||
"""Identifies the site the hh file originated from"""
|
||||
if file.endswith('.txt'):
|
||||
self.filelist[file] = ''
|
||||
archive = False
|
||||
for site, info in sitelist.iteritems():
|
||||
mod = __import__(info[1])
|
||||
obj = getattr(mod, info[2], None)
|
||||
|
||||
for kodec in self.__listof(obj.codepage):
|
||||
try:
|
||||
in_fh = codecs.open(file, 'r', kodec)
|
||||
whole_file = in_fh.read()
|
||||
in_fh.close()
|
||||
|
||||
if info[2] in ('OnGame', 'Winamax'):
|
||||
m = obj.re_HandInfo.search(whole_file)
|
||||
elif info[2] in ('PartyPoker'):
|
||||
m = obj.re_GameInfoRing.search(whole_file)
|
||||
if not m:
|
||||
m = obj.re_GameInfoTrny.search(whole_file)
|
||||
else:
|
||||
m = obj.re_GameInfo.search(whole_file)
|
||||
if re_SplitArchive.search(whole_file):
|
||||
archive = True
|
||||
if m:
|
||||
self.filelist[file] = [info[0]] + [info[1]] + [kodec] + [archive]
|
||||
break
|
||||
except:
|
||||
pass
|
||||
|
||||
def main(argv=None):
|
||||
if argv is None:
|
||||
argv = sys.argv[1:]
|
||||
|
||||
config = Configuration.Config(file = "HUD_config.test.xml")
|
||||
in_path = 'regression-test-files/'
|
||||
IdSite = IdentifySite(config, in_path)
|
||||
|
||||
print "\n----------- SITE LIST -----------"
|
||||
for site, info in IdSite.sitelist.iteritems():
|
||||
print site, info
|
||||
print "----------- END SITE LIST -----------"
|
||||
|
||||
print "\n----------- ID REGRESSION FILES -----------"
|
||||
for file, site in IdSite.filelist.iteritems():
|
||||
print file, site
|
||||
print "----------- END ID REGRESSION FILES -----------"
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
|
@ -53,6 +53,15 @@ def fpdb_options():
|
|||
help = _("Print version information and exit."))
|
||||
parser.add_option("-u", "--usage", action="store_true", dest="usage", default=False,
|
||||
help=_("Print some useful one liners"))
|
||||
# The following options are used for SplitHandHistory.py
|
||||
parser.add_option("-f", "--file", dest="filename", metavar="FILE", default=None,
|
||||
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("-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("-n", "--numhands", 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()
|
||||
|
|
207
pyfpdb/SplitHandHistory.py
Normal file
207
pyfpdb/SplitHandHistory.py
Normal file
|
@ -0,0 +1,207 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
#Copyright 2010 Chaz Littlejohn
|
||||
#This program is free software: you can redistribute it and/or modify
|
||||
#it under the terms of the GNU Affero General Public License as published by
|
||||
#the Free Software Foundation, version 3 of the License.
|
||||
#
|
||||
#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 Affero General Public License
|
||||
#along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#In the "official" distribution you can find the license in agpl-3.0.txt.
|
||||
|
||||
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
|
||||
|
||||
(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):
|
||||
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
|
||||
|
||||
#Acquire re_SplitHands for this hh
|
||||
filter_name = filter.replace("ToFpdb", "")
|
||||
mod = __import__(filter)
|
||||
obj = getattr(mod, filter_name, None)
|
||||
self.re_SplitHands = obj.re_SplitHands
|
||||
|
||||
#Determine line delimiter type if any
|
||||
if self.re_SplitHands.match('\n\n'):
|
||||
self.line_delimiter = '\n\n'
|
||||
if self.re_SplitHands.match('\n\n\n'):
|
||||
self.line_delimiter = '\n\n\n'
|
||||
|
||||
#Add new line addendum for sites which match SplitHand to next line as well
|
||||
if filter_name == 'OnGame':
|
||||
self.line_addendum = '*'
|
||||
if filter_name == 'Carbon':
|
||||
self.line_addendum = '<game'
|
||||
|
||||
#Open the gargantuan file
|
||||
for kodec in self.__listof(codepage):
|
||||
try:
|
||||
infile = codecs.open(self.in_path, 'r', kodec)
|
||||
except IOError:
|
||||
print 'File not found'
|
||||
sys.exit(2)
|
||||
|
||||
#Split with do_hands_per_file if archive and paragraphs if a regular hh
|
||||
if self.archive:
|
||||
nn = 0
|
||||
while True:
|
||||
nn += 1
|
||||
check = self.do_hands_per_file(infile, nn)
|
||||
if check is None:
|
||||
print '%s processed' % self.in_path
|
||||
break
|
||||
else:
|
||||
filenum = 0
|
||||
while not self.filedone:
|
||||
filenum += 1
|
||||
outfile = self.new_file(filenum)
|
||||
handnum = 0
|
||||
for hand in self.paragraphs(infile, None, self.line_addendum):
|
||||
outfile.write(hand)
|
||||
if self.line_delimiter:
|
||||
outfile.write(self.line_delimiter)
|
||||
handnum += 1
|
||||
if handnum >= 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, basename+'-%06d.txt' % fileno)
|
||||
print '-> %s' % name
|
||||
newfile = file(name, 'w')
|
||||
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:
|
||||
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 #<num>)
|
||||
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()
|
||||
|
||||
while len(l) < 3:
|
||||
l = infile.readline()
|
||||
|
||||
while len(l) > 2:
|
||||
l = l.replace('\r\n', '\n')
|
||||
outfile.write(l)
|
||||
l = infile.readline()
|
||||
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.config:
|
||||
options.config = Configuration.Config(file = "HUD_config.test.xml")
|
||||
|
||||
if options.filename:
|
||||
SplitHH = SplitHandHistory(options.config, options.filename, options.outpath, options.hands,
|
||||
options.hhc, options.archive)
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
Loading…
Reference in New Issue
Block a user