Skip to content

Commit

Permalink
Chage some open() to use with..as
Browse files Browse the repository at this point in the history
  • Loading branch information
tofu-rocketry committed May 8, 2024
1 parent d72ebaa commit 2b856f6
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 26 deletions.
7 changes: 3 additions & 4 deletions apel/db/loader/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,9 @@ def startup(self):
log.warning("Check that the dbloader is not running, then remove the file.")
raise LoaderException("The dbloader cannot start while pidfile exists.")
try:
f = open(self._pidfile, "w")
f.write(str(os.getpid()))
f.write("\n")
f.close()
with open(self._pidfile, "w") as f:
f.write(str(os.getpid()))
f.write("\n")
except IOError as e:
log.warning("Failed to create pidfile %s: %s", self._pidfile, e)

Expand Down
15 changes: 6 additions & 9 deletions bin/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,17 +201,14 @@ def scan_dir(parser, dirpath, reparse, expr, apel_db, processed):
# format we will get IOError, empty files can
# give EOFError as well.
for method in (bz2.BZ2File, gzip.open, open):
try: # this is for Python < 2.5
try:
fp = method(abs_file, 'rb')
try:
with method(abs_file, 'rb') as fp:
parsed, total = parse_file(parser, apel_db,
fp, reparse)
break
except (IOError, EOFError):
if method == open:
raise
finally:
fp.close()
break
except (IOError, EOFError):
if method == open:
raise
except IOError as e:
log.error('Cannot parse file %s: %s', item, e)
except ApelDbException as e:
Expand Down
5 changes: 2 additions & 3 deletions bin/retrieve_dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,10 @@ def dns_from_file(path):
assume that they're DNs, but we'll check later.
'''

dn_file = open(path)
dns = dn_file.readlines()
with open(path) as dn_file:
dns = dn_file.readlines()
# get rid of any whitespace in the list of strings
dns = [dn.strip() for dn in dns]
dn_file.close()
return dns


Expand Down
7 changes: 3 additions & 4 deletions bin/summariser.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,9 @@ def runprocess(db_config_file, config_file, log_config_file):
print("Error initialising summariser: %s" % err)
sys.exit(1)
try:
f = open(pidfile, "w")
f.write(str(os.getpid()))
f.write("\n")
f.close()
with open(pidfile, "w") as f:
f.write(str(os.getpid()))
f.write("\n")
except IOError as e:
log.warning("Failed to create pidfile %s: %s", pidfile, e)
# If we fail to create a pidfile, don't start the summariser
Expand Down
5 changes: 2 additions & 3 deletions test/test_hashing.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ def test_calculate_hash(self):
data_hash = '3d1eb00cc63828b36882f076f35c8cdd'

tmpname = tempfile.mktemp('hashtest')
fp = open(tmpname, 'wb')
fp.write(data)
fp.close()
with open(tmpname, 'wb') as fp:
fp.write(data)

self.assertEqual(data_hash, calculate_hash(tmpname))

Expand Down
5 changes: 2 additions & 3 deletions test/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ def test_stdout_logging(self):

# Only check bit after timestamp as that doesn't change.
self.assertEqual(output[23:], " - test_logging - INFO - out\n")
f = open(self.path)
self.assertEqual(f.readline()[23:], " - test_logging - INFO - out\n")
f.close()
with open(self.path) as f:
self.assertEqual(f.readline()[23:], " - test_logging - INFO - out\n")

def test_boring_logging(self):
"""Check that logging without handlers at least runs without errors."""
Expand Down

0 comments on commit 2b856f6

Please sign in to comment.