From 32aabb100bfc9ecc538e24122bbbabc568ba0c52 Mon Sep 17 00:00:00 2001 From: beville Date: Tue, 4 Dec 2012 01:16:58 +0000 Subject: [PATCH] Renaming now can use filename, or specified metadata Added an issuestring parser for complex issue numbers with suffixes git-svn-id: http://comictagger.googlecode.com/svn/trunk@194 6c5673fe-1810-88d6-992b-cd32ca31540c --- comictagger.py | 87 ++++++++++++++++++---------------------------- comicvinetalker.py | 7 ++-- issueidentifier.py | 8 ++--- issuestring.py | 82 +++++++++++++++++++++++++++++++++++++++++++ options.py | 4 +-- 5 files changed, 121 insertions(+), 67 deletions(-) create mode 100644 issuestring.py diff --git a/comictagger.py b/comictagger.py index 0741c1a..6ca7215 100755 --- a/comictagger.py +++ b/comictagger.py @@ -43,7 +43,7 @@ from comicarchive import ComicArchive from issueidentifier import IssueIdentifier from genericmetadata import GenericMetadata from comicvinetalker import ComicVineTalker, ComicVineTalkerException - +from issuestring import IssueString import utils import codecs @@ -58,6 +58,28 @@ def cli_mode( opts, settings ): print "Processing: ", f process_file_cli( f, opts, settings ) + +def create_local_metadata( opts, ca, cix, cbi, comet): + + md = GenericMetadata() + + if opts.data_style == MetaDataStyle.CIX and cix: + md = ca.readCIX() + elif opts.data_style == MetaDataStyle.CBI and cbi: + md = ca.readCBI() + elif opts.data_style == MetaDataStyle.COMET and comet: + md = ca.readCoMet() + + # now, overlay the parsed filename info + if opts.parse_filename: + md.overlay( ca.metadataFromFilename() ) + + # finally, use explicit stuff + if opts.metadata is not None: + md.overlay( opts.metadata ) + + return md + def process_file_cli( filename, opts, settings ): @@ -171,26 +193,9 @@ def process_file_cli( filename, opts, settings ): elif opts.save_tags: # OK we're gonna do a save of some new data - md = GenericMetadata() + md = create_local_metadata( opts, ca, cix, cbi, comet) - # First read in existing data, if it's there - if opts.data_style == MetaDataStyle.CIX and cix: - md = ca.readCIX() - elif opts.data_style == MetaDataStyle.CBI and cbi: - md = ca.readCBI() - elif opts.data_style == MetaDataStyle.COMET and comet: - md = ca.readCoMet() - - # now, overlay the new data onto the old, in order - - if opts.parse_filename: - md.overlay( ca.metadataFromFilename() ) - - if opts.metadata is not None: - md.overlay( opts.metadata ) - - - # finally, search online + # now, search online if opts.search_online: ii = IssueIdentifier( ca, settings ) @@ -268,36 +273,8 @@ def process_file_cli( filename, opts, settings ): elif opts.rename_file: - md = GenericMetadata() - # First read in existing data, if it's there - if opts.data_style == MetaDataStyle.CIX and cix: - md = ca.readCIX() - elif opts.data_style == MetaDataStyle.CBI and cbi: - md = ca.readCBI() - elif opts.data_style == MetaDataStyle.COMET and comet: - md = ca.readCoMet() + md = create_local_metadata( opts, ca, cix, cbi, comet) - if md.isEmpty: - print "Comic archive contains no tags!" - - if opts.data_style == MetaDataStyle.CIX: - if cix: - md = ca.readCIX() - else: - print "Comic archive contains no ComicRack tags!" - - if opts.data_style == MetaDataStyle.CBI: - if cbi: - md = ca.readCBI() - else: - print "Comic archive contains no ComicBookLover tags!" - - if opts.data_style == MetaDataStyle.COMET: - if comet: - md = ca.readCoMet() - else: - print "Comic archive contains no CoMet tags!" - # TODO move this to ComicArchive, or maybe another class??? new_name = "" if md.series is not None: @@ -310,7 +287,7 @@ def process_file_cli( filename, opts, settings ): new_name += " v{0}".format( md.volume ) if md.issue is not None: - new_name += " #{:03d}".format( int(md.issue) ) + new_name += " #{0}".format( IssueString(md.issue).asString(pad=3) ) else: print "Can't rename without issue number" return @@ -335,14 +312,16 @@ def process_file_cli( filename, opts, settings ): #HACK #opts.dryrun = True - #HACK - + #HACK + + suffix = "" if not opts.dryrun: # rename the file os.rename( filename, new_abs_path ) else: - print "dry-run option was set, so nothing was changed, but here is the proposed filename:" - print "'{0}'".format(new_abs_path) + suffix = " (dry-run, no change)" + + print "renamed '{0}' -> '{1}' {2}".format(os.path.basename(filename), new_name, suffix) diff --git a/comicvinetalker.py b/comicvinetalker.py index 9d3ae79..d52b7aa 100644 --- a/comicvinetalker.py +++ b/comicvinetalker.py @@ -43,6 +43,7 @@ import utils from settings import ComicTaggerSettings from comicvinecacher import ComicVineCacher from genericmetadata import GenericMetadata +from issuestring import IssueString class ComicVineTalkerException(Exception): pass @@ -203,11 +204,7 @@ class ComicVineTalker(QObject): metadata.series = issue_results['volume']['name'] - # format the issue number string nicely, since it's usually something like "2.00" - num_f = float(issue_results['issue_number']) - num_s = str( int(math.floor(num_f)) ) - if math.floor(num_f) != num_f: - num_s = str( num_f ) + num_s = IssueString(issue['issue_number']).asString() metadata.issue = num_s metadata.title = issue_results['name'] diff --git a/issueidentifier.py b/issueidentifier.py index dbcf7a3..7742b14 100644 --- a/issueidentifier.py +++ b/issueidentifier.py @@ -34,6 +34,7 @@ from genericmetadata import GenericMetadata from comicvinetalker import ComicVineTalker, ComicVineTalkerException from imagehasher import ImageHasher from imagefetcher import ImageFetcher, ImageFetcherException +from issuestring import IssueString import utils @@ -328,13 +329,8 @@ class IssueIdentifier: issue_list = cv_series_results['issues'] for issue in issue_list: + num_s = IssueString(issue['issue_number']).asString() - # format the issue number string nicely, since it's usually something like "2.00" - num_f = float(issue['issue_number']) - num_s = str( int(math.floor(num_f)) ) - if math.floor(num_f) != num_f: - num_s = str( num_f ) - # look for a matching issue number if num_s == keys['issue_number']: # found a matching issue number! now get the issue data diff --git a/issuestring.py b/issuestring.py new file mode 100644 index 0000000..b48079f --- /dev/null +++ b/issuestring.py @@ -0,0 +1,82 @@ +""" +Class for handling the odd permutations of an 'issue number' that the comics industry throws at us +""" + +""" +Copyright 2012 Anthony Beville + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +""" + +import utils +import math +import re + +class IssueString: + def __init__(self, text): + self.text = text + + #strip out non float-y stuff + tmp_num_str = re.sub('[^0-9.-]',"", text ) + + if tmp_num_str == "": + self.num = None + self.suffix = text + + else: + if tmp_num_str.count(".") > 1: + #make sure it's a valid float or int. + parts = tmp_num_str.split('.') + self.num = float( parts[0] + '.' + parts[1] ) + else: + self.num = float( tmp_num_str ) + + self.suffix = "" + parts = text.split(tmp_num_str) + if len( parts ) > 1 : + self.suffix = parts[1] + + def asString( self, pad = 0 ): + #return the float, left size zero-padded, with suffix attached + negative = self.num < 0 + + num_f = abs(self.num) + + num_int = int( num_f ) + num_s = str( num_int ) + if float( num_int ) != num_f: + num_s = str( num_f ) + + num_s += self.suffix + + # create padding + padding = "" + l = len( str(num_int)) + if l < pad : + padding = "0" * (pad - l) + + num_s = padding + num_s + if negative: + num_s = "-" + num_s + + return num_s + + def asFloat( self ): + #return the float, left size zero-padded, with suffix attached + return self.num + + def asInt( self ): + #return the int, left size zero-padded, with suffix attached + return int( self.num ) + + diff --git a/options.py b/options.py index ce2b2bf..d8a1022 100644 --- a/options.py +++ b/options.py @@ -228,6 +228,6 @@ If no options are given, {0} will run in windowed mode if self.save_tags and self.data_style is None: self.display_help_and_quit( "Please specify the type to save with -t", 1 ) - if self.rename_file and self.data_style is None: - self.display_help_and_quit( "Please specify the type to use for renaming with -t", 1 ) + #if self.rename_file and self.data_style is None: + # self.display_help_and_quit( "Please specify the type to use for renaming with -t", 1 )