increase usage of 'with' statement

issue2047
review41002
This commit is contained in:
Tshepang Lekhonkhobe
2011-06-07 20:03:04 +02:00
parent 6b6cf99919
commit fa5a1aa4dc
5 changed files with 44 additions and 42 deletions
+21 -16
View File
@@ -2,6 +2,7 @@
#This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
from __future__ import with_statement
from setuptools import setup, find_packages
import os
import glob
@@ -212,9 +213,12 @@ if os.name == 'nt':
query_loaders = Popen(os.path.join(gtk_dir,'bin','gdk-pixbuf-query-loaders'),
stdout=PIPE).stdout.read()
query_loaders = query_loaders.replace(gtk_dir.replace(os.sep, '/') + '/', '')
loaders = open(os.path.join(dist_dir, 'etc', 'gtk-2.0', 'gdk-pixbuf.loaders'), 'w')
loaders.writelines([line + "\n" for line in query_loaders.split(os.linesep)])
loaders.close()
loaders_path = os.path.join(dist_dir, 'etc', 'gtk-2.0',
'gdk-pixbuf.loaders')
with open(loaders_path, 'w') as loaders:
loaders.writelines([line + "\n" for line in
query_loaders.split(os.linesep)])
if os.path.isdir(os.path.join(dist_dir, 'lib')):
shutil.rmtree(os.path.join(dist_dir, 'lib'))
@@ -277,14 +281,13 @@ elif os.name == 'mac' \
query_pango = Popen(os.path.join(gtk_dir, 'bin', 'pango-querymodules'),
stdout=PIPE).stdout.read()
query_pango = query_pango.replace(gtk_dir, '@executable_path/../Resources')
pango_modules = open(os.path.join(resources_dir, 'pango.modules'), 'w')
pango_modules.write(query_pango)
pango_modules.close()
pango_modules_path = os.path.join(resources_dir, 'pango.modules')
with open(pango_modules_path, 'w') as pango_modules:
pango_modules.write(query_pango)
pangorc = open(os.path.join(resources_dir, 'pangorc'), 'w')
pangorc.write('[Pango]\n')
pangorc.write('ModuleFiles=./pango.modules\n')
pangorc.close()
with open(os.path.join(resources_dir, 'pangorc'), 'w') as pangorc:
pangorc.write('[Pango]\n')
pangorc.write('ModuleFiles=./pango.modules\n')
if os.path.isdir(os.path.join(gtk_2_dist_dir, gtk_binary_version, 'loaders')):
shutil.rmtree(os.path.join(gtk_2_dist_dir, gtk_binary_version, 'loaders'))
@@ -299,9 +302,10 @@ elif os.name == 'mac' \
query_loaders = Popen(os.path.join(gtk_dir,'bin','gdk-pixbuf-query-loaders'),
stdout=PIPE).stdout.read()
query_loaders = query_loaders.replace(gtk_dir, '@executable_path/../Resources')
loaders = open(os.path.join(resources_dir, 'gdk-pixbuf.loaders'), 'w')
loaders.write(query_loaders)
loaders.close()
loaders_path = os.path.join(resources_dir, 'gdk-pixbuf.loaders')
with open(loaders_path, 'w') as loaders:
loaders.write(query_loaders)
if os.path.isdir(os.path.join(gtk_2_dist_dir, gtk_binary_version, 'immodules')):
shutil.rmtree(os.path.join(gtk_2_dist_dir, gtk_binary_version, 'immodules'))
@@ -311,9 +315,10 @@ elif os.name == 'mac' \
query_immodules = Popen(os.path.join(gtk_dir, 'bin', 'gtk-query-immodules-2.0'),
stdout=PIPE).stdout.read()
query_immodules = query_immodules.replace(gtk_dir, '@executable_path/../Resources')
immodules = open(os.path.join(resources_dir, 'gtk.immodules'), 'w')
immodules.write(query_immodules)
immodules.close()
immodules_path = os.path.join(resources_dir, 'gtk.immodules')
with open(immodules_path, 'w') as immodules:
immodules.write(query_immodules)
shutil.copy(os.path.join(gtk_dir, 'share', 'themes', 'Clearlooks',
'gtk-2.0', 'gtkrc'), os.path.join(resources_dir, 'gtkrc'))
+3 -3
View File
@@ -1,5 +1,6 @@
#This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
from __future__ import with_statement
import time
import datetime
import tryton.rpc as rpc
@@ -58,9 +59,8 @@ class Action(object):
fp_name = os.path.join(dtemp,
name.replace(os.sep, '_').replace(os.altsep or os.sep, '_') \
+ os.extsep + type)
file_d = open(fp_name, 'wb')
file_d.write(base64.decodestring(data))
file_d.close()
with open(fp_name, 'wb') as file_d:
file_d.write(base64.decodestring(data))
if email_print:
mailto(to=email.get('to'), cc=email.get('cc'),
subject=email.get('subject'), body=email.get('body'),
+13 -14
View File
@@ -1,5 +1,6 @@
#This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
from __future__ import with_statement
import os
from tryton.config import get_config_dir
@@ -15,23 +16,21 @@ class Fingerprints(dict):
def load(self):
if not os.path.isfile(KNOWN_HOSTS_PATH):
return
known_hosts = open(KNOWN_HOSTS_PATH)
for line in known_hosts.xreadlines():
line = line.strip()
try:
key, sha1 = line.split(' ')
host, port = key.rsplit(':', 1)
except ValueError:
continue
self[(host, port)] = sha1
known_hosts.close()
with open(KNOWN_HOSTS_PATH) as known_hosts:
for line in known_hosts.xreadlines():
line = line.strip()
try:
key, sha1 = line.split(' ')
host, port = key.rsplit(':', 1)
except ValueError:
continue
self[(host, port)] = sha1
def save(self):
lines = []
known_hosts = open(KNOWN_HOSTS_PATH, 'w')
known_hosts.writelines('%s:%s %s' % (host, port, sha1)
+ os.linesep for (host, port), sha1 in self.iteritems())
known_hosts.close()
with open(KNOWN_HOSTS_PATH, 'w') as known_hosts:
known_hosts.writelines('%s:%s %s' % (host, port, sha1)
+ os.linesep for (host, port), sha1 in self.iteritems())
def __setitem__(self, key, value):
assert isinstance(key, tuple)
+4 -6
View File
@@ -1634,9 +1634,8 @@ class Main(object):
dialog = DBRestore(self.window, filename=filename)
url, dbname, passwd, update = dialog.run(self.window)
if dbname:
file_p = open(filename, 'rb')
data_b64 = base64.encodestring(file_p.read())
file_p.close()
with open(filename, 'rb') as file_p:
data_b64 = base64.encodestring(file_p.read())
host, port = url.rsplit(':' , 1)
rpcprogress = common.RPCProgress('db_exec', (host, int(port),
'restore', dbname, passwd, data_b64, update), self.window)
@@ -1713,9 +1712,8 @@ class Main(object):
filename=dbname + '-' + time.strftime('%Y%m%d%H%M') + '.dump')
if filename:
file_ = open(filename, 'wb')
file_.write(dump)
file_.close()
with open(filename, 'wb') as file_:
file_.write(dump)
common.message(_("Database backuped successfully!"), \
parent=self.window)
else:
+3 -3
View File
@@ -3,6 +3,7 @@
"""
Inter-Process Communication
"""
from __future__ import with_statement
import tempfile
import os
import threading
@@ -157,9 +158,8 @@ class FileClient(IPCClient):
return False
tmpdir = open(self.filename, 'r').readline().strip()
_, tmpfile = tempfile.mkstemp(dir=tmpdir, text=True)
tmpfile = open(tmpfile, 'w')
print >> tmpfile, message
tmpfile.close()
with open(tmpfile, 'w') as tmpfile:
print >> tmpfile, message
return True