Allow self.codepage to be a list of codecs to try.

This commit is contained in:
Ray 2009-08-23 19:40:39 -04:00
parent 2e0c743671
commit cb8bc13ceb
2 changed files with 19 additions and 7 deletions

View File

@ -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

View File

@ -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)
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, self.codepage))
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