1 """Test program for MimeWriter module.
3 The test program was too big to comfortably fit in the MimeWriter
4 class, so it's here in its own file.
6 This should generate Barry's example, modulo some quotes and newlines.
10 import unittest
, StringIO
11 from test
.test_support
import run_unittest
14 warnings
.filterwarnings("ignore", "the MimeWriter module is deprecated.*",
17 from MimeWriter
import MimeWriter
23 DOCUMENTATION "A simple Seller interface to test ILU"
31 def __setup__(self, maxprice):
32 self._maxprice = maxprice
34 def __main__(self, kos):
35 """Entry point upon arrival at a new KOS."""
37 # B4 == Barry's Big Bass Business :-)
38 seller = broker.lookup('Seller_1.Seller', 'B4')
40 price = seller.price()
41 print 'Seller wants $', price, '... '
42 if price > self._maxprice:
47 print 'no seller found here'
48 ''' # Don't ask why this comment is here
51 # instantiate a buyer instance and put it in a magic place for the KOS
58 ("Interpreter", "python"),
59 ("Interpreter-Version", "1.3"),
60 ("Owner-Name", "Barry Warsaw"),
61 ("Owner-Rendezvous", "bwarsaw@cnri.reston.va.us"),
62 ("Home-KSS", "kss.cnri.reston.va.us"),
63 ("Identifier", "hdl://cnri.kss/my_first_knowbot"),
64 ("Launch-Date", "Mon Feb 12 16:39:03 EST 1996"),
68 ("Metadata-Type", "complex"),
69 ("Metadata-Key", "connection"),
70 ("Access", "read-only"),
71 ("Connection-Description", "Barry's Big Bass Business"),
72 ("Connection-Id", "B4"),
73 ("Connection-Direction", "client"),
77 ("Metadata-Type", "complex"),
78 ("Metadata-Key", "generic-interface"),
79 ("Access", "read-only"),
80 ("Connection-Description", "Generic Interface for All Knowbots"),
81 ("Connection-Id", "generic-kp"),
82 ("Connection-Direction", "client"),
87 From: bwarsaw@cnri.reston.va.us
88 Date: Mon Feb 12 17:21:48 EST 1996
89 To: kss-submit@cnri.reston.va.us
91 Content-Type: multipart/knowbot;
92 boundary="801spam999";
95 This is a multi-part message in MIME format.
98 Content-Type: multipart/knowbot-metadata;
103 Content-Type: message/rfc822
104 KP-Metadata-Type: simple
107 KPMD-Interpreter: python
108 KPMD-Interpreter-Version: 1.3
109 KPMD-Owner-Name: Barry Warsaw
110 KPMD-Owner-Rendezvous: bwarsaw@cnri.reston.va.us
111 KPMD-Home-KSS: kss.cnri.reston.va.us
112 KPMD-Identifier: hdl://cnri.kss/my_first_knowbot
113 KPMD-Launch-Date: Mon Feb 12 16:39:03 EST 1996
116 Content-Type: text/isl
117 KP-Metadata-Type: complex
118 KP-Metadata-Key: connection
120 KP-Connection-Description: Barry's Big Bass Business
122 KP-Connection-Direction: client
127 DOCUMENTATION "A simple Seller interface to test ILU"
133 Content-Type: message/external-body;
135 URL="hdl://cnri.kss/generic-knowbot"
137 Content-Type: text/isl
138 KP-Metadata-Type: complex
139 KP-Metadata-Key: generic-interface
141 KP-Connection-Description: Generic Interface for All Knowbots
142 KP-Connection-Id: generic-kp
143 KP-Connection-Direction: client
149 Content-Type: multipart/knowbot-code;
150 boundary="803spam999"
154 Content-Type: text/plain
155 KP-Module-Name: BuyerKP
158 def __setup__(self, maxprice):
159 self._maxprice = maxprice
161 def __main__(self, kos):
162 """Entry point upon arrival at a new KOS."""
163 broker = kos.broker()
164 # B4 == Barry's Big Bass Business :-)
165 seller = broker.lookup('Seller_1.Seller', 'B4')
167 price = seller.price()
168 print 'Seller wants $', price, '... '
169 if price > self._maxprice:
172 print "I'll take it!"
174 print 'no seller found here'
179 Content-Type: multipart/knowbot-state;
180 boundary="804spam999"
185 Content-Type: text/plain
188 # instantiate a buyer instance and put it in a magic place for the KOS
191 __kp__.__setup__(500)
198 class MimewriterTest(unittest
.TestCase
):
201 buf
= StringIO
.StringIO()
205 toplevel
= MimeWriter(buf
)
206 toplevel
.addheader("From", "bwarsaw@cnri.reston.va.us")
207 toplevel
.addheader("Date", "Mon Feb 12 17:21:48 EST 1996")
208 toplevel
.addheader("To", "kss-submit@cnri.reston.va.us")
209 toplevel
.addheader("MIME-Version", "1.0")
211 # Toplevel body parts
213 f
= toplevel
.startmultipartbody("knowbot", "801spam999",
214 [("version", "0.1")], prefix
=0)
215 f
.write("This is a multi-part message in MIME format.\n")
217 # First toplevel body part: metadata
219 md
= toplevel
.nextpart()
220 md
.startmultipartbody("knowbot-metadata", "802spam999")
225 md1
.addheader("KP-Metadata-Type", "simple")
226 md1
.addheader("KP-Access", "read-only")
227 m
= MimeWriter(md1
.startbody("message/rfc822"))
228 for key
, value
in SIMPLE_METADATA
:
229 m
.addheader("KPMD-" + key
, value
)
236 for key
, value
in COMPLEX_METADATA
:
237 md2
.addheader("KP-" + key
, value
)
238 f
= md2
.startbody("text/isl")
245 f
= md3
.startbody("message/external-body",
246 [("access-type", "URL"),
247 ("URL", "hdl://cnri.kss/generic-knowbot")])
249 for key
, value
in EXTERNAL_METADATA
:
250 md3
.addheader("KP-" + key
, value
)
251 md3
.startbody("text/isl")
252 # Phantom body doesn't need to be written
256 # Second toplevel body part: code
258 code
= toplevel
.nextpart()
259 code
.startmultipartbody("knowbot-code", "803spam999")
261 # Code: buyer program source
263 buyer
= code
.nextpart()
264 buyer
.addheader("KP-Module-Name", "BuyerKP")
265 f
= buyer
.startbody("text/plain")
270 # Third toplevel body part: state
272 state
= toplevel
.nextpart()
273 state
.addheader("KP-Main-Module", "main")
274 state
.startmultipartbody("knowbot-state", "804spam999")
276 # State: a bunch of assignments
278 st
= state
.nextpart()
279 st
.addheader("KP-Module-Name", "main")
280 f
= st
.startbody("text/plain")
285 # End toplevel body parts
289 self
.assertEqual(buf
.getvalue(), OUTPUT
)
292 run_unittest(MimewriterTest
)
294 if __name__
== '__main__':