[apycotlib] modify AbstractFilteredFileChecker to use os.walk
authorPaul Tonelli <paul.tonelli@logilab.fr>
Mon, 07 Oct 2013 10:54:39 +0200
changeset 1426 18ad7618cdfe
parent 1425 c20cae42ba4e
child 1427 c9dd4b0dc364
[apycotlib] modify AbstractFilteredFileChecker to use os.walk os.path.walk (previously used) is deprecated in python 3. This requires to move the file checks in a separate method
_apycotlib/checkers/__init__.py
--- a/_apycotlib/checkers/__init__.py	Wed Aug 07 12:42:44 2013 +0200
+++ b/_apycotlib/checkers/__init__.py	Mon Oct 07 10:54:39 2013 +0200
@@ -4,7 +4,8 @@
 
 __docformat__ = "restructuredtext en"
 
-from os.path import walk, splitext, split, join
+from os import walk
+from os.path import splitext, split, join
 
 from logilab.common.textutils import splitstrip
 from logilab.common.proc import RESOURCE_LIMIT_EXCEPTION
@@ -76,8 +77,22 @@
             },
         }
 
+    def filename_filter(self, dirpath, dirnames, filenames):
+        """Prune unwanted directories from dirnames (in place) and remove
+        unwanted files from filenames (inplace). The dirpath argument is provided to
+        enable more complex dirname/filename matching.
+        """
+        for dirname in dirnames[:]:
+            if self.ignored and join(dirpath,dirname).endswith(tuple(self.ignored)):
+                dirnames.remove(dirname)
+        for filename in filenames[:]:
+            if not ((self.extensions is None or
+                     filename.endswith(tuple(self.extensions))) and not
+                     join(dirpath, filename).endswith(tuple(self.ignored))):
+                filenames.remove(filename)
+
     def __init__(self, writer, options=None, extensions=None):
-        BaseChecker.__init__(self, writer, options)
+        super(AbstractFilteredFileChecker, self).__init__(writer, options)
         self.extensions = extensions or self.checked_extensions
         if isinstance(self.extensions, basestring):
             self.extensions = (self.extensions,)
@@ -94,29 +109,21 @@
         """
         self.set_status(SUCCESS)
         self._nbanalyzed = 0
-        ignored = self.options.get('ignore')
-        def walk_handler(arg, directory, fnames):
-            """walk callback handler"""
-            full_path = [(filename, join(directory, filename)) for filename in fnames]
-            for fname, fpath in full_path:
-                for ign_pat in ignored:
-                    if ign_pat.endswith((fpath, fname)):
-                        fnames.remove(fname) # fnames need to be replace in place
-            for filename in fnames:
-                ext = splitext(filename)[1]
-                if self.extensions is None or ext in self.extensions:
-                    try:
-                        self.set_status(self.check_file(join(directory, filename)))
-                    except RESOURCE_LIMIT_EXCEPTION:
-                        raise
-                    except Exception, ex:
-                        self.writer.fatal(u"%s", ex, path=filename, tb=True)
-                        self.set_status(ERROR)
-                    self._nbanalyzed += 1
-
+        self.ignored = self.options.get('ignore')
         files_root = self.files_root(test)
         self.writer.raw('file root', files_root)
-        walk(self.files_root(test), walk_handler, files_root)
+        for dirpath, dirnames, filenames in walk(self.files_root(test)):
+            #inplace pruning of dirnames and filenames
+            self.filename_filter(dirpath, dirnames, filenames)
+            for filename in filenames:
+                try:
+                    self.set_status(self.check_file(join(dirpath, filename)))
+                except RESOURCE_LIMIT_EXCEPTION:
+                    raise
+                except Exception, ex:
+                    self.writer.fatal(u"%s", ex, path=filename, tb=True)
+                    self.set_status(ERROR)
+                self._nbanalyzed += 1
         self.writer.raw('total files analyzed', self._nbanalyzed)
         if self._nbanalyzed <= 0:
             self.set_status(NODATA)