def attachment(querystr, n): db = Database() query = Query(db, querystr) if query.count_messages() != 1: redirect('/!/%s/' % querystr) else: message = next(iter(query.search_messages())) parts = message.get_message_parts() i = n - 1 if i >= len(parts): redirect('/!/%s/' % querystr) else: part = parts[i] content_type = part.get_content_type() response.content_type = content_type # response.charset = part.get_content_charset() fn = part.get_filename() if fn != None: response.headers['content-disposition'] = 'filename="%s";' % unidecode(fn).replace('"', '') payload = message.get_part(n) if 'html' in content_type.lower(): return clean_html(payload) else: return payload
def get_training_data(progress=False): training_data = [] training_labels = [] db = Database() # query that returns all the messages q = Query(db, '') if progress: count = q.count_messages() n = 0 pbar = ProgressBar(widgets=[Percentage(), Bar(), ETA()], maxval=count).start() data = [] for m in q.search_messages(): if progress: n += 1 pbar.update(n) data.append(m.get_header('To')) data.append(m.get_header('From')) data.append(m.get_header('Subject')) data.append(m.get_part(1).decode("utf8", errors="ignore")) try: training_data.append('\n'.join(data)) except UnicodeDecodeError: print map(lambda x: type(x), data) sys.exit(1) training_labels.append(erase_irrelevant_tags(list(m.get_tags()))) data = [] if progress: pbar.finish() return training_data, training_labels
def message(message_id): query = Query(db, 'id:%s' % message_id) if not query.count_messages() == 1: return 'Message not found', 404 m = next(query.search_messages()) return { 'heading': m.get_header('subject'), 'body': m.get_part(1), }
def search(querystr): db = Database() query = Query(db, querystr) if query.count_messages() == 1: message = next(iter(query.search_messages())) title = message.get_header('subject') try: parts = [(i + 1, part.get_filename('No description')) \ for i, part in enumerate(message.get_message_parts())] body = message.get_part(1) except UnicodeDecodeError: parts = [] body = 'There was an encoding problem with this message.' else: title = 'Results for "%s"' % querystr parts = [] body = None return { 'title': title, 'parts': parts, 'body': body, 'threads': list(hierarchy(query)), }
def main(): parser = argparse.ArgumentParser( description="Sync message 'X-Keywords' header with notmuch tags.") parser.add_argument("-V", "--version", action="version", version="%(prog)s " + "v%s (%s)" % (__version__, __date__)) parser.add_argument("-q", "--query", dest="query", required=True, help="notmuch database query string") parser.add_argument("-p", "--db-path", dest="dbpath", help="notmuch database path (default to try user configuration)") parser.add_argument("-n", "--dry-run", dest="dryrun", action="store_true", help="dry run") parser.add_argument("-v", "--verbose", dest="verbose", action="store_true", help="show verbose information") # Exclusive argument group for sync mode exgroup1 = parser.add_mutually_exclusive_group(required=True) exgroup1.add_argument("-m", "--merge-keywords-tags", dest="direction_merge", action="store_true", help="merge 'X-Keywords' and tags and update both") exgroup1.add_argument("-k", "--keywords-to-tags", dest="direction_keywords2tags", action="store_true", help="sync 'X-Keywords' to notmuch tags") exgroup1.add_argument("-t", "--tags-to-keywords", dest="direction_tags2keywords", action="store_true", help="sync notmuch tags to 'X-Keywords'") # Exclusive argument group for tag operation mode exgroup2 = parser.add_mutually_exclusive_group(required=False) exgroup2.add_argument("-a", "--add-only", dest="mode_addonly", action="store_true", help="only add notmuch tags") exgroup2.add_argument("-r", "--remove-only", dest="mode_removeonly", action="store_true", help="only remove notmuch tags") # Parse args = parser.parse_args() # Sync direction if args.direction_merge: sync_direction = SyncDirection.MERGE_KEYWORDS_TAGS elif args.direction_keywords2tags: sync_direction = SyncDirection.KEYWORDS_TO_TAGS elif args.direction_tags2keywords: sync_direction = SyncDirection.TAGS_TO_KEYWORDS else: raise ValueError("Invalid synchronization direction") # Sync mode if args.mode_addonly: sync_mode = SyncMode.ADD_ONLY elif args.mode_removeonly: sync_mode = SyncMode.REMOVE_ONLY else: sync_mode = SyncMode.ADD_REMOVE # if args.dbpath: dbpath = os.path.abspath(os.path.expanduser(args.dbpath)) else: dbpath = None # db = Database(path=dbpath, create=False, mode=Database.MODE.READ_WRITE) dbinfo = get_notmuch_revision(dbpath=dbpath) q = Query(db, args.query) total_msgs = q.count_messages() msgs = q.search_messages() # if args.verbose: print("# Notmuch database path: %s" % dbpath) print("# Database revision: %d (uuid: %s)" % (dbinfo['revision'], dbinfo['uuid'])) print("# Query: %s" % args.query) print("# Sync direction: %s" % sync_direction.name) print("# Sync mode: %s" % sync_mode.name) print("# Total messages to check: %d" % total_msgs) print("# Dryn run: %s" % args.dryrun) # for msg in msgs: kwmsg = KwMessage(msg) kwmsg.sync(direction=sync_direction, mode=sync_mode, dryrun=args.dryrun, verbose=args.verbose) # db.close()