Esempio n. 1
0
 def get_start_end_pos(self, ss, sl, newName):
     """Get the start and end positions of the given name."""
     frm, to = utils.calc_slice_pos(ss, sl)
     if to == None:
         tail = ''
     else:
         tail = newName[to:]
     return frm, to, tail
Esempio n. 2
0
 def get_start_end_pos(self, ss, sl, newName):
     """Get the start and end positions of the given name."""
     frm, to = utils.calc_slice_pos(ss, sl)
     if to == None:
         tail = ''
     else:
         tail = newName[to:]
     return frm, to, tail
Esempio n. 3
0
    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)
Esempio n. 4
0
    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)
Esempio n. 5
0
    def sort_items(self, joinedItems):
        """Sorting the given list of items."""
        # load 'em up
        params = self.params.load()

        # Create sorting helper function based on user selections
        #
        # sorting by section
        if params.byPosition:
            s = params.PosStart
            l = params.PosLength
            frm, to = utils.calc_slice_pos(s, l)
            def _folder_key(x):
                return os.path.basename(x[0])[frm:to].lower()
            def _files_key(x):
                dir, file = os.path.split(x[0])
                return (dir, file[frm:to].lower())

        # inteligent number sorting
        elif params.intelySort:
            # return the number in the name
            def _get_numb(x):
                try:
                    compare = float(re.sub('\D', '', x[0]))
                except ValueError:
                    compare = x[0]
                return compare
            # sort by the number
            def _folder_key(x):
                return _get_numb(x)
            def _files_key(x):
                compare = _get_numb(x)
                return (os.path.dirname(x[0]), compare)

        # sort by item attributes
        elif params.statSort:
            selection = params.statSortChoice

            # retrieval from Exif tags
            if selection > 6:
                def _folder_key(x):
                    return 0
                def _files_key(x):
                    return self._get_from_exif(x[0], selection)

            # retrieval from os.stat
            else:
                # choices are in different order than os.stat
                ref = ('st_ctime', 'st_mtime', 'st_atime', 'st_size', 'st_mode',
                       'st_uid', 'st_gid')
                s = ref[selection]
                def _folder_key(x):
                    stat = os.stat(x[0])
                    return getattr(stat, s)
                def _files_key(x):
                    stat = os.stat(x[0])
                    return getattr(stat, s)

        # normal sorting
        else:
            def _folder_key(x):
                return x[0].lower()
            def _files_key(x):
                return (os.path.dirname(x[0]), x[0].lower())

        # Now apply created helper defs to sort
        #
        # sort items automatically
        if not params.manually:
            main.currentItem = None
            self.fc = 0
            def _dirs_top(f):
                if f[1] == 0:
                    self.fc += 1
                return f[1]
            def _dirs_bottom(f):
                if f[1] == 0:
                    return 1
                else:
                    self.fc += 1
                    return 0

            # place directories where in list?
            if params.dirsPlace == 0:
                joinedItems.sort(key=_dirs_top)
            if params.dirsPlace == 1:
                joinedItems.sort(key=_dirs_bottom)

            # separate files from folders
            folders = joinedItems[:self.fc]
            files = joinedItems[self.fc:]

            # sort both
            folders.sort(key=_folder_key)
            files.sort(key=_files_key)

            # join back together
            joinedItems[:self.fc] = folders
            joinedItems[self.fc:] = files

            # reverse sort list?
            if params.descending:
                joinedItems.reverse()
        return joinedItems
Esempio n. 6
0
    def sort_items(self, joinedItems):
        """Sorting the given list of items."""
        # load 'em up
        params = self.params.load()

        # Create sorting helper function based on user selections
        #
        # sorting by section
        if params.byPosition:
            s = params.PosStart
            l = params.PosLength
            frm, to = utils.calc_slice_pos(s, l)

            def _folder_key(x):
                return os.path.basename(x[0])[frm:to].lower()

            def _files_key(x):
                dir, file = os.path.split(x[0])
                return (dir, file[frm:to].lower())

        # inteligent number sorting
        elif params.intelySort:
            # return the number in the name
            def _get_numb(x):
                try:
                    compare = float(re.sub('\D', '', x[0]))
                except ValueError:
                    compare = x[0]
                return compare

            # sort by the number
            def _folder_key(x):
                return _get_numb(x)

            def _files_key(x):
                compare = _get_numb(x)
                return (os.path.dirname(x[0]), compare)

        # sort by item attributes
        elif params.statSort:
            selection = params.statSortChoice

            # retrieval from Exif tags
            if selection > 6:

                def _folder_key(x):
                    return 0

                def _files_key(x):
                    return self._get_from_exif(x[0], selection)

            # retrieval from os.stat
            else:
                # choices are in different order than os.stat
                ref = ('st_ctime', 'st_mtime', 'st_atime', 'st_size',
                       'st_mode', 'st_uid', 'st_gid')
                s = ref[selection]

                def _folder_key(x):
                    stat = os.stat(x[0])
                    return getattr(stat, s)

                def _files_key(x):
                    stat = os.stat(x[0])
                    return getattr(stat, s)

        # normal sorting
        else:

            def _folder_key(x):
                return x[0].lower()

            def _files_key(x):
                return (os.path.dirname(x[0]), x[0].lower())

        # Now apply created helper defs to sort
        #
        # sort items automatically
        if not params.manually:
            main.currentItem = None
            self.fc = 0

            def _dirs_top(f):
                if f[1] == 0:
                    self.fc += 1
                return f[1]

            def _dirs_bottom(f):
                if f[1] == 0:
                    return 1
                else:
                    self.fc += 1
                    return 0

            # place directories where in list?
            if params.dirsPlace == 0:
                joinedItems.sort(key=_dirs_top)
            if params.dirsPlace == 1:
                joinedItems.sort(key=_dirs_bottom)

            # separate files from folders
            folders = joinedItems[:self.fc]
            files = joinedItems[self.fc:]

            # sort both
            folders.sort(key=_folder_key)
            files.sort(key=_files_key)

            # join back together
            joinedItems[:self.fc] = folders
            joinedItems[self.fc:] = files

            # reverse sort list?
            if params.descending:
                joinedItems.reverse()
        return joinedItems
Esempio n. 7
0
    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
Esempio n. 8
0
    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