Projet

Général

Profil

0009-misc-fix-warnings-about-unclosed-files-51516.patch

Benjamin Dauvergne, 01 mars 2021 15:06

Télécharger (5,64 ko)

Voir les différences:

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(-)
tests/test_commands.py
31 31

  
32 32
def test_runscript_command(tmpdir):
33 33
    script_path = '%s/script.py' % str(tmpdir)
34
    open(script_path, 'w').write('# N/A')
34
    with open(script_path, 'w') as fd:
35
        fd.write('# N/A')
35 36
    call_command('runscript', script_path)
36 37

  
37 38

  
zoo/zoo_nanterre/synchronize_federations.py
52 52
        self.delete_count = 0
53 53
        self.unknown_count = 0
54 54
        max_federation = 0
55
        for i, line in enumerate(default_storage.open(self.action.csv_filepath, 'r')):
56
            line = line.strip()
57
            self.federations.append(line)
58
            try:
59
                max_federation = max(max_federation, int(line))
60
            except ValueError as e:
61
                pass
55
        with default_storage.open(self.action.csv_filepath, 'r') as fd:
56
            for i, line in enumerate(fd):
57
                line = line.strip()
58
                self.federations.append(line)
59
                try:
60
                    max_federation = max(max_federation, int(line))
61
                except ValueError:
62
                    pass
62 63
        self.federations.sort()
63 64
        app_id = self.action.app_id
64 65
        individus = Entity.objects.filter(
......
254 255
        stream = getattr(self, prefix)
255 256
        if not stream:
256 257
            return None
258
        stream.close()
257 259
        url_name = 'admin:synchronize-federations-%s-%s' % (action, prefix.replace('_', '-'))
258 260
        filename = getattr(self, prefix + '_csv_filename')
259 261
        return reverse(url_name, kwargs={
zoo/zoo_nanterre/views.py
116 116
    report = job.action.report
117 117
    if not report:
118 118
        raise Http404('no report')
119
    text_report = report
120
    if six.PY3:
121
        text_report = io.TextIOWrapper(text_report, encoding='utf-8')
122
    reader = csv.reader(text_report)
123
    next(reader)
124
    actions = [row for row in reader if row[6] != 'KEEP']
125
    context = dict(
126
        model_admin.admin_site.each_context(request),
127
        title=job.created,
128
        job=job,
129
        csv=actions,
130
        csv_url=job.action.download_report_url,
131
        csv_filesize=report.size,
132
    )
133
    return TemplateResponse(request, "admin/zoo_data/entity/synchronize_federations_report.html", context)
119
    with report:
120
        text_report = report
121
        if six.PY3:
122
            text_report = io.TextIOWrapper(text_report, encoding='utf-8')
123
        reader = csv.reader(text_report)
124
        next(reader)
125
        actions = [row for row in reader if row[6] != 'KEEP']
126
        context = dict(
127
            model_admin.admin_site.each_context(request),
128
            title=job.created,
129
            job=job,
130
            csv=actions,
131
            csv_url=job.action.download_report_url,
132
            csv_filesize=report.size,
133
        )
134
        return TemplateResponse(request, "admin/zoo_data/entity/synchronize_federations_report.html", context)
134 135

  
135 136

  
136 137
@permission_required('zoo_data.action1_entity')
......
148 149
    jobs = SynchronizeFederationsAction.get_jobs()
149 150
    job = get_object_or_404(jobs, id=job_id)
150 151
    report = job.action.apply_report
151
    if not report:
152
        raise Http404('no report')
153
    text_report = report
154
    if six.PY3:
155
        text_report = io.TextIOWrapper(text_report, encoding='utf-8')
156
    reader = csv.reader(text_report)
157
    next(reader)
158
    actions = [row for row in reader if row[6] != 'KEEP']
159
    context = dict(
160
        model_admin.admin_site.each_context(request),
161
        title=u'Application - %s' % job.created,
162
        job=job,
163
        csv=actions,
164
        csv_url=job.action.download_apply_report_url,
165
        csv_filesize=report.size,
166
    )
167
    return TemplateResponse(request, "admin/zoo_data/entity/synchronize_federations_report.html", context)
152
    with report:
153
        if not report:
154
            raise Http404('no report')
155
        text_report = report
156
        if six.PY3:
157
            text_report = io.TextIOWrapper(text_report, encoding='utf-8')
158
        reader = csv.reader(text_report)
159
        next(reader)
160
        actions = [row for row in reader if row[6] != 'KEEP']
161
        context = dict(
162
            model_admin.admin_site.each_context(request),
163
            title=u'Application - %s' % job.created,
164
            job=job,
165
            csv=actions,
166
            csv_url=job.action.download_apply_report_url,
167
            csv_filesize=report.size,
168
        )
169
        return TemplateResponse(request, "admin/zoo_data/entity/synchronize_federations_report.html", context)
168 170

  
169 171

  
170 172
@permission_required('zoo_data.action1_entity')
171
-