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:
parent
4fe958d888
commit
04a9b4c910
2 changed files with 20 additions and 5 deletions
|
@ -875,7 +875,19 @@ class FreeCellSolver_Hint(Base_Solver_Hint):
|
||||||
assert re.match(r'^\s*(?:' + RE + r')?(?:\s+' + RE + r')*\s*$', s)
|
assert re.match(r'^\s*(?:' + RE + r')?(?:\s+' + RE + r')*\s*$', s)
|
||||||
return re.findall(r'\b' + RE + r'\b', 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')
|
line = line_p.rstrip('\r\n')
|
||||||
m = re.match(r'^(?:Foundations:|Founds?:)\s*(.*)', line)
|
m = re.match(r'^(?:Foundations:|Founds?:)\s*(.*)', line)
|
||||||
if m:
|
if m:
|
||||||
|
|
|
@ -1226,14 +1226,17 @@ Unsupported game for import.
|
||||||
if not idir:
|
if not idir:
|
||||||
idir = self.app.dn.savegames
|
idir = self.app.dn.savegames
|
||||||
d = tkinter_tkfiledialog.Open()
|
d = tkinter_tkfiledialog.Open()
|
||||||
filename = d.show(filetypes=self.FILETYPES,
|
if True:
|
||||||
defaultextension=self.DEFAULTEXTENSION,
|
filename = d.show(filetypes=self.FILETYPES,
|
||||||
initialdir=idir, initialfile=ifile)
|
defaultextension=self.DEFAULTEXTENSION,
|
||||||
|
initialdir=idir, initialfile=ifile)
|
||||||
|
else:
|
||||||
|
filename = '/home/shlomif/m2.txt'
|
||||||
if filename:
|
if filename:
|
||||||
filename = os.path.normpath(filename)
|
filename = os.path.normpath(filename)
|
||||||
# filename = os.path.normcase(filename)
|
# filename = os.path.normcase(filename)
|
||||||
if os.path.isfile(filename):
|
if os.path.isfile(filename):
|
||||||
with open(filename, 'r') as fh:
|
with open(filename, 'r+b') as fh:
|
||||||
game = self.game
|
game = self.game
|
||||||
game.Solver_Class(game, self).importFile(fh, game, self)
|
game.Solver_Class(game, self).importFile(fh, game, self)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue