def test_max_age(self): from afew import MailMover with Database() as db: expect_inbox = set([ create_mail('In inbox, tagged archive, old\n', self.inbox, db, ['archive'], old=True), ]) expect_archive = set([ create_mail('In inbox, tagged archive\n', self.inbox, db, ['archive']), ]) expect_spam = set([]) mover = MailMover.MailMover(max_age=15, quiet=True) mover.move('.inbox', self.rules['.inbox']) mover.move('.archive', self.rules['.archive']) mover.move('.spam', self.rules['.spam']) mover.close() with Database() as db: self.assertEqual(expect_inbox, self.get_folder_content(db, '.inbox')) self.assertEqual(expect_archive, self.get_folder_content(db, '.archive')) self.assertEqual(expect_spam, self.get_folder_content(db, '.spam'))
def test_all_rule_cases(self): from afew import MailMover with Database() as db: expect_inbox = set([ create_mail('In inbox, untagged\n', self.inbox, db, []), create_mail('In archive, untagged\n', self.archive, db, []), create_mail('In spam, untagged\n', self.spam, db, []), ]) expect_archive = set([ create_mail('In inbox, tagged archive\n', self.inbox, db, ['archive']), create_mail('In archive, tagged archive\n', self.archive, db, ['archive']), create_mail('In spam, tagged archive\n', self.spam, db, ['archive']), ]) expect_spam = set([ create_mail('In inbox, tagged spam\n', self.inbox, db, ['spam']), create_mail('In inbox, tagged archive, spam\n', self.inbox, db, ['archive', 'spam']), create_mail('In archive, tagged spam\n', self.archive, db, ['spam']), create_mail('In archive, tagged archive, spam\n', self.archive, db, ['archive', 'spam']), create_mail('In spam, tagged spam\n', self.spam, db, ['spam']), create_mail('In spam, tagged archive, spam\n', self.spam, db, ['archive', 'spam']), ]) mover = MailMover.MailMover(quiet=True) mover.move('.inbox', self.rules['.inbox']) mover.move('.archive', self.rules['.archive']) mover.move('.spam', self.rules['.spam']) mover.close() with Database() as db: self.assertEqual(expect_inbox, self.get_folder_content(db, '.inbox')) self.assertEqual(expect_archive, self.get_folder_content(db, '.archive')) self.assertEqual(expect_spam, self.get_folder_content(db, '.spam'))
def _make_dkim_validity_filter(): """Make `DKIMValidityFilter` with mocked `DKIMValidityFilter.add_tags` method, so in tests we can easily check what tags were added by filter without fiddling with db. """ tags = set() add_tags = _AddTags(tags) dkim_filter = DKIMValidityFilter(Database()) dkim_filter.add_tags = add_tags return dkim_filter, tags
def setUp(self): self.test_dir = tempfile.mkdtemp() os.environ['MAILDIR'] = self.test_dir os.environ['NOTMUCH_CONFIG'] = os.path.join(self.test_dir, 'notmuch-config') notmuch_settings['database'] = {'path': self.test_dir} notmuch_settings['new'] = {'tags': 'new'} write_notmuch_settings() # Create notmuch database Database().open(create=True).close() self.root = mailbox.Maildir(self.test_dir) self.inbox = self.root.add_folder('inbox') self.archive = self.root.add_folder('archive') self.spam = self.root.add_folder('spam') # Dict of rules that are passed to MailMover. # # The top level key represents a particular mail directory to work on. # # The second level key is the notmuch query that MailMover will execute, # and its value is the directory to move the matching emails to. self.rules = { '.inbox': { 'tag:archive AND NOT tag:spam': '.archive', 'tag:spam': '.spam', }, '.archive': { 'NOT tag:archive AND NOT tag:spam': '.inbox', 'tag:spam': '.spam', }, '.spam': { 'NOT tag:spam AND tag:archive': '.archive', 'NOT tag:spam AND NOT tag:archive': '.inbox', }, }
def main(): if sys.version_info < (3, 6): sys.exit("Python 3.6 or later is required.") args = parser.parse_args() no_actions = len(filter(None, (args.tag, args.watch, args.move_mails))) if no_actions == 0: sys.exit('You need to specify an action') elif no_actions > 1: sys.exit('Please specify exactly one action') no_query_modifiers = len(filter(None, (args.all, args.new, args.query))) if no_query_modifiers == 0 and not args.watch \ and not args.move_mails: sys.exit('You need to specify one of --new, --all or a query string') elif no_query_modifiers > 1: sys.exit('Please specify either --all, --new or a query string') read_notmuch_settings(args.notmuch_config) if args.new: query_string = get_notmuch_new_query() elif args.all: query_string = '' else: query_string = ' '.join(args.query) loglevel = { 0: logging.WARNING, 1: logging.INFO, 2: logging.DEBUG, }[min(2, args.verbosity)] logging.basicConfig(level=loglevel) sys.path.insert(0, user_config_dir) for file_name in glob.glob1(user_config_dir, '*.py'): logging.info('Importing user filter %r' % (file_name, )) __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() with Database() as database: configured_filter_chain = get_filter_chain(database) if args.enable_filters: args.enable_filters = args.enable_filters.split(',') all_filters_set = set(all_filters.keys()) enabled_filters_set = set(args.enable_filters) if not all_filters_set.issuperset(enabled_filters_set): sys.exit('Unknown filter(s) selected: %s' % (' '.join( enabled_filters_set.difference(all_filters_set)))) args.enable_filters = [ all_filters[filter_name](database) for filter_name in args.enable_filters ] else: args.enable_filters = configured_filter_chain inner_main(args, database, query_string)
def main(): options, args = option_parser.parse_args() no_actions = len( filter_compat(None, (options.tag, options.watch, options.update or options.update_reference, options.learn, options.classify, options.move_mails))) if no_actions == 0: sys.exit('You need to specify an action') elif no_actions > 1: sys.exit( 'Please specify exactly one action (both update actions can be given at once)' ) no_query_modifiers = len( filter_compat(None, (options.all, options.new, args))) if no_query_modifiers == 0 and not \ (options.update or options.update_reference or options.watch) and not \ options.move_mails: sys.exit('You need to specify one of --new, --all or a query string') elif no_query_modifiers > 1: sys.exit('Please specify either --all, --new or a query string') read_notmuch_settings(options.notmuch_config) if options.new: query_string = get_notmuch_new_query() elif options.all: query_string = '' elif not (options.update or options.update_reference): query_string = ' '.join(args) elif options.update or options.update_reference: query_string = '%i..%i' % ( time.time() - options.reference_set_timeframe * 24 * 60 * 60, time.time()) else: sys.exit('Weird... please file a bug containing your command line.') loglevel = { 0: logging.WARNING, 1: logging.INFO, 2: logging.DEBUG, }[min(2, options.verbosity)] logging.basicConfig(level=loglevel) sys.path.insert(0, user_config_dir) # py2.7 compat hack glob_pattern = b'*.py' if sys.version_info[0] == 2 else '*.py' for file_name in glob.glob1(user_config_dir, glob_pattern): print('Importing user filter %r' % (file_name, )) __import__(file_name[:-3], level=0) 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) if options.enable_filters: options.enable_filters = options.enable_filters.split(',') all_filters_set = set(all_filters.keys()) enabled_filters_set = set(options.enable_filters) if not all_filters_set.issuperset(enabled_filters_set): sys.exit('Unknown filter(s) selected: %s' % (' '.join( enabled_filters_set.difference(all_filters_set)))) options.enable_filters = [ all_filters[filter_name](database) for filter_name in options.enable_filters ] else: options.enable_filters = configured_filter_chain inner_main(options, database, query_string)