paulb@1 | 1 | #!/usr/bin/env python |
paulb@1 | 2 | |
paulb@1 | 3 | """ |
paulb@1 | 4 | Generic Web framework interfaces. |
paulb@3 | 5 | The WebStack architecture consists of the following layers: |
paulb@3 | 6 | |
paulb@3 | 7 | * Framework - The underlying Web framework implementation. |
paulb@3 | 8 | * Adapter - Code operating under the particular framework which creates |
paulb@3 | 9 | WebStack abstractions and issues them to the application. |
paulb@3 | 10 | * Resources - Units of functionality operating within the hosted Web |
paulb@3 | 11 | application. |
paulb@3 | 12 | |
paulb@3 | 13 | Resources can act as both content producers within an application and as request |
paulb@3 | 14 | dispatchers to other resources; in the latter role, they may be referred to as |
paulb@3 | 15 | directors. |
paulb@1 | 16 | """ |
paulb@1 | 17 | |
paulb@1 | 18 | class ContentType: |
paulb@1 | 19 | |
paulb@1 | 20 | "A container for content type information." |
paulb@1 | 21 | |
paulb@5 | 22 | def __init__(self, content_type, charset=None): |
paulb@1 | 23 | self.content_type = content_type |
paulb@1 | 24 | self.charset = charset |
paulb@1 | 25 | |
paulb@1 | 26 | class Transaction: |
paulb@1 | 27 | |
paulb@1 | 28 | """ |
paulb@1 | 29 | A generic transaction interface containing framework-specific methods to be |
paulb@1 | 30 | overridden. |
paulb@1 | 31 | """ |
paulb@1 | 32 | |
paulb@28 | 33 | def commit(self): |
paulb@28 | 34 | |
paulb@28 | 35 | """ |
paulb@28 | 36 | A special method, synchronising the transaction with framework-specific |
paulb@28 | 37 | objects. |
paulb@28 | 38 | """ |
paulb@28 | 39 | |
paulb@28 | 40 | pass |
paulb@28 | 41 | |
paulb@1 | 42 | # Utility methods. |
paulb@1 | 43 | |
paulb@1 | 44 | def parse_content_type(self, content_type_field): |
paulb@1 | 45 | |
paulb@1 | 46 | """ |
paulb@1 | 47 | Determine the content type and charset from the supplied |
paulb@1 | 48 | 'content_type_field' string. |
paulb@1 | 49 | """ |
paulb@1 | 50 | |
paulb@1 | 51 | if content_type_field is None: |
paulb@1 | 52 | return ContentType(None, "iso-8859-1") |
paulb@1 | 53 | |
paulb@1 | 54 | t = content_type_field.split("; charset=") |
paulb@1 | 55 | if len(t) == 1: |
paulb@1 | 56 | return ContentType(t[0], "iso-8859-1") |
paulb@1 | 57 | else: |
paulb@1 | 58 | return ContentType(t[0], t[1]) |
paulb@1 | 59 | |
paulb@5 | 60 | def format_content_type(self, content_type): |
paulb@5 | 61 | |
paulb@5 | 62 | """ |
paulb@5 | 63 | Format the given 'content_type' object, producing a string suitable for |
paulb@5 | 64 | the response header field. |
paulb@5 | 65 | """ |
paulb@5 | 66 | |
paulb@5 | 67 | if content_type.charset: |
paulb@5 | 68 | field = "%s; charset=%s" % (content_type.content_type, content_type.charset) |
paulb@5 | 69 | else: |
paulb@5 | 70 | field = content_type.content_type |
paulb@5 | 71 | |
paulb@5 | 72 | # Make sure that only ASCII is used in the header. |
paulb@5 | 73 | |
paulb@5 | 74 | return field.encode("US-ASCII") |
paulb@5 | 75 | |
paulb@49 | 76 | def format_header_value(self, value): |
paulb@49 | 77 | |
paulb@49 | 78 | """ |
paulb@49 | 79 | Format the given header 'value'. Typically, this just ensures the usage |
paulb@49 | 80 | of US-ASCII. |
paulb@49 | 81 | """ |
paulb@49 | 82 | |
paulb@49 | 83 | return value.encode("US-ASCII") |
paulb@49 | 84 | |
paulb@1 | 85 | def parse_content_preferences(self, accept_preference): |
paulb@1 | 86 | |
paulb@1 | 87 | """ |
paulb@1 | 88 | Returns the preferences as requested by the user agent. The preferences are |
paulb@1 | 89 | returned as a list of codes in the same order as they appeared in the |
paulb@1 | 90 | appropriate environment variable. In other words, the explicit weighting |
paulb@1 | 91 | criteria are ignored. |
paulb@1 | 92 | |
paulb@1 | 93 | As the 'accept_preference' parameter, values for language and charset |
paulb@1 | 94 | preferences are appropriate. |
paulb@1 | 95 | """ |
paulb@1 | 96 | |
paulb@1 | 97 | accept_defs = accept_preference.split(",") |
paulb@1 | 98 | accept_prefs = [] |
paulb@1 | 99 | for accept_def in accept_defs: |
paulb@1 | 100 | t = accept_def.split(";") |
paulb@1 | 101 | if len(t) >= 1: |
paulb@1 | 102 | accept_prefs.append(t[0].strip()) |
paulb@1 | 103 | return accept_prefs |
paulb@1 | 104 | |
paulb@65 | 105 | def convert_to_list(self, value): |
paulb@65 | 106 | |
paulb@65 | 107 | """ |
paulb@65 | 108 | Returns a single element list containing 'value' if it is not itself a list, a |
paulb@65 | 109 | tuple, or None. If 'value' is a list then it is itself returned; if 'value' is a |
paulb@65 | 110 | tuple then a new list containing the same elements is returned; if 'value' is None |
paulb@65 | 111 | then an empty list is returned. |
paulb@65 | 112 | """ |
paulb@65 | 113 | |
paulb@65 | 114 | if type(value) == type([]): |
paulb@65 | 115 | return value |
paulb@65 | 116 | elif type(value) == type(()): |
paulb@65 | 117 | return list(value) |
paulb@65 | 118 | elif value is None: |
paulb@65 | 119 | return [] |
paulb@65 | 120 | else: |
paulb@65 | 121 | return [value] |
paulb@65 | 122 | |
paulb@1 | 123 | # Request-related methods. |
paulb@1 | 124 | |
paulb@1 | 125 | def get_request_stream(self): |
paulb@1 | 126 | |
paulb@1 | 127 | """ |
paulb@1 | 128 | A framework-specific method which returns the request stream for |
paulb@1 | 129 | the transaction. |
paulb@1 | 130 | """ |
paulb@1 | 131 | |
paulb@1 | 132 | raise NotImplementedError, "get_request_stream" |
paulb@1 | 133 | |
paulb@1 | 134 | def get_request_method(self): |
paulb@1 | 135 | |
paulb@1 | 136 | """ |
paulb@1 | 137 | A framework-specific method which gets the request method. |
paulb@1 | 138 | """ |
paulb@1 | 139 | |
paulb@1 | 140 | raise NotImplementedError, "get_request_method" |
paulb@1 | 141 | |
paulb@1 | 142 | def get_headers(self): |
paulb@1 | 143 | |
paulb@1 | 144 | """ |
paulb@65 | 145 | A framework-specific method which returns all request headers. |
paulb@1 | 146 | """ |
paulb@1 | 147 | |
paulb@1 | 148 | raise NotImplementedError, "get_headers" |
paulb@1 | 149 | |
paulb@65 | 150 | def get_header_values(self, key): |
paulb@65 | 151 | |
paulb@65 | 152 | """ |
paulb@65 | 153 | A framework-specific method which returns a list of all request header |
paulb@65 | 154 | values associated with the given 'key'. Note that according to RFC 2616, |
paulb@65 | 155 | 'key' is treated as a case-insensitive string. |
paulb@65 | 156 | """ |
paulb@65 | 157 | |
paulb@65 | 158 | raise NotImplementedError, "get_header_values" |
paulb@65 | 159 | |
paulb@1 | 160 | def get_content_type(self): |
paulb@1 | 161 | |
paulb@1 | 162 | """ |
paulb@1 | 163 | A framework-specific method which gets the content type specified on the |
paulb@1 | 164 | request, along with the charset employed. |
paulb@1 | 165 | """ |
paulb@1 | 166 | |
paulb@1 | 167 | raise NotImplementedError, "get_content_type" |
paulb@1 | 168 | |
paulb@1 | 169 | def get_content_charsets(self): |
paulb@1 | 170 | |
paulb@1 | 171 | """ |
paulb@1 | 172 | Returns the character set preferences. |
paulb@1 | 173 | """ |
paulb@1 | 174 | |
paulb@1 | 175 | raise NotImplementedError, "get_content_charsets" |
paulb@1 | 176 | |
paulb@1 | 177 | def get_content_languages(self): |
paulb@1 | 178 | |
paulb@1 | 179 | """ |
paulb@1 | 180 | A framework-specific method which extracts language information from |
paulb@1 | 181 | the transaction. |
paulb@1 | 182 | """ |
paulb@1 | 183 | |
paulb@1 | 184 | raise NotImplementedError, "get_content_languages" |
paulb@1 | 185 | |
paulb@5 | 186 | def get_path(self): |
paulb@5 | 187 | |
paulb@5 | 188 | """ |
paulb@5 | 189 | A framework-specific method which gets the entire path from the request. |
paulb@5 | 190 | """ |
paulb@5 | 191 | |
paulb@5 | 192 | raise NotImplementedError, "get_path" |
paulb@5 | 193 | |
paulb@1 | 194 | def get_path_info(self): |
paulb@1 | 195 | |
paulb@1 | 196 | """ |
paulb@1 | 197 | A framework-specific method which gets the "path info" (the part of the |
paulb@1 | 198 | URL after the resource name handling the current request) from the |
paulb@1 | 199 | request. |
paulb@1 | 200 | """ |
paulb@1 | 201 | |
paulb@1 | 202 | raise NotImplementedError, "get_path_info" |
paulb@1 | 203 | |
paulb@69 | 204 | def get_query_string(self): |
paulb@69 | 205 | |
paulb@69 | 206 | """ |
paulb@69 | 207 | A framework-specific method which gets the query string from the path in |
paulb@69 | 208 | the request. |
paulb@69 | 209 | """ |
paulb@69 | 210 | |
paulb@69 | 211 | raise NotImplementedError, "get_query_string" |
paulb@69 | 212 | |
paulb@69 | 213 | # Higher level request-related methods. |
paulb@69 | 214 | |
paulb@69 | 215 | def get_fields_from_path(self): |
paulb@69 | 216 | |
paulb@69 | 217 | """ |
paulb@69 | 218 | A framework-specific method which extracts the form fields from the |
paulb@69 | 219 | path specified in the transaction. The underlying framework may refuse |
paulb@69 | 220 | to supply fields from the path if handling a POST transaction. |
paulb@69 | 221 | |
paulb@69 | 222 | The returned object should employ the cgi.FieldStorage interface. |
paulb@69 | 223 | """ |
paulb@69 | 224 | |
paulb@69 | 225 | raise NotImplementedError, "get_fields_from_path" |
paulb@69 | 226 | |
paulb@69 | 227 | def get_fields_from_body(self): |
paulb@69 | 228 | |
paulb@69 | 229 | """ |
paulb@69 | 230 | A framework-specific method which extracts the form fields from the |
paulb@69 | 231 | message body in the transaction. |
paulb@69 | 232 | |
paulb@69 | 233 | The returned object should employ the cgi.FieldStorage interface. |
paulb@69 | 234 | """ |
paulb@69 | 235 | |
paulb@69 | 236 | raise NotImplementedError, "get_fields_from_body" |
paulb@69 | 237 | |
paulb@1 | 238 | def get_fields(self): |
paulb@1 | 239 | |
paulb@1 | 240 | """ |
paulb@1 | 241 | A framework-specific method which extracts the form fields from the |
paulb@69 | 242 | transaction. Typically, the origin of the form fields will be affected |
paulb@69 | 243 | by the method specified in the transaction. |
paulb@69 | 244 | |
paulb@69 | 245 | The returned object should employ the cgi.FieldStorage interface. |
paulb@1 | 246 | """ |
paulb@1 | 247 | |
paulb@1 | 248 | raise NotImplementedError, "get_fields" |
paulb@1 | 249 | |
paulb@45 | 250 | def get_user(self): |
paulb@1 | 251 | |
paulb@1 | 252 | """ |
paulb@45 | 253 | A framework-specific method which extracts user information from the |
paulb@45 | 254 | transaction. |
paulb@1 | 255 | """ |
paulb@1 | 256 | |
paulb@45 | 257 | raise NotImplementedError, "get_user" |
paulb@1 | 258 | |
paulb@1 | 259 | # Response-related methods. |
paulb@1 | 260 | |
paulb@1 | 261 | def get_response_stream(self): |
paulb@1 | 262 | |
paulb@1 | 263 | """ |
paulb@1 | 264 | A framework-specific method which returns the response stream for |
paulb@1 | 265 | the transaction. |
paulb@1 | 266 | """ |
paulb@1 | 267 | |
paulb@1 | 268 | raise NotImplementedError, "get_response_stream" |
paulb@1 | 269 | |
paulb@42 | 270 | def get_response_code(self): |
paulb@42 | 271 | |
paulb@42 | 272 | """ |
paulb@42 | 273 | Get the response code associated with the transaction. If no response |
paulb@42 | 274 | code is defined, None is returned. |
paulb@42 | 275 | """ |
paulb@42 | 276 | |
paulb@42 | 277 | raise NotImplementedError, "get_response_code" |
paulb@42 | 278 | |
paulb@42 | 279 | def set_response_code(self, response_code): |
paulb@42 | 280 | |
paulb@42 | 281 | """ |
paulb@42 | 282 | Set the 'response_code' using a numeric constant defined in the HTTP |
paulb@42 | 283 | specification. |
paulb@42 | 284 | """ |
paulb@42 | 285 | |
paulb@42 | 286 | raise NotImplementedError, "set_response_code" |
paulb@42 | 287 | |
paulb@67 | 288 | def set_header_value(self, header, value): |
paulb@49 | 289 | |
paulb@49 | 290 | """ |
paulb@49 | 291 | Set the HTTP 'header' with the given 'value'. |
paulb@49 | 292 | """ |
paulb@49 | 293 | |
paulb@67 | 294 | raise NotImplementedError, "set_header_value" |
paulb@49 | 295 | |
paulb@1 | 296 | def set_content_type(self, content_type): |
paulb@1 | 297 | |
paulb@1 | 298 | """ |
paulb@1 | 299 | A framework-specific method which sets the 'content_type' for the |
paulb@1 | 300 | response. |
paulb@1 | 301 | """ |
paulb@1 | 302 | |
paulb@1 | 303 | raise NotImplementedError, "set_content_type" |
paulb@1 | 304 | |
paulb@1 | 305 | class Resource: |
paulb@1 | 306 | |
paulb@1 | 307 | "A generic resource interface." |
paulb@1 | 308 | |
paulb@3 | 309 | def respond(self, trans): |
paulb@3 | 310 | |
paulb@3 | 311 | """ |
paulb@3 | 312 | An application-specific method which performs activities on the basis of |
paulb@3 | 313 | the transaction object 'trans'. |
paulb@3 | 314 | """ |
paulb@3 | 315 | |
paulb@3 | 316 | raise NotImplementedError, "respond" |
paulb@1 | 317 | |
paulb@1 | 318 | # vim: tabstop=4 expandtab shiftwidth=4 |