Switch to context manager

It's considered better practice to use context manager
when writing files.

Change-Id: I9169292c7cc83d10a52368d7c7c01df55ec1c1c5
This commit is contained in:
Arie
2016-10-01 00:10:12 +03:00
committed by Arie Bregman
parent 4f7d1de85e
commit f75e67c73d
3 changed files with 49 additions and 45 deletions

View File

@@ -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 from don import models

View File

@@ -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.core.urlresolvers import reverse
from django import http
from horizon import tables
import time
from don import api from don import api
from don import tables as don_tables 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): class ArchiveView(tables.DataTableView):
@@ -24,26 +36,7 @@ def dbview(request):
data = api.get_collection(request, id) data = api.get_collection(request, id)
pwd = settings.ROOT_PATH pwd = settings.ROOT_PATH
JSON_FILE = pwd + '/don/ovs/don.json' JSON_FILE = pwd + '/don/ovs/don.json'
don = open(JSON_FILE, 'w') with open(JSON_FILE, 'w') as f:
don.write(str(data.data)) f.write(str(data.data))
don.close()
return http.HttpResponseRedirect( return http.HttpResponseRedirect(
reverse('horizon:don:ovs:view')) 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'))
'''

View File

@@ -90,24 +90,23 @@ def run_ping_command(cmd, comment=''):
universal_newlines=True).replace('\t', ' ') universal_newlines=True).replace('\t', ' ')
def report_file_open(report_file): def report_file_write(report_file):
f = open(report_file, 'w') with open(report_file, 'w') as f:
f.write('<html>\n') f.write('<html>\n')
f.write('<head>\n') f.write('<head>\n')
f.write( f.write(
'<script type="text/javascript" src="{{ STATIC_URL }}/don/CollapsibleLists.js"></script>\n') '<script type="text/javascript" src="{{ STATIC_URL }}/don/CollapsibleLists.js"></script>\n')
f.write( f.write(
'<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}/don/don.css">\n') '<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}/don/don.css">\n')
f.write('<title>DON: Analysis Results</title>\n') f.write('<title>DON: Analysis Results</title>\n')
f.write('</head>\n') f.write('</head>\n')
f.write('<body onload=CollapsibleLists.apply()>\n') f.write('<body onload=CollapsibleLists.apply()>\n')
return f
def report_file_close(file_handle): def report_file_close(report_file):
file_handle.write('</body>\n') with open(report_file, 'a') as f:
file_handle.write('</html>\n') f.write('</body>\n')
f.write('</html>\n')
def print_ping_result(cmds, overall_result, info, comment=None): def print_ping_result(cmds, overall_result, info, comment=None):
@@ -364,12 +363,13 @@ def analyze(json_filename, params):
test_suite[test]['html'] = lines test_suite[test]['html'] = lines
debug(params['test:report_file']) 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(): for test in test_suite.keys():
if test_suite[test]['html']: if test_suite[test]['html']:
for line in test_suite[test]['html']: for line in test_suite[test]['html']:
f.write(line) with open(params['test:report_file'], 'a') as f:
report_file_close(f) f.write(line)
report_file_close(params['test:report_file'])
os.chdir(CUR_DIR) os.chdir(CUR_DIR)