Fixes for Python 2.6
[animesorter.git] / animesorter2.py
index 04073be..e85a2b3 100755 (executable)
@@ -36,14 +36,15 @@ import sys
 import errno
 import shutil
 import logging
-from optparse import OptionParser
+import optparse
+
+PROGRAM_NAME = 'animesorter2'
+PROGRAM_VERSION = '2.1.0'
 
 ### Default Configuration Variables ###
 DICT_FILE = os.path.join ('~','.config','animesorter2','animesorter.dict')
 WORK_DIR =  os.path.join ('~','downloads','usenet')
 SORT_DIR =  os.path.join ('/','data','Anime')
-TYPES_REGEX = '.*(avi|ogm|mkv|mp4|\d\d\d)$'
-
 
 class AnimeSorter2:
 
@@ -91,7 +92,7 @@ class AnimeSorter2:
                 f.close()
         except IOError:
             logging.critical ('Opening dictionary: %s FAILED' % self.options.dict_file)
-            sys.exit()
+            sys.exit(1)
 
         ### Find all of the valid lines in the file
         valid_lines = [l for l in raw_lines if self.__valid_dict_line (l)]
@@ -247,7 +248,7 @@ def main():
     logging.basicConfig (level=logging.INFO, format='%(message)s')
 
     ### Get the program options
-    parser = OptionParser()
+    parser = optparse.OptionParser()
     parser.add_option('-q', '--quiet', action='store_true', dest='quiet',
             default=False, help="Don't print status messages to stdout")
     parser.add_option('-d', '--dict', dest='dict_file', default=DICT_FILE,
@@ -262,6 +263,10 @@ def main():
             help='Confirm each move', action='store_true')
     parser.add_option('-p', '--pretend', dest='pretend', default=False,
             help='Enable pretend mode', action='store_true')
+    parser.add_option('-e', '--editor', dest='run_editor', default=False,
+            help='Run editor on dictionary', action='store_true')
+    parser.add_option('-V', '--version', dest='version', default=False,
+            help='Show version and exit', action='store_true')
 
     ## Parse the options
     (options, args) = parser.parse_args()
@@ -271,12 +276,35 @@ 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))
 
+    ## Show version if necessary
+    if options.version:
+        print '%s - %s' % (PROGRAM_NAME, PROGRAM_VERSION)
+        print
+        print 'Copyright (c) 2005-2007 Ira W. Snyder (devel@irasnyder.com)'
+        print 'This program comes with ABSOLUTELY NO WARRANTY.'
+        print 'This is free software, and you are welcome to redistribute it'
+        print 'under certain conditions. See the file COPYING for details.'
+
+        sys.exit (0)
+
+    ## Run editor if necessary
+    if options.run_editor:
+        editor = os.getenv ('EDITOR')
+
+        if editor != None:
+            os.system ('%s %s' % (editor, options.dict_file))
+        else:
+            logging.critical ('Default editor could not be found!')
+            sys.exit (1)
+
+        sys.exit (0) # successful, but exit anyway
+
     # Change the loglevel if we're running in quiet mode
     if options.quiet:
         logging.getLogger().setLevel (logging.CRITICAL)
 
-    as = AnimeSorter2(options)
-    as.main()
+    sorter = AnimeSorter2(options)
+    sorter.main()
 
 if __name__ == '__main__':
     main ()