From 6c470337f4da0817529fb5e9952ffc11757192f7 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Mon, 1 Mar 2021 14:46:06 +0100 Subject: [PATCH 9/9] misc: fix warnings about unclosed files (#51516) --- tests/test_commands.py | 3 +- zoo/zoo_nanterre/synchronize_federations.py | 16 ++--- zoo/zoo_nanterre/views.py | 66 +++++++++++---------- 3 files changed, 45 insertions(+), 40 deletions(-) diff --git a/tests/test_commands.py b/tests/test_commands.py index fe077f4..9bbef6e 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -31,7 +31,8 @@ def test_runjobs_command(db): def test_runscript_command(tmpdir): script_path = '%s/script.py' % str(tmpdir) - open(script_path, 'w').write('# N/A') + with open(script_path, 'w') as fd: + fd.write('# N/A') call_command('runscript', script_path) diff --git a/zoo/zoo_nanterre/synchronize_federations.py b/zoo/zoo_nanterre/synchronize_federations.py index ed1f5ea..cdfe4ca 100644 --- a/zoo/zoo_nanterre/synchronize_federations.py +++ b/zoo/zoo_nanterre/synchronize_federations.py @@ -52,13 +52,14 @@ class SynchronizeFederationsImport(object): self.delete_count = 0 self.unknown_count = 0 max_federation = 0 - for i, line in enumerate(default_storage.open(self.action.csv_filepath, 'r')): - line = line.strip() - self.federations.append(line) - try: - max_federation = max(max_federation, int(line)) - except ValueError as e: - pass + with default_storage.open(self.action.csv_filepath, 'r') as fd: + for i, line in enumerate(fd): + line = line.strip() + self.federations.append(line) + try: + max_federation = max(max_federation, int(line)) + except ValueError: + pass self.federations.sort() app_id = self.action.app_id individus = Entity.objects.filter( @@ -254,6 +255,7 @@ class SynchronizeFederationsAction(object): stream = getattr(self, prefix) if not stream: return None + stream.close() url_name = 'admin:synchronize-federations-%s-%s' % (action, prefix.replace('_', '-')) filename = getattr(self, prefix + '_csv_filename') return reverse(url_name, kwargs={ diff --git a/zoo/zoo_nanterre/views.py b/zoo/zoo_nanterre/views.py index b2f5fb6..967775f 100644 --- a/zoo/zoo_nanterre/views.py +++ b/zoo/zoo_nanterre/views.py @@ -116,21 +116,22 @@ def synchronize_federations_report(request, job_id, model_admin, *args, **kwargs report = job.action.report if not report: raise Http404('no report') - text_report = report - if six.PY3: - text_report = io.TextIOWrapper(text_report, encoding='utf-8') - reader = csv.reader(text_report) - next(reader) - actions = [row for row in reader if row[6] != 'KEEP'] - context = dict( - model_admin.admin_site.each_context(request), - title=job.created, - job=job, - csv=actions, - csv_url=job.action.download_report_url, - csv_filesize=report.size, - ) - return TemplateResponse(request, "admin/zoo_data/entity/synchronize_federations_report.html", context) + with report: + text_report = report + if six.PY3: + text_report = io.TextIOWrapper(text_report, encoding='utf-8') + reader = csv.reader(text_report) + next(reader) + actions = [row for row in reader if row[6] != 'KEEP'] + context = dict( + model_admin.admin_site.each_context(request), + title=job.created, + job=job, + csv=actions, + csv_url=job.action.download_report_url, + csv_filesize=report.size, + ) + return TemplateResponse(request, "admin/zoo_data/entity/synchronize_federations_report.html", context) @permission_required('zoo_data.action1_entity') @@ -148,23 +149,24 @@ def synchronize_federations_apply_report(request, job_id, model_admin, *args, ** jobs = SynchronizeFederationsAction.get_jobs() job = get_object_or_404(jobs, id=job_id) report = job.action.apply_report - if not report: - raise Http404('no report') - text_report = report - if six.PY3: - text_report = io.TextIOWrapper(text_report, encoding='utf-8') - reader = csv.reader(text_report) - next(reader) - actions = [row for row in reader if row[6] != 'KEEP'] - context = dict( - model_admin.admin_site.each_context(request), - title=u'Application - %s' % job.created, - job=job, - csv=actions, - csv_url=job.action.download_apply_report_url, - csv_filesize=report.size, - ) - return TemplateResponse(request, "admin/zoo_data/entity/synchronize_federations_report.html", context) + with report: + if not report: + raise Http404('no report') + text_report = report + if six.PY3: + text_report = io.TextIOWrapper(text_report, encoding='utf-8') + reader = csv.reader(text_report) + next(reader) + actions = [row for row in reader if row[6] != 'KEEP'] + context = dict( + model_admin.admin_site.each_context(request), + title=u'Application - %s' % job.created, + job=job, + csv=actions, + csv_url=job.action.download_apply_report_url, + csv_filesize=report.size, + ) + return TemplateResponse(request, "admin/zoo_data/entity/synchronize_federations_report.html", context) @permission_required('zoo_data.action1_entity') -- 2.30.0