Commits

clach04  committed 63c7b56

Start of path recursive file walking.

  • Participants
  • Parent commits f8857c5

Comments (0)

Files changed (2)

                 raise StopIteration
 
 
+###############################################################
+
+def path_walker(path_to_search, filename_filter=None, abspath=False):
+    """Walk directory of files, directory depth first, returns generator
+    """
+    if abspath:
+        path_to_search = os.path.abspath(path_to_search)
+    def always_true(*args, **kwargs):
+        # Use a function rather than Lamda
+        return True
+    
+    if filename_filter is None:
+        filename_filter = always_true
+    ## Requires os.walk (python 2.3 and later).
+    ## Pure Python versions for earlier versions available from:
+    ##  http://osdir.com/ml/lang.jython.user/2006-04/msg00032.html
+    ## but lacks "topdown" support, walk class later
+    for dirpath, dirnames, filenames in os.walk(path_to_search, topdown=False):
+        filenames.sort()
+        for temp_filename in filenames:
+            if filename_filter(temp_filename):
+                temp_filename = os.path.join(dirpath, temp_filename)
+                yield temp_filename
+
+###############################################################
+
 def get_file_listings(path_of_files, recursive=False, include_size=False, return_list=True):
     """return_list=True, if False returns dict
     """

File test_sksync.py

         file_list = sksync.get_file_listings(self.test_dir, recursive=False, include_size=True, return_list=True)
         canon = [('test3.txt', 1345316010000L, 1L), ('test1.txt', 1345316082000L, 1L), ('test2.txt', 1345316070000L, 1L)]
         self.assertEqual(canon, file_list)
-
-    def test_recursive_dir(self):
+    
+    def test_recursive_dir1(self):
+        file_list = list(sksync.path_walker(self.test_dir))
+        canon = [os.path.join(self.test_dir, 'subdir1', 'test1.txt'),
+                os.path.join(self.test_dir, 'subdir1', 'test2.txt'),
+                os.path.join(self.test_dir, 'subdir1', 'test3.txt'),
+                os.path.join(self.test_dir, 'test1.txt'),
+                os.path.join(self.test_dir, 'test2.txt'),
+                os.path.join(self.test_dir, 'test3.txt'),
+                ]
+        self.assertEqual(canon, file_list)
+    
+    def test_recursive_dir2(self):
         file_list = sksync.get_file_listings(self.test_dir, recursive=True, include_size=True, return_list=True)
-        canon = []  # TODO
+        canon = [os.path.join(self.test_dir, 'subdir1', 'test1.txt'),
+                os.path.join(self.test_dir, 'subdir1', 'test2.txt'),
+                os.path.join(self.test_dir, 'subdir1', 'test3.txt'),
+                os.path.join(self.test_dir, 'test1.txt'),
+                os.path.join(self.test_dir, 'test2.txt'),
+                os.path.join(self.test_dir, 'test3.txt'),
+                ]
         self.assertEqual(canon, file_list)