# HG changeset patch # User Paul Boddie # Date 1387667132 -3600 # Node ID 2bc08d9904fa1f55a5830bacedb8d3f8d4a09389 # Parent 799c7b86719f1643a7e54e65d6f53b9df63996d7 Added support for uploading files directly. Simplified the code somewhat. diff -r 799c7b86719f -r 2bc08d9904fa tests/test_message.py --- a/tests/test_message.py Sat Dec 21 01:48:54 2013 +0100 +++ b/tests/test_message.py Sun Dec 22 00:05:32 2013 +0100 @@ -1,10 +1,24 @@ #!/usr/bin/env python from MoinMessage import Message -from email.mime.multipart import MIMEMultipart +from email.mime.application import MIMEApplication from email.mime.text import MIMEText +from os.path import split import sys +def add_update(message, action, part): + if action == "file": + action = "store" + part["Update-Action"] = action + message.add_update(part) + +def readfile(filename): + f = open(filename, "rb") + try: + return MIMEApplication(f.read()) + finally: + f.close() + if __name__ == "__main__": try: type = sys.argv[1] @@ -24,17 +38,27 @@ # updates or as an alternative part to a single update. for arg in args: - part = MIMEText(arg, "moin", sys.stdin.encoding) + + # Read files and assign filenames to them, or read argument strings, and + # adopt them as parts. + + if action == "file": + part = readfile(arg) + part["Content-Disposition"] = split(arg)[-1] + else: + part = MIMEText(arg, "moin", sys.stdin.encoding) + if type == "collection": - part["Update-Action"] = action - message.add_update(part) + add_update(message, action, part) else: parts.append(part) + # Obtain the alternative representations as an update and add it to the + # message. + if type != "collection": multipart = message.get_update(parts) - multipart["Update-Action"] = action - message.add_update(multipart) + add_update(message, action, multipart) text = message.get_payload() print text diff -r 799c7b86719f -r 2bc08d9904fa tests/test_send.py --- a/tests/test_send.py Sat Dec 21 01:48:54 2013 +0100 +++ b/tests/test_send.py Sun Dec 22 00:05:32 2013 +0100 @@ -1,9 +1,24 @@ #!/usr/bin/env python from MoinMessage import * +from email.mime.application import MIMEApplication from email.mime.text import MIMEText +from os.path import split import sys +def add_update(message, action, part): + if action == "file": + action = "store" + part["Update-Action"] = action + message.add_update(part) + +def readfile(filename): + f = open(filename, "rb") + try: + return MIMEApplication(f.read()) + finally: + f.close() + if __name__ == "__main__": try: signer = sys.argv[1] @@ -27,28 +42,53 @@ args = None if not args: - print >>sys.stderr, "Need a signer, recipient, URL, update type, action and some updates as arguments to this program." - print >>sys.stderr, "Syntax:" - print >>sys.stderr, sys.argv[0], " " \ - "[ --forward ] " \ - " ..." + print >>sys.stderr, """\ +Need a signer, recipient, URL, update type, action and some updates as arguments to this program. + +Syntax: + +%s [ --forward ] \\ + ... + +The update type is typically "collection" for a number of separate updates, +or any other value for a single multipart update consisting of several parts. + +The action may be "update", "replace" or "store", with subsequent updates +being text strings containing text for individual updates (or parts of a single +update). If action is "file", however, subsequent updates must refer to files +and communicated updates will be given a "store" update action. +""" % sys.argv[0] sys.exit(1) message = Message() parts = [] + # A collection of updates involves adding each update directly to the + # message. A single update involves collecting the given parts, combining + # them and putting the combined update in the message. + for arg in args: - part = MIMEText(arg, "moin", sys.stdin.encoding) + + # Read files and assign filenames to them, or read argument strings, and + # adopt them as parts. + + if action == "file": + part = readfile(arg) + part["Content-Disposition"] = split(arg)[-1] + else: + part = MIMEText(arg, "moin", sys.stdin.encoding) + if type == "collection": - part["Update-Action"] = action - message.add_update(part) + add_update(message, action, part) else: parts.append(part) + # Obtain the alternative representations as an update and add it to the + # message. + if type != "collection": multipart = message.get_update(parts) - multipart["Update-Action"] = action - message.add_update(multipart) + add_update(message, action, multipart) email_message = message.get_payload() gpg = GPG()