Subversion Repositories programming

Rev

Rev 366 | Rev 371 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
181 ira 1
#!/usr/bin/env python
2
 
3
# Copyright: Ira W. Snyder (devel@irasnyder.com)
4
# License: GNU General Public License v2 (or at your option, any later version)
5
 
368 ira 6
import os, re, shutil, sys
181 ira 7
from optparse import OptionParser
8
 
9
### Default Configuration Variables ###
10
DICT_FILE = '~/.config/animesorter2/animesorter.dict'
11
WORK_DIR =  '~/downloads/usenet'
12
SORT_DIR =  '/data/Anime'
13
TYPES_REGEX = '.*(avi|ogm|mkv|mp4|\d\d\d)$'
14
 
368 ira 15
### Functions for the printing system
16
def print_prog_header():
17
    print 'Regular Expression File Sorter (aka animesorter)'
18
    print '================================================================================'
19
    print 'Copyright (c) 2005,2006, Ira W. Snyder (devel@irasnyder.com)'
20
    print 'All rights reserved.'
21
    print 'This program is licensed under the GNU GPL v2'
22
    print
181 ira 23
 
368 ira 24
def print_move_file_suc(f, t):
25
    print 'Moved %s to %s' % (f, t)
26
 
27
def print_move_file_fail(f, t):
28
    print 'FAILED to move %s to %s' % (f, t)
29
 
30
def print_dir_create_suc(d):
31
    print 'Created directory %s' % (d, )
32
 
33
def print_dir_create_fail(d):
34
    print 'Failed to create directory %s' % (d, )
35
 
36
def print_dict_suc(dic, num):
37
    print 'Successfully loaded %d records from %s\n' % (num, dic)
38
 
39
def print_dict_fail(dic):
40
    print 'Opening dictionary: %s FAILED' % (dic, )
41
 
42
def print_dict_bad_line(dic):
43
    print 'Bad line in dictionary: %s' % (dic, )
44
 
181 ira 45
class AnimeSorter2:
46
 
47
    def __init__(self, options):
48
        self.options = options
49
 
50
    def parse_dict(self):
51
        """Parses a dictionary file containing the sort definitions in the form:
52
        DIRECTORY = PATTERN
53
 
54
        Returns a list of tuples of the form (compiled_regex, to_directory)"""
55
 
56
        try:
57
            f = open(self.options.dict_file, 'r', 0)
58
            try:
59
                data = f.read()
60
            finally:
61
                f.close()
62
        except IOError:
368 ira 63
            print_dict_fail (self.options.dict_file)
181 ira 64
            sys.exit()
65
 
66
        ### Get a LIST containing each line in the file
67
        lines = [l for l in data.split('\n') if len(l) > 0]
68
 
366 ira 69
        ### Remove comments / blank lines (zero length lines already removed above)
70
        regex = re.compile ('^\s*#.*$')
71
        lines = [l for l in lines if not re.match (regex, l)]
72
        regex = re.compile ('^\s*$')
73
        lines = [l for l in lines if not re.match (regex, l)]
74
 
181 ira 75
        ### Split each line into a tuple, and strip each element of spaces
76
        result = self.split_lines(lines)
77
        result = [(re.compile(r), d) for r, d in result]
78
 
79
        ### Give some information about the dictionary we are using
368 ira 80
        print_dict_suc (self.options.dict_file, len(result))
181 ira 81
 
82
        return tuple(result)
83
 
84
    def split_lines(self, lines):
85
 
86
        result = []
87
 
88
        for l in lines:
184 ira 89
 
366 ira 90
            try:
91
                r, d = l.split('=')
92
                r = r.strip()
93
                d = d.strip()
94
            except ValueError:
368 ira 95
                print_dict_bad_line (l)
366 ira 96
                continue
181 ira 97
 
366 ira 98
            result.append((r, d))
181 ira 99
 
100
        return result
101
 
102
    def get_matches(self, files, pattern):
103
        """get_matches(files, pattern):
104
 
105
        files is type LIST
106
        pattern is type sre.SRE_Pattern
107
 
108
        Returns a list of the files matching the pattern as type sre.SRE_Match."""
109
 
110
        matches = [m for m in files if pattern.search(m)]
111
        return matches
112
 
113
    def move_files(self, files, fromdir, todir):
114
        """move_files(files, fromdir, todir):
115
        Move the files represented by the list FILES from FROMDIR to TODIR"""
116
        ## Check for a non-default directory
