def write_tags(ids, tags): db = Database(mode=Database.MODE.READ_WRITE) for i, ts in zip(ids, tags): m = db.find_message(i) m.remove_tag("new") m.add_tag("inbox") for t in ts: m.add_tag(t)
def _get_notmuch_message(self, mid): """returns :class:`notmuch.database.Message` with given id""" mode = Database.MODE.READ_ONLY db = Database(path=self.path, mode=mode) try: return db.find_message(mid) except: errmsg = 'no message with id %s exists!' % mid raise NonexistantObjectError(errmsg)
def main(): # Handle command line options #------------------------------------ # No option given, print USAGE and exit if len(sys.argv) == 1: Notmuch().cmd_usage() #------------------------------------ elif sys.argv[1] == 'setup': """Interactively setup notmuch for first use.""" exit("Not implemented.") #------------------------------------- elif sys.argv[1] == 'new': """Check for new and removed messages.""" Notmuch().cmd_new() #------------------------------------- elif sys.argv[1] == 'help': """Print the help text""" Notmuch().cmd_help(sys.argv[1:]) #------------------------------------- elif sys.argv[1] == 'part': part() #------------------------------------- elif sys.argv[1] == 'search': search() #------------------------------------- elif sys.argv[1] == 'show': show() #------------------------------------- elif sys.argv[1] == 'reply': db = Database() if len(sys.argv) == 2: # no search term. abort exit("Error: notmuch reply requires at least one search term.") # mangle arguments wrapping terms with spaces in quotes querystr = quote_query_line(sys.argv[2:]) msgs = Query(db, querystr).search_messages() print Notmuch().format_reply(msgs) #------------------------------------- elif sys.argv[1] == 'count': if len(sys.argv) == 2: # no further search term, count all querystr = '' else: # mangle arguments wrapping terms with spaces in quotes querystr = quote_query_line(sys.argv[2:]) print Database().create_query(querystr).count_messages() #------------------------------------- elif sys.argv[1] == 'tag': # build lists of tags to be added and removed add = [] remove = [] while not sys.argv[2] == '--' and \ (sys.argv[2].startswith('+') or sys.argv[2].startswith('-')): if sys.argv[2].startswith('+'): # append to add list without initial + add.append(sys.argv.pop(2)[1:]) else: # append to remove list without initial - remove.append(sys.argv.pop(2)[1:]) # skip eventual '--' if sys.argv[2] == '--': sys.argv.pop(2) # the rest is search terms querystr = quote_query_line(sys.argv[2:]) db = Database(mode=Database.MODE.READ_WRITE) msgs = Query(db, querystr).search_messages() for msg in msgs: # actually add and remove all tags map(msg.add_tag, add) map(msg.remove_tag, remove) #------------------------------------- elif sys.argv[1] == 'search-tags': if len(sys.argv) == 2: # no further search term print "\n".join(Database().get_all_tags()) else: # mangle arguments wrapping terms with spaces in quotes querystr = quote_query_line(sys.argv[2:]) db = Database() msgs = Query(db, querystr).search_messages() print "\n".join([t for t in msgs.collect_tags()]) #------------------------------------- elif sys.argv[1] == 'dump': if len(sys.argv) == 2: f = sys.stdout else: f = open(sys.argv[2], "w") db = Database() query = Query(db, '') query.set_sort(Query.SORT.MESSAGE_ID) msgs = query.search_messages() for msg in msgs: f.write("%s (%s)\n" % (msg.get_message_id(), msg.get_tags())) #------------------------------------- elif sys.argv[1] == 'restore': if len(sys.argv) == 2: print("No filename given. Reading dump from stdin.") f = sys.stdin else: f = open(sys.argv[2], "r") # split the msg id and the tags MSGID_TAGS = re.compile("(\S+)\s\((.*)\)$") db = Database(mode=Database.MODE.READ_WRITE) #read each line of the dump file for line in f: msgs = MSGID_TAGS.match(line) if not msgs: sys.stderr.write("Warning: Ignoring invalid input line: %s" % line) continue # split line in components and fetch message msg_id = msgs.group(1) new_tags = set(msgs.group(2).split()) msg = db.find_message(msg_id) if msg == None: sys.stderr.write( "Warning: Cannot apply tags to missing message: %s\n" % msg_id) continue # do nothing if the old set of tags is the same as the new one old_tags = set(msg.get_tags()) if old_tags == new_tags: continue # set the new tags msg.freeze() # only remove tags if the new ones are not a superset anyway if not (new_tags > old_tags): msg.remove_all_tags() for tag in new_tags: msg.add_tag(tag) msg.thaw() #------------------------------------- else: # unknown command exit("Error: Unknown command '%s' (see \"notmuch help\")" % sys.argv[1])
def main(): # Handle command line options #------------------------------------ # No option given, print USAGE and exit if len(sys.argv) == 1: Notmuch().cmd_usage() #------------------------------------ elif sys.argv[1] == 'setup': """Interactively setup notmuch for first use.""" exit("Not implemented.") #------------------------------------- elif sys.argv[1] == 'new': """Check for new and removed messages.""" Notmuch().cmd_new() #------------------------------------- elif sys.argv[1] == 'help': """Print the help text""" Notmuch().cmd_help(sys.argv[1:]) #------------------------------------- elif sys.argv[1] == 'part': part() #------------------------------------- elif sys.argv[1] == 'search': search() #------------------------------------- elif sys.argv[1] == 'show': show() #------------------------------------- elif sys.argv[1] == 'reply': db = Database() if len(sys.argv) == 2: # no search term. abort exit("Error: notmuch reply requires at least one search term.") # mangle arguments wrapping terms with spaces in quotes querystr = quote_query_line(sys.argv[2:]) msgs = Query(db, querystr).search_messages() print Notmuch().format_reply(msgs) #------------------------------------- elif sys.argv[1] == 'count': if len(sys.argv) == 2: # no further search term, count all querystr = '' else: # mangle arguments wrapping terms with spaces in quotes querystr = quote_query_line(sys.argv[2:]) print Database().create_query(querystr).count_messages() #------------------------------------- elif sys.argv[1] == 'tag': # build lists of tags to be added and removed add = [] remove = [] while not sys.argv[2] == '--' and \ (sys.argv[2].startswith('+') or sys.argv[2].startswith('-')): if sys.argv[2].startswith('+'): # append to add list without initial + add.append(sys.argv.pop(2)[1:]) else: # append to remove list without initial - remove.append(sys.argv.pop(2)[1:]) # skip eventual '--' if sys.argv[2] == '--': sys.argv.pop(2) # the rest is search terms querystr = quote_query_line(sys.argv[2:]) db = Database(mode=Database.MODE.READ_WRITE) msgs = Query(db, querystr).search_messages() for msg in msgs: # actually add and remove all tags map(msg.add_tag, add) map(msg.remove_tag, remove) #------------------------------------- elif sys.argv[1] == 'search-tags': if len(sys.argv) == 2: # no further search term print "\n".join(Database().get_all_tags()) else: # mangle arguments wrapping terms with spaces in quotes querystr = quote_query_line(sys.argv[2:]) db = Database() msgs = Query(db, querystr).search_messages() print "\n".join([t for t in msgs.collect_tags()]) #------------------------------------- elif sys.argv[1] == 'dump': # TODO: implement "dump <filename>" if len(sys.argv) == 2: f = sys.stdout else: f = open(sys.argv[2], "w") db = Database() query = Query(db, '') query.set_sort(Query.SORT.MESSAGE_ID) msgs = query.search_messages() for msg in msgs: f.write("%s (%s)\n" % (msg.get_message_id(), msg.get_tags())) #------------------------------------- elif sys.argv[1] == 'restore': if len(sys.argv) == 2: print("No filename given. Reading dump from stdin.") f = sys.stdin else: f = open(sys.argv[2], "r") # split the msg id and the tags MSGID_TAGS = re.compile("(\S+)\s\((.*)\)$") db = Database(mode=Database.MODE.READ_WRITE) #read each line of the dump file for line in f: msgs = MSGID_TAGS.match(line) if not msgs: sys.stderr.write("Warning: Ignoring invalid input line: %s" % line) continue # split line in components and fetch message msg_id = msgs.group(1) new_tags = set(msgs.group(2).split()) msg = db.find_message(msg_id) if msg == None: sys.stderr.write( "Warning: Cannot apply tags to missing message: %s\n" % msg_id) continue # do nothing if the old set of tags is the same as the new one old_tags = set(msg.get_tags()) if old_tags == new_tags: continue # set the new tags msg.freeze() # only remove tags if the new ones are not a superset anyway if not (new_tags > old_tags): msg.remove_all_tags() for tag in new_tags: msg.add_tag(tag) msg.thaw() #------------------------------------- else: # unknown command exit("Error: Unknown command '%s' (see \"notmuch help\")" % sys.argv[1])
def get_message(self, mid): """returns the message with given id as alot.message.Message object""" mode = Database.MODE.READ_ONLY db = Database(path=self.path, mode=mode) msg = db.find_message(mid) return Message(self, msg)
def get_message(self, mid): """returns :class:`Message` with given message id (str)""" mode = Database.MODE.READ_ONLY db = Database(path=self.path, mode=mode) msg = db.find_message(mid) return Message(self, msg)