afewmail / afew

an initial tagging script for notmuch mail
ISC License
325 stars 98 forks source link

Relative path in database.path of notmuch config breaks FolderNameFilter #303

Closed rustikus closed 2 years ago

rustikus commented 3 years ago

Hi,

I found an issue related to FolderNameFilter which breaks the portability of config files between i.e. MacOS and Linux. I sync configs between systems leveraging specifics with variables like $HOME in paths. Unfortunately notmuch does not support variables but setting database.path as a relative path. This helps portability between MacOS and Linux if you sync to the same mail folder.

That is my example of database.path set to Mail in notmuch.

DEBUG:afew.filters.FolderNameFilter.FolderNameFilter:found folders set() for message 'Großartige Neuigkeiten: M.Zuiko Digital ED 150-400mm F4.5 TC1.25x IS PRO Objektiv, Firmwareupdate für die E-M1X und tolle Winteraktionen'

That is my example of database.path set to /Users/name/Mail in notmuch.

DEBUG:afew.filters.FolderNameFilter.FolderNameFilter:found folders {'Trash', 'home'} for message 'Großartige Neuigkeiten: M.Zuiko Digital ED 150-400mm F4.5 TC1.25x IS PRO Objektiv, Firmwareupdate für die E-M1X und tolle Winteraktionen'

I think the problem is in file FolderNameFilter.py and specifically this part

        self.__filename_pattern = '{mail_root}/(?P<maildirs>.*)/(cur|new)/[^/]+'.format(
            mail_root=notmuch_settings.get('database', 'path').rstrip('/'))

Interestingly the Database.py already has a solution which is i.e. used by MailMover to get the database path.

   def _calculate_db_path(self):
        """
        Calculates the path to use for the database. Supports notmuch's
        methodology including falling back to $MAILDIR or $HOME/mail if a path
        is not specified and using $HOME/<path> if path is relative.
        """
        default_path = os.environ.get('MAILDIR', '{}/mail'.format(os.environ.get('HOME')))
        db_path = notmuch_settings.get('database', 'path', fallback=default_path)

        # If path is relative, notmuch prepends $HOME in front
        if not os.path.isabs(db_path):
            db_path = '{}/{}'.format(os.environ.get('HOME'), db_path)

        return db_path

Hope this helps! Thanks

GuillaumeSeren commented 3 years ago

Hello ! Ah yes I think that code should use the '_calculate_db_path()' and also this filter really a dedicated test.

If you want to help on those, feel free to open a pull-request.