Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apply foldersort after folderfilter #27

Closed
lahwaacz opened this issue Mar 17, 2013 · 11 comments
Closed

apply foldersort after folderfilter #27

lahwaacz opened this issue Mar 17, 2013 · 11 comments

Comments

@lahwaacz
Copy link

Hi,
the 'foldersort' option doesn't work, it's completely ignored. Also there's no mention of it in man page.

@shiquanwang
Copy link

I also noticed that.
It seems now the input args for the foldersort is of class IMAPfolder and has no method startswith.

@ghost ghost assigned konvpalto Aug 27, 2013
@konvpalto
Copy link
Member

8b6f10e in the "next" branch should fix that. Please, test.

@lahwaacz
Copy link
Author

I was able to achieve what I wanted, foldersort now works as expected. I was just wondering, why foldersort is applied before folderfilter? This way I have to manually check in the compare function if the folder names are filtered out or not.

@konvpalto
Copy link
Member

Sorting and excluding should be commutative: filtering before sorting will give you the same results as sorting after filtering if filtering has no side effects on the folders. I agree that sorting after filtering will be faster because one has to sort less, but may be I don't understand some implications of your situation? Why do you need to check filtered folders inside sort comparator?

@lahwaacz
Copy link
Author

I construct mapping dictionary for nametrans from two lists of keys and values:

mapping_remote = ['INBOX', '[Gmail]/Drafts', '[Gmail]/Spam', ...]
mapping_local = ['INBOX', 'drafts', 'spam', ...]
mapping_gmail = dict(zip(mapping_remote, mapping_local))

This is used by both nametrans and folderfilter:

def nt_remote(mapping):
    def inner(folder):
        try:
            return mapping[folder]
        except:
            return folder
    return inner

def nt_local(mapping):
    r_mapping = dict(zip(mapping.values(), mapping.keys()))
    def inner(folder):
        try:
            return r_mapping[folder]
        except:
            return folder
    return inner

def exclude(mapping):
    def inner(folder):
        if folder in mapping.keys():
            return True
        return False
    return inner

In config, that's used like this:

nametrans = nt_remote(mapping_gmail)
folderfilter = exclude(mapping_gmail)

If sorting would be done after filtering, I could use sort comparator based simply on mapping_local.index(item), that's simply

def folder_cmp(lst):
    def inner(x, y):
        return cmp(lst.index(x), lst.index(y))
    return inner

and of course in config:

foldersort = folder_cmp(mapping_local)

Instead I use this horrible thing:

def folder_cmp(lst):
    def inner(x, y):
        if x in lst and y in lst:
            return cmp(lst.index(x), lst.index(y))
        elif x in lst:
            return -1
        elif y in lst:
            return 1
        else:
            return 0
    return inner

@konvpalto
Copy link
Member

Well, it turns out that it is not that simple to achieve what you want, but I am working on that. Might take a couple weeks, though: EBUSY most of the time.

aroig pushed a commit to aroig/offlineimap that referenced this issue Dec 24, 2013
Bring the description in the template offlineimap.conf in sync to the
actual implementation: pass folder names to the sorting function, not
the offlineimap.folder.IMAP.IMAPFolder objects themselves.

GitHub issue: OfflineIMAP#27
Signed-off-by: Eygene Ryabinkin <rea@codelabs.ru>
@nicolas33 nicolas33 changed the title 'foldersort' option doesn't work apply foldersort _after_ folderfilter Mar 24, 2015
@nicolas33 nicolas33 changed the title apply foldersort _after_ folderfilter apply foldersort after folderfilter Mar 24, 2015
@nicolas33
Copy link
Member

Looked at this and as previously said, it's far from trivial patch. Don't expect changes any time soon.

@nicolas33 nicolas33 assigned nicolas33 and unassigned konvpalto Mar 24, 2015
@nicolas33 nicolas33 added this to the undefined milestone Mar 24, 2015
@nicolas33 nicolas33 removed their assignment Mar 26, 2015
@nicolas33
Copy link
Member

nicolas33 commented Jun 24, 2016

Both compare features are:

I guess there's a way to use the same key sorting for both. I'm using none of those features. Could anyone try to make both identical in behaviour?

Also, I think we might stop supporting the "cmp" way of sorting folder names for Python2.

@nicolas33
Copy link
Member

Instead of

def folder_cmp(lst):
    def inner(x, y):
        if x in lst and y in lst:
            return cmp(lst.index(x), lst.index(y))
        elif x in lst:
            return -1
        elif y in lst:
            return 1
        else:
            return 0
    return inner

I think the following code would provide the expected behaviour:

def folder_cmp(lst):
    def inner(x, y):
        try:
            return cmp(lst.index(x), lst.index(y))
        except KeyError:
            return 0
    return inner

Anyway, since this was not implemented for years, I'm closing. I guess that the benefit don't worth the trouble.

@lahwaacz
Copy link
Author

Your solution does not consider the case when only one of the arguments is not in the list, in which case the result should be either 1 or -1. Hence it is not a correct comparator.

@nicolas33
Copy link
Member

Yes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants