85316cadd4dce9a2381e4b9be05335b69b22a525
[rarslave2.git] / RarslaveDetector.py
1 #!/usr/bin/env python
2 # vim: set ts=4 sts=4 sw=4 textwidth=92:
3
4 from RarslaveCommon import *
5
6 # PAR2Set-derived types
7 import PAR2Set_JOIN
8 import PAR2Set_ZIP
9 import PAR2Set_OLDRAR
10 import PAR2Set_NEWRAR
11 import PAR2Set_EF_OLDRAR
12 import PAR2Set_EF_NEWRAR
13
14 class RarslaveDetector (object):
15
16         # A tuple of tuples with the following definition:
17         # (TYPE_NAME, DETECTION_FUNCTION, PAR2Set-derived class)
18
19         TYPES = (       (PAR2Set_JOIN.detect_JOIN, PAR2Set_JOIN.PAR2Set_JOIN),
20                                 (PAR2Set_ZIP.detect_ZIP, PAR2Set_ZIP.PAR2Set_ZIP),
21                                 (PAR2Set_OLDRAR.detect_OLDRAR, PAR2Set_OLDRAR.PAR2Set_OLDRAR),
22                                 (PAR2Set_NEWRAR.detect_NEWRAR, PAR2Set_NEWRAR.PAR2Set_NEWRAR),
23                                 (PAR2Set_EF_OLDRAR.detect_EF_OLDRAR, PAR2Set_EF_OLDRAR.PAR2Set_EF_OLDRAR),
24                                 (PAR2Set_EF_NEWRAR.detect_EF_NEWRAR, PAR2Set_EF_NEWRAR.PAR2Set_EF_NEWRAR),
25                         )
26
27         def __init__ (self, dir, p2file):
28
29                 # The real "meat" of the class
30                 self.dir = dir
31                 self.p2file = p2file
32                 self.basename = get_basename (p2file)
33
34                 # Find files that match by name only
35                 self.name_matched_files = find_name_matches (self.dir, self.basename)
36
37                 # Find all par2 files for this set using name matches
38                 self.all_p2files = find_par2_files (self.name_matched_files)
39
40                 # Try to get the protected files for this set
41                 self.prot_matched_files = parse_all_par2 (self.dir, self.p2file, self.all_p2files)
42
43         def runMatchingTypes (self):
44                 # Now tries to run every type of PAR2Set-derived class for which the detector
45                 # detects that the class is valid.
46
47                 detected = False
48
49                 for (detector, classname) in self.TYPES:
50                         if detector (self.name_matched_files, self.prot_matched_files):
51                                 # The detector matched, so we're up and running!
52
53                                 p2set = classname (self.dir, self.p2file)
54
55                                 detected = True
56                                 debugMessage ('Detected type: %s' % p2set)
57
58                                 # Try to have rarslave do it's thing
59                                 ret = p2set.runAll ()
60
61                                 # If something already worked, there is no need to continue,
62                                 # since we've already finished!
63                                 if ret == SUCCESS:
64                                         break
65                                 else:
66                                         fatalMessage ('Detected type failed for: %s' % self.p2file)
67
68                 # Make sure we detected at least one valid type
69                 if not detected:
70                         fatalMessage ('Unable to determine type: %s' % self.p2file)
71                         verboseMessage ('The following information will help in writing a detector:')
72                         verboseMessage ('name_matches: %s' % self.name_matched_files)
73                         verboseMessage ('prot_matches: %s' % self.prot_matched_files)
74                         return -EDETECT
75
76                 # Make sure that something worked
77                 if ret != SUCCESS:
78                         fatalMessage ('All types failed for: %s' % self.p2file)
79
80                 return ret
81
82 def main ():
83         pass
84
85 if __name__ == '__main__':
86         main ()
87