kivymd / KivyMD

KivyMD is a collection of Material Design compliant widgets for use with Kivy, a framework for cross-platform, touch-enabled graphical applications. https://youtube.com/c/KivyMD https://twitter.com/KivyMD https://habr.com/ru/users/kivymd https://stackoverflow.com/tags/kivymd
https://kivymd.readthedocs.io
MIT License
2.14k stars 655 forks source link

MDFileManager sort_by function #1672

Open grekichi opened 2 months ago

grekichi commented 2 months ago

Description of the Bug

Hi there !

I try to use MDFilemanager's 'sort_by' fucntion for file open command creation. But I cannnot open files when I set 'date' and 'size' for 'sort_by'. 'name' and 'type' are no problem.

So I checked source data of 'MDFileManager'. And I found out this cause as follows. Return object are file name, not file path.

Is this the intended design or bug ?

Thanks in advance Grekichi

Code and Logs

# About kivymd/uix/filemanager/filemanager.py

# from row no. 810

    def __sort_files(self, files):
        def sort_by_name(files):
            files.sort(key=locale.strxfrm)
            files.sort(key=str.casefold)
            return files

        if self.sort_by == "name":
            sorted_files = sort_by_name(files)
        elif self.sort_by == "date":
            _files = sort_by_name(files)
            _sorted_files = [os.path.join(self.current_path, f) for f in _files]
            _sorted_files.sort(key=os.path.getmtime, reverse=True)
            **sorted_files = [os.path.basename(f) for f in _sorted_files]** ←
        elif self.sort_by == "size":
            _files = sort_by_name(files)
            _sorted_files = [os.path.join(self.current_path, f) for f in _files]
            _sorted_files.sort(key=os.path.getsize, reverse=True)
            **sorted_files = [os.path.basename(f) for f in _sorted_files]** ←
        elif self.sort_by == "type":
            _files = sort_by_name(files)
            sorted_files = sorted(
                _files,
                key=lambda f: (os.path.splitext(f)[1], os.path.splitext(f)[0]),
            )
        else:
            sorted_files = files

# my idea:  "sorted_files = _sorted_files" instead of target rows

Versions