1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/WebStack/Twisted.py Tue Jan 27 23:38:38 2004 +0000
1.3 @@ -0,0 +1,131 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Twisted classes.
1.8 +"""
1.9 +
1.10 +import Generic
1.11 +
1.12 +class Transaction(Generic.Transaction):
1.13 +
1.14 + """
1.15 + Twisted transaction interface.
1.16 + """
1.17 +
1.18 + def __init__(self, trans):
1.19 +
1.20 + "Initialise the transaction using the Twisted transaction 'trans'."
1.21 +
1.22 + self.trans = trans
1.23 +
1.24 + # Request-related methods.
1.25 +
1.26 + def get_request_stream(self):
1.27 +
1.28 + """
1.29 + A framework-specific method which returns the request stream for
1.30 + the transaction.
1.31 + """
1.32 +
1.33 + return self.trans
1.34 +
1.35 + def get_request_method(self):
1.36 +
1.37 + """
1.38 + A framework-specific method which gets the request method.
1.39 + """
1.40 +
1.41 + return self.trans.method
1.42 +
1.43 + def get_headers(self):
1.44 +
1.45 + """
1.46 + A framework-specific method which returns the request headers.
1.47 + NOTE: Experimental, since framework support varies somewhat.
1.48 + """
1.49 +
1.50 + # NOTE: Accessing attribute of transaction object.
1.51 +
1.52 + return self.trans.received_headers
1.53 +
1.54 + def get_content_type(self):
1.55 +
1.56 + """
1.57 + A framework-specific method which gets the content type specified on the
1.58 + request, along with the charset employed.
1.59 + """
1.60 +
1.61 + return self.parse_content_type(self.trans.getHeader("Content-Type"))
1.62 +
1.63 + def get_content_charsets(self):
1.64 +
1.65 + """
1.66 + Returns the character set preferences.
1.67 + """
1.68 +
1.69 + return self.parse_content_preferences(self.trans.getHeader("Accept-Language"))
1.70 +
1.71 + def get_content_languages(self):
1.72 +
1.73 + """
1.74 + A framework-specific method which extracts language information from
1.75 + the transaction.
1.76 + """
1.77 +
1.78 + return self.parse_content_preferences(self.trans.getHeader("Accept-Charset"))
1.79 +
1.80 + def get_path_info(self):
1.81 +
1.82 + """
1.83 + A framework-specific method which gets the "path info" (the part of the
1.84 + URL after the resource name handling the current request) from the
1.85 + request.
1.86 + """
1.87 +
1.88 + raise NotImplementedError, "get_path_info"
1.89 +
1.90 + # Higher level request-related methods.
1.91 +
1.92 + def get_fields(self):
1.93 +
1.94 + """
1.95 + A framework-specific method which extracts the form fields from the
1.96 + transaction.
1.97 + """
1.98 +
1.99 + # NOTE: Discard multiple field values.
1.100 +
1.101 + return dict([(key, value[0]) for (key, value) in self.trans.args.items()])
1.102 +
1.103 + def get_agent_information(self):
1.104 +
1.105 + """
1.106 + A framework-specific method which extracts agent information from
1.107 + the transaction.
1.108 + """
1.109 +
1.110 + return None
1.111 +
1.112 + # Response-related methods.
1.113 +
1.114 + def get_response_stream(self):
1.115 +
1.116 + """
1.117 + A framework-specific method which returns the response stream for
1.118 + the transaction.
1.119 + """
1.120 +
1.121 + return self.trans.content
1.122 +
1.123 + def set_content_type(self, content_type):
1.124 +
1.125 + """
1.126 + A framework-specific method which sets the 'content_type' for the
1.127 + response.
1.128 + """
1.129 +
1.130 + # Make sure that only ASCII is used in the header.
1.131 +
1.132 + self.trans.setHeader("Content-Type", content_type.encode("US-ASCII"))
1.133 +
1.134 +# vim: tabstop=4 expandtab shiftwidth=4