test_MimeWriter.py 4.43 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
"""Test program for MimeWriter module.

The test program was too big to comfortably fit in the MimeWriter
class, so it's here in its own file.

This should generate Barry's example, modulo some quotes and newlines.

"""


from MimeWriter import MimeWriter

SELLER = '''\
INTERFACE Seller-1;

TYPE Seller = OBJECT
    DOCUMENTATION "A simple Seller interface to test ILU"
    METHODS
19
            price():INTEGER,
20 21 22 23 24 25
    END;
'''

BUYER = '''\
class Buyer:
    def __setup__(self, maxprice):
26
        self._maxprice = maxprice
27 28

    def __main__(self, kos):
29 30 31 32 33 34 35 36 37 38 39 40 41 42
        """Entry point upon arrival at a new KOS."""
        broker = kos.broker()
        # B4 == Barry's Big Bass Business :-)
        seller = broker.lookup('Seller_1.Seller', 'B4')
        if seller:
            price = seller.price()
            print 'Seller wants $', price, '... '
            if price > self._maxprice:
                print 'too much!'
            else:
                print "I'll take it!"
        else:
            print 'no seller found here'
'''                                     # Don't ask why this comment is here
43 44 45 46 47 48 49 50 51

STATE = '''\
# instantiate a buyer instance and put it in a magic place for the KOS
# to find.
__kp__ = Buyer()
__kp__.__setup__(500)
'''

SIMPLE_METADATA = [
52 53 54 55 56 57 58 59
        ("Interpreter", "python"),
        ("Interpreter-Version", "1.3"),
        ("Owner-Name", "Barry Warsaw"),
        ("Owner-Rendezvous", "bwarsaw@cnri.reston.va.us"),
        ("Home-KSS", "kss.cnri.reston.va.us"),
        ("Identifier", "hdl://cnri.kss/my_first_knowbot"),
        ("Launch-Date", "Mon Feb 12 16:39:03 EST 1996"),
        ]
60 61

COMPLEX_METADATA = [
62 63 64 65 66 67 68
        ("Metadata-Type", "complex"),
        ("Metadata-Key", "connection"),
        ("Access", "read-only"),
        ("Connection-Description", "Barry's Big Bass Business"),
        ("Connection-Id", "B4"),
        ("Connection-Direction", "client"),
        ]
69 70

EXTERNAL_METADATA = [
71 72 73 74 75 76 77
        ("Metadata-Type", "complex"),
        ("Metadata-Key", "generic-interface"),
        ("Access", "read-only"),
        ("Connection-Description", "Generic Interface for All Knowbots"),
        ("Connection-Id", "generic-kp"),
        ("Connection-Direction", "client"),
        ]
78 79 80 81 82 83


def main():
    import sys

    # Toplevel headers
84

85 86 87 88 89 90 91
    toplevel = MimeWriter(sys.stdout)
    toplevel.addheader("From", "bwarsaw@cnri.reston.va.us")
    toplevel.addheader("Date", "Mon Feb 12 17:21:48 EST 1996")
    toplevel.addheader("To", "kss-submit@cnri.reston.va.us")
    toplevel.addheader("MIME-Version", "1.0")

    # Toplevel body parts
92

93
    f = toplevel.startmultipartbody("knowbot", "801spam999",
94
                                    [("version", "0.1")], prefix=0)
95 96 97 98 99 100 101 102
    f.write("This is a multi-part message in MIME format.\n")

    # First toplevel body part: metadata

    md = toplevel.nextpart()
    md.startmultipartbody("knowbot-metadata", "802spam999")

    # Metadata part 1
103

104 105 106 107 108
    md1 = md.nextpart()
    md1.addheader("KP-Metadata-Type", "simple")
    md1.addheader("KP-Access", "read-only")
    m = MimeWriter(md1.startbody("message/rfc822"))
    for key, value in SIMPLE_METADATA:
109
        m.addheader("KPMD-" + key, value)
110 111 112 113 114 115 116
    m.flushheaders()
    del md1

    # Metadata part 2

    md2 = md.nextpart()
    for key, value in COMPLEX_METADATA:
117
        md2.addheader("KP-" + key, value)
118 119 120 121 122 123 124 125
    f = md2.startbody("text/isl")
    f.write(SELLER)
    del md2

    # Metadata part 3

    md3 = md.nextpart()
    f = md3.startbody("message/external-body",
126 127
                      [("access-type", "URL"),
                       ("URL", "hdl://cnri.kss/generic-knowbot")])
128 129
    m = MimeWriter(f)
    for key, value in EXTERNAL_METADATA:
130
        md3.addheader("KP-" + key, value)
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    md3.startbody("text/isl")
    # Phantom body doesn't need to be written

    md.lastpart()

    # Second toplevel body part: code

    code = toplevel.nextpart()
    code.startmultipartbody("knowbot-code", "803spam999")

    # Code: buyer program source

    buyer = code.nextpart()
    buyer.addheader("KP-Module-Name", "BuyerKP")
    f = buyer.startbody("text/plain")
    f.write(BUYER)

    code.lastpart()

    # Third toplevel body part: state

    state = toplevel.nextpart()
    state.addheader("KP-Main-Module", "main")
    state.startmultipartbody("knowbot-state", "804spam999")

    # State: a bunch of assignments

    st = state.nextpart()
    st.addheader("KP-Module-Name", "main")
    f = st.startbody("text/plain")
    f.write(STATE)

    state.lastpart()

    # End toplevel body parts

    toplevel.lastpart()


main()