Rev 373 | Blame | Compare with Previous | Last modification | View Log | RSS feed
#!/usr/bin/env python# Copyright: Ira W. Snyder (devel@irasnyder.com)# License: GNU General Public License v2 (or at your option, any later version)import osimport reimport sysimport errnoimport shutilfrom optparse import OptionParser### Default Configuration Variables ###DICT_FILE = '~/.config/animesorter2/animesorter.dict'WORK_DIR = '~/downloads/usenet'SORT_DIR = '/data/Anime'TYPES_REGEX = '.*(avi|ogm|mkv|mp4|\d\d\d)$'class AnimeSorter2:def __init__(self, options):self.options = optionsdef parse_dict(self):"""Parses a dictionary file containing the sort definitions in the form:DIRECTORY = PATTERNReturns a list of tuples of the form (compiled_regex, to_directory)"""try:f = open(self.options.dict_file, 'r', 0)try:data = f.read()finally:f.close()except IOError:self.print_dict_fail (self.options.dict_file)sys.exit()### Get a LIST containing each line in the filelines = [l for l in data.split('\n') if len(l) > 0]### Remove comments / blank lines (zero length lines already removed above)regex = re.compile ('^\s*#.*$')lines = [l for l in lines if not re.match (regex, l)]regex = re.compile ('^\s*$')lines = [l for l in lines if not re.match (regex, l)]### Split each line into a tuple, and strip each element of spacesresult = self.split_lines(lines)result = [(re.compile(r), d) for r, d in result]### Give some information about the dictionary we are usingself.print_dict_suc (self.options.dict_file, len(result))return tuple(result)def split_lines(self, lines):result = []for l in lines:try:r, d = l.split('=')r = r.strip()d = d.strip()except ValueError:self.print_dict_bad_line (l)continueresult.append((r, d))return resultdef get_matches(self, files, pattern):"""get_matches(files, pattern):files is type LISTpattern is type sre.SRE_PatternReturns a list of the files matching the pattern as type sre.SRE_Match."""matches = [m for m in files if pattern.search(m)]return matchesdef as_makedirs (self, dirname):"""Call os.makedirs(dirname), but check first whether we are in pretendmode, or if we're running interactively."""if not os.path.isdir (dirname):if self.options.pretend:self.print_dir_create_pretend (dirname)return 0if self.get_user_choice ('Make directory?: %s' % (dirname, )):try:os.makedirs (dirname)self.print_dir_create_suc (dirname)except:self.print_dir_create_fail (dirname)return errno.EIOreturn 0def as_move_single_file (self, f, fromdir, todir):"""Move the single file named $f from the directory $fromdir to thedirectory $todir"""srcname = os.path.join (fromdir, f)dstname = os.path.join (todir, f)if self.options.pretend:self.print_move_file_pretend (f, todir)return 0 # successif self.get_user_choice ('Move file?: %s --> %s' % (srcname, dstname)):try:shutil.move (srcname, dstname)self.print_move_file_suc (f, todir)except:self.print_move_file_fail (f, todir)return errno.EIOreturn 0def move_files(self, files, fromdir, todir):"""move_files(files, fromdir, todir):Move the files represented by the list FILES from FROMDIR to TODIR"""ret = 0## Check for a non-default directoryif todir[0] != '/':todir = os.path.join(self.options.output_dir, todir)## Create the directory if it doesn't existret = self.as_makedirs (todir)if ret:# we cannot continue, since we can't make the directoryreturn ret## Try to move every file, one at a timefor f in files:ret = self.as_move_single_file (f, fromdir, todir)if ret:# something bad happened when moving a filebreakreturn retdef __dir_walker(self, dict, root, dirs, files):## Get all of the files in the directory that are of the correct typestypes_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 matchesfor regex, todir in dict:matches = self.get_matches(raw_matches, regex)## Move the files if we've found someif len(matches) > 0:self.move_files(matches, root, todir)def get_user_choice(self, prompt):# If we're not in interactive mode, then always return Trueif self.options.interactive == False:return True# Get the user's choice since we're not in interactive modedone = Falsewhile not done:s = raw_input('%s [y/N]: ' % (prompt, )).lower()if s == 'y' or s == 'yes':return Trueif s == 'n' or s == 'no' or s == '':return Falseprint 'Response not understood, try again.'def main(self):## Print the program's headerself.print_prog_header ()## Parse the dictionarydict = self.parse_dict()if self.options.recursive:## Start walking through directoriesfor root, dirs, files in os.walk(self.options.start_dir):self.__dir_walker(dict, root, dirs, 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)])############################################################################### Functions for the printing system############################################################################def print_prog_header(self):if self.options.quiet:returnprint 'Regular Expression File Sorter (aka animesorter)'print '================================================================================'print 'Copyright (c) 2005,2006, Ira W. Snyder (devel@irasnyder.com)'print 'All rights reserved.'print 'This program is licensed under the GNU GPL v2'def print_move_file_suc(self, f, t):if self.options.quiet:returnprint 'Moved %s to %s' % (f, t)def print_move_file_fail(self, f, t):print 'FAILED to move %s to %s' % (f, t)def print_move_file_pretend (self, f, t):print 'Will move %s to %s' % (f, t)def print_dir_create_suc(self, d):if self.options.quiet:returnprint 'Created directory %s' % (d, )def print_dir_create_fail(self, d):print 'Failed to create directory %s' % (d, )def print_dir_create_pretend (self, d):print 'Will create directory %s' % (d, )def print_dict_suc(self, dic, num):if self.options.quiet:returnprint 'Successfully loaded %d records from %s\n' % (num, dic)def print_dict_fail(self, dic):print 'Opening dictionary: %s FAILED' % (dic, )def print_dict_bad_line(self, dic):print 'Bad line in dictionary: %s' % (dic, )### MAIN IS HERE ###def main():### Get the program optionsparser = OptionParser()parser.add_option('-q', '--quiet', action='store_true', dest='quiet',default=False, help="Don't print status messages to stdout")parser.add_option('-d', '--dict', dest='dict_file', default=DICT_FILE,help='Read dictionary from FILE', metavar='FILE')parser.add_option('-n', '--not-recursive', action='store_false', dest='recursive',default=True, help='don\'t run recursively')parser.add_option('-s', '--start-dir', dest='start_dir', default=WORK_DIR,help='Start running at directory DIR', metavar='DIR')parser.add_option('-o', '--output-dir', dest='output_dir', default=SORT_DIR,help='Sort files into DIR', metavar='DIR')parser.add_option('-i', '--interactive', dest='interactive', default=False,help='Confirm each move', action='store_true')parser.add_option('-p', '--pretend', dest='pretend', default=False,help='Enable pretend mode', action='store_true')## Parse the options(options, args) = parser.parse_args()## Correct directoriesoptions.dict_file = os.path.abspath(os.path.expanduser(options.dict_file))options.start_dir = os.path.abspath(os.path.expanduser(options.start_dir))options.output_dir = os.path.abspath(os.path.expanduser(options.output_dir))as = AnimeSorter2(options)as.main()if __name__ == '__main__':main ()# vim: set ts=4 sw=4 sts=4 expandtab: