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

refactor/modernize the calls to open()

use "with", flags
This commit is contained in:
Shlomi Fish 2020-10-06 18:43:15 +03:00
parent bf9ce90a5b
commit 95a0d7d007
7 changed files with 21 additions and 28 deletions

View file

@ -519,7 +519,8 @@ class PysolMenubar(PysolMenubarTk):
text += os.linesep text += os.linesep
enc = locale.getpreferredencoding() enc = locale.getpreferredencoding()
try: try:
open(fn, 'a').write(text.encode(enc, 'replace')) with open(fn, 'at') as fh:
fh.write(text.encode(enc, 'replace'))
except Exception as err: except Exception as err:
d = MfxExceptionDialog( d = MfxExceptionDialog(
self.top, err, self.top, err,

View file

@ -32,7 +32,7 @@ def read_cardset_config(dirname, filename):
This function returns None if any errors occurred. This function returns None if any errors occurred.
""" """
with open(filename, "r") as f: with open(filename, "rt") as f:
lines = f.readlines() lines = f.readlines()
lines = [line.strip() for line in lines] lines = [line.strip() for line in lines]
if not lines[0].startswith("PySol"): if not lines[0].startswith("PySol"):

View file

@ -498,19 +498,15 @@ def test(args=None):
fn = 'test.html' fn = 'test.html'
if fn == '-': if fn == '-':
f = sys.stdin data = sys.stdin.read()
else: else:
try: try:
f = open(fn, 'r') with open(fn, 'rt') as fh:
data = fh.read()
except IOError as msg: except IOError as msg:
print(fn, ":", msg) print(fn, ":", msg)
sys.exit(1) sys.exit(1)
data = f.read()
if f is not sys.stdin:
f.close()
if silent: if silent:
f = formatter.NullFormatter() f = formatter.NullFormatter()
else: else:

View file

@ -285,30 +285,23 @@ class KwStruct:
# ************************************************************************ # ************************************************************************
def pickle(obj, filename, protocol=0): def pickle(obj, filename, protocol=0):
f = None
try: try:
f = open(filename, "wb") with open(filename, "wb") as fh:
Pickler(f, protocol).dump(obj) Pickler(fh, protocol).dump(obj)
f.close()
f = None
# print "Pickled", filename # print "Pickled", filename
finally: finally:
if f: pass
f.close()
def unpickle(filename): def unpickle(filename):
f, obj = None, None obj = None
try: try:
f = open(filename, "rb") with open(filename, "rb") as fh:
x = Unpickler(f).load() x = Unpickler(fh).load()
f.close()
f = None
obj = x obj = x
# print "Unpickled", filename # print "Unpickled", filename
finally: finally:
if f: pass
f.close()
return obj return obj

View file

@ -524,9 +524,10 @@ class CardsetInfoDialog(MfxDialog):
frame.rowconfigure(1, weight=1) frame.rowconfigure(1, weight=1)
# #
text = '' text = ''
f = os.path.join(cardset.dir, "COPYRIGHT") fn = os.path.join(cardset.dir, "COPYRIGHT")
try: try:
text = open(f).read() with open(fn, "rt") as fh:
text = fh.read()
except Exception: except Exception:
pass pass
if text: if text:

View file

@ -487,9 +487,10 @@ class CardsetInfoDialog(MfxDialog):
frame.rowconfigure(1, weight=1) frame.rowconfigure(1, weight=1)
# #
text = '' text = ''
f = os.path.join(cardset.dir, "COPYRIGHT") fn = os.path.join(cardset.dir, "COPYRIGHT")
try: try:
text = open(f).read() with open(fn, "rt") as fh:
text = fh.read()
except Exception: except Exception:
pass pass
if text: if text:

View file

@ -295,7 +295,8 @@ class Base_HTMLViewer:
import codecs import codecs
return codecs.open(url, encoding='utf-8') return codecs.open(url, encoding='utf-8')
else: else:
return open(url, "rb") with open(url, "rb") as fh:
return fh
return my_open(url), url return my_open(url), url
def display(self, url, add=1, relpath=1, xview=0, yview=0): def display(self, url, add=1, relpath=1, xview=0, yview=0):