new file renamer class
git-svn-id: http://comictagger.googlecode.com/svn/trunk@263 6c5673fe-1810-88d6-992b-cd32ca31540c
This commit is contained in:
parent
1efdc0e623
commit
a602c42f0e
@ -43,7 +43,8 @@ from comicarchive import ComicArchive
|
||||
from issueidentifier import IssueIdentifier
|
||||
from genericmetadata import GenericMetadata
|
||||
from comicvinetalker import ComicVineTalker, ComicVineTalkerException
|
||||
from issuestring import IssueString
|
||||
from filerenamer import FileRenamer
|
||||
|
||||
import utils
|
||||
import codecs
|
||||
|
||||
@ -386,34 +387,21 @@ def process_file_cli( filename, opts, settings, match_results ):
|
||||
use_tags = False
|
||||
|
||||
md = create_local_metadata( opts, ca, use_tags )
|
||||
|
||||
# TODO move this to ComicArchive, or maybe another class???
|
||||
new_name = ""
|
||||
if md.series is not None:
|
||||
new_name += "{0}".format( md.series )
|
||||
else:
|
||||
|
||||
if md.series is None:
|
||||
print msg_hdr + "Can't rename without series name"
|
||||
return
|
||||
|
||||
if md.volume is not None:
|
||||
new_name += " v{0}".format( md.volume )
|
||||
|
||||
if md.issue is not None:
|
||||
new_name += " #{0}".format( IssueString(md.issue).asString(pad=3) )
|
||||
#else:
|
||||
# print msg_hdr + "Can't rename without issue number"
|
||||
# return
|
||||
|
||||
if md.issueCount is not None:
|
||||
new_name += " (of {0})".format( md.issueCount )
|
||||
|
||||
if md.year is not None:
|
||||
new_name += " ({0})".format( md.year )
|
||||
|
||||
if ca.isZip():
|
||||
new_name += ".cbz"
|
||||
new_ext = ".cbz"
|
||||
elif ca.isRar():
|
||||
new_name += ".cbr"
|
||||
new_ext = ".cbr"
|
||||
else:
|
||||
new_ext = None # default
|
||||
|
||||
renamer = FileRenamer( md )
|
||||
renamer.setTemplate( "%series% v%volume% %issue% (of %issuecount%) (%year%)" )
|
||||
new_name = renamer.determineName( filename, ext=new_ext )
|
||||
|
||||
if new_name == os.path.basename(filename):
|
||||
print msg_hdr + "Filename is already good!"
|
||||
|
112
filerenamer.py
Normal file
112
filerenamer.py
Normal file
@ -0,0 +1,112 @@
|
||||
"""
|
||||
Functions for renaming files based on metadata
|
||||
"""
|
||||
|
||||
"""
|
||||
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 os
|
||||
import re
|
||||
from issuestring import IssueString
|
||||
|
||||
class FileRenamer:
|
||||
def __init__( self, metadata ):
|
||||
self.setMetadata( metadata )
|
||||
self.setTemplate( "%series% v%volume% #%issue% (of %issuecount%) (%year%)" )
|
||||
self.smart_cleanup = True
|
||||
self.issue_zero_padding = 3
|
||||
|
||||
def setMetadata( self, metadata ):
|
||||
self.metdata = metadata
|
||||
|
||||
def setIssueZeroPadding( self, count ):
|
||||
self.issue_zero_padding = count
|
||||
|
||||
def setSmartCleanup( self, on ):
|
||||
self.smart_cleanup = on
|
||||
|
||||
def setTemplate( self, template ):
|
||||
self.template = template
|
||||
|
||||
def replaceToken( self, text, value, token ):
|
||||
|
||||
#helper func
|
||||
def isToken( word ):
|
||||
return (word[0] == "%" and word[-1:] == "%")
|
||||
|
||||
if value is not None:
|
||||
return text.replace( token, str(value) )
|
||||
else:
|
||||
if self.smart_cleanup:
|
||||
# smart cleanup means we want to remove anything appended to token if it's empty
|
||||
# (e.g "#%issue%" or "v%volume%" )
|
||||
# (TODO: This could fail if there is more than one token appended together, I guess)
|
||||
text_list = text.split()
|
||||
|
||||
#special case for issuecount, remove preceding non-token word, as in "...(of %issuecount%)..."
|
||||
if token == '%issuecount%':
|
||||
for idx,word in enumerate( text_list ):
|
||||
if token in word and not isToken(text_list[idx -1]) :
|
||||
text_list[idx -1] = ""
|
||||
|
||||
text_list = [ x for x in text_list if token not in x ]
|
||||
return " ".join( text_list )
|
||||
else:
|
||||
return text.replace( token, "" )
|
||||
|
||||
def determineName( self, filename, ext=None ):
|
||||
|
||||
md = self.metdata
|
||||
new_name = self.template
|
||||
|
||||
#print u"{0}".format(md)
|
||||
|
||||
new_name = self.replaceToken( new_name, md.series, '%series%')
|
||||
new_name = self.replaceToken( new_name, md.volume, '%volume%')
|
||||
|
||||
if md.issue is not None:
|
||||
issue_str = "{0}".format( IssueString(md.issue).asString(pad=self.issue_zero_padding) )
|
||||
else:
|
||||
issue_str = None
|
||||
new_name = self.replaceToken( new_name, issue_str, '%issue%')
|
||||
|
||||
new_name = self.replaceToken( new_name, md.issueCount, '%issuecount%')
|
||||
new_name = self.replaceToken( new_name, md.year, '%year%')
|
||||
new_name = self.replaceToken( new_name, md.publisher, '%publisher%')
|
||||
new_name = self.replaceToken( new_name, md.title, '%title%')
|
||||
|
||||
if self.smart_cleanup:
|
||||
|
||||
# remove empty braces,brackets, parentheses
|
||||
new_name = re.sub("\(\s*[-:]*\s*\)", "", new_name )
|
||||
new_name = re.sub("\[\s*[-:]*\s*\]", "", new_name )
|
||||
new_name = re.sub("\{\s*[-:]*\s*\}", "", new_name )
|
||||
|
||||
# remove remove duplicate -, _,
|
||||
new_name = re.sub("[-_]+\s+", "- ", new_name )
|
||||
new_name = re.sub("(\s-)+", " -", new_name )
|
||||
|
||||
# remove duplicate spaces
|
||||
new_name = " ".join(new_name.split())
|
||||
|
||||
if ext is None:
|
||||
ext = os.path.splitext( filename )[1]
|
||||
|
||||
new_name += ext
|
||||
|
||||
return new_name
|
||||
|
||||
|
7
todo.txt
7
todo.txt
@ -3,15 +3,17 @@ Features
|
||||
-----------------------------------------------------
|
||||
|
||||
File rename
|
||||
renamer class
|
||||
-Dialog
|
||||
formatting with missing pieces. "Smart Clean-up"
|
||||
|
||||
ComicVine setting: Set Start year as volume
|
||||
|
||||
|
||||
-----------------------------------------------------
|
||||
Bugs
|
||||
-----------------------------------------------------
|
||||
|
||||
Auto-select failure when year is off by one. Maybe check with a wider radius??
|
||||
Avengers 181 - Francois.. encoding error in credits
|
||||
|
||||
-----------------------------------------------------
|
||||
Big Future Features
|
||||
@ -30,6 +32,7 @@ pyComicMetaThis CBI features
|
||||
-----------------------------------------------------
|
||||
Small(er) Future Feature
|
||||
-----------------------------------------------------
|
||||
Parse out the rest of the scan info from filename
|
||||
|
||||
Style sheets for windows/mac/linux
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user