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

Refactoring / code cleanup.

See:

* https://en.wikipedia.org/wiki/Code_refactoring

* https://www.refactoring.com/

* https://www.joelonsoftware.com/2002/01/23/rub-a-dub-dub/

Some small optimisations may have slipped in as well.
This commit is contained in:
Shlomi Fish 2019-05-31 17:22:49 +03:00
parent 26e5aeb045
commit 9acc97be8a
4 changed files with 36 additions and 62 deletions

View file

@ -518,15 +518,12 @@ class PysolMenubar(PysolMenubarTk):
text += os.linesep text += os.linesep
enc = locale.getpreferredencoding() enc = locale.getpreferredencoding()
try: try:
fd = open(fn, 'a') open(fn, 'a').write(text.encode(enc, 'replace'))
fd.write(text.encode(enc, 'replace'))
except Exception as err: except Exception as err:
d = MfxExceptionDialog( d = MfxExceptionDialog(
self.top, err, self.top, err,
text=_("Error while writing to file")) text=_("Error while writing to file"))
else: else:
if fd:
fd.close()
d = MfxMessageDialog( d = MfxMessageDialog(
self.top, title=TITLE+_(" Info"), bitmap="info", self.top, title=TITLE+_(" Info"), bitmap="info",
text=_("Comments were appended to\n\n") + fn) text=_("Comments were appended to\n\n") + fn)
@ -537,7 +534,6 @@ class PysolMenubar(PysolMenubarTk):
# #
def _mStatsSave(self, player, filename, write_method): def _mStatsSave(self, player, filename, write_method):
file = None
if player is None: if player is None:
text = _("Demo statistics") text = _("Demo statistics")
filename = filename + "_demo" filename = filename + "_demo"
@ -546,17 +542,12 @@ class PysolMenubar(PysolMenubarTk):
filename = os.path.join(self.app.dn.config, filename + ".txt") filename = os.path.join(self.app.dn.config, filename + ".txt")
filename = os.path.normpath(filename) filename = os.path.normpath(filename)
try: try:
file = open(filename, "a") a = FileStatsFormatter(self.app, open(filename, "a"))
a = FileStatsFormatter(self.app, file)
write_method(a, player) write_method(a, player)
except EnvironmentError as ex: except EnvironmentError as ex:
if file:
file.close()
MfxExceptionDialog(self.top, ex, MfxExceptionDialog(self.top, ex,
text=_("Error while writing to file")) text=_("Error while writing to file"))
else: else:
if file:
file.close()
MfxMessageDialog( MfxMessageDialog(
self.top, title=TITLE+_(" Info"), bitmap="info", self.top, title=TITLE+_(" Info"), bitmap="info",
text=text + _(" were appended to\n\n") + filename) text=text + _(" were appended to\n\n") + filename)

View file

@ -1059,13 +1059,8 @@ Please select a %s type %s.
# read & parse a cardset config.txt file - see class Cardset in resource.py # read & parse a cardset config.txt file - see class Cardset in resource.py
def _readCardsetConfig(self, dirname, filename): def _readCardsetConfig(self, dirname, filename):
f = None with open(filename, "r") as f:
try:
f = open(filename, "r")
lines = f.readlines() lines = f.readlines()
finally:
if f:
f.close()
lines = [l.strip() for l in lines] lines = [l.strip() for l in lines]
if not lines[0].startswith("PySol"): if not lines[0].startswith("PySol"):
return None return None
@ -1309,7 +1304,7 @@ Please select a %s type %s.
# #
def initResource(self, manager, dirs, ext_re, Resource_Class): def initResource(self, manager, dirs, ext_re, Resource_Class):
found, t = [], {} found, t = [], set()
for dirname in dirs: for dirname in dirs:
dirname = dirname.strip() dirname = dirname.strip()
if dirname: if dirname:
@ -1328,7 +1323,7 @@ Please select a %s type %s.
obj.name = n obj.name = n
key = n.lower() key = n.lower()
if key not in t: if key not in t:
t[key] = 1 t.add(key)
found.append((n, obj)) found.append((n, obj))
except EnvironmentError: except EnvironmentError:
pass pass

View file

@ -3165,14 +3165,9 @@ Please report this bug."""))
def _loadGame(self, filename, app): def _loadGame(self, filename, app):
game = None game = None
f = None with open(filename, "rb") as f:
try:
f = open(filename, "rb")
game = self._undumpGame(Unpickler(f), app) game = self._undumpGame(Unpickler(f), app)
game.gstats.loaded = game.gstats.loaded + 1 game.gstats.loaded = game.gstats.loaded + 1
finally:
if f:
f.close()
return game return game
def _undumpGame(self, p, app): def _undumpGame(self, p, app):
@ -3269,15 +3264,9 @@ in the current implementation.''') % version)
return game return game
def _saveGame(self, filename, protocol=-1): def _saveGame(self, filename, protocol=-1):
f = None if self.canSaveGame():
try: with open(filename, "wb") as f:
if not self.canSaveGame():
raise Exception("Cannot save this game.")
f = open(filename, "wb")
self._dumpGame(Pickler(f, protocol)) self._dumpGame(Pickler(f, protocol))
finally:
if f:
f.close()
def _dumpGame(self, p, bookmark=0): def _dumpGame(self, p, bookmark=0):
self.updateTime() self.updateTime()

View file

@ -399,11 +399,11 @@ def write_game(app, game=None):
check_game = False check_game = False
# print '===>', fn # print '===>', fn
fd = open(fn, 'w') with open(fn, 'w') as fd:
fd.write('''\ fd.write('''\
## -*- coding: utf-8 -*- ## -*- coding: utf-8 -*-
## THIS FILE WAS GENERATED AUTOMATICALLY BY SOLITAIRE WIZARD ## THIS FILE WAS GENERATED AUTOMATICALLY BY THE SOLITAIRE WIZARD
## DO NOT EDIT ## DO NOT EDIT
from pysollib.customgame import CustomGame, registerCustomGame from pysollib.customgame import CustomGame, registerCustomGame
@ -441,7 +441,6 @@ class MyCustomGame(CustomGame):
registerCustomGame(MyCustomGame) registerCustomGame(MyCustomGame)
''') ''')
fd.close()
loadGame(mn, fn, check_game=check_game) loadGame(mn, fn, check_game=check_game)