diff --git a/pysollib/actions.py b/pysollib/actions.py index b75ad9ed01..febe015c2f 100644 --- a/pysollib/actions.py +++ b/pysollib/actions.py @@ -518,15 +518,12 @@ def mEditGameComment(self, *args): text += os.linesep enc = locale.getpreferredencoding() try: - fd = open(fn, 'a') - fd.write(text.encode(enc, 'replace')) + open(fn, 'a').write(text.encode(enc, 'replace')) except Exception as err: d = MfxExceptionDialog( self.top, err, text=_("Error while writing to file")) else: - if fd: - fd.close() d = MfxMessageDialog( self.top, title=TITLE+_(" Info"), bitmap="info", text=_("Comments were appended to\n\n") + fn) @@ -537,7 +534,6 @@ def mEditGameComment(self, *args): # def _mStatsSave(self, player, filename, write_method): - file = None if player is None: text = _("Demo statistics") filename = filename + "_demo" @@ -546,17 +542,12 @@ def _mStatsSave(self, player, filename, write_method): filename = os.path.join(self.app.dn.config, filename + ".txt") filename = os.path.normpath(filename) try: - file = open(filename, "a") - a = FileStatsFormatter(self.app, file) + a = FileStatsFormatter(self.app, open(filename, "a")) write_method(a, player) except EnvironmentError as ex: - if file: - file.close() MfxExceptionDialog(self.top, ex, text=_("Error while writing to file")) else: - if file: - file.close() MfxMessageDialog( self.top, title=TITLE+_(" Info"), bitmap="info", text=text + _(" were appended to\n\n") + filename) diff --git a/pysollib/app.py b/pysollib/app.py index 878bfbb92e..d04047a6c8 100644 --- a/pysollib/app.py +++ b/pysollib/app.py @@ -1059,13 +1059,8 @@ def loadPlugins(self, dirname): # read & parse a cardset config.txt file - see class Cardset in resource.py def _readCardsetConfig(self, dirname, filename): - f = None - try: - f = open(filename, "r") + with open(filename, "r") as f: lines = f.readlines() - finally: - if f: - f.close() lines = [l.strip() for l in lines] if not lines[0].startswith("PySol"): return None @@ -1309,7 +1304,7 @@ def _my_list_dir(self, dirname): # def initResource(self, manager, dirs, ext_re, Resource_Class): - found, t = [], {} + found, t = [], set() for dirname in dirs: dirname = dirname.strip() if dirname: @@ -1328,7 +1323,7 @@ def initResource(self, manager, dirs, ext_re, Resource_Class): obj.name = n key = n.lower() if key not in t: - t[key] = 1 + t.add(key) found.append((n, obj)) except EnvironmentError: pass diff --git a/pysollib/game.py b/pysollib/game.py index c213eb780a..753a6783a5 100644 --- a/pysollib/game.py +++ b/pysollib/game.py @@ -3165,14 +3165,9 @@ def saveGame(self, filename, protocol=-1): def _loadGame(self, filename, app): game = None - f = None - try: - f = open(filename, "rb") + with open(filename, "rb") as f: game = self._undumpGame(Unpickler(f), app) game.gstats.loaded = game.gstats.loaded + 1 - finally: - if f: - f.close() return game def _undumpGame(self, p, app): @@ -3269,15 +3264,9 @@ def validate(v, txt): return game def _saveGame(self, filename, protocol=-1): - f = None - try: - if not self.canSaveGame(): - raise Exception("Cannot save this game.") - f = open(filename, "wb") - self._dumpGame(Pickler(f, protocol)) - finally: - if f: - f.close() + if self.canSaveGame(): + with open(filename, "wb") as f: + self._dumpGame(Pickler(f, protocol)) def _dumpGame(self, p, bookmark=0): self.updateTime() diff --git a/pysollib/wizardutil.py b/pysollib/wizardutil.py index 84c651366b..1fdb90000f 100644 --- a/pysollib/wizardutil.py +++ b/pysollib/wizardutil.py @@ -399,11 +399,11 @@ def write_game(app, game=None): check_game = False # print '===>', fn - fd = open(fn, 'w') + with open(fn, 'w') as fd: - fd.write('''\ + fd.write('''\ ## -*- coding: utf-8 -*- -## THIS FILE WAS GENERATED AUTOMATICALLY BY SOLITAIRE WIZARD +## THIS FILE WAS GENERATED AUTOMATICALLY BY THE SOLITAIRE WIZARD ## DO NOT EDIT from pysollib.customgame import CustomGame, registerCustomGame @@ -413,35 +413,34 @@ class MyCustomGame(CustomGame): SETTINGS = { ''') - for w in WizardWidgets: - if isinstance(w, six.string_types): - continue - v = w.variable.get() - if w.widget in ('menu', 'preset'): - v = w.translation_map[v] - if v == w.default: - # save only unique values - continue - if isinstance(v, int): - fd.write(" '%s': %i,\n" % (w.var_name, v)) - else: - if w.var_name == 'name': - # escape - v = v.replace('\\', '\\\\') - v = v.replace("'", "\\'") - if isinstance(v, six.text_type): - v = v.encode('utf-8') - if not v: - v = 'Invalid Game Name' - fd.write(" '%s': '%s',\n" % (w.var_name, v)) - fd.write(" 'gameid': %i,\n" % gameid) + for w in WizardWidgets: + if isinstance(w, six.string_types): + continue + v = w.variable.get() + if w.widget in ('menu', 'preset'): + v = w.translation_map[v] + if v == w.default: + # save only unique values + continue + if isinstance(v, int): + fd.write(" '%s': %i,\n" % (w.var_name, v)) + else: + if w.var_name == 'name': + # escape + v = v.replace('\\', '\\\\') + v = v.replace("'", "\\'") + if isinstance(v, six.text_type): + v = v.encode('utf-8') + if not v: + v = 'Invalid Game Name' + fd.write(" '%s': '%s',\n" % (w.var_name, v)) + fd.write(" 'gameid': %i,\n" % gameid) - fd.write('''\ - } + fd.write('''\ + } registerCustomGame(MyCustomGame) ''') - fd.close() loadGame(mn, fn, check_game=check_game)