Skip to content

Commit

Permalink
Merge pull request #91 from larkery/master
Browse files Browse the repository at this point in the history
Behave well when using mbsync to sync maildirs
  • Loading branch information
Justus Winter committed Feb 23, 2015
2 parents 1468b47 + 7532c81 commit fcbf93f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
18 changes: 15 additions & 3 deletions afew/MailMover.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from .Database import Database
from .utils import get_message_summary
from datetime import date, datetime, timedelta
import uuid


class MailMover(Database):
Expand All @@ -33,7 +34,7 @@ class MailMover(Database):
'''


def __init__(self, max_age=0, dry_run=False):
def __init__(self, max_age=0, rename = False, dry_run=False):
super(MailMover, self).__init__()
self.db = notmuch.Database(self.db_path)
self.query = 'folder:{folder} AND {subquery}'
Expand All @@ -44,7 +45,18 @@ def __init__(self, max_age=0, dry_run=False):
self.query += ' AND {start}..{now}'.format(start=start.strftime('%s'),
now=now.strftime('%s'))
self.dry_run = dry_run

self.rename = rename

def get_new_name(self, fname, destination):
if self.rename:
return os.path.join(
destination,
# 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

def move(self, maildir, rules):
'''
Expand Down Expand Up @@ -72,7 +84,7 @@ def move(self, maildir, rules):
if self.dry_run:
continue
try:
shutil.copy2(fname, destination)
shutil.copy2(fname, self.get_new_name(fname, destination))
to_delete_fnames.append(fname)
except shutil.Error as e:
# this is ugly, but shutil does not provide more
Expand Down
5 changes: 5 additions & 0 deletions afew/Settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,8 @@ def get_mail_move_age():
max_age = settings.get(mail_mover_section, 'max_age')
return max_age

def get_mail_move_rename():
rename = False
if settings.has_option(mail_mover_section, 'rename'):
rename = settings.get(mail_mover_section, 'rename').lower() == 'true'
return rename
3 changes: 2 additions & 1 deletion afew/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from afew.utils import filter_compat
from afew.FilterRegistry import all_filters
from afew.Settings import user_config_dir, get_filter_chain
from afew.Settings import get_mail_move_rules, get_mail_move_age
from afew.Settings import get_mail_move_rules, get_mail_move_age, get_mail_move_rename
from afew.NotmuchSettings import read_notmuch_settings, get_notmuch_new_query

option_parser = optparse.OptionParser(
Expand Down Expand Up @@ -183,6 +183,7 @@ def main():
if options.move_mails:
options.mail_move_rules = get_mail_move_rules()
options.mail_move_age = get_mail_move_age()
options.mail_move_rename = get_mail_move_rename()

with Database() as database:
configured_filter_chain = get_filter_chain(database)
Expand Down
2 changes: 1 addition & 1 deletion afew/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def main(options, database, query_string):
print('%s --> %s' % (message, category))
elif options.move_mails:
for maildir, rules in options.mail_move_rules.items():
mover = MailMover(options.mail_move_age, options.dry_run)
mover = MailMover(options.mail_move_age, options.mail_move_rename, options.dry_run)
mover.move(maildir, rules)
mover.close()
else:
Expand Down

0 comments on commit fcbf93f

Please sign in to comment.