def gen_recipients_from_cache(options, progress=False): if not os.path.exists(cache_messages_path): return {} max_age = options['max_age'] dstore = shelve.open(cache_messages_path, flag='r', protocol=2) recipients = {} n_messages = len(dstore) log.info('cache only, %d messages', n_messages) if progress: pstatus = log.PercentStatus(n_messages, prefix=' ') for identifier in dstore: if progress: pstatus.inc() pstatus.output() msg = scan.Message() msg.from_dict_only(dstore[identifier]) if options['skip_multiple_recipients'] and len(msg.to_emails) > 1: continue if options['exclude_mails_to_me'] and filter_any(config.is_this_me, msg.to_emails): continue if options['only_include_mails_from_me'] and not config.is_this_me(msg.from_email): continue if max_age >= 0 and msg.age > max_age: continue if msg.to_emails_str not in recipients: r = scan.Recipient(msg.to_emails, msg) recipients[msg.to_emails_str] = r else: recipients[msg.to_emails_str].add(msg) if progress: pstatus.finish() dstore.close() return recipients
def gen_recipients(mailboxes, options, use_cache=True, clean_cache=False, progress=False): base = os.path.dirname(cache_messages_path) if not os.path.exists(base): os.makedirs(base) if not os.path.exists(cache_messages_path): # if there is no cache, it doesn't need to be cleaned clean_cache = False max_age = options['max_age'] flag = 'r' if clean_cache else 'c' dstore = shelve.open(cache_messages_path, flag=flag, protocol=2) dstore_write = shelve.open(cache_messages_tmp_path, flag='n', protocol=2) if clean_cache else dstore recipients = {} n_mailboxes = len(mailboxes) for i, mb in enumerate(mailboxes): if progress: n_messages = len(mb) log.info('[%d/%d] %s: %d messages', i+1, n_mailboxes, mb.path, n_messages) pstatus = log.PercentStatus(n_messages, prefix=' ') for msg in mb.messages(): if progress: pstatus.inc() pstatus.output() d = dstore.get(msg.identifier) if use_cache and d and not msg.has_changed(d): msg.from_dict(d) if clean_cache: dstore_write[msg.identifier] = d else: if not msg.parse_header(): continue d = {} msg.parse_body() if msg.identifier: msg.to_dict(d) dstore_write[msg.identifier] = d if options['skip_multiple_recipients'] and len(msg.to_emails) > 1: continue if options['exclude_mails_to_me'] and filter_any(config.is_this_me, msg.to_emails): continue if options['only_include_mails_from_me'] and not config.is_this_me(msg.from_email): continue if max_age >= 0 and msg.age > max_age: continue if msg.to_emails_str not in recipients: r = scan.Recipient(msg.to_emails, msg) recipients[msg.to_emails_str] = r else: recipients[msg.to_emails_str].add(msg) if progress: pstatus.finish() dstore.close() if clean_cache: dstore_write.close() os.rename(cache_messages_tmp_path, cache_messages_path) return recipients