def rename_item(self, path, name, ext, original): newName = self.join_ext(name, ext) if not newName: return path, name, ext # basic settings search = self.search searchValues = search.searchValues if searchValues[0] == u"reg-exp" and searchValues[1]: isRegExp = True else: isRegExp = False # do the modifications based on selected operations # regular expression if isRegExp: try: newName = searchValues[2].sub(self.comds[self.op], newName) except (sre_constants.error, AttributeError) as err: main.set_status_msg(_(u"Regular-Expression: %s") % err, u'warn') # so we know not to change status text after RE error msg: app.REmsg = True pass # show RE error message from search, if any if search.REmsg: main.set_status_msg(search.REmsg, u'warn') app.REmsg = True # text elif searchValues[0] == u"text" and searchValues[2]: find = searchValues[2] #case insensitive if not searchValues[1]: found = search.case_insensitive(newName) for match in found: newName = newName.replace(match, self.comds[self.op](find)) #case sensitive: else: newName = newName.replace(find, self.comds[self.op](find)) # between elif searchValues[0] == u"between": positions = search.get_between_to_from(newName) if positions: frm = positions[0] to = positions[1] newName = newName[:frm] + self.comds[self.op](newName[frm:to]) + newName[to:] # positioning: elif searchValues[0] == u"position": ss = search.repl_from.GetValue() sl = search.repl_len.GetValue() frm, to, tail = search.get_start_end_pos(ss, sl, newName) # apply positioning newName = newName[:frm] + self.comds[self.op](newName[frm:to]) + tail #nothing set, change entire name: else: newName = self.comds[self.op](newName) name, ext = self.split_ext(newName, name, ext) return path, name, ext
def find_in_name(self, newName, search, searchValues): # regular expression: frm = 0 to = 0 if searchValues[0] == u"reg-exp" and searchValues[1]: try: match = searchValues[2].findall(newName)[0] frm = newName.index(match) to = frm + len(match) except (AttributeError, IndexError): pass # show RE error message from search, if any if search.REmsg: main.set_status_msg(search.REmsg, u'warn') app.REmsg = True # text: elif searchValues[0] == u"text" and searchValues[2]: #case insensitive if not searchValues[1]: found = search.case_insensitive(newName) if len(found) > 0: match = found[0] frm = newName.index(match) to = frm + len(match) # case sensitive elif searchValues[2] in newName: match = searchValues[2] frm = newName.index(match) to = frm + len(match) # position elif searchValues[0] == u"position": ss = search.repl_from.GetValue() sl = search.repl_len.GetValue() frm, to = utils.calc_slice_pos(ss, sl) # between elif searchValues[0] == u"between": positions = search.get_between_to_from(newName) if positions: frm = positions[0] to = positions[1] return (frm, to)
def rename_item(self, path, name, ext, original): newName = self.join_ext(name, ext) if not newName: return path, name, ext # basic settings search = self.search searchValues = search.searchValues # calculate initial values moveTo = u':Do_Not_Move:' moveMod = self.replMoveTextMod.GetStringSelection() # by position: if self.replMovePos.GetValue(): moveByPosition = True moveTo = self.replMovePosValue.GetValue() # by searching for text: else: moveByPosition = False moveTxt = self.replMoveTextValue.GetValue() # regular expression: if self.regExpPanel.regExpr.GetValue(): self.moveRE = self.regExpPanel.create_regex(moveTxt) # default to not finding anything: moveMatch = "" # FIRST FIND THE ORIGINAL # regular expression: if searchValues[0] == u"reg-exp" and searchValues[1]: try: moveMatch = searchValues[2].findall(newName)[0] except (AttributeError, IndexError): pass # show RE error message from search, if any if search.REmsg: main.set_status_msg(search.REmsg, u'warn') app.REmsg = True # text: elif searchValues[0] == u"text" and searchValues[2]: find = searchValues[2] #case insensitive if not searchValues[1]: found = search.case_insensitive(newName) if len(found) > 0: moveMatch = found[0] # case sensitive: elif find in newName: moveMatch = find # position elif searchValues[0] == u"position": ss = search.repl_from.GetValue() sl = search.repl_len.GetValue() frm, to = utils.calc_slice_pos(ss, sl) moveMatch = newName[frm:to] # between elif searchValues[0] == u"between": positions = search.get_between_to_from(newName) if positions: frm = positions[0] to = positions[1] moveMatch = newName[frm:to] # remove the original, saving a backup in case no match found: oldName = newName newName = newName.replace(moveMatch, "", 1) # now find where to move to if not by position: if not moveByPosition: if moveTxt: # text has to contain something # regular expression: if self.moveRE: moveTxt = self.moveRE.findall(newName) # if match is found, get first item in list: if len(moveTxt) > 0: moveTxt = moveTxt[0] else: moveTxt = '' # get the index of match: try: moveTo = newName.index(moveTxt) except (ValueError): moveTo = u':Do_Not_Move:' pass else: if moveMod == _(u"after"): moveTo = moveTo + len(moveTxt) elif moveMod != _(u"before"): moveTo = moveMod else: moveTo = u':Do_Not_Move:' # finally recreate string: if moveTo != u':Do_Not_Move:': # position specified: if moveTo != moveMod and moveByPosition: if moveTo == -1: newName = newName + moveMatch elif moveTo < -1: newName = newName[:moveTo + 1] + moveMatch + newName[moveTo + 1:] else: newName = newName[:moveTo] + moveMatch + newName[moveTo:] # text specified else: if moveTo == _(u"replace") and moveMatch: newName = newName.replace(moveTxt, moveMatch, 1) elif moveMatch: newName = newName[:moveTo] + moveMatch + newName[moveTo:] del oldName # no match found else: newName = oldName name, ext = self.split_ext(newName, name, ext) return path, name, ext
def rename_item(self, path, name, ext, original): newName = self.join_ext(name, ext) if not newName: return path, name, ext # basic settings search = self.search searchValues = search.searchValues if searchValues[0] == u"reg-exp" and searchValues[1]: isRegExp = True else: isRegExp = False # do the modifications based on selected operations # regular expression if isRegExp: try: newName = searchValues[2].sub(self.comds[self.op], newName) except (sre_constants.error, AttributeError) as err: main.set_status_msg( _(u"Regular-Expression: %s") % err, u'warn') # so we know not to change status text after RE error msg: app.REmsg = True pass # show RE error message from search, if any if search.REmsg: main.set_status_msg(search.REmsg, u'warn') app.REmsg = True # text elif searchValues[0] == u"text" and searchValues[2]: find = searchValues[2] #case insensitive if not searchValues[1]: found = search.case_insensitive(newName) for match in found: newName = newName.replace(match, self.comds[self.op](find)) #case sensitive: else: newName = newName.replace(find, self.comds[self.op](find)) # between elif searchValues[0] == u"between": positions = search.get_between_to_from(newName) if positions: frm = positions[0] to = positions[1] newName = newName[:frm] + self.comds[self.op]( newName[frm:to]) + newName[to:] # positioning: elif searchValues[0] == u"position": ss = search.repl_from.GetValue() sl = search.repl_len.GetValue() frm, to, tail = search.get_start_end_pos(ss, sl, newName) # apply positioning newName = newName[:frm] + self.comds[self.op]( newName[frm:to]) + tail #nothing set, change entire name: else: newName = self.comds[self.op](newName) name, ext = self.split_ext(newName, name, ext) return path, name, ext