1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/WebStack/Generic.py Tue Jan 27 23:38:38 2004 +0000
1.3 @@ -0,0 +1,168 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Generic Web framework interfaces.
1.8 +"""
1.9 +
1.10 +class ContentType:
1.11 +
1.12 + "A container for content type information."
1.13 +
1.14 + def __init__(self, content_type, charset):
1.15 + self.content_type = content_type
1.16 + self.charset = charset
1.17 +
1.18 +class Transaction:
1.19 +
1.20 + """
1.21 + A generic transaction interface containing framework-specific methods to be
1.22 + overridden.
1.23 + """
1.24 +
1.25 + # Utility methods.
1.26 +
1.27 + def parse_content_type(self, content_type_field):
1.28 +
1.29 + """
1.30 + Determine the content type and charset from the supplied
1.31 + 'content_type_field' string.
1.32 + """
1.33 +
1.34 + if content_type_field is None:
1.35 + return ContentType(None, "iso-8859-1")
1.36 +
1.37 + t = content_type_field.split("; charset=")
1.38 + if len(t) == 1:
1.39 + return ContentType(t[0], "iso-8859-1")
1.40 + else:
1.41 + return ContentType(t[0], t[1])
1.42 +
1.43 + def parse_content_preferences(self, accept_preference):
1.44 +
1.45 + """
1.46 + Returns the preferences as requested by the user agent. The preferences are
1.47 + returned as a list of codes in the same order as they appeared in the
1.48 + appropriate environment variable. In other words, the explicit weighting
1.49 + criteria are ignored.
1.50 +
1.51 + As the 'accept_preference' parameter, values for language and charset
1.52 + preferences are appropriate.
1.53 + """
1.54 +
1.55 + accept_defs = accept_preference.split(",")
1.56 + accept_prefs = []
1.57 + for accept_def in accept_defs:
1.58 + t = accept_def.split(";")
1.59 + if len(t) >= 1:
1.60 + accept_prefs.append(t[0].strip())
1.61 + return accept_prefs
1.62 +
1.63 + # Request-related methods.
1.64 +
1.65 + def get_request_stream(self):
1.66 +
1.67 + """
1.68 + A framework-specific method which returns the request stream for
1.69 + the transaction.
1.70 + """
1.71 +
1.72 + raise NotImplementedError, "get_request_stream"
1.73 +
1.74 + def get_request_method(self):
1.75 +
1.76 + """
1.77 + A framework-specific method which gets the request method.
1.78 + """
1.79 +
1.80 + raise NotImplementedError, "get_request_method"
1.81 +
1.82 + def get_headers(self):
1.83 +
1.84 + """
1.85 + A framework-specific method which returns the request headers.
1.86 + NOTE: Experimental, since framework support varies somewhat.
1.87 + """
1.88 +
1.89 + raise NotImplementedError, "get_headers"
1.90 +
1.91 + def get_content_type(self):
1.92 +
1.93 + """
1.94 + A framework-specific method which gets the content type specified on the
1.95 + request, along with the charset employed.
1.96 + """
1.97 +
1.98 + raise NotImplementedError, "get_content_type"
1.99 +
1.100 + def get_content_charsets(self):
1.101 +
1.102 + """
1.103 + Returns the character set preferences.
1.104 + """
1.105 +
1.106 + raise NotImplementedError, "get_content_charsets"
1.107 +
1.108 + def get_content_languages(self):
1.109 +
1.110 + """
1.111 + A framework-specific method which extracts language information from
1.112 + the transaction.
1.113 + """
1.114 +
1.115 + raise NotImplementedError, "get_content_languages"
1.116 +
1.117 + def get_path_info(self):
1.118 +
1.119 + """
1.120 + A framework-specific method which gets the "path info" (the part of the
1.121 + URL after the resource name handling the current request) from the
1.122 + request.
1.123 + """
1.124 +
1.125 + raise NotImplementedError, "get_path_info"
1.126 +
1.127 + def get_fields(self):
1.128 +
1.129 + """
1.130 + A framework-specific method which extracts the form fields from the
1.131 + transaction.
1.132 + """
1.133 +
1.134 + raise NotImplementedError, "get_fields"
1.135 +
1.136 + def get_agent_information(self):
1.137 +
1.138 + """
1.139 + A framework-specific method which extracts agent information from
1.140 + the transaction.
1.141 + """
1.142 +
1.143 + raise NotImplementedError, "get_agent_information"
1.144 +
1.145 + # Response-related methods.
1.146 +
1.147 + def get_response_stream(self):
1.148 +
1.149 + """
1.150 + A framework-specific method which returns the response stream for
1.151 + the transaction.
1.152 + """
1.153 +
1.154 + raise NotImplementedError, "get_response_stream"
1.155 +
1.156 + def set_content_type(self, content_type):
1.157 +
1.158 + """
1.159 + A framework-specific method which sets the 'content_type' for the
1.160 + response.
1.161 + """
1.162 +
1.163 + raise NotImplementedError, "set_content_type"
1.164 +
1.165 +class Resource:
1.166 +
1.167 + "A generic resource interface."
1.168 +
1.169 + pass
1.170 +
1.171 +# vim: tabstop=4 expandtab shiftwidth=4
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/WebStack/ModPython.py Tue Jan 27 23:38:38 2004 +0000
2.3 @@ -0,0 +1,128 @@
2.4 +#!/usr/bin/env python
2.5 +
2.6 +"""
2.7 +mod_python classes.
2.8 +"""
2.9 +
2.10 +import Generic
2.11 +from mod_python.util import FieldStorage
2.12 +
2.13 +class Transaction(Generic.Transaction):
2.14 +
2.15 + """
2.16 + mod_python transaction interface.
2.17 + """
2.18 +
2.19 + def __init__(self, trans):
2.20 +
2.21 + "Initialise the transaction using the mod_python transaction 'trans'."
2.22 +
2.23 + self.trans = trans
2.24 +
2.25 + # Request-related methods.
2.26 +
2.27 + def get_request_stream(self):
2.28 +
2.29 + """
2.30 + A framework-specific method which returns the request stream for
2.31 + the transaction.
2.32 + """
2.33 +
2.34 + return self.trans
2.35 +
2.36 + def get_request_method(self):
2.37 +
2.38 + """
2.39 + A framework-specific method which gets the request method.
2.40 + """
2.41 +
2.42 + return self.trans.method
2.43 +
2.44 + def get_headers(self):
2.45 +
2.46 + """
2.47 + A framework-specific method which returns the request headers.
2.48 + NOTE: Experimental, since framework support varies somewhat.
2.49 + """
2.50 +
2.51 + return self.trans.headers_in
2.52 +
2.53 + def get_content_type(self):
2.54 +
2.55 + """
2.56 + A framework-specific method which gets the content type specified on the
2.57 + request, along with the charset employed.
2.58 + """
2.59 +
2.60 + return self.parse_content_type(self.trans.content_type)
2.61 +
2.62 + def get_content_charsets(self):
2.63 +
2.64 + """
2.65 + Returns the character set preferences.
2.66 + """
2.67 +
2.68 + return self.parse_content_preferences(self.trans.headers_in["Accept-Charset"])
2.69 +
2.70 + def get_content_languages(self):
2.71 +
2.72 + """
2.73 + A framework-specific method which extracts language information from
2.74 + the transaction.
2.75 + """
2.76 +
2.77 + return self.parse_content_preferences(self.trans.headers_in["Accept-Language"])
2.78 +
2.79 + def get_path_info(self):
2.80 +
2.81 + """
2.82 + A framework-specific method which gets the "path info" (the part of the
2.83 + URL after the resource name handling the current request) from the
2.84 + request.
2.85 + """
2.86 +
2.87 + raise NotImplementedError, "get_path_info"
2.88 +
2.89 + # Higher level request-related methods.
2.90 +
2.91 + def get_fields(self):
2.92 +
2.93 + """
2.94 + A framework-specific method which extracts the form fields from the
2.95 + transaction.
2.96 + """
2.97 +
2.98 + return FieldStorage(self.trans, keep_blank_values=1)
2.99 +
2.100 + def get_agent_information(self):
2.101 +
2.102 + """
2.103 + A framework-specific method which extracts agent information from
2.104 + the transaction.
2.105 + """
2.106 +
2.107 + return None
2.108 +
2.109 + # Response-related methods.
2.110 +
2.111 + def get_response_stream(self):
2.112 +
2.113 + """
2.114 + A framework-specific method which returns the response stream for
2.115 + the transaction.
2.116 + """
2.117 +
2.118 + return self.trans
2.119 +
2.120 + def set_content_type(self, content_type):
2.121 +
2.122 + """
2.123 + A framework-specific method which sets the 'content_type' for the
2.124 + response.
2.125 + """
2.126 +
2.127 + # Make sure that only ASCII is used in the header.
2.128 +
2.129 + self.trans.content_type = content_type.encode("US-ASCII")
2.130 +
2.131 +# vim: tabstop=4 expandtab shiftwidth=4
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/WebStack/Twisted.py Tue Jan 27 23:38:38 2004 +0000
3.3 @@ -0,0 +1,131 @@
3.4 +#!/usr/bin/env python
3.5 +
3.6 +"""
3.7 +Twisted classes.
3.8 +"""
3.9 +
3.10 +import Generic
3.11 +
3.12 +class Transaction(Generic.Transaction):
3.13 +
3.14 + """
3.15 + Twisted transaction interface.
3.16 + """
3.17 +
3.18 + def __init__(self, trans):
3.19 +
3.20 + "Initialise the transaction using the Twisted transaction 'trans'."
3.21 +
3.22 + self.trans = trans
3.23 +
3.24 + # Request-related methods.
3.25 +
3.26 + def get_request_stream(self):
3.27 +
3.28 + """
3.29 + A framework-specific method which returns the request stream for
3.30 + the transaction.
3.31 + """
3.32 +
3.33 + return self.trans
3.34 +
3.35 + def get_request_method(self):
3.36 +
3.37 + """
3.38 + A framework-specific method which gets the request method.
3.39 + """
3.40 +
3.41 + return self.trans.method
3.42 +
3.43 + def get_headers(self):
3.44 +
3.45 + """
3.46 + A framework-specific method which returns the request headers.
3.47 + NOTE: Experimental, since framework support varies somewhat.
3.48 + """
3.49 +
3.50 + # NOTE: Accessing attribute of transaction object.
3.51 +
3.52 + return self.trans.received_headers
3.53 +
3.54 + def get_content_type(self):
3.55 +
3.56 + """
3.57 + A framework-specific method which gets the content type specified on the
3.58 + request, along with the charset employed.
3.59 + """
3.60 +
3.61 + return self.parse_content_type(self.trans.getHeader("Content-Type"))
3.62 +
3.63 + def get_content_charsets(self):
3.64 +
3.65 + """
3.66 + Returns the character set preferences.
3.67 + """
3.68 +
3.69 + return self.parse_content_preferences(self.trans.getHeader("Accept-Language"))
3.70 +
3.71 + def get_content_languages(self):
3.72 +
3.73 + """
3.74 + A framework-specific method which extracts language information from
3.75 + the transaction.
3.76 + """
3.77 +
3.78 + return self.parse_content_preferences(self.trans.getHeader("Accept-Charset"))
3.79 +
3.80 + def get_path_info(self):
3.81 +
3.82 + """
3.83 + A framework-specific method which gets the "path info" (the part of the
3.84 + URL after the resource name handling the current request) from the
3.85 + request.
3.86 + """
3.87 +
3.88 + raise NotImplementedError, "get_path_info"
3.89 +
3.90 + # Higher level request-related methods.
3.91 +
3.92 + def get_fields(self):
3.93 +
3.94 + """
3.95 + A framework-specific method which extracts the form fields from the
3.96 + transaction.
3.97 + """
3.98 +
3.99 + # NOTE: Discard multiple field values.
3.100 +
3.101 + return dict([(key, value[0]) for (key, value) in self.trans.args.items()])
3.102 +
3.103 + def get_agent_information(self):
3.104 +
3.105 + """
3.106 + A framework-specific method which extracts agent information from
3.107 + the transaction.
3.108 + """
3.109 +
3.110 + return None
3.111 +
3.112 + # Response-related methods.
3.113 +
3.114 + def get_response_stream(self):
3.115 +
3.116 + """
3.117 + A framework-specific method which returns the response stream for
3.118 + the transaction.
3.119 + """
3.120 +
3.121 + return self.trans.content
3.122 +
3.123 + def set_content_type(self, content_type):
3.124 +
3.125 + """
3.126 + A framework-specific method which sets the 'content_type' for the
3.127 + response.
3.128 + """
3.129 +
3.130 + # Make sure that only ASCII is used in the header.
3.131 +
3.132 + self.trans.setHeader("Content-Type", content_type.encode("US-ASCII"))
3.133 +
3.134 +# vim: tabstop=4 expandtab shiftwidth=4
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/WebStack/Webware.py Tue Jan 27 23:38:38 2004 +0000
4.3 @@ -0,0 +1,135 @@
4.4 +#!/usr/bin/env python
4.5 +
4.6 +"""
4.7 +Webware classes.
4.8 +"""
4.9 +
4.10 +import Generic
4.11 +
4.12 +class Transaction(Generic.Transaction):
4.13 +
4.14 + """
4.15 + Webware transaction interface.
4.16 + """
4.17 +
4.18 + def __init__(self, trans):
4.19 +
4.20 + "Initialise the transaction using the Webware transaction 'trans'."
4.21 +
4.22 + self.trans = trans
4.23 +
4.24 + # Request-related methods.
4.25 +
4.26 + def get_request_stream(self):
4.27 +
4.28 + """
4.29 + A framework-specific method which returns the request stream for
4.30 + the transaction.
4.31 + """
4.32 +
4.33 + stream = self.trans.request().rawInput(rewind=1)
4.34 + if stream is None:
4.35 + return StringIO.StringIO("")
4.36 +
4.37 + return stream
4.38 +
4.39 + def get_request_method(self):
4.40 +
4.41 + """
4.42 + A framework-specific method which gets the request method.
4.43 + """
4.44 +
4.45 + return self.trans.request().method()
4.46 +
4.47 + def get_headers(self):
4.48 +
4.49 + """
4.50 + A framework-specific method which returns the request headers.
4.51 + NOTE: Experimental, since framework support varies somewhat.
4.52 + """
4.53 +
4.54 + # NOTE: Webware doesn't really provide access to headers in the request.
4.55 +
4.56 + return {}
4.57 +
4.58 + def get_content_type(self):
4.59 +
4.60 + """
4.61 + A framework-specific method which gets the content type specified on the
4.62 + request, along with the charset employed.
4.63 + """
4.64 +
4.65 + return self.parse_content_type(self.trans.request().contentType())
4.66 +
4.67 + def get_content_charsets(self):
4.68 +
4.69 + """
4.70 + Returns the character set preferences.
4.71 + NOTE: Requires enhancements to HTTPRequest.
4.72 + """
4.73 +
4.74 + return self.trans.request().contentCharsets()
4.75 +
4.76 + def get_content_languages(self):
4.77 +
4.78 + """
4.79 + A framework-specific method which extracts language information from
4.80 + the transaction.
4.81 + NOTE: Requires enhancements to HTTPRequest.
4.82 + """
4.83 +
4.84 + return self.trans.request().contentLanguages()
4.85 +
4.86 + def get_path_info(self):
4.87 +
4.88 + """
4.89 + A framework-specific method which gets the "path info" (the part of the
4.90 + URL after the resource name handling the current request) from the
4.91 + request.
4.92 + """
4.93 +
4.94 + raise NotImplementedError, "get_path_info"
4.95 +
4.96 + # Higher level request-related methods.
4.97 +
4.98 + def get_fields(self):
4.99 +
4.100 + """
4.101 + A framework-specific method which extracts the form fields from the
4.102 + transaction.
4.103 + """
4.104 +
4.105 + return self.trans.request().fields()
4.106 +
4.107 + def get_agent_information(self):
4.108 +
4.109 + """
4.110 + A framework-specific method which extracts agent information from
4.111 + the transaction.
4.112 + """
4.113 +
4.114 + return None
4.115 +
4.116 + # Response-related methods.
4.117 +
4.118 + def get_response_stream(self):
4.119 +
4.120 + """
4.121 + A framework-specific method which returns the response stream for
4.122 + the transaction.
4.123 + """
4.124 +
4.125 + return self.trans.response()
4.126 +
4.127 + def set_content_type(self, content_type):
4.128 +
4.129 + """
4.130 + A framework-specific method which sets the 'content_type' for the
4.131 + response.
4.132 + """
4.133 +
4.134 + # Make sure that only ASCII is used in the header.
4.135 +
4.136 + return self.trans.response().setHeader("Content-Type", content_type.encode("US-ASCII"))
4.137 +
4.138 +# vim: tabstop=4 expandtab shiftwidth=4