[meld] Clean up _vc.listdir slightly



commit 3ce093b9271df8661b198ecaa7657eaef41acd70
Author: Kai Willadsen <kai willadsen gmail com>
Date:   Sun May 19 15:32:33 2013 +1000

    Clean up _vc.listdir slightly

 meld/vc/_vc.py |   30 +++++++++++++-----------------
 1 files changed, 13 insertions(+), 17 deletions(-)
---
diff --git a/meld/vc/_vc.py b/meld/vc/_vc.py
index f52447b..f02d169 100644
--- a/meld/vc/_vc.py
+++ b/meld/vc/_vc.py
@@ -22,6 +22,7 @@
 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
+import itertools
 import os
 import re
 import subprocess
@@ -49,6 +50,13 @@ assert len(conflicts) == CONFLICT_MAX
 DATA_NAME, DATA_STATE, DATA_REVISION, DATA_OPTIONS = list(range(4))
 
 
+# Lifted from the itertools recipes section
+def partition(pred, iterable):
+    t1, t2 = itertools.tee(iterable)
+    return (list(itertools.ifilterfalse(pred, t1)),
+            list(itertools.ifilter(pred, t2)))
+
+
 class Entry(object):
     # These are the possible states of files. Be sure to get the colons correct.
     states = _("Ignored:Unversioned:::Error::Newly added:Modified:Conflict:Removed:Missing:Not 
present").split(":")
@@ -217,30 +225,18 @@ class Vc(object):
         regex = re.compile(self.PATCH_INDEX_RE, re.M)
         return [f.strip() for f in regex.findall(patch)]
 
-    def listdir_filter(self, entries):
-        return [f for f in entries if f != self.VC_DIR]
-
     # Determine if a directory is a valid git/svn/hg/cvs/etc repository
     def valid_repo(self):
         return True
 
-    def listdir(self, start):
-        start = start or "."
-        cfiles = []
-        cdirs = []
+    def listdir(self, path="."):
         try:
-            entries = os.listdir(start)
-            entries.sort()
+            entries = sorted(e for e in os.listdir(path) if e != self.VC_DIR)
         except OSError:
             entries = []
-        for f in self.listdir_filter(entries):
-            fname = os.path.join(start, f)
-            lname = fname
-            if os.path.isdir(fname):
-                cdirs.append((f, lname))
-            else:
-                cfiles.append((f, lname))
-        dirs, files = self.lookup_files(cdirs, cfiles, start)
+        full_entries = [(f, os.path.join(path, f)) for f in entries]
+        cfiles, cdirs = partition(lambda e: os.path.isdir(e[1]), full_entries)
+        dirs, files = self.lookup_files(cdirs, cfiles, path)
         return dirs + files
 
     def lookup_files(self, dirs, files, directory=None):


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