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