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