""" Insert Alternating A and B after counter Plugin =============================================== Sample class which can be used as a plugin for Peter's Flexible Renaming Kit (PFrank) Just copy the class definition (no need to copy the other declarations or test code) to your PFrankUser.py file and add a reference to the class in the PFrankUser.py 'UserRenamerList' table. Or just rename this file as PFrankUser.py and move it to your PFrank install folder if you don't want any other plugins. More details can be found in the PFrankUser.py.template file that is included with the PFrank download. Feb 1, 2008 Initial Creation 0.1 """ ########################################## # list your imported modules here. # If you get import errors, then the modules are not # present in the environment. In this case you will have to # insert the required module in the PFrank install folder. ########################################## # start of user imported modules import re import os # end of user imported modules. ########################################## # The following modules must always be imported ########################################## # start of required imported modules from SearchRep_init import configurator from SearchRepCase import SearchRepLower # end of required imported modules class CountNumberAB (SearchRepLower) : """ This is a sample renamer class. The SearchRepLower base class is mandatory. This plugin command changes names to numbered sequences where either the letter 'a' or 'b' are added to the number. """ def __init__ (self) : """ this is the initilaizer for the classee. This is mandatory. """ # call base class initializer. This is mandatory SearchRepLower.__init__(self) # This is the ID string that will appear in the pre-defined command # pull down list. It will also appear in the custom list when # inserted. The id will also be used in the scan summary # The name 'self.idstring' must not change. Set the assigned string # on the right-hand side of the assignment statement to whatever # name you want. self.idstring = "(User) - Counter Plus a,b" # this call should remain here self.initforscan() def initforscan(self) : """ This function is called whenever a new scan or rescan is made. Use it to reset anything that needs to be reinitialized before a scan. This function is mandatory. """ self.number = 1 self.letter = 'a' pass def fixnames(self, filename) : """ This function converts name of file to a new name. Input: string representing the old filename or portion (either All, Prefix, or Extension) The name does not include the path to the file. Output: change indicator True if change to input string occurred, otherwise False string representing the new filename When the old filename string is passed into this routine, then depending on where this user command is placed in the custom list, the string could be in the middle of a transformation. e.g. if the original filename was ABCDEF.jpg, and the first command of the custom list inserted a counter in front of the name, and the second command was this user command, then the filename string that is passed in could be something like 0010-ABCDEF.jpg. This name of course no longer looks like the original name. If you needed to extract meta data from the file, then you would not be able to use the passed in string. Therefore the global variable 'configurator.filename' is provided which has the full path to the filename. You can then just open that name, extract the meta data, and then close the file. """ # full path to original filename fullPathForFile = configurator.filename prefix, extension = os.path.splitext(filename) ######################################################################### # Insert code to modify the filename here ######################################################################### # count a, b, then increment number counter prefix = "%04d"%self.number + str(self.letter) # increment letter letter = ord(self.letter) + 1 self.letter = chr (letter) if self.letter == 'c' : self.letter = 'a' self.number += 1 newname = prefix + extension ######################################################################### # these statements are mandatory ######################################################################### change = False if filename != newname : change = True return change, newname ######################################################################### # end of the class definition ######################################################################### ################################################################### # Create the Renamer Objects in a list. The list is mandatory. # The name of the list must not change. # You can list as many names in the list as you like. PFrank will try to # load all of them. Each name in the list must correspond to the # name of a user command renaming class. A name can only appear # once in the list. # # User Command Objects are created with the following syntax: # ClassName() # i.e. the name of the class followed by () ################################################################### UserRenamerList = [ #UserRenamer1(), # object created for first user renaming class #UserRenamer2(), # object created for second user renaming class CountNumberAB(), ] ###################################### # Code for testing the renamer objects ###################################### if __name__ == '__main__' : """ This is some test code to test the renaming objects. Test the code using the python IDLE tool to verify that things work. Then try importing the file to PFrank """ print "Testing User Defined Renaming Classes" fullpathTable = [ #"TestFile.txt", #"TestFile1.jpg", #"Test32File3.mp3", #"Test1.mp3", #"33T.ogg", "C:\Documents and Settings\peter\Desktop\\neftest\sample.mp3", "C:\Documents and Settings\peter\Desktop\\neftest\sample2.mp3", "C:\Documents and Settings\peter\Desktop\\neftest\samplethree.mp3", "C:\Documents and Settings\peter\Desktop\\neftest\samplefour.mp3", "C:\Documents and Settings\peter\Desktop\\neftest\samplefice.mp3", ] for renamer in UserRenamerList : print "*************Testing Renamer: ", renamer.idstring for path in fullpathTable : # save the full path in configurator object just as PFrank does configurator.filename = path basename, name = os.path.split(path) # call fixnames with name of file (or folder) change, newname = renamer.fixnames(name) print "Oldname is: ", name print "Newname is: ", newname assert (change == (not name == newname)) print ""