scripts/dtrx

branch
trunk
changeset 65
0aea49161478
parent 62
17d845dacff5
child 66
af0b822b012e
equal deleted inserted replaced
64:d7b46cae939b 65:0aea49161478
47 WITHOUT ANY WARRANTY; without even the implied warranty of 47 WITHOUT ANY WARRANTY; without even the implied warranty of
48 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 48 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
49 Public License for more details.""" % (VERSION,) 49 Public License for more details.""" % (VERSION,)
50 50
51 MATCHING_DIRECTORY = 1 51 MATCHING_DIRECTORY = 1
52 ONE_ENTRY = 2 52 ONE_ENTRY_KNOWN = 2
53 BOMB = 3 53 BOMB = 3
54 EMPTY = 4 54 EMPTY = 4
55 ONE_ENTRY_KNOWN = 5 55 ONE_ENTRY_FILE = 'file'
56 ONE_ENTRY_DIRECTORY = 'directory'
57
58 ONE_ENTRY_UNKNOWN = [ONE_ENTRY_FILE, ONE_ENTRY_DIRECTORY]
56 59
57 EXTRACT_HERE = 1 60 EXTRACT_HERE = 1
58 EXTRACT_WRAP = 2 61 EXTRACT_WRAP = 2
59 EXTRACT_RENAME = 3 62 EXTRACT_RENAME = 3
60 63
211 if not self.contents: 214 if not self.contents:
212 self.content_type = EMPTY 215 self.content_type = EMPTY
213 elif len(self.contents) == 1: 216 elif len(self.contents) == 1:
214 if self.basename() == self.contents[0]: 217 if self.basename() == self.contents[0]:
215 self.content_type = MATCHING_DIRECTORY 218 self.content_type = MATCHING_DIRECTORY
219 elif os.path.isdir(self.contents[0]):
220 self.content_type = ONE_ENTRY_DIRECTORY
216 else: 221 else:
217 self.content_type = ONE_ENTRY 222 self.content_type = ONE_ENTRY_FILE
218 self.content_name = self.contents[0] 223 self.content_name = self.contents[0]
219 if os.path.isdir(self.contents[0]): 224 if os.path.isdir(self.contents[0]):
220 self.content_name += '/' 225 self.content_name += '/'
221 else: 226 else:
222 self.content_type = BOMB 227 self.content_type = BOMB
558 563
559 564
560 class MatchHandler(BaseHandler): 565 class MatchHandler(BaseHandler):
561 def can_handle(contents, options): 566 def can_handle(contents, options):
562 return ((contents == MATCHING_DIRECTORY) or 567 return ((contents == MATCHING_DIRECTORY) or
563 ((contents == ONE_ENTRY) and 568 ((contents in ONE_ENTRY_UNKNOWN) and
564 options.one_entry_policy.ok_for_match())) 569 options.one_entry_policy.ok_for_match()))
565 can_handle = staticmethod(can_handle) 570 can_handle = staticmethod(can_handle)
566 571
567 def organize(self): 572 def organize(self):
568 source = os.path.join(self.extractor.target, 573 source = os.path.join(self.extractor.target,
611 self.permanent_policy = self.answers[''] 616 self.permanent_policy = self.answers['']
612 else: 617 else:
613 self.permanent_policy = None 618 self.permanent_policy = None
614 619
615 def ask_question(self, question): 620 def ask_question(self, question):
616 question = textwrap.wrap(question) + self.choices 621 question = question + self.choices
617 while True: 622 while True:
618 print "\n".join(question) 623 print "\n".join(question)
619 try: 624 try:
620 answer = raw_input(self.prompt) 625 answer = raw_input(self.prompt)
621 except EOFError: 626 except EOFError:
636 " * extract it Inside another directory", 641 " * extract it Inside another directory",
637 " * extract it and Rename the directory", 642 " * extract it and Rename the directory",
638 " * extract it Here"] 643 " * extract it Here"]
639 prompt = "What do you want to do? (I/r/h) " 644 prompt = "What do you want to do? (I/r/h) "
640 645
641 def prep(self, archive_filename, entry_name): 646 def prep(self, archive_filename, extractor):
642 question = ("%s contains one entry: %s." % 647 question = ["%s contains one %s, but it has a weird name." %
643 (archive_filename, entry_name)) 648 (archive_filename, extractor.content_type)]
649 question.append(" Expected: " + extractor.basename())
650 question.append(" Actual: " + extractor.content_name)
644 self.current_policy = (self.permanent_policy or 651 self.current_policy = (self.permanent_policy or
645 self.ask_question(question)) 652 self.ask_question(question))
646 653
647 def ok_for_match(self): 654 def ok_for_match(self):
648 return self.current_policy in (EXTRACT_RENAME, EXTRACT_HERE) 655 return self.current_policy in (EXTRACT_RENAME, EXTRACT_HERE)
675 question = ("%s contains %s other archive files." % 682 question = ("%s contains %s other archive files." %
676 (current_filename, archive_count)) 683 (current_filename, archive_count))
677 else: 684 else:
678 question = ("%s contains another archive: %s." % 685 question = ("%s contains another archive: %s." %
679 (current_filename, extractor.included_archives[0])) 686 (current_filename, extractor.included_archives[0]))
687 question = textwrap.wrap(question)
680 if target == '.': 688 if target == '.':
681 target = '' 689 target = ''
682 included_root = extractor.included_root 690 included_root = extractor.included_root
683 if included_root == './': 691 if included_root == './':
684 included_root = '' 692 included_root = ''
853 def __init__(self, options, filenames): 861 def __init__(self, options, filenames):
854 BaseAction.__init__(self, options, filenames) 862 BaseAction.__init__(self, options, filenames)
855 self.did_print = False 863 self.did_print = False
856 864
857 def get_handler(self, extractor): 865 def get_handler(self, extractor):
858 if extractor.content_type == ONE_ENTRY: 866 if extractor.content_type in ONE_ENTRY_UNKNOWN:
859 self.options.one_entry_policy.prep(self.current_filename, 867 self.options.one_entry_policy.prep(self.current_filename,
860 extractor.content_name) 868 extractor)
861 for handler in self.handlers: 869 for handler in self.handlers:
862 if handler.can_handle(extractor.content_type, self.options): 870 if handler.can_handle(extractor.content_type, self.options):
863 logger.debug("using %s handler" % (handler.__name__,)) 871 logger.debug("using %s handler" % (handler.__name__,))
864 self.current_handler = handler(extractor, self.options) 872 self.current_handler = handler(extractor, self.options)
865 break 873 break

mercurial