From 10dd317026fbbb62d2f2b2edad5ac8a0f20b2da9 Mon Sep 17 00:00:00 2001 From: Sebastian Spaeth Date: Thu, 16 Feb 2012 11:03:33 +0100 Subject: [PATCH] Folder: Implement __eq__ for folders This allows to compare folders directly with strings. It also allows constructs such as "if 'moo' in repo.getfolders()". See the code documentation for the exact behavior (it basically is equal if it's the same instance *or* a string matching the untranslated folder name. Signed-off-by: Sebastian Spaeth --- offlineimap/folder/Base.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/offlineimap/folder/Base.py b/offlineimap/folder/Base.py index 1c05370..fcea99d 100644 --- a/offlineimap/folder/Base.py +++ b/offlineimap/folder/Base.py @@ -479,3 +479,20 @@ class BaseFolder(object): self.ui.error(e, exc_info()[2], "Syncing folder %s [acc: %s]" %\ (self, self.accountname)) raise # raise unknown Exceptions so we can fix them + + def __eq__(self, other): + """Comparisons work either on string comparing folder names or + on the same instance + + MailDirFolder('foo') == 'foo' --> True + a = MailDirFolder('foo'); a == b --> True + MailDirFolder('foo') == 'moo' --> False + MailDirFolder('foo') == IMAPFolder('foo') --> False + MailDirFolder('foo') == MaildirFolder('foo') --> False + """ + if isinstance(other, basestring): + return other == self.name + return id(self) == id(other) + + def __ne__(self, other): + return not self.__eq__(other)