Use exceptions for error handling
[rarslave2.git] / PAR2Set / Join.py
index c2b055f..1dd680a 100644 (file)
 #!/usr/bin/env python
 # vim: set ts=4 sts=4 sw=4 textwidth=92:
 
 #!/usr/bin/env python
 # vim: set ts=4 sts=4 sw=4 textwidth=92:
 
-import logging
-import PAR2Set.Base
-from RarslaveCommon import *
+"""
+Holds the Join class.
+
+This module works with normal joined sets.
+
+It will detect sets like the following:
+X.par2
+X.vol0+1.par2
+...
+
+X.001
+X.002
+...
 
 
+Where the PAR2 files protect a file named X.avi, or similar other types.
+It will not work where the PAR2 files are protecting the X.001, etc files
+directly.
+"""
+
+__author__    = "Ira W. Snyder (devel@irasnyder.com)"
+__copyright__ = "Copyright (c) 2006,2007 Ira W. Snyder (devel@irasnyder.com)"
+__license__   = "GNU GPL v2 (or, at your option, any later version)"
+
+#    Join.py
 #
 #
-# This is a regular joined file type
+#    Copyright (C) 2006,2007  Ira W. Snyder (devel@irasnyder.com)
 #
 #
-# It will detect sets like the following:
-# X.par2
-# X.vol0+1.par2
-# X.vol1+2.par2
-# X.001
-# X.002
-# X.003
+#    This program is free software; you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation; either version 2 of the License, or
+#    (at your option) any later version.
 #
 #
-# Where the PAR2 files protect a file named X.avi (or similar). It will not
-# work where the PAR2 files are protecting the .001, etc files directly.
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
 #
 #
+#    You should have received a copy of the GNU General Public License
+#    along with this program; if not, write to the Free Software
+#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+import re
+import os
+import logging
+import PAR2Set.Base
+import rsutil.common
+
 
 def detector (name_files, prot_files):
 
 def detector (name_files, prot_files):
-       return has_a_match ('^.*\.\d\d\d$', name_files) \
-                       and not has_a_match ('^.*\.\d\d\d$', prot_files)
+       """Detects a Join set"""
+
+       return rsutil.common.has_a_match ('^.*\.\d\d\d$', name_files) \
+                       and not rsutil.common.has_a_match ('^.*\.\d\d\d$', prot_files)
 
 
 class Join (PAR2Set.Base.Base):
 
 
 
 class Join (PAR2Set.Base.Base):
 
+       """Class for normal joined-file sets"""
+
        def __repr__ (self):
                return 'JOIN'
 
        def find_joinfiles (self):
        def __repr__ (self):
                return 'JOIN'
 
        def find_joinfiles (self):
-               return find_matches ('^.*\.\d\d\d$', self.name_matched_files)
+               """Finds files which contain data to be joined together"""
 
 
-       def runVerifyAndRepair (self):
-               PAR2_CMD = config_get_value ('commands', 'par2repair')
+               return rsutil.common.find_matches ('^.*\.\d\d\d$', self.name_matched_files)
 
 
-               # assemble the command
-               # par2repair -- PAR2 PAR2_EXTRA [JOIN_FILES]
-               command = "%s \"%s\" " % (PAR2_CMD, self.p2file)
+       def runVerifyAndRepair (self):
+               """Verify and Repair a PAR2Set. This version extends the PAR2Set.Base.Base
+                  version by adding the datafiles to be joined at the end of the command
+                  line.
 
 
-               for f in self.all_p2files:
-                       if f != self.p2file:
-                               command += "\"%s\" " % os.path.split (f)[1]
+                  This is done using the par2repair command by default"""
 
 
-               for f in self.find_joinfiles ():
-                       command += "\"%s\" " % os.path.split (f)[1]
+               rsutil.common.run_command(['par2repair'] + self.all_p2files + self.find_joinfiles(), self.dir)
 
 
-               # run the command
-               ret = run_command (command, self.dir)
+       def find_deleteable_files (self):
+               """Find all files which are deletable by using the regular expression from the
+                  configuration file"""
 
 
-               # check the result
-               if ret != 0:
-                       logging.critical ('PAR2 Check / Repair failed: %s' % self.p2file)
-                       return -ECHECK
+               DELETE_REGEX = rsutil.common.config_get_value ('regular expressions', 'delete_regex')
+               dregex = re.compile (DELETE_REGEX, re.IGNORECASE)
 
 
-               return SUCCESS
+               return [f for f in self.all_files if dregex.match (f) and \
+                               f not in self.prot_matched_files]
 
        def find_extraction_heads (self):
 
        def find_extraction_heads (self):
+               """Find the extraction heads. Since this should not be an extractable set,
+                  we return the files which are protected directly by the PAR2 files."""
+
                return self.prot_matched_files
 
        def extraction_function (self, file, todir):
                return self.prot_matched_files
 
        def extraction_function (self, file, todir):
-               """Extract a single file of this type to the given directory"""
-
-               NOEXTRACT_CMD = config_get_value ('commands', 'noextract')
-
-               # Make sure that both files are not the same file. If they are, don't run at all.
-               if os.path.samefile (file, os.path.join (todir, file)):
-                       return SUCCESS
-
-               cmd = NOEXTRACT_CMD % (file, todir)
-               ret = run_command (cmd)
+               """Extract a single file of the Join type.
 
 
-               # Check error code
-               if ret != 0:
-                       return -EEXTRACT
+                  file -- the file to extract
+                  todir -- the directory to extract to
 
 
-               return SUCCESS
+                  This command ignores the extraction if file and todir+file are the same
+                  file. This keeps things like mv working smoothly."""
 
 
+               # The Join type doesn't need any extraction
+               pass