Skip to content

Commit

Permalink
Fix ResourceWarning from unclosed excel files
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtylerwalls committed Oct 8, 2024
1 parent 9332d01 commit 830d574
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
7 changes: 4 additions & 3 deletions arches/app/etl_modules/base_import_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,13 +354,14 @@ def read(self, request=None, source=None):
if file.split(".")[-1] == "xlsx":
try:
uploaded_file_path = os.path.join(self.temp_dir, file)
workbook = load_workbook(
filename=default_storage.open(uploaded_file_path)
)
opened_file = default_storage.open(uploaded_file_path)
workbook = load_workbook(filename=opened_file, read_only=True)
self.validate_uploaded_file(workbook)
has_valid_excel_file = True
except:
pass
else:
opened_file.close()
if not has_valid_excel_file:
title = _("Invalid Uploaded File")
message = _(
Expand Down
5 changes: 4 additions & 1 deletion arches/app/etl_modules/branch_excel_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ def stage_excel_file(self, file, summary, cursor):
uploaded_file_path = os.path.join(
settings.UPLOADED_FILES_DIR, "tmp", self.loadid, file
)
workbook = load_workbook(filename=default_storage.open(uploaded_file_path))
opened_file = default_storage.open(uploaded_file_path)
workbook = load_workbook(filename=opened_file, read_only=True)
graphid = self.get_graphid(workbook)
nodegroup_lookup, nodes = self.get_graph_tree(graphid)
node_lookup = self.get_node_lookup(nodes)
Expand All @@ -272,6 +273,8 @@ def stage_excel_file(self, file, summary, cursor):
worksheet, cursor, node_lookup, nodegroup_lookup
)
summary["files"][file]["worksheets"].append(details)
opened_file.close()

cursor.execute(
"""UPDATE load_event SET load_details = %s WHERE loadid = %s""",
(json.dumps(summary), self.loadid),
Expand Down
5 changes: 4 additions & 1 deletion arches/app/etl_modules/tile_excel_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ def stage_excel_file(self, file, summary, cursor):
uploaded_file_path = os.path.join(
settings.UPLOADED_FILES_DIR, "tmp", self.loadid, file
)
workbook = load_workbook(filename=default_storage.open(uploaded_file_path))
opened_file = default_storage.open(uploaded_file_path)
workbook = load_workbook(filename=opened_file, read_only=True)
graphid = self.get_graphid(workbook)
nodegroup_lookup, nodes = self.get_graph_tree(graphid)
node_lookup = self.get_node_lookup(nodes)
Expand All @@ -271,6 +272,8 @@ def stage_excel_file(self, file, summary, cursor):
worksheet, cursor, node_lookup, nodegroup_lookup
)
summary["files"][file]["worksheets"].append(details)
opened_file.close()

cursor.execute(
"""UPDATE load_event SET load_details = %s WHERE loadid = %s""",
(json.dumps(summary), self.loadid),
Expand Down
2 changes: 1 addition & 1 deletion arches/app/utils/file_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_unknown_filetypes(self, file, extension=None):
errors.append(f"File type is not permitted: {extension}")
case "xlsx":
try:
load_workbook(io.BytesIO(file))
load_workbook(io.BytesIO(file), read_only=True)
except (InvalidFileException, zipfile.BadZipFile):
errors.append("Invalid xlsx workbook")
case "csv":
Expand Down

0 comments on commit 830d574

Please sign in to comment.