Unified locale handling.

Added support for importing filenames containig non-latin symbols
This commit is contained in:
grindi 2009-08-12 21:55:19 +04:00
parent 25923b02d1
commit bdfc6ef83f
3 changed files with 14 additions and 18 deletions

View File

@ -39,6 +39,7 @@ if os.name == 'nt':
# FreePokerTools modules # FreePokerTools modules
import Configuration import Configuration
from fpdb_simple import LOCALE_ENCODING
# Each TableWindow object must have the following attributes correctly populated: # Each TableWindow object must have the following attributes correctly populated:
# tw.name = the table name from the title bar, which must to match the table name # tw.name = the table name from the title bar, which must to match the table name
@ -231,19 +232,12 @@ def discover_nt_by_name(c, tablename):
titles = {} titles = {}
win32gui.EnumWindows(win_enum_handler, titles) win32gui.EnumWindows(win_enum_handler, titles)
def getDefaultEncoding():
# FIXME: if somebody know better place fot this function - move it
# FIXME: it's better to use GetCPInfo for windows http://msdn.microsoft.com/en-us/library/dd318078(VS.85).aspx
# but i have no idea, how to call it
import locale
return locale.getpreferredencoding()
for hwnd in titles: for hwnd in titles:
#print "Tables.py: tablename =", tablename, "title =", titles[hwnd] #print "Tables.py: tablename =", tablename, "title =", titles[hwnd]
try: try:
# maybe it's better to make global titles[hwnd] decoding? # maybe it's better to make global titles[hwnd] decoding?
# this can blow up in XP on some windows, eg firefox displaying http://docs.python.org/tutorial/classes.html # this can blow up in XP on some windows, eg firefox displaying http://docs.python.org/tutorial/classes.html
if not tablename.lower() in titles[hwnd].decode(getDefaultEncoding()).lower(): continue if not tablename.lower() in titles[hwnd].decode(LOCALE_ENCODING).lower(): continue
except: except:
continue continue
if 'History for table:' in titles[hwnd]: continue # Everleaf Network HH viewer window if 'History for table:' in titles[hwnd]: continue # Everleaf Network HH viewer window

View File

@ -380,8 +380,9 @@ class Importer:
conv = None conv = None
(stored, duplicates, partial, errors, ttime) = (0, 0, 0, 0, 0) (stored, duplicates, partial, errors, ttime) = (0, 0, 0, 0, 0)
# Load filter, process file, pass returned filename to import_fpdb_file file = file.decode(fpdb_simple.LOCALE_ENCODING)
# Load filter, process file, pass returned filename to import_fpdb_file
if self.settings['threads'] > 0 and self.writeq != None: if self.settings['threads'] > 0 and self.writeq != None:
print "\nConverting " + file + " (" + str(q.qsize()) + ")" print "\nConverting " + file + " (" + str(q.qsize()) + ")"
else: else:

View File

@ -38,7 +38,7 @@ MYSQL_INNODB = 2
PGSQL = 3 PGSQL = 3
SQLITE = 4 SQLITE = 4
(localename, encoding) = locale.getdefaultlocale() LOCALE_ENCODING = locale.getdefaultlocale()[1]
class DuplicateError(Exception): class DuplicateError(Exception):
def __init__(self, value): def __init__(self, value):
@ -546,7 +546,7 @@ def parseActionType(line):
#parses the ante out of the given line and checks which player paid it, updates antes accordingly. #parses the ante out of the given line and checks which player paid it, updates antes accordingly.
def parseAnteLine(line, isTourney, names, antes): def parseAnteLine(line, isTourney, names, antes):
for i, name in enumerate(names): for i, name in enumerate(names):
if line.startswith(name.encode(encoding)): if line.startswith(name.encode(LOCALE_ENCODING)):
pos = line.rfind("$") + 1 pos = line.rfind("$") + 1
if not isTourney: if not isTourney:
antes[i] += float2int(line[pos:]) antes[i] += float2int(line[pos:])
@ -708,7 +708,7 @@ def parseHandStartTime(topline):
def findName(line): def findName(line):
pos1 = line.find(":") + 2 pos1 = line.find(":") + 2
pos2 = line.rfind("(") - 1 pos2 = line.rfind("(") - 1
return unicode(line[pos1:pos2], encoding) return unicode(line[pos1:pos2], LOCALE_ENCODING)
def parseNames(lines): def parseNames(lines):
return [findName(line) for line in lines] return [findName(line) for line in lines]
@ -825,7 +825,7 @@ def parseTourneyNo(topline):
def parseWinLine(line, names, winnings, isTourney): def parseWinLine(line, names, winnings, isTourney):
#print "parseWinLine: line:",line #print "parseWinLine: line:",line
for i,n in enumerate(names): for i,n in enumerate(names):
n = n.encode(encoding) n = n.encode(LOCALE_ENCODING)
if line.startswith(n): if line.startswith(n):
if isTourney: if isTourney:
pos1 = line.rfind("collected ") + 10 pos1 = line.rfind("collected ") + 10
@ -1035,14 +1035,15 @@ def recognisePlayerIDs(db, names, site_id):
def recognisePlayerNo(line, names, atype): def recognisePlayerNo(line, names, atype):
#print "recogniseplayerno, names:",names #print "recogniseplayerno, names:",names
for i in xrange(len(names)): for i in xrange(len(names)):
encodedName = names[i].encode(LOCALE_ENCODING)
if (atype=="unbet"): if (atype=="unbet"):
if (line.endswith(names[i].encode(encoding))): if (line.endswith(encodedName)):
return (i) return (i)
elif (line.startswith("Dealt to ")): elif (line.startswith("Dealt to ")):
#print "recognisePlayerNo, card precut, line:",line #print "recognisePlayerNo, card precut, line:",line
tmp=line[9:] tmp=line[9:]
#print "recognisePlayerNo, card postcut, tmp:",tmp #print "recognisePlayerNo, card postcut, tmp:",tmp
if (tmp.startswith(names[i].encode(encoding))): if (tmp.startswith(encodedName)):
return (i) return (i)
elif (line.startswith("Seat ")): elif (line.startswith("Seat ")):
if (line.startswith("Seat 10")): if (line.startswith("Seat 10")):
@ -1050,10 +1051,10 @@ def recognisePlayerNo(line, names, atype):
else: else:
tmp=line[8:] tmp=line[8:]
if (tmp.startswith(names[i].encode(encoding))): if (tmp.startswith(encodedName)):
return (i) return (i)
else: else:
if (line.startswith(names[i].encode(encoding))): if (line.startswith(encodedName)):
return (i) return (i)
#if we're here we mustve failed #if we're here we mustve failed
raise FpdbError ("failed to recognise player in: "+line+" atype:"+atype) raise FpdbError ("failed to recognise player in: "+line+" atype:"+atype)