Guido van Rossum | 64e736b | 1998-10-02 01:23:47 +0000 | [diff] [blame] | 1 | """Test program for MimeWriter module. |
| 2 | |
| 3 | The test program was too big to comfortably fit in the MimeWriter |
| 4 | class, so it's here in its own file. |
| 5 | |
| 6 | This should generate Barry's example, modulo some quotes and newlines. |
| 7 | |
| 8 | """ |
| 9 | |
| 10 | |
| 11 | from MimeWriter import MimeWriter |
| 12 | |
| 13 | SELLER = '''\ |
| 14 | INTERFACE Seller-1; |
| 15 | |
| 16 | TYPE Seller = OBJECT |
| 17 | DOCUMENTATION "A simple Seller interface to test ILU" |
| 18 | METHODS |
| 19 | price():INTEGER, |
| 20 | END; |
| 21 | ''' |
| 22 | |
| 23 | BUYER = '''\ |
| 24 | class Buyer: |
| 25 | def __setup__(self, maxprice): |
| 26 | self._maxprice = maxprice |
| 27 | |
| 28 | def __main__(self, kos): |
| 29 | """Entry point upon arrival at a new KOS.""" |
| 30 | broker = kos.broker() |
| 31 | # B4 == Barry's Big Bass Business :-) |
| 32 | seller = broker.lookup('Seller_1.Seller', 'B4') |
| 33 | if seller: |
| 34 | price = seller.price() |
| 35 | print 'Seller wants $', price, '... ' |
| 36 | if price > self._maxprice: |
| 37 | print 'too much!' |
| 38 | else: |
| 39 | print "I'll take it!" |
| 40 | else: |
| 41 | print 'no seller found here' |
| 42 | ''' # Don't ask why this comment is here |
| 43 | |
| 44 | STATE = '''\ |
| 45 | # instantiate a buyer instance and put it in a magic place for the KOS |
| 46 | # to find. |
| 47 | __kp__ = Buyer() |
| 48 | __kp__.__setup__(500) |
| 49 | ''' |
| 50 | |
| 51 | SIMPLE_METADATA = [ |
| 52 | ("Interpreter", "python"), |
| 53 | ("Interpreter-Version", "1.3"), |
| 54 | ("Owner-Name", "Barry Warsaw"), |
| 55 | ("Owner-Rendezvous", "bwarsaw@cnri.reston.va.us"), |
| 56 | ("Home-KSS", "kss.cnri.reston.va.us"), |
| 57 | ("Identifier", "hdl://cnri.kss/my_first_knowbot"), |
| 58 | ("Launch-Date", "Mon Feb 12 16:39:03 EST 1996"), |
| 59 | ] |
| 60 | |
| 61 | COMPLEX_METADATA = [ |
| 62 | ("Metadata-Type", "complex"), |
| 63 | ("Metadata-Key", "connection"), |
| 64 | ("Access", "read-only"), |
| 65 | ("Connection-Description", "Barry's Big Bass Business"), |
| 66 | ("Connection-Id", "B4"), |
| 67 | ("Connection-Direction", "client"), |
| 68 | ] |
| 69 | |
| 70 | EXTERNAL_METADATA = [ |
| 71 | ("Metadata-Type", "complex"), |
| 72 | ("Metadata-Key", "generic-interface"), |
| 73 | ("Access", "read-only"), |
| 74 | ("Connection-Description", "Generic Interface for All Knowbots"), |
| 75 | ("Connection-Id", "generic-kp"), |
| 76 | ("Connection-Direction", "client"), |
| 77 | ] |
| 78 | |
| 79 | |
| 80 | def main(): |
| 81 | import sys |
| 82 | |
| 83 | # Toplevel headers |
| 84 | |
| 85 | toplevel = MimeWriter(sys.stdout) |
| 86 | toplevel.addheader("From", "bwarsaw@cnri.reston.va.us") |
| 87 | toplevel.addheader("Date", "Mon Feb 12 17:21:48 EST 1996") |
| 88 | toplevel.addheader("To", "kss-submit@cnri.reston.va.us") |
| 89 | toplevel.addheader("MIME-Version", "1.0") |
| 90 | |
| 91 | # Toplevel body parts |
| 92 | |
| 93 | f = toplevel.startmultipartbody("knowbot", "801spam999", |
| 94 | [("version", "0.1")], prefix=0) |
| 95 | f.write("This is a multi-part message in MIME format.\n") |
| 96 | |
| 97 | # First toplevel body part: metadata |
| 98 | |
| 99 | md = toplevel.nextpart() |
| 100 | md.startmultipartbody("knowbot-metadata", "802spam999") |
| 101 | |
| 102 | # Metadata part 1 |
| 103 | |
| 104 | md1 = md.nextpart() |
| 105 | md1.addheader("KP-Metadata-Type", "simple") |
| 106 | md1.addheader("KP-Access", "read-only") |
| 107 | m = MimeWriter(md1.startbody("message/rfc822")) |
| 108 | for key, value in SIMPLE_METADATA: |
| 109 | m.addheader("KPMD-" + key, value) |
| 110 | m.flushheaders() |
| 111 | del md1 |
| 112 | |
| 113 | # Metadata part 2 |
| 114 | |
| 115 | md2 = md.nextpart() |
| 116 | for key, value in COMPLEX_METADATA: |
| 117 | md2.addheader("KP-" + key, value) |
| 118 | f = md2.startbody("text/isl") |
| 119 | f.write(SELLER) |
| 120 | del md2 |
| 121 | |
| 122 | # Metadata part 3 |
| 123 | |
| 124 | md3 = md.nextpart() |
| 125 | f = md3.startbody("message/external-body", |
| 126 | [("access-type", "URL"), |
| 127 | ("URL", "hdl://cnri.kss/generic-knowbot")]) |
| 128 | m = MimeWriter(f) |
| 129 | for key, value in EXTERNAL_METADATA: |
| 130 | md3.addheader("KP-" + key, value) |
| 131 | md3.startbody("text/isl") |
| 132 | # Phantom body doesn't need to be written |
| 133 | |
| 134 | md.lastpart() |
| 135 | |
| 136 | # Second toplevel body part: code |
| 137 | |
| 138 | code = toplevel.nextpart() |
| 139 | code.startmultipartbody("knowbot-code", "803spam999") |
| 140 | |
| 141 | # Code: buyer program source |
| 142 | |
| 143 | buyer = code.nextpart() |
| 144 | buyer.addheader("KP-Module-Name", "BuyerKP") |
| 145 | f = buyer.startbody("text/plain") |
| 146 | f.write(BUYER) |
| 147 | |
| 148 | code.lastpart() |
| 149 | |
| 150 | # Third toplevel body part: state |
| 151 | |
| 152 | state = toplevel.nextpart() |
| 153 | state.addheader("KP-Main-Module", "main") |
| 154 | state.startmultipartbody("knowbot-state", "804spam999") |
| 155 | |
| 156 | # State: a bunch of assignments |
| 157 | |
| 158 | st = state.nextpart() |
| 159 | st.addheader("KP-Module-Name", "main") |
| 160 | f = st.startbody("text/plain") |
| 161 | f.write(STATE) |
| 162 | |
| 163 | state.lastpart() |
| 164 | |
| 165 | # End toplevel body parts |
| 166 | |
| 167 | toplevel.lastpart() |
| 168 | |
| 169 | |
| 170 | main() |