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 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_info(self): 78 79 """ 80 A framework-specific method which gets the "path info" (the part of the 81 URL after the resource name handling the current request) from the 82 request. 83 """ 84 85 raise NotImplementedError, "get_path_info" 86 87 # Higher level request-related methods. 88 89 def get_fields(self): 90 91 """ 92 A framework-specific method which extracts the form fields from the 93 transaction. 94 """ 95 96 # NOTE: Discard multiple field values. 97 98 return dict([(key, value[0]) for (key, value) in self.trans.args.items()]) 99 100 def get_agent_information(self): 101 102 """ 103 A framework-specific method which extracts agent information from 104 the transaction. 105 """ 106 107 return None 108 109 # Response-related methods. 110 111 def get_response_stream(self): 112 113 """ 114 A framework-specific method which returns the response stream for 115 the transaction. 116 """ 117 118 return self.trans.content 119 120 def set_content_type(self, content_type): 121 122 """ 123 A framework-specific method which sets the 'content_type' for the 124 response. 125 """ 126 127 # Make sure that only ASCII is used in the header. 128 129 self.trans.setHeader("Content-Type", content_type.encode("US-ASCII")) 130 131 # vim: tabstop=4 expandtab shiftwidth=4