# HG changeset patch # User paulb # Date 1075246718 0 # Node ID 479e36f115eafb52d82ed820f3fe5a34d937f39f # Parent 013e0b2e89503afc798dc89e5366933ebb11c147 [project @ 2004-01-27 23:38:38 by paulb] Initial revision diff -r 013e0b2e8950 -r 479e36f115ea WebStack/Generic.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WebStack/Generic.py Tue Jan 27 23:38:38 2004 +0000 @@ -0,0 +1,168 @@ +#!/usr/bin/env python + +""" +Generic Web framework interfaces. +""" + +class ContentType: + + "A container for content type information." + + def __init__(self, content_type, charset): + self.content_type = content_type + self.charset = charset + +class Transaction: + + """ + A generic transaction interface containing framework-specific methods to be + overridden. + """ + + # Utility methods. + + def parse_content_type(self, content_type_field): + + """ + Determine the content type and charset from the supplied + 'content_type_field' string. + """ + + if content_type_field is None: + return ContentType(None, "iso-8859-1") + + t = content_type_field.split("; charset=") + if len(t) == 1: + return ContentType(t[0], "iso-8859-1") + else: + return ContentType(t[0], t[1]) + + def parse_content_preferences(self, accept_preference): + + """ + Returns the preferences as requested by the user agent. The preferences are + returned as a list of codes in the same order as they appeared in the + appropriate environment variable. In other words, the explicit weighting + criteria are ignored. + + As the 'accept_preference' parameter, values for language and charset + preferences are appropriate. + """ + + accept_defs = accept_preference.split(",") + accept_prefs = [] + for accept_def in accept_defs: + t = accept_def.split(";") + if len(t) >= 1: + accept_prefs.append(t[0].strip()) + return accept_prefs + + # Request-related methods. + + def get_request_stream(self): + + """ + A framework-specific method which returns the request stream for + the transaction. + """ + + raise NotImplementedError, "get_request_stream" + + def get_request_method(self): + + """ + A framework-specific method which gets the request method. + """ + + raise NotImplementedError, "get_request_method" + + def get_headers(self): + + """ + A framework-specific method which returns the request headers. + NOTE: Experimental, since framework support varies somewhat. + """ + + raise NotImplementedError, "get_headers" + + def get_content_type(self): + + """ + A framework-specific method which gets the content type specified on the + request, along with the charset employed. + """ + + raise NotImplementedError, "get_content_type" + + def get_content_charsets(self): + + """ + Returns the character set preferences. + """ + + raise NotImplementedError, "get_content_charsets" + + def get_content_languages(self): + + """ + A framework-specific method which extracts language information from + the transaction. + """ + + raise NotImplementedError, "get_content_languages" + + def get_path_info(self): + + """ + A framework-specific method which gets the "path info" (the part of the + URL after the resource name handling the current request) from the + request. + """ + + raise NotImplementedError, "get_path_info" + + def get_fields(self): + + """ + A framework-specific method which extracts the form fields from the + transaction. + """ + + raise NotImplementedError, "get_fields" + + def get_agent_information(self): + + """ + A framework-specific method which extracts agent information from + the transaction. + """ + + raise NotImplementedError, "get_agent_information" + + # Response-related methods. + + def get_response_stream(self): + + """ + A framework-specific method which returns the response stream for + the transaction. + """ + + raise NotImplementedError, "get_response_stream" + + def set_content_type(self, content_type): + + """ + A framework-specific method which sets the 'content_type' for the + response. + """ + + raise NotImplementedError, "set_content_type" + +class Resource: + + "A generic resource interface." + + pass + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 013e0b2e8950 -r 479e36f115ea WebStack/ModPython.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WebStack/ModPython.py Tue Jan 27 23:38:38 2004 +0000 @@ -0,0 +1,128 @@ +#!/usr/bin/env python + +""" +mod_python classes. +""" + +import Generic +from mod_python.util import FieldStorage + +class Transaction(Generic.Transaction): + + """ + mod_python transaction interface. + """ + + def __init__(self, trans): + + "Initialise the transaction using the mod_python transaction 'trans'." + + self.trans = trans + + # Request-related methods. + + def get_request_stream(self): + + """ + A framework-specific method which returns the request stream for + the transaction. + """ + + return self.trans + + def get_request_method(self): + + """ + A framework-specific method which gets the request method. + """ + + return self.trans.method + + def get_headers(self): + + """ + A framework-specific method which returns the request headers. + NOTE: Experimental, since framework support varies somewhat. + """ + + return self.trans.headers_in + + def get_content_type(self): + + """ + A framework-specific method which gets the content type specified on the + request, along with the charset employed. + """ + + return self.parse_content_type(self.trans.content_type) + + def get_content_charsets(self): + + """ + Returns the character set preferences. + """ + + return self.parse_content_preferences(self.trans.headers_in["Accept-Charset"]) + + def get_content_languages(self): + + """ + A framework-specific method which extracts language information from + the transaction. + """ + + return self.parse_content_preferences(self.trans.headers_in["Accept-Language"]) + + def get_path_info(self): + + """ + A framework-specific method which gets the "path info" (the part of the + URL after the resource name handling the current request) from the + request. + """ + + raise NotImplementedError, "get_path_info" + + # Higher level request-related methods. + + def get_fields(self): + + """ + A framework-specific method which extracts the form fields from the + transaction. + """ + + return FieldStorage(self.trans, keep_blank_values=1) + + def get_agent_information(self): + + """ + A framework-specific method which extracts agent information from + the transaction. + """ + + return None + + # Response-related methods. + + def get_response_stream(self): + + """ + A framework-specific method which returns the response stream for + the transaction. + """ + + return self.trans + + def set_content_type(self, content_type): + + """ + A framework-specific method which sets the 'content_type' for the + response. + """ + + # Make sure that only ASCII is used in the header. + + self.trans.content_type = content_type.encode("US-ASCII") + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 013e0b2e8950 -r 479e36f115ea WebStack/Twisted.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WebStack/Twisted.py Tue Jan 27 23:38:38 2004 +0000 @@ -0,0 +1,131 @@ +#!/usr/bin/env python + +""" +Twisted classes. +""" + +import Generic + +class Transaction(Generic.Transaction): + + """ + Twisted transaction interface. + """ + + def __init__(self, trans): + + "Initialise the transaction using the Twisted transaction 'trans'." + + self.trans = trans + + # Request-related methods. + + def get_request_stream(self): + + """ + A framework-specific method which returns the request stream for + the transaction. + """ + + return self.trans + + def get_request_method(self): + + """ + A framework-specific method which gets the request method. + """ + + return self.trans.method + + def get_headers(self): + + """ + A framework-specific method which returns the request headers. + NOTE: Experimental, since framework support varies somewhat. + """ + + # NOTE: Accessing attribute of transaction object. + + return self.trans.received_headers + + def get_content_type(self): + + """ + A framework-specific method which gets the content type specified on the + request, along with the charset employed. + """ + + return self.parse_content_type(self.trans.getHeader("Content-Type")) + + def get_content_charsets(self): + + """ + Returns the character set preferences. + """ + + return self.parse_content_preferences(self.trans.getHeader("Accept-Language")) + + def get_content_languages(self): + + """ + A framework-specific method which extracts language information from + the transaction. + """ + + return self.parse_content_preferences(self.trans.getHeader("Accept-Charset")) + + def get_path_info(self): + + """ + A framework-specific method which gets the "path info" (the part of the + URL after the resource name handling the current request) from the + request. + """ + + raise NotImplementedError, "get_path_info" + + # Higher level request-related methods. + + def get_fields(self): + + """ + A framework-specific method which extracts the form fields from the + transaction. + """ + + # NOTE: Discard multiple field values. + + return dict([(key, value[0]) for (key, value) in self.trans.args.items()]) + + def get_agent_information(self): + + """ + A framework-specific method which extracts agent information from + the transaction. + """ + + return None + + # Response-related methods. + + def get_response_stream(self): + + """ + A framework-specific method which returns the response stream for + the transaction. + """ + + return self.trans.content + + def set_content_type(self, content_type): + + """ + A framework-specific method which sets the 'content_type' for the + response. + """ + + # Make sure that only ASCII is used in the header. + + self.trans.setHeader("Content-Type", content_type.encode("US-ASCII")) + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 013e0b2e8950 -r 479e36f115ea WebStack/Webware.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WebStack/Webware.py Tue Jan 27 23:38:38 2004 +0000 @@ -0,0 +1,135 @@ +#!/usr/bin/env python + +""" +Webware classes. +""" + +import Generic + +class Transaction(Generic.Transaction): + + """ + Webware transaction interface. + """ + + def __init__(self, trans): + + "Initialise the transaction using the Webware transaction 'trans'." + + self.trans = trans + + # Request-related methods. + + def get_request_stream(self): + + """ + A framework-specific method which returns the request stream for + the transaction. + """ + + stream = self.trans.request().rawInput(rewind=1) + if stream is None: + return StringIO.StringIO("") + + return stream + + def get_request_method(self): + + """ + A framework-specific method which gets the request method. + """ + + return self.trans.request().method() + + def get_headers(self): + + """ + A framework-specific method which returns the request headers. + NOTE: Experimental, since framework support varies somewhat. + """ + + # NOTE: Webware doesn't really provide access to headers in the request. + + return {} + + def get_content_type(self): + + """ + A framework-specific method which gets the content type specified on the + request, along with the charset employed. + """ + + return self.parse_content_type(self.trans.request().contentType()) + + def get_content_charsets(self): + + """ + Returns the character set preferences. + NOTE: Requires enhancements to HTTPRequest. + """ + + return self.trans.request().contentCharsets() + + def get_content_languages(self): + + """ + A framework-specific method which extracts language information from + the transaction. + NOTE: Requires enhancements to HTTPRequest. + """ + + return self.trans.request().contentLanguages() + + def get_path_info(self): + + """ + A framework-specific method which gets the "path info" (the part of the + URL after the resource name handling the current request) from the + request. + """ + + raise NotImplementedError, "get_path_info" + + # Higher level request-related methods. + + def get_fields(self): + + """ + A framework-specific method which extracts the form fields from the + transaction. + """ + + return self.trans.request().fields() + + def get_agent_information(self): + + """ + A framework-specific method which extracts agent information from + the transaction. + """ + + return None + + # Response-related methods. + + def get_response_stream(self): + + """ + A framework-specific method which returns the response stream for + the transaction. + """ + + return self.trans.response() + + def set_content_type(self, content_type): + + """ + A framework-specific method which sets the 'content_type' for the + response. + """ + + # Make sure that only ASCII is used in the header. + + return self.trans.response().setHeader("Content-Type", content_type.encode("US-ASCII")) + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 013e0b2e8950 -r 479e36f115ea WebStack/__init__.py