Subversion Repositories programming

Rev

Rev 366 | Rev 373 | Go to most recent revision | 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 os, re, shutil, sys
from 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)$'

### Functions for the printing system
def print_prog_header():
    print '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'
    print

def print_move_file_suc(f, t):
    print 'Moved %s to %s' % (f, t)

def print_move_file_fail(f, t):
    print 'FAILED to move %s to %s' % (f, t)

def print_dir_create_suc(d):
    print 'Created directory %s' % (d, )

def print_dir_create_fail(d):
    print 'Failed to create directory %s' % (d, )

def print_dict_suc(dic, num):
    print 'Successfully loaded %d records from %s\n' % (num, dic)

def print_dict_fail(dic):
    print 'Opening dictionary: %s FAILED' % (dic, )

def print_dict_bad_line(dic):
    print 'Bad line in dictionary: %s' % (dic, )

class AnimeSorter2:

    def __init__(self, options):
        self.options = options

    def parse_dict(self):
        """Parses a dictionary file containing the sort definitions in the form:
        DIRECTORY = PATTERN

        Returns 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:
            print_dict_fail (self.options.dict_file)
            sys.exit()

        ### Get a LIST containing each line in the file
        lines = [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 spaces
        result = self.split_lines(lines)
        result = [(re.compile(r), d) for r, d in result]

        ### Give some information about the dictionary we are using
        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:
                print_dict_bad_line (l)
                continue

            result.append((r, d))

        return result

    def get_matches(self, files, pattern):
        """get_matches(files, pattern):

        files is type LIST
        pattern is type sre.SRE_Pattern

        Returns a list of the files matching the pattern as type sre.SRE_Match."""

        matches = [m for m in files if pattern.search(m)]
        return matches

    def move_files(self, files, fromdir, todir):
        """move_files(files, fromdir, todir):
        Move the files represented by the list FILES from FROMDIR to TODIR"""
        ## Check for a non-default directory
        if todir[0] != '/':
            todir = os.path.join(self.options.output_dir, todir)

        ## Create the directory if it doesn't exist
        if not os.path.isdir(todir):
            try:
                if self.get_user_choice('Make directory?: %s' % (todir, )):
                    os.makedirs(todir)
                    print_dir_create_suc (todir)
            except:
                print_dir_create_fail (todir)

        ## Try to move every file, one at a time
        for f in files:
            srcname = os.path.join(fromdir, f)
            dstname = os.path.join(todir, f)

            try:
                if self.get_user_choice('Move file?: %s --> %s' % (srcname, dstname)):
                    shutil.move(srcname, dstname)
                    print_move_file_suc (f, todir)
            except:
                print_move_file_fail (f, todir)


    def __dir_walker(self, dict, root, dirs, files):

        ## Get all of the files in the directory that are of the correct types
        types_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 matches
        for regex, todir in dict:
            matches = self.get_matches(raw_matches, regex)

            ## Move the files if we've found some
            if 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 True
        if self.options.interactive == False:
            return True

        # Get the user's choice since we're not in interactive mode
        done = False
        while not done:
            s = raw_input('%s [y/n]: ' % (prompt, )).lower()

            if s == 'y' or s == 'yes':
                return True

            if s == 'n' or s == 'no':
                return False

            print 'Response not understood, try again.'

    def main(self):

        ## Print the program's header
        print_prog_header ()

        ## Parse the dictionary
        dict = self.parse_dict()

        if self.options.recursive:
            ## Start walking through directories
            for 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)])

### MAIN IS HERE ###
def main():

    ### Get the program options
    parser = OptionParser()
    parser.add_option('-q', '--quiet', action='store_false', dest='verbose',
            default=True, 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')

    ## Parse the options
    (options, args) = parser.parse_args()

    ## Correct directories
    options.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: