def addParam(self, paramName, paramVal): """adds a form parameter paramName with the (string) value paramVal """ msg = Message() msg["Content-Disposition"] = "form-data" msg.set_param("name", paramName, "Content-Disposition") msg.set_payload(paramVal) self.attach(msg)
def addFile(self, paramName, fileName, data): """attaches the contents of fileName under the http parameter name paramName. """ msg = Message() msg.set_type("application/octet-stream") msg["Content-Disposition"] = "form-data" msg.set_param("name", paramName, "Content-Disposition") msg.set_param("filename", fileName, "Content-Disposition") msg.set_payload(data) self.attach(msg)
def generateMessage(self, text): """generateMessageObject(text) This will create a headered, MIME-formatted message, with content type as text/plain,format=flowed and will flow the passed in text. Note an agent will still need to call the prepare function to space stfuf any sections which require it. You can take the return value, and just attach it, or turn it into a full message by adding the required headers.""" msg = Message() msg.set_type("text/plain") msg.set_param("format", "flowed") msg.set_payload(self.flow(text)) return msg
if marshaler is None: continue payload = Message() attach = False contentType = marshaler.getContentType() payloadCharset = marshaler.getCharset(charset) if contentType is not None: payload.set_type(contentType) attach = True if payloadCharset is not None: # using set_charset() would also add transfer encoding, # which we don't want to do always payload.set_param('charset', payloadCharset) attach = True value = marshaler.marshal(charset, primary=True) if value is not None: payload.set_payload(value) attach = True if attach: marshaler.postProcessMessage(payload) msg.attach(payload) return msg
def constructMessage(context, fields, charset='utf-8'): msg = Message() primary = [] # First get all headers, storing primary fields for later for name, field in fields: if IPrimaryField.providedBy(field): primary.append((name, field,)) break # marshaler = queryMultiAdapter((context, field,), IFieldMarshaler) # if marshaler is None: # LOG.debug("No marshaler found for field %s of %s" % (name, repr(context))) # continue # # try: # value = marshaler.marshal(charset, primary=False) # except ValueError, e: # LOG.debug("Marshaling of %s for %s failed: %s" % (name, repr(context), str(e))) # continue # # if value is None: # value = '' # elif not isinstance(value, str): # raise ValueError("Marshaler for field %s did not return a string" % name) # # if marshaler.ascii and '\n' not in value: # msg[name] = value # else: # msg[name] = Header(value, charset) # Then deal with the primary field # If there's a single primary field, we have a non-multipart message with # a string payload if len(primary) == 1: name, field = primary[0] marshaler = queryMultiAdapter((context, field,), IFieldMarshaler) # import pdb # pdb.set_trace() if marshaler is not None: contentType = marshaler.getContentType() payloadCharset = marshaler.getCharset(charset) # if contentType is not None: # msg.set_type(contentType) # # if payloadCharset is not None: # # using set_charset() would also add transfer encoding, # # which we don't want to do always # msg.set_param('charset', payloadCharset) value = marshaler.marshal(charset, primary=True) # import pdb # pdb.set_trace() if value is not None: msg.set_payload(value) # marshaler.postProcessMessage(msg) # Otherwise, we return a multipart message elif len(primary) > 1: msg.set_type('multipart/mixed') for name, field in primary: marshaler = queryMultiAdapter((context, field,), IFieldMarshaler) if marshaler is None: continue payload = Message() attach = False contentType = marshaler.getContentType() payloadCharset = marshaler.getCharset(charset) if contentType is not None: payload.set_type(contentType) attach = True if payloadCharset is not None: # using set_charset() would also add transfer encoding, # which we don't want to do always payload.set_param('charset', payloadCharset) attach = True value = marshaler.marshal(charset, primary=True) if value is not None: payload.set_payload(value) attach = True if attach: marshaler.postProcessMessage(payload) msg.attach(payload) return msg