[alacarte] Use with statements for management



commit 10f611c074ae7b2ab0ec48ff3b8946c5f8e19970
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Fri Nov 16 23:26:14 2012 -0500

    Use with statements for management

 Alacarte/MainWindow.py |    5 ++---
 Alacarte/MenuEditor.py |   23 +++++++++++------------
 2 files changed, 13 insertions(+), 15 deletions(-)
---
diff --git a/Alacarte/MainWindow.py b/Alacarte/MainWindow.py
index ffd3dcc..df602cf 100644
--- a/Alacarte/MainWindow.py
+++ b/Alacarte/MainWindow.py
@@ -320,9 +320,8 @@ class MainWindow(object):
 
         if not os.path.isfile(file_path):
             data = open(item.get_desktop_file_path()).read()
-            fd = codecs.open(file_path, 'w', 'utf8')
-            fd.write(data)
-            fd.close()
+            with codecs.open(file_path, 'w', 'utf8') as f:
+                f.write(data)
 
         if file_path not in self.edit_pool:
             self.edit_pool.append(file_path)
diff --git a/Alacarte/MenuEditor.py b/Alacarte/MenuEditor.py
index 8f4fbaf..1d39f95 100644
--- a/Alacarte/MenuEditor.py
+++ b/Alacarte/MenuEditor.py
@@ -49,9 +49,8 @@ class MenuEditor(object):
         self.load()
 
     def save(self):
-        fd = codecs.open(self.path, 'w', 'utf8')
-        fd.write(self.dom.toprettyxml())
-        fd.close()
+        with codecs.open(self.path, 'w', 'utf8') as f:
+            f.write(self.dom.toprettyxml())
 
     def restoreToSystem(self):
         self.restoreTree(self.tree.get_root_directory())
@@ -260,9 +259,8 @@ class MenuEditor(object):
 
         contents, length = keyfile.to_data()
 
-        f = codecs.open(out_path, 'w', 'utf8')
-        f.write(contents)
-        f.close()
+        with codecs.open(out_path, 'w', 'utf8') as f:
+            f.write(contents)
 
         self.addItem(new_parent, file_id, dom)
         self.positionItem(new_parent, ('Item', file_id), before, after)
@@ -401,9 +399,10 @@ class MenuEditor(object):
 
         contents, length = keyfile.to_data()
 
-        f = codecs.open(os.path.join(util.getUserItemPath(), file_id), 'w', 'utf8')
-        f.write(contents)
-        f.close()
+        path = os.path.join(util.getUserItemPath(), file_id)
+        with codecs.open(path, 'w', 'utf8') as f:
+            f.write(contents)
+
         return file_id
 
     def writeMenu(self, menu, **kwargs):
@@ -422,9 +421,9 @@ class MenuEditor(object):
 
         contents, length = keyfile.to_data()
 
-        f = codecs.open(os.path.join(util.getUserDirectoryPath(), file_id), 'w', 'utf8')
-        f.write(contents)
-        f.close()
+        path = os.path.join(util.getUserDirectoryPath(), file_id)
+        with codecs.open(path, 'w', 'utf8') as f:
+            f.write(contents)
         return file_id
 
     def getXmlNodesByName(self, name, element):



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]