Directory/Node: replace some bare open/close's with with's

This commit is contained in:
Jeremy O'Brien
2026-05-28 11:59:51 -04:00
parent 7bc691169a
commit ad10301569
2 changed files with 8 additions and 12 deletions
+4 -6
View File
@@ -97,9 +97,8 @@ class Directory:
"announce_stream": self.announce_stream
}
file = open(self.app.directorypath, "wb")
file.write(msgpack.packb(directory))
file.close()
with open(self.app.directorypath, "wb") as file:
file.write(msgpack.packb(directory))
except Exception as e:
RNS.log("Could not write directory to disk. Then contained exception was: "+str(e), RNS.LOG_ERROR)
@@ -107,10 +106,9 @@ class Directory:
def load_from_disk(self):
if os.path.isfile(self.app.directorypath):
try:
file = open(self.app.directorypath, "rb")
unpacked_directory = msgpack.unpackb(file.read())
with open(self.app.directorypath, "rb") as file:
unpacked_directory = msgpack.unpackb(file.read())
unpacked_list = unpacked_directory["entry_list"]
file.close()
entries = {}
for e in unpacked_list:
+4 -6
View File
@@ -128,9 +128,8 @@ class Node:
allowed_input = allowed_result.stdout
else:
fh = open(allowed_path, "rb")
allowed_input = fh.read()
fh.close()
with open(allowed_path, "rb") as fh:
allowed_input = fh.read()
allowed_hash_strs = allowed_input.splitlines()
@@ -176,9 +175,8 @@ class Node:
generated = subprocess.run([file_path], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, env=env_map)
return generated.stdout
else:
fh = open(file_path, "rb")
response_data = fh.read()
fh.close()
with open(file_path, "rb") as fh:
response_data = fh.read()
return response_data
else:
RNS.log("Request denied", RNS.LOG_VERBOSE)