Change the directory walker to a more sane implementation
authorIra W. Snyder <devel@irasnyder.com>
Thu, 11 Jan 2007 00:45:47 +0000 (16:45 -0800)
committerIra W. Snyder <devel@irasnyder.com>
Thu, 11 Jan 2007 00:45:47 +0000 (16:45 -0800)
The directory walker was a very complex piece of code where none was
required. This changes it to be much simpler.

Signed-off-by: Ira W. Snyder <devel@irasnyder.com>
animesorter2.py

index 24aa755..04073be 100755 (executable)
@@ -49,6 +49,7 @@ class AnimeSorter2:
 
     def __init__(self, options):
         self.options = options
+        self.dict = None
 
     def __valid_dict_line (self, line):
         if len(line) <= 0:
@@ -124,17 +125,6 @@ class AnimeSorter2:
 
         return tuple (result)
 
-    def get_matches(self, files, pattern):
-        """get_matches(files, pattern):
-
-        files is type LIST
-        pattern is type sre.SRE_Pattern
-
-        Returns a list of the files matching the pattern as type sre.SRE_Match."""
-
-        matches = [m for m in files if pattern.search(m)]
-        return matches
-
     def as_makedirs (self, dirname):
         """Call os.makedirs(dirname), but check first whether we are in pretend
            mode, or if we're running interactively."""
@@ -183,9 +173,9 @@ class AnimeSorter2:
 
         ret = 0
 
-        ## Check for a non-default directory
-        if todir[0] != '/':
-            todir = os.path.join(self.options.output_dir, todir)
+        # Leave immediately if we have nothing to do
+        if len(files) <= 0:
+            return ret
 
         ## Create the directory if it doesn't exist
         ret = self.as_makedirs (todir)
@@ -204,19 +194,11 @@ class AnimeSorter2:
 
         return ret
 
-    def __dir_walker(self, dict, root, dirs, files):
-
-        ## Get all of the files in the directory that are of the correct types
-        types_re = re.compile(TYPES_REGEX, re.IGNORECASE)
-        raw_matches = [f for f in files if types_re.match(f)]
-
-        ### Loop through the dictionary and try to move everything that matches
-        for regex, todir in dict:
-            matches = self.get_matches(raw_matches, regex)
+    def __dir_walker(self, rootdir, files):
 
-            ## Move the files if we've found some
-            if len(matches) > 0:
-                self.move_files(matches, root, todir)
+        for (r,d) in self.dict:
+            matches = [f for f in files if r.match(f)]
+            self.move_files (matches, rootdir, d)
 
     def get_user_choice(self, prompt):
 
@@ -247,16 +229,15 @@ class AnimeSorter2:
         logging.info ('')
 
         ## Parse the dictionary
-        dict = self.parse_dict()
+        self.dict = self.parse_dict()
 
         if self.options.recursive:
             ## Start walking through directories
             for root, dirs, files in os.walk(self.options.start_dir):
-                self.__dir_walker(dict, root, dirs, files)
+                self.__dir_walker(root, files)
         else:
-            self.__dir_walker(dict, self.options.start_dir,
-                    [d for d in os.listdir(self.options.start_dir) if os.path.isdir(d)],
-                    [f for f in os.listdir(self.options.start_dir) if os.path.isfile(f)])
+            self.__dir_walker(self.options.start_dir, [f for f in
+                    os.listdir(self.options.start_dir) if os.path.isfile(f)])
 
 
 ### MAIN IS HERE ###