1 #!/usr/bin/env python 2 3 from MoinMessage import Message 4 from email.mime.application import MIMEApplication 5 from email.mime.text import MIMEText 6 from os.path import split 7 import sys 8 9 def add_update(message, action, part): 10 if action == "file": 11 action = "store" 12 part["Update-Action"] = action 13 message.add_update(part) 14 15 def readfile(filename): 16 f = open(filename, "rb") 17 try: 18 return MIMEApplication(f.read()) 19 finally: 20 f.close() 21 22 if __name__ == "__main__": 23 try: 24 type = sys.argv[1] 25 action = sys.argv[2] 26 args = sys.argv[3:] 27 except IndexError: 28 args = None 29 30 if not args: 31 print >>sys.stderr, "Need an update type, update action and some updates as arguments to this program." 32 sys.exit(1) 33 34 message = Message() 35 parts = [] 36 37 # Add each content fragment as either a separate update to a collection of 38 # updates or as an alternative part to a single update. 39 40 for arg in args: 41 42 # Read files and assign filenames to them, or read argument strings, and 43 # adopt them as parts. 44 45 if action == "file": 46 part = readfile(arg) 47 part["Content-Disposition"] = split(arg)[-1] 48 else: 49 part = MIMEText(arg, "moin", sys.stdin.encoding) 50 51 if type == "collection": 52 add_update(message, action, part) 53 else: 54 parts.append(part) 55 56 # Obtain the alternative representations as an update and add it to the 57 # message. 58 59 if type != "collection": 60 multipart = message.get_update(parts) 61 add_update(message, action, multipart) 62 63 text = message.get_payload() 64 print text 65 66 # vim: tabstop=4 expandtab shiftwidth=4