[PATCH v2] learn --mbnames-prune CLI option

Nicolas Sebrecht nicolas.s-dev at laposte.net
Tue Jul 5 23:41:38 BST 2016


On Tue, Jul 05, 2016 at 11:29:09PM +0200, Łukasz Żarnowiecki wrote:
> On Sun, Jun 26, 2016 at 06:39:46PM +0200, Nicolas Sebrecht wrote:
> > This is usefull to remove dangling entries for removed accounts or if mbnames is
> > not enabled anymore.
> 
> I was wondering if it would be possible that those accounts could be
> detected based on configuration.
> 
> If account is no longer defined in configuration the remove mbnames
> associated with it.

That's what it does.

> > --- a/offlineimap/mbnames.py
> > +++ b/offlineimap/mbnames.py

<...>

> > +def _is_enabled(conf):
> > +    return False
> > +
> 
> What is the purpose of this function?

Dead code. Thanks.

> >      def addAccountFolder(self, accountname, folder_root, foldername):
> >          """Add foldername entry for an account."""

> You are mixing underscore and camel case naming convention.
> 
> >  
> > +    def is_enabled(self):
> > +        return self._enabled
> 
> Now it is with underscore...

Yes. Current code base has a lot of mixes. I've used underscore names
for simple accessors.

> >      def write(self):
> >          itemlist = []
> >  
> >          try:
> > -            for foo in listdir(self._mbnamesdir):
> > -                foo = path.join(self._mbnamesdir, foo)
> > -                if path.isfile(foo) and foo[-5:] == '.json':
> > -                    try:
> > -                        with open(foo, 'rt') as intermediateFile:
> > -                            for item in json.load(intermediateFile):
> > -                                itemlist.append(item)
> > -                    except Exception as e:
> > -                        self.ui.error(
> > -                            e,
> > -                            exc_info()[2],
> > -                            "intermediate mbnames file %s not properly read"% foo
> > -                        )
> > +            for intermediateFile in self._iterIntermediateFiles():
> > +                try:
> > +                    with open(intermediateFile, 'rt') as intermediateFD:
> > +                        for item in json.load(intermediateFD):
> > +                            itemlist.append(item)
> 
> Maybe we could use one try with many except, this code looks a little
> bit like spaghetti.

Hmm, looks like I forgot the send the latest patch. Sorry.

> > +                except Exception as e:
> > +                    self.ui.error(
> > +                        e,
> > +                        exc_info()[2],
> > +                        ("intermediate mbnames file %s not properly read"%
> > +                            intermediateFile)
> > +                    )
> >          except OSError:
> >              pass
> 
> Maybe self self.ui.error?

Good point.

Patch follows.

-- >% --
>From b3209e38c063adda3d4572846e3d5d57e0a2f53a Mon Sep 17 00:00:00 2001
Message-Id: <b3209e38c063adda3d4572846e3d5d57e0a2f53a.1467758422.git.nicolas.s-dev at laposte.net>
From: Nicolas Sebrecht <nicolas.s-dev at laposte.net>
Date: Wed, 6 Jul 2016 00:38:43 +0200
Subject: [PATCH] mbnames: output message on errors while reading intermediate
 files

Signed-off-by: Nicolas Sebrecht <nicolas.s-dev at laposte.net>
---
 offlineimap/mbnames.py | 33 +++++++++++++++------------------
 1 file changed, 15 insertions(+), 18 deletions(-)

diff --git a/offlineimap/mbnames.py b/offlineimap/mbnames.py
index 267bb1e..f8ea577 100644
--- a/offlineimap/mbnames.py
+++ b/offlineimap/mbnames.py
@@ -35,9 +35,6 @@ _mbLock = Lock()
 _mbnames = None
 
 
-def _is_enabled(conf):
-    return False
-
 def add(accountname, folder_root, foldername):
     global _mbnames
     if _mbnames.is_enabled() is not True:
@@ -224,21 +221,21 @@ class _Mbnames(object):
     def write(self):
         itemlist = []
 
-        try:
-            for intermediateFile in self._iterIntermediateFiles():
-                try:
-                    with open(intermediateFile, 'rt') as intermediateFD:
-                        for item in json.load(intermediateFD):
-                            itemlist.append(item)
-                except Exception as e:
-                    self.ui.error(
-                        e,
-                        exc_info()[2],
-                        ("intermediate mbnames file %s not properly read"%
-                            intermediateFile)
-                    )
-        except OSError:
-            pass
+        for intermediateFile in self._iterIntermediateFiles():
+            try:
+                with open(intermediateFile, 'rt') as intermediateFD:
+                    for item in json.load(intermediateFD):
+                        itemlist.append(item)
+            except (OSError, IOError) as e:
+                self.ui.error("could not read intermediate mbnames file '%s':"
+                    "%s"% (intermediateFile, str(e)))
+            except Exception as e:
+                self.ui.error(
+                    e,
+                    exc_info()[2],
+                    ("intermediate mbnames file %s not properly read"%
+                        intermediateFile)
+                )
 
         itemlist.sort(key=self._func_sortkey)
         itemlist = [self._peritem % d for d in itemlist]
-- 
Nicolas Sebrecht




More information about the OfflineIMAP-project mailing list