+ elif sys.argv[1] == 'restore':
+ import re
+ 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:
+ m = MSGID_TAGS.match(line)
+ if not m:
+ sys.stderr.write("Warning: Ignoring invalid input line: %s" %
+ line)
+ continue
+ # split line in components and fetch message
+ msg_id = m.group(1)
+ new_tags= set(m.group(2).split())
+ msg = db.find_message(msg_id)
+
+ if msg == None:
+ sys.stderr.write(
+ "Warning: Cannot apply tags to missing message: %s\n" % 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()
+
+ #-------------------------------------