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

Łukasz Żarnowiecki dolohow at outlook.com
Tue Jul 5 22:29:09 BST 2016


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.

> --- a/offlineimap/mbnames.py
> +++ b/offlineimap/mbnames.py
> @@ -19,22 +19,28 @@
>  import re   # For folderfilter.
>  import json
>  from threading import Lock
> -from os import listdir, makedirs, path
> +from os import listdir, makedirs, path, unlink
>  from sys import exc_info
>  try:
>      import UserDict
> -except ImportError:
> -    # Py3
> +except ImportError: # Py3.
>      from collections import UserDict
> +try:
> +    from ConfigParser import NoSectionError
> +except ImportError: # Py3.
> +    from configparser import NoSectionError
>  
>  
>  _mbLock = Lock()
>  _mbnames = None
>  
>  
> +def _is_enabled(conf):
> +    return False
> +

What is the purpose of this function?

>      def addAccountFolder(self, accountname, folder_root, foldername):
>          """Add foldername entry for an account."""
> @@ -167,23 +202,41 @@ class _Mbnames(object):
>  
>          return self._incremental

You are mixing underscore and camel case naming convention.

>  
> +    def is_enabled(self):
> +        return self._enabled

Now it is with underscore...

>      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.

> +                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?




More information about the OfflineIMAP-project mailing list