1
0
Fork 0
mirror of https://github.com/shlomif/PySolFC.git synced 2025-04-05 00:02:29 -04:00

Handle the utf-8 BOM in the import.

This commit is contained in:
Shlomi Fish 2018-04-30 20:33:23 +03:00
parent 4fe958d888
commit 04a9b4c910
2 changed files with 20 additions and 5 deletions

View file

@ -875,7 +875,19 @@ class FreeCellSolver_Hint(Base_Solver_Hint):
assert re.match(r'^\s*(?:' + RE + r')?(?:\s+' + RE + r')*\s*$', s)
return re.findall(r'\b' + RE + r'\b', s)
for line_p in fh:
# Based on https://stackoverflow.com/questions/8898294 - thanks!
def mydecode(s):
if sys.version_info < (3,):
return s
for encoding in "utf-8-sig", "utf-8":
try:
return s.decode(encoding)
except UnicodeDecodeError:
continue
return s.decode("latin-1") # will always work
mytext = mydecode(fh.read())
for line_p in mytext.splitlines():
line = line_p.rstrip('\r\n')
m = re.match(r'^(?:Foundations:|Founds?:)\s*(.*)', line)
if m:

View file

@ -1226,14 +1226,17 @@ Unsupported game for import.
if not idir:
idir = self.app.dn.savegames
d = tkinter_tkfiledialog.Open()
filename = d.show(filetypes=self.FILETYPES,
defaultextension=self.DEFAULTEXTENSION,
initialdir=idir, initialfile=ifile)
if True:
filename = d.show(filetypes=self.FILETYPES,
defaultextension=self.DEFAULTEXTENSION,
initialdir=idir, initialfile=ifile)
else:
filename = '/home/shlomif/m2.txt'
if filename:
filename = os.path.normpath(filename)
# filename = os.path.normcase(filename)
if os.path.isfile(filename):
with open(filename, 'r') as fh:
with open(filename, 'r+b') as fh:
game = self.game
game.Solver_Class(game, self).importFile(fh, game, self)