[PATCH] Draft of maxfetch option

Łukasz Żarnowiecki dolohow at outlook.com
Thu May 5 18:41:41 BST 2016


Signed-off-by: Łukasz Żarnowiecki <dolohow at outlook.com>
---

This is just a draft for you to take a look if this option actually make
any sense.  I thought about it and I think it might be useful for
someone.  I am wondering if it is possible to pass search condition like
you did in "_msg_to_fetch" for "imapobj.search".  If not, I probably
should use "cachemessagelist" where I can slice the list, but I guess
the messages are not sorted by date.  Any hint would be valuable.

Please take a look I tell me what you think.


 offlineimap.conf              | 16 ++++++++++++++++
 offlineimap/folder/Base.py    |  4 ++++
 offlineimap/folder/Maildir.py |  3 +++
 3 files changed, 23 insertions(+)

diff --git a/offlineimap.conf b/offlineimap.conf
index 6f05dcc..706bee7 100644
--- a/offlineimap.conf
+++ b/offlineimap.conf
@@ -364,6 +364,22 @@ remoterepository = RemoteExample
 
 # This option stands in the [Account Test] section.
 #
+# maxfetch enables you to sync only the last specified number of emails. This is
+# useful when you have limited bandwidth or when you know that the messages
+# could be rotated on the IMAP server.
+#
+# This works per folder. For OfflineIMAP it will be like these messages do not
+# exist.
+#
+# maxfetch is allowed only when the local folder of type Maildir.
+#
+# The maxfetch option expects an integer.
+#
+#maxfetch = 3
+
+
+# This option stands in the [Account Test] section.
+#
 # Maildir file format uses colon (:) separator between uniq name and info.
 # Unfortunatelly colon is not allowed character in windows file name. If you
 # enable maildir-windows-compatible option, OfflineIMAP will be able to store
diff --git a/offlineimap/folder/Base.py b/offlineimap/folder/Base.py
index 14b0867..d4d3c84 100644
--- a/offlineimap/folder/Base.py
+++ b/offlineimap/folder/Base.py
@@ -306,6 +306,10 @@ class BaseFolder(object):
 
         return len(self.getmessagelist())
 
+    def getmaxfetch(self):
+        return self.config.getdefaultint("Account %s"%
+            self.accountname, "maxfetch", None)
+
     def getmessage(self, uid):
         """Returns the content of the specified message."""
 
diff --git a/offlineimap/folder/Maildir.py b/offlineimap/folder/Maildir.py
index 77a774b..718b233 100644
--- a/offlineimap/folder/Maildir.py
+++ b/offlineimap/folder/Maildir.py
@@ -152,6 +152,7 @@ class MaildirFolder(BaseFolder):
         """
 
         maxsize = self.getmaxsize()
+        maxfetch = self.getmaxfetch()
 
         retval = {}
         files = []
@@ -162,6 +163,8 @@ class MaildirFolder(BaseFolder):
                          filename in os.listdir(fulldirname))
 
         date_excludees = {}
+        if maxfetch:
+            files = files[-maxfetch:]
         for dirannex, filename in files:
             if filename.startswith('.'):
                 continue # Ignore dot files.
-- 
2.8.2





More information about the OfflineIMAP-project mailing list