PyXR

c:\python24\lib \ test \ test_MimeWriter.py



0001 """Test program for MimeWriter module.
0002 
0003 The test program was too big to comfortably fit in the MimeWriter
0004 class, so it's here in its own file.
0005 
0006 This should generate Barry's example, modulo some quotes and newlines.
0007 
0008 """
0009 
0010 
0011 from MimeWriter import MimeWriter
0012 
0013 SELLER = '''\
0014 INTERFACE Seller-1;
0015 
0016 TYPE Seller = OBJECT
0017     DOCUMENTATION "A simple Seller interface to test ILU"
0018     METHODS
0019             price():INTEGER,
0020     END;
0021 '''
0022 
0023 BUYER = '''\
0024 class Buyer:
0025     def __setup__(self, maxprice):
0026         self._maxprice = maxprice
0027 
0028     def __main__(self, kos):
0029         """Entry point upon arrival at a new KOS."""
0030         broker = kos.broker()
0031         # B4 == Barry's Big Bass Business :-)
0032         seller = broker.lookup('Seller_1.Seller', 'B4')
0033         if seller:
0034             price = seller.price()
0035             print 'Seller wants $', price, '... '
0036             if price > self._maxprice:
0037                 print 'too much!'
0038             else:
0039                 print "I'll take it!"
0040         else:
0041             print 'no seller found here'
0042 '''                                     # Don't ask why this comment is here
0043 
0044 STATE = '''\
0045 # instantiate a buyer instance and put it in a magic place for the KOS
0046 # to find.
0047 __kp__ = Buyer()
0048 __kp__.__setup__(500)
0049 '''
0050 
0051 SIMPLE_METADATA = [
0052         ("Interpreter", "python"),
0053         ("Interpreter-Version", "1.3"),
0054         ("Owner-Name", "Barry Warsaw"),
0055         ("Owner-Rendezvous", "bwarsaw@cnri.reston.va.us"),
0056         ("Home-KSS", "kss.cnri.reston.va.us"),
0057         ("Identifier", "hdl://cnri.kss/my_first_knowbot"),
0058         ("Launch-Date", "Mon Feb 12 16:39:03 EST 1996"),
0059         ]
0060 
0061 COMPLEX_METADATA = [
0062         ("Metadata-Type", "complex"),
0063         ("Metadata-Key", "connection"),
0064         ("Access", "read-only"),
0065         ("Connection-Description", "Barry's Big Bass Business"),
0066         ("Connection-Id", "B4"),
0067         ("Connection-Direction", "client"),
0068         ]
0069 
0070 EXTERNAL_METADATA = [
0071         ("Metadata-Type", "complex"),
0072         ("Metadata-Key", "generic-interface"),
0073         ("Access", "read-only"),
0074         ("Connection-Description", "Generic Interface for All Knowbots"),
0075         ("Connection-Id", "generic-kp"),
0076         ("Connection-Direction", "client"),
0077         ]
0078 
0079 
0080 def main():
0081     import sys
0082 
0083     # Toplevel headers
0084 
0085     toplevel = MimeWriter(sys.stdout)
0086     toplevel.addheader("From", "bwarsaw@cnri.reston.va.us")
0087     toplevel.addheader("Date", "Mon Feb 12 17:21:48 EST 1996")
0088     toplevel.addheader("To", "kss-submit@cnri.reston.va.us")
0089     toplevel.addheader("MIME-Version", "1.0")
0090 
0091     # Toplevel body parts
0092 
0093     f = toplevel.startmultipartbody("knowbot", "801spam999",
0094                                     [("version", "0.1")], prefix=0)
0095     f.write("This is a multi-part message in MIME format.\n")
0096 
0097     # First toplevel body part: metadata
0098 
0099     md = toplevel.nextpart()
0100     md.startmultipartbody("knowbot-metadata", "802spam999")
0101 
0102     # Metadata part 1
0103 
0104     md1 = md.nextpart()
0105     md1.addheader("KP-Metadata-Type", "simple")
0106     md1.addheader("KP-Access", "read-only")
0107     m = MimeWriter(md1.startbody("message/rfc822"))
0108     for key, value in SIMPLE_METADATA:
0109         m.addheader("KPMD-" + key, value)
0110     m.flushheaders()
0111     del md1
0112 
0113     # Metadata part 2
0114 
0115     md2 = md.nextpart()
0116     for key, value in COMPLEX_METADATA:
0117         md2.addheader("KP-" + key, value)
0118     f = md2.startbody("text/isl")
0119     f.write(SELLER)
0120     del md2
0121 
0122     # Metadata part 3
0123 
0124     md3 = md.nextpart()
0125     f = md3.startbody("message/external-body",
0126                       [("access-type", "URL"),
0127                        ("URL", "hdl://cnri.kss/generic-knowbot")])
0128     m = MimeWriter(f)
0129     for key, value in EXTERNAL_METADATA:
0130         md3.addheader("KP-" + key, value)
0131     md3.startbody("text/isl")
0132     # Phantom body doesn't need to be written
0133 
0134     md.lastpart()
0135 
0136     # Second toplevel body part: code
0137 
0138     code = toplevel.nextpart()
0139     code.startmultipartbody("knowbot-code", "803spam999")
0140 
0141     # Code: buyer program source
0142 
0143     buyer = code.nextpart()
0144     buyer.addheader("KP-Module-Name", "BuyerKP")
0145     f = buyer.startbody("text/plain")
0146     f.write(BUYER)
0147 
0148     code.lastpart()
0149 
0150     # Third toplevel body part: state
0151 
0152     state = toplevel.nextpart()
0153     state.addheader("KP-Main-Module", "main")
0154     state.startmultipartbody("knowbot-state", "804spam999")
0155 
0156     # State: a bunch of assignments
0157 
0158     st = state.nextpart()
0159     st.addheader("KP-Module-Name", "main")
0160     f = st.startbody("text/plain")
0161     f.write(STATE)
0162 
0163     state.lastpart()
0164 
0165     # End toplevel body parts
0166 
0167     toplevel.lastpart()
0168 
0169 
0170 main()
0171 

Generated by PyXR 0.9.4
SourceForge.net Logo