117
        if todir[0] != '/':
118
            todir = os.path.join(self.options.output_dir, todir)
119
 
120
        ## Create the directory if it doesn't exist
121
        if not os.path.isdir(todir):
122
            try:
123
                if self.get_user_choice('Make directory?: %s' % (todir, )):
124
                    os.makedirs(todir)
368 ira 125
                    print_dir_create_suc (todir)
181 ira 126
            except:
368 ira 127
                print_dir_create_fail (todir)
181 ira 128
 
129
        ## Try to move every file, one at a time
130
        for f in files:
131
            srcname = os.path.join(fromdir, f)
132
            dstname = os.path.join(todir, f)
133
 
134
            try:
135
                if self.get_user_choice('Move file?: %s --> %s' % (srcname, dstname)):
136
                    shutil.move(srcname, dstname)
368 ira 137
                    print_move_file_suc (f, todir)
181 ira 138
            except:
368 ira 139
                print_move_file_fail (f, todir)
181 ira 140
 
141
 
142
    def __dir_walker(self, dict, root, dirs, files):
143
 
144
        ## Get all of the files in the directory that are of the correct types
145
        types_re = re.compile(TYPES_REGEX, re.IGNORECASE)
146
        raw_matches = [f for f in files if types_re.match(f)]
147
 
148
        ### Loop through the dictionary and try to move everything that matches
149
        for regex, todir in dict:
150
            matches = self.get_matches(raw_matches, regex)
151
 
152
            ## Move the files if we've found some
153
            if len(matches) > 0:
154
                self.move_files(matches, root, todir)
155
 
156
    def get_user_choice(self, prompt):
157
 
158
        # If we're not in interactive mode, then always return True
159
        if self.options.interactive == False:
160
            return True
161
 
162
        # Get the user's choice since we're not in interactive mode
163
        done = False
164
        while not done:
165
            s = raw_input('%s [y/n]: ' % (prompt, )).lower()
166
 
167
            if s == 'y' or s == 'yes':
168
                return True
169
 
170
            if s == 'n' or s == 'no':
171
                return False
172
 
173
            print 'Response not understood, try again.'
174
 
175
    def main(self):
176
 
177
        ## Print the program's header
368 ira 178
        print_prog_header ()
181 ira 179
 
180
        ## Parse the dictionary
181
        dict = self.parse_dict()
182
 
183
        if self.options.recursive:
184
            ## Start walking through directories
185
            for root, dirs, files in os.walk(self.options.start_dir):
186
                self.__dir_walker(dict, root, dirs, files)
187
        else:
188
            self.__dir_walker(dict, self.options.start_dir,
189
                    [d for d in os.listdir(self.options.start_dir) if os.path.isdir(d)],
190
                    [f for f in os.listdir(self.options.start_dir) if os.path.isfile(f)])
191
 
192
### MAIN IS HERE ###
368 ira 193
def main():
181 ira 194
 
195
    ### Get the program options
196
    parser = OptionParser()
197
    parser.add_option('-q', '--quiet', action='store_false', dest='verbose',
198
            default=True, help='Don\'t print status messages to stdout')
199
    parser.add_option('-d', '--dict', dest='dict_file', default=DICT_FILE,
200
            help='Read dictionary from FILE', metavar='FILE')
201
    parser.add_option('-n', '--not-recursive', action='store_false', dest='recursive',
202
            default=True, help='don\'t run recursively')
203
    parser.add_option('-s', '--start-dir', dest='start_dir', default=WORK_DIR,
204
            help='Start running at directory DIR', metavar='DIR')
205
    parser.add_option('-o', '--output-dir', dest='output_dir', default=SORT_DIR,
206
            help='Sort files into DIR', metavar='DIR')
207
    parser.add_option('-i', '--interactive', dest='interactive', default=False,
208
            help='Confirm each move', action='store_true')
209
 
210
    ## Parse the options
211
    (options, args) = parser.parse_args()
212
 
213
    ## Correct directories
214
    options.dict_file = os.path.abspath(os.path.expanduser(options.dict_file))
215
    options.start_dir = os.path.abspath(os.path.expanduser(options.start_dir))
216
    options.output_dir = os.path.abspath(os.path.expanduser(options.output_dir))
217
 
218
    as = AnimeSorter2(options)
219
    as.main()
220
 
368 ira 221
if __name__ == '__main__':
222
    main ()
223
 
224
# vim: set ts=4 sw=4 sts=4 expandtab: