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()

Reply via email to