1 #!/usr/bin/env python 2 3 """ 4 Twisted classes. 5 """ 6 7 import Generic 8 9 class Transaction(Generic.Transaction): 10 11 """ 12 Twisted transaction interface. 13 """ 14 15 def __init__(self, trans): 16 17 "Initialise the transaction using the Twisted transaction 'trans'." 18 19 self.trans = trans 20 21 # Request-related methods. 22 23 def get_request_stream(self): 24 25 """ 26 A framework-specific method which returns the request stream for 27 the transaction. 28 """ 29 30 return self.trans.content 31 32 def get_request_method(self): 33 34 """ 35 A framework-specific method which gets the request method. 36 """ 37 38 return self.trans.method 39 40 def get_headers(self): 41 42 """ 43 A framework-specific method which returns the request headers. 44 NOTE: Experimental, since framework support varies somewhat. 45 """ 46 47 # NOTE: Accessing attribute of transaction object. 48 49 return self.trans.received_headers 50 51 def get_content_type(self): 52 53 """ 54 A framework-specific method which gets the content type specified on the 55 request, along with the charset employed. 56 """ 57 58 return self.parse_content_type(self.trans.getHeader("Content-Type")) 59 60 def get_content_charsets(self): 61 62 """ 63 Returns the character set preferences. 64 """ 65 66 return self.parse_content_preferences(self.trans.getHeader("Accept-Language")) 67 68 def get_content_languages(self): 69 70 """ 71 A framework-specific method which extracts language information from 72 the transaction. 73 """ 74 75 return self.parse_content_preferences(self.trans.getHeader("Accept-Charset")) 76 77 def get_path(self): 78 79 """ 80 A framework-specific method which gets the entire path from the request. 81 """ 82 83 # NOTE: The path and "path info" are mostly equivalent for Twisted. 84 85 return self.trans.postpath 86 87 def get_path_info(self): 88 89 """ 90 A framework-specific method which gets the "path info" (the part of the 91 URL after the resource name handling the current request) from the 92 request. 93 """ 94 95 return self.trans.postpath 96 97 # Higher level request-related methods. 98 99 def get_fields(self): 100 101 """ 102 A framework-specific method which extracts the form fields from the 103 transaction. 104 """ 105 106 # NOTE: Discard multiple field values. 107 108 return dict([(key, value[0]) for (key, value) in self.trans.args.items()]) 109 110 def get_agent_information(self): 111 112 """ 113 A framework-specific method which extracts agent information from 114 the transaction. 115 """ 116 117 return None 118 119 # Response-related methods. 120 121 def get_response_stream(self): 122 123 """ 124 A framework-specific method which returns the response stream for 125 the transaction. 126 """ 127 128 return self.trans 129 130 def set_content_type(self, content_type): 131 132 """ 133 A framework-specific method which sets the 'content_type' for the 134 response. 135 """ 136 137 self.trans.setHeader("Content-Type", self.format_content_type(content_type)) 138 139 # vim: tabstop=4 expandtab shiftwidth=4