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