Subversion Repositories programming


Rev 184 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#!/usr/bin/env python

# Copyright: Ira W. Snyder (
# Start Date: 2006-01-11
# End Date:
# License: GNU General Public License v2 (or at your option, any later version)

import os, re, shutil, sys, threading, time
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)$'

### Enum for the print_queue
DICT_BAD_LINE) = range(9)

class AnimeSorter2:

    def __init__(self, options):
        self.options = options
        self.__print_queue = []

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

        Returns a list of tuples of the form (compiled_regex, to_directory)"""

            f = open(self.options.dict_file, 'r', 0)
                data =
        except IOError:
            self.add_to_print_queue(DICT_FAIL, self.options.dict_file)

        ### Get a LIST containing each line in the file
        lines = [l for l in data.split('\n') if len(l) > 0]

        ### 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
        self.add_to_print_queue(DICT_SUC, self.options.dict_file, len(result))

        return tuple(result)

    def split_lines(self, lines):

        result = []

        for l in lines:
            if len(l) > 0:
                if l[0] == '#':

                    r, d = l.split('=')
                    r = r.strip()
                    d = d.strip()
                except ValueError:
                    self.add_to_print_queue(DICT_BAD_LINE, l)

                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]
        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):
                if self.get_user_choice('Make directory?: %s' % (todir, )):
                    self.add_to_print_queue(DIR_CREATE_SUC, todir)
                self.add_to_print_queue(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)

                if self.get_user_choice('Move file?: %s --> %s' % (srcname, dstname)):
                    shutil.move(srcname, dstname)
                    self.add_to_print_queue(MOV_FILE_SUC, f, todir)
                self.add_to_print_queue(MOV_FILE_FAIL, f, todir)

    def print_prog_header(self, arg1, arg2):
        print 'Regular Expression File Sorter (aka animesorter)'
        print '================================================================================'
        print 'Copyright (c) 2005,2006, Ira W. Snyder ('
        print 'All rights reserved.'
        print 'This program is licensed under the GNU GPL v2'

    def print_dir_header(self, arg1, arg2):
        print 'Working in directory: %s' % arg1
        print '================================================================================'

    def print_move_file_suc(self, arg1, arg2):
        print 'Moved %s to %s' % (arg1, arg2)

    def print_move_file_fail(self, arg1, arg2):
        print 'FAILED to move %s to %s' % (arg1, arg2)

    def print_dir_create_suc(self, arg1, arg2):
        print 'Created directory %s' % (arg1, )

    def print_dir_create_fail(self, arg1, arg2):
        print 'Failed to create directory %s' % (arg1, )

    def print_dict_suc(self, arg1, arg2):
        print 'Using dictionary file: %s' % (arg1, )
        print 'Successfully loaded %d records' % (arg2, )

    def print_dict_fail(self, arg1, arg2):
        print 'Opening dictionary: %s FAILED' % (arg1, )

    def print_dict_bad_line(self, arg1, arg2):
        print 'Bad line in dictionary: %s' % (arg1, )

    def print_thread_routine(self):

        # Loop forever
        while True:

            # If we are being told to stop, and we have nothing to print, STOP
            if self.__stop_print_thread == True and len(self.__print_queue) == 0:

            # If we have something to print
            if len(self.__print_queue) > 0:
                (item_type, item_from, item_to) = self.__print_queue[0]
                del self.__print_queue[0]

                # Emulate a switch statement
                { PROG_HDR : self.print_prog_header,
                  DIR_HDR  : self.print_dir_header,
                  MOV_FILE_SUC : self.print_move_file_suc,
                  MOV_FILE_FAIL : self.print_move_file_fail,
                  DIR_CREATE_SUC : self.print_dir_create_suc,
                  DIR_CREATE_FAIL : self.print_dir_create_fail,
                  DICT_SUC : self.print_dict_suc,
                  DICT_FAIL : self.print_dict_fail,
                  DICT_BAD_LINE : self.print_dict_bad_line } [item_type](item_from, item_to)


    def add_to_print_queue(self, item_type, item_from=None, item_to=None):
        self.__print_queue.append( (item_type, item_from, item_to) )

    def start_print_queue(self):
        if self.options.verbose:
            self.__stop_print_thread = False
            self.__print_thread = threading.Thread(target=self.print_thread_routine)

    def stop_print_queue(self):
        self.__stop_print_thread = True

    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):

        ## Start the print queue

        ## Print the program's 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)
            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)])

        ## Stop the print queue

### MAIN IS HERE ###
if __name__ == '__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)