Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions afew/MailMover.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class MailMover(Database):
'''


def __init__(self, max_age=0, rename = False, dry_run=False):
def __init__(self, max_age=0, rename = False, dry_run=False, notmuch_args=''):
super().__init__()
self.db = notmuch.Database(self.db_path)
self.query = 'folder:"{folder}" AND {subquery}'
Expand All @@ -31,17 +31,21 @@ def __init__(self, max_age=0, rename = False, dry_run=False):
now=now.strftime('%s'))
self.dry_run = dry_run
self.rename = rename
self.notmuch_args = notmuch_args

def get_new_name(self, fname, destination):
basename = os.path.basename(fname)
submaildir = os.path.split(os.path.split(fname)[0])[1]
if self.rename:
return os.path.join(
destination,
parts = basename.split(':')
if len(parts) > 1:
flagpart = ':' + parts[-1]
else:
flagpart = ''
# construct a new filename, composed of a made-up ID and the flags part
# of the original filename.
str(uuid.uuid1()) + ':' + os.path.basename(fname).split(':')[-1]
)
else:
return destination
basename = str(uuid.uuid1()) + flagpart
return os.path.join(destination, submaildir, basename)

def move(self, maildir, rules):
'''
Expand All @@ -52,7 +56,7 @@ def move(self, maildir, rules):
to_delete_fnames = []
moved = False
for query in rules.keys():
destination = '{}/{}/cur/'.format(self.db_path, rules[query])
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where did that cur here go?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparantly, #200 and #201 got merged together when they were both merged (or I rebased one PR branch too early, or github computes the diff relative to the original merge base).
In any case, this is reflected in the change to new_name(): In short, afew used to move mail from new to cur but failed to update flags (everyone moving to cur should update flags), so #201 made afew leave mail in the respective subdir rather than chopping off new/cur/.. and appending cur..

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, but this still doesn't feel quite readable to me.

I could guess submaildir is cur or new, but just taking [1] out of os.path.split()'s tail sounds scary - what happens to the other path components?

Could this maybe be made slightly more readable way, maybe by adding some comments?

We could also probably use pathlib paths around instead of strings.

destination = '{}/{}/'.format(self.db_path, rules[query])
main_query = self.query.format(
folder=maildir.replace("\"", "\\\""), subquery=query)
logging.debug("query: {}".format(main_query))
Expand Down Expand Up @@ -107,7 +111,7 @@ def __update_db(self, maildir):
Update the database after mail files have been moved in the filesystem.
'''
try:
check_call(['notmuch', 'new'])
check_call(['notmuch', 'new'] + self.notmuch_args.split())
except CalledProcessError as err:
logging.error("Could not update notmuch database " \
"after syncing maildir '{}': {}".format(maildir, err))
Expand Down
4 changes: 4 additions & 0 deletions afew/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@
help='be more verbose, can be given multiple times'
)

options_group.add_argument(
'-N', '--notmuch-args', default='',
help='arguments for notmuch new (in move mode)'
)

def main():
if sys.version_info < (3,4):
Expand Down
2 changes: 1 addition & 1 deletion afew/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def main(options, database, query_string):
quick_find_dirs_hack(database.db_path))
elif options.move_mails:
for maildir, rules in options.mail_move_rules.items():
mover = MailMover(options.mail_move_age, options.mail_move_rename, options.dry_run)
mover = MailMover(options.mail_move_age, options.mail_move_rename, options.dry_run, options.notmuch_args)
mover.move(maildir, rules)
mover.close()
else:
Expand Down
4 changes: 4 additions & 0 deletions docs/commandline.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ traditional mail server. Tag your mails with notmuch, call afew
`--move-mails` in an offlineimap presynchook and enjoy a clean inbox
in your webinterface/GUI-client at work.

Note that in move mode, afew calls `notmuch new` after moving mails around.
You can use `afew -m --notmuch-args=--no-hooks` in a pre-new notmuch hook
to avoid loops.

For information on how to configure rules for move mode, what you can
do with it and what you can't, please refer to :doc:`move_mode`.

Expand Down