Subversion Repositories programming

Compare Revisions

Ignore whitespace Rev 180 → Rev 181

/projects/animesorter/animesorter.py
8,167 → 8,172
WORK_DIR = os.path.expanduser('~/downloads/usenet')
SORT_DIR = os.path.expanduser('/data/Anime')
 
### Globals
options = {}
move_status = []
class AnimeSorter:
 
def __init__(self):
self.options = {}
self.move_status = []
 
def parse_dict():
"""Parses a dictionary file containing the sort definitions in the form:
DIRECTORY = PATTERN
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(options.dict_file, 'r', 0)
Returns a list of tuples of the form (compiled_regex, to_directory)"""
 
try:
data = f.read()
finally:
f.close()
except IOError:
print 'Error opening %s ... exiting!' % options.dict_file
sys.exit()
f = open(self.options.dict_file, 'r', 0)
try:
data = f.read()
finally:
f.close()
except IOError:
print 'Error opening %s ... exiting!' % 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]
### 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 = [(r, d) for r, d in [l.split('=') for l in lines]]
result = [(r.strip() ,d.strip()) for r, d in result]
result = [(re.compile(r), d) for r, d in result]
### Split each line into a tuple, and strip each element of spaces
result = [(r, d) for r, d in [l.split('=') for l in lines]]
result = [(r.strip() ,d.strip()) for r, d in result]
result = [(re.compile(r), d) for r, d in result]
 
return tuple(result)
return tuple(result)
 
def get_types_re():
"""Returns a compiled regular expression which will find objects of the
types specified in this function's definition."""
types = ('avi', 'ogm', 'mkv', 'mp4')
types_regex = ''
def get_types_re(self):
"""Returns a compiled regular expression which will find objects of the
types specified in this function's definition."""
 
for i in types:
types_regex += '%s|' % i
types = ('avi', 'ogm', 'mkv', 'mp4')
types_regex = ''
 
types_regex = '.*(%s)$' % types_regex[:-1]
for i in types:
types_regex += '%s|' % i
 
return re.compile(types_regex, re.IGNORECASE)
types_regex = '.*(%s)$' % types_regex[:-1]
 
