From cb8bc13cebdf56de261b18301789b7183513d72f Mon Sep 17 00:00:00 2001 From: Ray Date: Sun, 23 Aug 2009 19:40:39 -0400 Subject: [PATCH] Allow self.codepage to be a list of codecs to try. --- pyfpdb/FulltiltToFpdb.py | 2 +- pyfpdb/HandHistoryConverter.py | 24 ++++++++++++++++++------ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/pyfpdb/FulltiltToFpdb.py b/pyfpdb/FulltiltToFpdb.py index 493a6617..30b9cb2d 100755 --- a/pyfpdb/FulltiltToFpdb.py +++ b/pyfpdb/FulltiltToFpdb.py @@ -29,7 +29,7 @@ class Fulltilt(HandHistoryConverter): sitename = "Fulltilt" filetype = "text" - codepage = "utf-16" + codepage = ["utf-16", "cp1252"] siteId = 1 # Needs to match id entry in Sites database # Static regexes diff --git a/pyfpdb/HandHistoryConverter.py b/pyfpdb/HandHistoryConverter.py index 4a9821f8..51819bfc 100644 --- a/pyfpdb/HandHistoryConverter.py +++ b/pyfpdb/HandHistoryConverter.py @@ -398,6 +398,10 @@ or None if we fail to get the info """ hands = hands + [Hand.Hand(self.sitename, self.gametype, l)] return hands + def __listof(self, x): + if isinstance(x, list) or isinstance(x, tuple): return x + else: return [x] + def readFile(self): """Open in_path according to self.codepage. Exceptions caught further up""" @@ -407,12 +411,20 @@ or None if we fail to get the info """ log.debug("Reading stdin with %s" % self.codepage) # is this necessary? or possible? or what? in_fh = codecs.getreader('cp1252')(sys.stdin) else: - in_fh = codecs.open(self.in_path, 'r', self.codepage) - in_fh.seek(self.index) - log.debug("Opened in_path: '%s' with %s" % (self.in_path, self.codepage)) - self.obs = in_fh.read() - self.index = in_fh.tell() - in_fh.close() + success = False + for kodec in self.__listof(self.codepage): + if success: break + print "trying", kodec + try: + in_fh = codecs.open(self.in_path, 'r', kodec) + in_fh.seek(self.index) + log.debug("Opened in_path: '%s' with %s" % (self.in_path, kodec)) + self.obs = in_fh.read() + self.index = in_fh.tell() + in_fh.close() + success = True + except: + pass elif(self.filetype == "xml"): doc = xml.dom.minidom.parse(filename) self.doc = doc