From f75e67c73df2fdb22e256d10ca9acd38560b36da Mon Sep 17 00:00:00 2001 From: Arie Date: Sat, 1 Oct 2016 00:10:12 +0300 Subject: [PATCH] Switch to context manager It's considered better practice to use context manager when writing files. Change-Id: I9169292c7cc83d10a52368d7c7c01df55ec1c1c5 --- openstack_dashboard/don/api.py | 11 ++++++ openstack_dashboard/don/archive/views.py | 45 ++++++++++-------------- openstack_dashboard/don/ovs/analyzer.py | 38 ++++++++++---------- 3 files changed, 49 insertions(+), 45 deletions(-) diff --git a/openstack_dashboard/don/api.py b/openstack_dashboard/don/api.py index 8503965..0f27985 100644 --- a/openstack_dashboard/don/api.py +++ b/openstack_dashboard/don/api.py @@ -1,3 +1,14 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. from don import models diff --git a/openstack_dashboard/don/archive/views.py b/openstack_dashboard/don/archive/views.py index bab02aa..62cbd5b 100644 --- a/openstack_dashboard/don/archive/views.py +++ b/openstack_dashboard/don/archive/views.py @@ -1,11 +1,23 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from django.conf import settings from django.core.urlresolvers import reverse +from django import http +from horizon import tables +import time + from don import api from don import tables as don_tables -from horizon import tables -# from horizon.views import APIView -import time -from django.conf import settings -from django import http class ArchiveView(tables.DataTableView): @@ -24,26 +36,7 @@ def dbview(request): data = api.get_collection(request, id) pwd = settings.ROOT_PATH JSON_FILE = pwd + '/don/ovs/don.json' - don = open(JSON_FILE, 'w') - don.write(str(data.data)) - don.close() + with open(JSON_FILE, 'w') as f: + f.write(str(data.data)) return http.HttpResponseRedirect( reverse('horizon:don:ovs:view')) - -''' -class DBView(APIView): - template_name = 'don/archive/view.html' - - def get_data(self,request, context, *args, **kwargs): - id = self.request.GET.get('id') - data = api.get_collection(self.request,id) - pwd = settings.ROOT_PATH - JSON_FILE = pwd + '/don/ovs/don.json' - don = open(JSON_FILE,'w') - don.write(str(data.data)) - don.close() - time.sleep(2) - return http.HttpResponseRedirect( - reverse('horizon:don:ovs:view')) - -''' diff --git a/openstack_dashboard/don/ovs/analyzer.py b/openstack_dashboard/don/ovs/analyzer.py index 5daa186..1971895 100644 --- a/openstack_dashboard/don/ovs/analyzer.py +++ b/openstack_dashboard/don/ovs/analyzer.py @@ -90,24 +90,23 @@ def run_ping_command(cmd, comment=''): universal_newlines=True).replace('\t', ' ') -def report_file_open(report_file): - f = open(report_file, 'w') - f.write('\n') - f.write('\n') - f.write( - '\n') - f.write( - '\n') - f.write('DON: Analysis Results\n') - f.write('\n') - f.write('\n') - - return f +def report_file_write(report_file): + with open(report_file, 'w') as f: + f.write('\n') + f.write('\n') + f.write( + '\n') + f.write( + '\n') + f.write('DON: Analysis Results\n') + f.write('\n') + f.write('\n') -def report_file_close(file_handle): - file_handle.write('\n') - file_handle.write('\n') +def report_file_close(report_file): + with open(report_file, 'a') as f: + f.write('\n') + f.write('\n') def print_ping_result(cmds, overall_result, info, comment=None): @@ -364,12 +363,13 @@ def analyze(json_filename, params): test_suite[test]['html'] = lines debug(params['test:report_file']) - f = report_file_open(params['test:report_file']) + report_file_write(params['test:report_file']) for test in test_suite.keys(): if test_suite[test]['html']: for line in test_suite[test]['html']: - f.write(line) - report_file_close(f) + with open(params['test:report_file'], 'a') as f: + f.write(line) + report_file_close(params['test:report_file']) os.chdir(CUR_DIR)