From 50909962d364484ab14841ae5bc06c1e42acbfa6 Mon Sep 17 00:00:00 2001 From: "beville@gmail.com" Date: Wed, 6 Feb 2013 00:14:37 +0000 Subject: [PATCH] Implemented export to zip on command line git-svn-id: http://comictagger.googlecode.com/svn/trunk@430 6c5673fe-1810-88d6-992b-cd32ca31540c --- comictagger.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ options.py | 33 +++++++++++++++++++++-------- 2 files changed, 81 insertions(+), 9 deletions(-) diff --git a/comictagger.py b/comictagger.py index e50d088..f94f0c8 100755 --- a/comictagger.py +++ b/comictagger.py @@ -222,6 +222,10 @@ def process_file_cli( filename, opts, settings, match_results ): if settings.rar_exe_path != "": ca.setExternalRarProgram( settings.rar_exe_path ) + if not os.path.lexists( filename ): + print >> sys.stderr,"Cannot find "+ filename + return + if not ca.seemsToBeAComicArchive(): print >> sys.stderr,"Sorry, but "+ filename + " is not a comic archive!" return @@ -485,6 +489,59 @@ def process_file_cli( filename, opts, settings, match_results ): print u"renamed '{0}' -> '{1}' {2}".format(os.path.basename(filename), new_name, suffix) + elif opts.export_to_zip: + msg_hdr = "" + if batch_mode: + msg_hdr = u"{0}: ".format(filename) + + if not ca.isRar(): + print >> sys.stderr, msg_hdr + "Archive is not a RAR." + return + + rar_file = os.path.abspath( os.path.abspath( filename ) ) + new_file = os.path.splitext(rar_file)[0] + ".cbz" + + if opts.abort_export_on_conflict and os.path.lexists( new_file ): + print msg_hdr + "{0} already exists in the that folder.".format(os.path.split(new_file)[1]) + return + + new_file = utils.unique_file( os.path.join( new_file ) ) + + delete_success = False + export_success = False + if not opts.dryrun: + if ca.exportAsZip( new_file ): + export_success = True + if opts.delete_rar_after_export: + try: + os.unlink( rar_file ) + except: + print >> sys.stderr, msg_hdr + "Error deleting original RAR after export" + delete_success = False + else: + delete_success = True + else: + # last export failed, so remove the zip, if it exists + if os.path.lexists( new_file ): + os.remove( new_file ) + else: + msg = msg_hdr + u"Dry-run: Would try to create {0}".format(os.path.split(new_file)[1]) + if opts.delete_rar_after_export: + msg += u" and delete orginal." + print msg + return + + msg = msg_hdr + if export_success: + msg += u"Archive exported successfully to: {0}".format( os.path.split(new_file)[1] ) + if opts.delete_rar_after_export and delete_success: + msg += u" (Original deleted) " + else: + msg += u"Archive failed to export!" + + print msg + + #----------------------------- def main(): diff --git a/options.py b/options.py index 3444c01..929b778 100644 --- a/options.py +++ b/options.py @@ -78,12 +78,16 @@ If no options are given, {0} will run in windowed mode Some names that can be used: series, issue, issueCount, year, publisher, title -r, --rename Rename the file based on specified tag style. - --noabort Don't abort save operation when online match is of low confidence + --noabort Don't abort save operation when online match is of low confidence + -e, --export-to-zip Export RAR archive to Zip format + --delete-rar Delete original RAR archive after successful export to Zip + --abort-on-conflict Don't export to zip if intended new filename exists (Otherwise, creates + a new unique filename) -v, --verbose Be noisy when doing what it does --terse Don't say much (for print mode) --version Display version -h, --help Display this message - """ + """ def __init__(self): @@ -96,6 +100,9 @@ If no options are given, {0} will run in windowed mode self.print_tags = False self.copy_tags = False self.delete_tags = False + self.export_to_zip = False + self.abort_export_on_conflict = False + self.delete_rar_after_export = False self.search_online = False self.dryrun = False self.abortOnLowConfidence = True @@ -179,14 +186,15 @@ If no options are given, {0} will run in windowed mode # parse command line options try: opts, args = getopt.getopt( input_args, - "hpdt:fm:vonsrc:i", + "hpdt:fm:vonsrc:ie", [ "help", "print", "delete", "type=", "copy=", "parsefilename", "metadata=", "verbose", "online", "dryrun", "save", "rename" , "raw", "noabort", "terse", "nooverwrite", - "interactive", "nosummary", "version", "id=" ]) - + "interactive", "nosummary", "version", "id=" + "export-to-zip", "delete-rar", "abort-on-conflict" ] ) + except getopt.GetoptError as err: self.display_msg_and_quit( str(err), 2 ) - + # process options for o, a in opts: if o in ("-h", "--help"): @@ -219,6 +227,12 @@ If no options are given, {0} will run in windowed mode self.save_tags = True if o in ("-r", "--rename"): self.rename_file = True + if o in ("-e", "--export_to_zip"): + self.export_to_zip = True + if o == "--delete-rar": + self.delete_rar_after_export = True + if o == "--abort-on-conflict": + self.abort_export_on_conflict = True if o in ("-f", "--parsefilename"): self.parse_filename = True if o == "--id": @@ -247,7 +261,7 @@ If no options are given, {0} will run in windowed mode else: self.display_msg_and_quit( "Invalid tag type", 1 ) - if self.print_tags or self.delete_tags or self.save_tags or self.copy_tags or self.rename_file: + if self.print_tags or self.delete_tags or self.save_tags or self.copy_tags or self.rename_file or self.export_to_zip: self.no_gui = True count = 0 @@ -256,9 +270,10 @@ If no options are given, {0} will run in windowed mode if self.save_tags: count += 1 if self.copy_tags: count += 1 if self.rename_file: count += 1 + if self.export_to_zip: count +=1 if count > 1: - self.display_msg_and_quit( "Must choose only one action of print, delete, save, copy, or rename", 1 ) + self.display_msg_and_quit( "Must choose only one action of print, delete, save, copy, rename, or export", 1 ) if len(args) > 0: if platform.system() == "Windows": @@ -273,7 +288,7 @@ If no options are given, {0} will run in windowed mode self.file_list = args if self.no_gui and self.filename is None: - self.display_msg_and_quit( "Command requires a filename!", 1 ) + self.display_msg_and_quit( "Command requires at least one filename!", 1 ) if self.delete_tags and self.data_style is None: self.display_msg_and_quit( "Please specify the type to delete with -t", 1 )