From: Ira W. Snyder Date: Wed, 10 Jan 2007 22:52:14 +0000 (-0800) Subject: Switch to built-in logging class X-Git-Tag: v2.1.0~7 X-Git-Url: https://www.irasnyder.com/gitweb/?p=animesorter.git;a=commitdiff_plain;h=75e4cb4bfd3e846687bfa6b1ee02810d474fe118 Switch to built-in logging class Switch over all of the code from the homegrown logger class to the built-in logging class. Signed-off-by: Ira W. Snyder --- diff --git a/animesorter2.py b/animesorter2.py index c56ad2b..9585394 100755 --- a/animesorter2.py +++ b/animesorter2.py @@ -35,6 +35,7 @@ import re import sys import errno import shutil +import logging from optparse import OptionParser ### Default Configuration Variables ### @@ -62,7 +63,7 @@ class AnimeSorter2: finally: f.close() except IOError: - self.print_dict_fail (self.options.dict_file) + logging.critical ('Opening dictionary: %s FAILED' % self.options.dict_file) sys.exit() ### Get a LIST containing each line in the file @@ -79,7 +80,8 @@ class AnimeSorter2: result = [(re.compile(r), d) for r, d in result] ### Give some information about the dictionary we are using - self.print_dict_suc (self.options.dict_file, len(result)) + logging.info ('Successfully loaded %d records from %s\n' % \ + (len(result), self.options.dict_file)) return tuple(result) @@ -94,7 +96,7 @@ class AnimeSorter2: r = r.strip() d = d.strip() except ValueError: - self.print_dict_bad_line (l) + logging.warning ('Bad line in dictionary: "%s"' % l) continue result.append((r, d)) @@ -119,16 +121,16 @@ class AnimeSorter2: if not os.path.isdir (dirname): if self.options.pretend: - self.print_dir_create_pretend (dirname) + logging.info ('Will create directory %s' % dirname) return 0 if self.get_user_choice ('Make directory?: %s' % (dirname, )): try: os.makedirs (dirname) - self.print_dir_create_suc (dirname) + logging.info ('Created directory %s' % dirname) except: - self.print_dir_create_fail (dirname) + logging.critical ('Failed to create directory %s' % dirname) return errno.EIO return 0 @@ -141,15 +143,15 @@ class AnimeSorter2: dstname = os.path.join (todir, f) if self.options.pretend: - self.print_move_file_pretend (f, todir) + logging.info ('Will move %s to %s' % (f, todir)) return 0 # success if self.get_user_choice ('Move file?: %s --> %s' % (srcname, dstname)): try: shutil.move (srcname, dstname) - self.print_move_file_suc (f, todir) + logging.info ('Moved %s to %s' % (f, todir)) except: - self.print_move_file_fail (f, todir) + logging.critical ('FAILED to move %s to %s' % (f, todir)) return errno.EIO return 0 @@ -217,7 +219,11 @@ class AnimeSorter2: def main(self): ## Print the program's header - self.print_prog_header () + logging.info ('Regular Expression File Sorter (aka animesorter)') + logging.info ('=' * 80) + logging.info ('Copyright (c) 2005-2007, Ira W. Snyder (devel@irasnyder.com)') + logging.info ('This program is licensed under the GNU GPL v2') + logging.info ('') ## Parse the dictionary dict = self.parse_dict() @@ -231,62 +237,13 @@ class AnimeSorter2: [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: - return - - 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(self, f, t): - if self.options.quiet: - return - - print '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: - return - - print '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: - return - - print '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(): + # Set up the logger + logging.basicConfig (level=logging.INFO, format='%(message)s') + ### Get the program options parser = OptionParser() parser.add_option('-q', '--quiet', action='store_true', dest='quiet', @@ -312,10 +269,13 @@ def main(): options.start_dir = os.path.abspath(os.path.expanduser(options.start_dir)) options.output_dir = os.path.abspath(os.path.expanduser(options.output_dir)) + # Change the loglevel if we're running in quiet mode + if options.quiet: + logging.getLogger().setLevel (logging.CRITICAL) + as = AnimeSorter2(options) as.main() if __name__ == '__main__': main () -# vim: set ts=4 sw=4 sts=4 expandtab: