From 4c2c8e64fb1838593a89b5a8882f70a967afd3a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20L=C3=B6thberg?= Date: Tue, 1 Oct 2019 22:49:05 +0200 Subject: [PATCH] MailMover: Add basic functionality for choosing between movers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Johannes Löthberg --- afew/Settings.py | 42 +++++++++++++++++++++++++----------------- afew/commands.py | 12 ++++++++---- afew/main.py | 7 ++++++- 3 files changed, 39 insertions(+), 22 deletions(-) diff --git a/afew/Settings.py b/afew/Settings.py index 44d6f3c..1d4f658 100644 --- a/afew/Settings.py +++ b/afew/Settings.py @@ -23,16 +23,14 @@ # All the values for keys listed here are interpreted as ;-delimited lists value_is_a_list = ['tags', 'tags_blacklist'] -mail_mover_section = 'MailMover' +folder_mail_mover_section = 'MailMover' section_re = re.compile(r'^(?P[a-z_][a-z0-9_]*)(\((?P[a-z_][a-z0-9_]*)\)|\.(?P\d+))?$', re.I) - - def get_filter_chain(database): filter_chain = [] for section in settings.sections(): - if section == 'global' or section == mail_mover_section: + if section in ['global', folder_mail_mover_section]: continue match = section_re.match(section) @@ -64,17 +62,29 @@ def get_filter_chain(database): return filter_chain +def get_mail_move_kind(): + return settings.get('global', 'mail_mover_kind', fallback='folder') + +def get_mail_move_section(kind): + if kind == 'folder': + return folder_mail_mover_section + +def get_mail_move_rules(kind): + section = get_mail_move_section(kind) + if kind == 'query': + rule_id_key = 'rules' + else: + rule_id_key = 'folders' -def get_mail_move_rules(): rule_pattern = re.compile(r"'(.+?)':((?P['\"])(.*?)(?P=quote)|\S+)") - if settings.has_option(mail_mover_section, 'folders'): + if settings.has_option(section, rule_id_key): all_rules = collections.OrderedDict() - for folder in shlex.split(settings.get(mail_mover_section, 'folders')): - if settings.has_option(mail_mover_section, folder): + for folder in shlex.split(settings.get(section, rule_id_key)): + if settings.has_option(section, folder): rules = collections.OrderedDict() raw_rules = re.findall(rule_pattern, - settings.get(mail_mover_section, folder)) + settings.get(section, folder)) for rule in raw_rules: query = rule[0] destination = rule[3] or rule[1] @@ -87,16 +97,14 @@ def get_mail_move_rules(): else: raise NameError("No folders defined to move mails from.") - -def get_mail_move_age(): +def get_mail_move_age(section): max_age = 0 - if settings.has_option(mail_mover_section, 'max_age'): - max_age = settings.get(mail_mover_section, 'max_age') + if settings.has_option(section, 'max_age'): + max_age = settings.get(section, 'max_age') return max_age - -def get_mail_move_rename(): +def get_mail_move_rename(section): rename = False - if settings.has_option(mail_mover_section, 'rename'): - rename = settings.get(mail_mover_section, 'rename').lower() == 'true' + if settings.has_option(section, 'rename'): + rename = settings.get(section, 'rename').lower() == 'true' return rename diff --git a/afew/commands.py b/afew/commands.py index 4017df6..8da57af 100644 --- a/afew/commands.py +++ b/afew/commands.py @@ -10,7 +10,8 @@ from afew.main import main as inner_main from afew.FilterRegistry import all_filters from afew.Settings import user_config_dir, get_filter_chain, \ - get_mail_move_rules, get_mail_move_age, get_mail_move_rename + get_mail_move_kind, get_mail_move_section, get_mail_move_rules, \ + get_mail_move_age, get_mail_move_rename from afew.NotmuchSettings import read_notmuch_settings, get_notmuch_new_query from afew.version import version @@ -136,9 +137,12 @@ def main(): __import__(file_name[:-3], level=0) if args.move_mails: - args.mail_move_rules = get_mail_move_rules() - args.mail_move_age = get_mail_move_age() - args.mail_move_rename = get_mail_move_rename() + args.mail_move_kind = get_mail_move_kind() + section = get_mail_move_section(args.mail_move_kind) + + args.mail_move_rules = get_mail_move_rules(args.mail_move_kind) + args.mail_move_age = get_mail_move_age(section) + args.mail_move_rename = get_mail_move_rename(section) with Database() as database: configured_filter_chain = get_filter_chain(database) diff --git a/afew/main.py b/afew/main.py index 6cc20d1..6151de2 100644 --- a/afew/main.py +++ b/afew/main.py @@ -24,8 +24,13 @@ def main(options, database, query_string): watch_for_new_files(options, database, quick_find_dirs_hack(database.db_path)) elif options.move_mails: + if options.mail_move_kind == 'folder': + mover_class = FolderMailMover + else: + sys.exit('Mail mover kind {:r} is not recognized'.format(options.mail_move_kind)) + for maildir, rules in options.mail_move_rules.items(): - mover = FolderMailMover( + mover = mover_class( max_age=options.mail_move_age, rename=options.mail_move_rename, dry_run=options.dry_run,