def get_matches(files, pattern):
"""get_matches(files, pattern):
return re.compile(types_regex, re.IGNORECASE)
 
files is type LIST
pattern is type sre.SRE_Pattern
def get_matches(self, files, pattern):
"""get_matches(files, 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
files is type LIST
pattern is type sre.SRE_Pattern
 
def move_files(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(options.output_dir, todir)
Returns a list of the files matching the pattern as type sre.SRE_Match."""
 
## Create the directory if it doesn't exist
if not os.path.isdir(todir):
try:
os.makedirs(todir)
except:
move_status.append('FAILED to create directory: %s' % todir)
matches = [m for m in files if pattern.search(m)]
return matches
 
## 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)
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)
 
try:
shutil.move(srcname, dstname)
except:
move_status.append('FAILED to move %s to %s' % (f, todir))
return
## Create the directory if it doesn't exist
if not os.path.isdir(todir):
try:
os.makedirs(todir)
except:
self.move_status.append('FAILED to create directory: %s' % todir)
 
move_status.append('Moved %s to %s' % (f, 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)
 
def print_prog_header():
if options.verbose:
print 'Regular Expression File Sorter (aka animesorter)'
print '================================================================================'
print 'Copyright (c) 2005, Ira W. Snyder (devel@irasnyder.com)'
print 'All rights reserved.'
print 'This program is licensed under the GNU GPL v2'
print
try:
shutil.move(srcname, dstname)
except:
self.move_status.append('FAILED to move %s to %s' % (f, todir))
return
 
def print_dir_header(dir):
if options.verbose:
print 'Working in directory: %s' % dir
print '================================================================================'
self.move_status.append('Moved %s to %s' % (f, todir))
 
def print_move_status():
if options.verbose:
for i in move_status:
print i
def print_prog_header(self):
if self.options.verbose:
print 'Regular Expression File Sorter (aka animesorter)'
print '================================================================================'
print 'Copyright (c) 2005, Ira W. Snyder (devel@irasnyder.com)'
print 'All rights reserved.'
print 'This program is licensed under the GNU GPL v2'
print
 
print
def print_dir_header(self, dir):
if self.options.verbose:
print 'Working in directory: %s' % dir
print '================================================================================'
 
def get_parsed_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')
def print_move_status(self):
if self.options.verbose:
for i in self.move_status:
print i
 
## Parse the options
(options, args) = parser.parse_args()
print
 
## Correct directories
options.dict_file = os.path.abspath(options.dict_file)
options.start_dir = os.path.abspath(options.start_dir)
options.output_dir = os.path.abspath(options.output_dir)
def get_parsed_options(self):
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')
 
return options
## Parse the options
(options, args) = parser.parse_args()
 
def main():
## Correct directories
options.dict_file = os.path.abspath(options.dict_file)
options.start_dir = os.path.abspath(options.start_dir)
options.output_dir = os.path.abspath(options.output_dir)
 
## Get Options
options = get_parsed_options()
return options
 
## Print the program's header
print_prog_header()
def main(self):
 
## Parse the dictionary
dict = parse_dict()
## Get Options
self.options = self.get_parsed_options()
 
## Start walking through directories
for root, dirs, files in os.walk(options.start_dir):
## Print the program's header
self.print_prog_header()
 
## Blank the status variable
move_status = []
## Parse the dictionary
dict = self.parse_dict()
 
## Get all of the files in the directory that are of the correct types
types_re = get_types_re()
raw_matches = [f for f in files if types_re.match(f)]
## Start walking through directories
for root, dirs, files in os.walk(self.options.start_dir):
 
### Loop through the dictionary and try to move everything that matches
for regex, todir in dict:
matches = get_matches(raw_matches, regex)
## Blank the status variable
self.move_status = []
 
## Move the files if we've found some
if len(matches) > 0:
move_files(matches, root, todir)
## Get all of the files in the directory that are of the correct types
types_re = self.get_types_re()
raw_matches = [f for f in files if types_re.match(f)]
 
if len(move_status) > 0:
## print header
print_dir_header(root)
### Loop through the dictionary and try to move everything that matches
for regex, todir in dict:
matches = self.get_matches(raw_matches, regex)
 
## print status
print_move_status()
## Move the files if we've found some
if len(matches) > 0:
self.move_files(matches, root, todir)
 
if len(self.move_status) > 0:
## print header
self.print_dir_header(root)
 
## print status
self.print_move_status()
 
### MAIN IS HERE ###
if __name__ == '__main__':
main()
as = AnimeSorter()
as.main()
 
/projects/animesorter/animesorter2.py
0,0 → 1,278
#!/usr/bin/env python
 
###
# Copyright: Ira W. Snyder (devel@irasnyder.com)
# 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
(PROG_HDR, DIR_HDR, MOV_FILE_SUC, MOV_FILE_FAIL,
DIR_CREATE_SUC, DIR_CREATE_FAIL, DICT_SUC, DICT_FAIL,
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:
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:
self.add_to_print_queue(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]
 
### 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] == '#':
continue
 
try:
r, d = l.split('=')
r = r.strip()
d = d.strip()
except ValueError:
self.add_to_print_queue(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)
self.add_to_print_queue(DIR_CREATE_SUC, todir)
except:
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)
 
try:
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)
except:
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 (devel@irasnyder.com)'
print 'All rights reserved.'
print 'This program is licensed under the GNU GPL v2'
print
 
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, )
print
 
def print_dict_fail(self, arg1, arg2):
print 'Opening dictionary: %s FAILED' % (arg1, )
sys.exit()
 
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:
break
 
# 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)
 
else:
time.sleep(1)
 
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)
self.__print_thread.start()
 
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
self.start_print_queue()
 
## Print the program's header
self.add_to_print_queue(PROG_HDR)
 
## 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)])
 
## Stop the print queue
self.stop_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)
as.main()
 
Property changes:
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property