From: "Brian Dolbec" <brian.dolbec@gmail.com> To: gentoo-commits@lists.gentoo.org Subject: [gentoo-commits] proj/layman:gsoc2014 commit in: layman/overlays/, layman/, layman/tests/ Date: Mon, 16 Jun 2014 03:37:30 +0000 (UTC) [thread overview] Message-ID: <1402792601.b47238ef6ef407c7c07a94b885b7243a089c797f.dol-sen@gentoo> (raw) commit: b47238ef6ef407c7c07a94b885b7243a089c797f Author: Devan Franchini <twitch153 <AT> gentoo <DOT> org> AuthorDate: Thu Jun 12 21:17:02 2014 +0000 Commit: Brian Dolbec <brian.dolbec <AT> gmail <DOT> com> CommitDate: Sun Jun 15 00:36:41 2014 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/layman.git;a=commit;h=b47238ef Implements the "with" syntax to open files --- layman/dbbase.py | 10 +++++----- layman/makeconf.py | 16 +++++----------- layman/overlays/tar.py | 9 +++++---- layman/remotedb.py | 14 ++++++-------- layman/reposconf.py | 6 ++++-- layman/tests/external.py | 5 ++--- 6 files changed, 27 insertions(+), 33 deletions(-) diff --git a/layman/dbbase.py b/layman/dbbase.py index 92d5cf9..e17e8f3 100644 --- a/layman/dbbase.py +++ b/layman/dbbase.py @@ -131,8 +131,8 @@ class DbBase(object): '''Read the overlay definition file.''' try: - df = fileopen(path, 'r') - document = df.read() + with fileopen(path, 'r') as df: + document = df.read() except Exception as error: if not self.ignore_init_read_errors: @@ -235,9 +235,9 @@ class DbBase(object): indent(tree) tree = ET.ElementTree(tree) try: - f = fileopen(path, 'w') - tree.write(f, encoding=_UNICODE) - f.close() + with fileopen(path, 'w') as f: + tree.write(f, encoding=_UNICODE) + except Exception as error: raise Exception('Failed to write to local overlays file: ' + path + '\nError was:\n' + str(error)) diff --git a/layman/makeconf.py b/layman/makeconf.py index f1eba09..15ad537 100644 --- a/layman/makeconf.py +++ b/layman/makeconf.py @@ -22,7 +22,7 @@ import codecs import re from layman.utils import path -from layman.compatibility import cmp_to_key +from layman.compatibility import cmp_to_key, fileopen #=============================================================================== # @@ -276,11 +276,8 @@ class ConfigHandler: return False try: - make_conf = codecs.open(self.path, 'w', 'utf-8') - - make_conf.write(content) - - make_conf.close() + with fileopen(self.path, 'w') as make_conf: + make_conf.write(content) except Exception as error: self.output.error('MakeConf: ConfigHandler.write(); Failed to write "'\ @@ -293,11 +290,8 @@ class ConfigHandler: Returns the content of the /var/lib/layman/make.conf file. ''' try: - make_conf = codecs.open(self.path, 'r', 'utf-8') - - self.data = make_conf.read() - - make_conf.close() + with fileopen(self.path, 'r') as make_conf: + self.data = make_conf.read() except Exception as error: self.output.error('ConfigHandler: content(); Failed to read "'\ diff --git a/layman/overlays/tar.py b/layman/overlays/tar.py index acbeece..fc15c56 100644 --- a/layman/overlays/tar.py +++ b/layman/overlays/tar.py @@ -34,8 +34,9 @@ import tempfile import xml.etree.ElementTree as ET # Python 2.5 -from layman.utils import path +from layman.compatibility import fileopen from layman.overlays.source import OverlaySource, require_supported +from layman.utils import path from layman.version import VERSION from sslfetch.connections import Connector @@ -120,9 +121,9 @@ class TarOverlay(OverlaySource): pkg = path([base, self.parent.name + ext]) try: - out_file = open(pkg, 'w+b') - out_file.write(tar) - out_file.close() + with fileopen(pkg, 'w+b') as out_file: + out_file.write(tar) + except Exception as error: raise Exception('Failed to store tar package in ' + pkg + '\nError was:' + str(error)) diff --git a/layman/remotedb.py b/layman/remotedb.py index f883799..79f4ec6 100644 --- a/layman/remotedb.py +++ b/layman/remotedb.py @@ -17,7 +17,6 @@ '''Handles different storage files.''' from __future__ import unicode_literals -from __future__ import with_statement __version__ = "$Id: db.py 309 2007-04-09 16:23:38Z wrobel $" @@ -261,8 +260,9 @@ class RemoteDB(DbBase): if url_timestamp != timestamp: self.output.debug('RemoteDB._fetch_file() opening file', 2) # Fetch the remote list - with open(filepath) as connection: + with fileopen(filepath) as connection: olist = connection.read() + else: self.output.info('Remote list already up to date: %s' % url, 4) @@ -324,14 +324,12 @@ class RemoteDB(DbBase): def write_cache(olist, mpath, tpath=None, timestamp=None): has_updates = False try: - out_file = fileopen(mpath, 'w') - out_file.write(olist) - out_file.close() + with fileopen(mpath, 'w') as out_file: + out_file.write(olist) if timestamp is not None and tpath is not None: - out_file = fileopen(tpath, 'w') - out_file.write(str(timestamp)) - out_file.close() + with fileopen(tpath, 'w') as out_file: + out_file.write(str(timestamp)) has_updates = True diff --git a/layman/reposconf.py b/layman/reposconf.py index a7a0166..c550a13 100644 --- a/layman/reposconf.py +++ b/layman/reposconf.py @@ -24,7 +24,8 @@ except: # Import for Python2 import ConfigParser -from layman.utils import path +from layman.compatibility import fileopen +from layman.utils import path class ConfigHandler: @@ -117,8 +118,9 @@ class ConfigHandler: @return boolean: represents a successful write. ''' try: - with open(self.path, 'w') as laymanconf: + with fileopen(self.path, 'w') as laymanconf: self.repo_conf.write(laymanconf) + return True except IOError as error: self.output.error('ReposConf: ConfigHandler.write(); Failed to write "'\ diff --git a/layman/tests/external.py b/layman/tests/external.py index 3505eeb..82825e2 100755 --- a/layman/tests/external.py +++ b/layman/tests/external.py @@ -99,9 +99,8 @@ class TarAddRemoveSync(unittest.TestCase): """ % { 'temp_tarball_url':urllib.pathname2url(temp_tarball_path), 'repo_name':repo_name} (fd, temp_collection_path) = tempfile.mkstemp() - f = os.fdopen(fd, 'w') - f.write(xml_text) - f.close() + with os.fdopen(fd, 'w') as f: + f.write(xml_text) # Make playground directory temp_dir_path = tempfile.mkdtemp()
WARNING: multiple messages have this Message-ID (diff)
From: "Brian Dolbec" <brian.dolbec@gmail.com> To: gentoo-commits@lists.gentoo.org Subject: [gentoo-commits] proj/layman:master commit in: layman/overlays/, layman/, layman/tests/ Date: Mon, 16 Jun 2014 03:40:19 +0000 (UTC) [thread overview] Message-ID: <1402792601.b47238ef6ef407c7c07a94b885b7243a089c797f.dol-sen@gentoo> (raw) Message-ID: <20140616034019.rH2ODvglvBljKF7k_uKCfw6bY_P-yvPZixT4nf-RMbE@z> (raw) commit: b47238ef6ef407c7c07a94b885b7243a089c797f Author: Devan Franchini <twitch153 <AT> gentoo <DOT> org> AuthorDate: Thu Jun 12 21:17:02 2014 +0000 Commit: Brian Dolbec <brian.dolbec <AT> gmail <DOT> com> CommitDate: Sun Jun 15 00:36:41 2014 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/layman.git;a=commit;h=b47238ef Implements the "with" syntax to open files --- layman/dbbase.py | 10 +++++----- layman/makeconf.py | 16 +++++----------- layman/overlays/tar.py | 9 +++++---- layman/remotedb.py | 14 ++++++-------- layman/reposconf.py | 6 ++++-- layman/tests/external.py | 5 ++--- 6 files changed, 27 insertions(+), 33 deletions(-) diff --git a/layman/dbbase.py b/layman/dbbase.py index 92d5cf9..e17e8f3 100644 --- a/layman/dbbase.py +++ b/layman/dbbase.py @@ -131,8 +131,8 @@ class DbBase(object): '''Read the overlay definition file.''' try: - df = fileopen(path, 'r') - document = df.read() + with fileopen(path, 'r') as df: + document = df.read() except Exception as error: if not self.ignore_init_read_errors: @@ -235,9 +235,9 @@ class DbBase(object): indent(tree) tree = ET.ElementTree(tree) try: - f = fileopen(path, 'w') - tree.write(f, encoding=_UNICODE) - f.close() + with fileopen(path, 'w') as f: + tree.write(f, encoding=_UNICODE) + except Exception as error: raise Exception('Failed to write to local overlays file: ' + path + '\nError was:\n' + str(error)) diff --git a/layman/makeconf.py b/layman/makeconf.py index f1eba09..15ad537 100644 --- a/layman/makeconf.py +++ b/layman/makeconf.py @@ -22,7 +22,7 @@ import codecs import re from layman.utils import path -from layman.compatibility import cmp_to_key +from layman.compatibility import cmp_to_key, fileopen #=============================================================================== # @@ -276,11 +276,8 @@ class ConfigHandler: return False try: - make_conf = codecs.open(self.path, 'w', 'utf-8') - - make_conf.write(content) - - make_conf.close() + with fileopen(self.path, 'w') as make_conf: + make_conf.write(content) except Exception as error: self.output.error('MakeConf: ConfigHandler.write(); Failed to write "'\ @@ -293,11 +290,8 @@ class ConfigHandler: Returns the content of the /var/lib/layman/make.conf file. ''' try: - make_conf = codecs.open(self.path, 'r', 'utf-8') - - self.data = make_conf.read() - - make_conf.close() + with fileopen(self.path, 'r') as make_conf: + self.data = make_conf.read() except Exception as error: self.output.error('ConfigHandler: content(); Failed to read "'\ diff --git a/layman/overlays/tar.py b/layman/overlays/tar.py index acbeece..fc15c56 100644 --- a/layman/overlays/tar.py +++ b/layman/overlays/tar.py @@ -34,8 +34,9 @@ import tempfile import xml.etree.ElementTree as ET # Python 2.5 -from layman.utils import path +from layman.compatibility import fileopen from layman.overlays.source import OverlaySource, require_supported +from layman.utils import path from layman.version import VERSION from sslfetch.connections import Connector @@ -120,9 +121,9 @@ class TarOverlay(OverlaySource): pkg = path([base, self.parent.name + ext]) try: - out_file = open(pkg, 'w+b') - out_file.write(tar) - out_file.close() + with fileopen(pkg, 'w+b') as out_file: + out_file.write(tar) + except Exception as error: raise Exception('Failed to store tar package in ' + pkg + '\nError was:' + str(error)) diff --git a/layman/remotedb.py b/layman/remotedb.py index f883799..79f4ec6 100644 --- a/layman/remotedb.py +++ b/layman/remotedb.py @@ -17,7 +17,6 @@ '''Handles different storage files.''' from __future__ import unicode_literals -from __future__ import with_statement __version__ = "$Id: db.py 309 2007-04-09 16:23:38Z wrobel $" @@ -261,8 +260,9 @@ class RemoteDB(DbBase): if url_timestamp != timestamp: self.output.debug('RemoteDB._fetch_file() opening file', 2) # Fetch the remote list - with open(filepath) as connection: + with fileopen(filepath) as connection: olist = connection.read() + else: self.output.info('Remote list already up to date: %s' % url, 4) @@ -324,14 +324,12 @@ class RemoteDB(DbBase): def write_cache(olist, mpath, tpath=None, timestamp=None): has_updates = False try: - out_file = fileopen(mpath, 'w') - out_file.write(olist) - out_file.close() + with fileopen(mpath, 'w') as out_file: + out_file.write(olist) if timestamp is not None and tpath is not None: - out_file = fileopen(tpath, 'w') - out_file.write(str(timestamp)) - out_file.close() + with fileopen(tpath, 'w') as out_file: + out_file.write(str(timestamp)) has_updates = True diff --git a/layman/reposconf.py b/layman/reposconf.py index a7a0166..c550a13 100644 --- a/layman/reposconf.py +++ b/layman/reposconf.py @@ -24,7 +24,8 @@ except: # Import for Python2 import ConfigParser -from layman.utils import path +from layman.compatibility import fileopen +from layman.utils import path class ConfigHandler: @@ -117,8 +118,9 @@ class ConfigHandler: @return boolean: represents a successful write. ''' try: - with open(self.path, 'w') as laymanconf: + with fileopen(self.path, 'w') as laymanconf: self.repo_conf.write(laymanconf) + return True except IOError as error: self.output.error('ReposConf: ConfigHandler.write(); Failed to write "'\ diff --git a/layman/tests/external.py b/layman/tests/external.py index 3505eeb..82825e2 100755 --- a/layman/tests/external.py +++ b/layman/tests/external.py @@ -99,9 +99,8 @@ class TarAddRemoveSync(unittest.TestCase): """ % { 'temp_tarball_url':urllib.pathname2url(temp_tarball_path), 'repo_name':repo_name} (fd, temp_collection_path) = tempfile.mkstemp() - f = os.fdopen(fd, 'w') - f.write(xml_text) - f.close() + with os.fdopen(fd, 'w') as f: + f.write(xml_text) # Make playground directory temp_dir_path = tempfile.mkdtemp()
next reply other threads:[~2014-06-16 3:37 UTC|newest] Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top 2014-06-16 3:37 Brian Dolbec [this message] 2014-06-16 3:40 ` [gentoo-commits] proj/layman:master commit in: layman/overlays/, layman/, layman/tests/ Brian Dolbec
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=1402792601.b47238ef6ef407c7c07a94b885b7243a089c797f.dol-sen@gentoo \ --to=brian.dolbec@gmail.com \ --cc=gentoo-commits@lists.gentoo.org \ --cc=gentoo-dev@lists.gentoo.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox