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@1 | 105 | # Request-related methods. |
paulb@1 | 106 | |
paulb@1 | 107 | def get_request_stream(self): |
paulb@1 | 108 | |
paulb@1 | 109 | """ |
paulb@1 | 110 | A framework-specific method which returns the request stream for |
paulb@1 | 111 | the transaction. |
paulb@1 | 112 | """ |
paulb@1 | 113 | |
paulb@1 | 114 | raise NotImplementedError, "get_request_stream" |
paulb@1 | 115 | |
paulb@1 | 116 | def get_request_method(self): |
paulb@1 | 117 | |
paulb@1 | 118 | """ |
paulb@1 | 119 | A framework-specific method which gets the request method. |
paulb@1 | 120 | """ |
paulb@1 | 121 | |
paulb@1 | 122 | raise NotImplementedError, "get_request_method" |
paulb@1 | 123 | |
paulb@1 | 124 | def get_headers(self): |
paulb@1 | 125 | |
paulb@1 | 126 | """ |
paulb@1 | 127 | A framework-specific method which returns the request headers. |
paulb@1 | 128 | NOTE: Experimental, since framework support varies somewhat. |
paulb@1 | 129 | """ |
paulb@1 | 130 | |
paulb@1 | 131 | raise NotImplementedError, "get_headers" |
paulb@1 | 132 | |
paulb@1 | 133 | def get_content_type(self): |
paulb@1 | 134 | |
paulb@1 | 135 | """ |
paulb@1 | 136 | A framework-specific method which gets the content type specified on the |
paulb@1 | 137 | request, along with the charset employed. |
paulb@1 | 138 | """ |
paulb@1 | 139 | |
paulb@1 | 140 | raise NotImplementedError, "get_content_type" |
paulb@1 | 141 | |
paulb@1 | 142 | def get_content_charsets(self): |
paulb@1 | 143 | |
paulb@1 | 144 | """ |
paulb@1 | 145 | Returns the character set preferences. |
paulb@1 | 146 | """ |
paulb@1 | 147 | |
paulb@1 | 148 | raise NotImplementedError, "get_content_charsets" |
paulb@1 | 149 | |
paulb@1 | 150 | def get_content_languages(self): |
paulb@1 | 151 | |
paulb@1 | 152 | """ |
paulb@1 | 153 | A framework-specific method which extracts language information from |
paulb@1 | 154 | the transaction. |
paulb@1 | 155 | """ |
paulb@1 | 156 | |
paulb@1 | 157 | raise NotImplementedError, "get_content_languages" |
paulb@1 | 158 | |
paulb@5 | 159 | def get_path(self): |
paulb@5 | 160 | |
paulb@5 | 161 | """ |
paulb@5 | 162 | A framework-specific method which gets the entire path from the request. |
paulb@5 | 163 | """ |
paulb@5 | 164 | |
paulb@5 | 165 | raise NotImplementedError, "get_path" |
paulb@5 | 166 | |
paulb@1 | 167 | def get_path_info(self): |
paulb@1 | 168 | |
paulb@1 | 169 | """ |
paulb@1 | 170 | A framework-specific method which gets the "path info" (the part of the |
paulb@1 | 171 | URL after the resource name handling the current request) from the |
paulb@1 | 172 | request. |
paulb@1 | 173 | """ |
paulb@1 | 174 | |
paulb@1 | 175 | raise NotImplementedError, "get_path_info" |
paulb@1 | 176 | |
paulb@1 | 177 | def get_fields(self): |
paulb@1 | 178 | |
paulb@1 | 179 | """ |
paulb@1 | 180 | A framework-specific method which extracts the form fields from the |
paulb@1 | 181 | transaction. |
paulb@1 | 182 | """ |
paulb@1 | 183 | |
paulb@1 | 184 | raise NotImplementedError, "get_fields" |
paulb@1 | 185 | |
paulb@45 | 186 | def get_user(self): |
paulb@1 | 187 | |
paulb@1 | 188 | """ |
paulb@45 | 189 | A framework-specific method which extracts user information from the |
paulb@45 | 190 | transaction. |
paulb@1 | 191 | """ |
paulb@1 | 192 | |
paulb@45 | 193 | raise NotImplementedError, "get_user" |
paulb@1 | 194 | |
paulb@1 | 195 | # Response-related methods. |
paulb@1 | 196 | |
paulb@1 | 197 | def get_response_stream(self): |
paulb@1 | 198 | |
paulb@1 | 199 | """ |
paulb@1 | 200 | A framework-specific method which returns the response stream for |
paulb@1 | 201 | the transaction. |
paulb@1 | 202 | """ |
paulb@1 | 203 | |
paulb@1 | 204 | raise NotImplementedError, "get_response_stream" |
paulb@1 | 205 | |
paulb@42 | 206 | def get_response_code(self): |
paulb@42 | 207 | |
paulb@42 | 208 | """ |
paulb@42 | 209 | Get the response code associated with the transaction. If no response |
paulb@42 | 210 | code is defined, None is returned. |
paulb@42 | 211 | """ |
paulb@42 | 212 | |
paulb@42 | 213 | raise NotImplementedError, "get_response_code" |
paulb@42 | 214 | |
paulb@42 | 215 | def set_response_code(self, response_code): |
paulb@42 | 216 | |
paulb@42 | 217 | """ |
paulb@42 | 218 | Set the 'response_code' using a numeric constant defined in the HTTP |
paulb@42 | 219 | specification. |
paulb@42 | 220 | """ |
paulb@42 | 221 | |
paulb@42 | 222 | raise NotImplementedError, "set_response_code" |
paulb@42 | 223 | |
paulb@49 | 224 | def set_header(self, header, value): |
paulb@49 | 225 | |
paulb@49 | 226 | """ |
paulb@49 | 227 | Set the HTTP 'header' with the given 'value'. |
paulb@49 | 228 | """ |
paulb@49 | 229 | |
paulb@49 | 230 | raise NotImplementedError, "set_header" |
paulb@49 | 231 | |
paulb@1 | 232 | def set_content_type(self, content_type): |
paulb@1 | 233 | |
paulb@1 | 234 | """ |
paulb@1 | 235 | A framework-specific method which sets the 'content_type' for the |
paulb@1 | 236 | response. |
paulb@1 | 237 | """ |
paulb@1 | 238 | |
paulb@1 | 239 | raise NotImplementedError, "set_content_type" |
paulb@1 | 240 | |
paulb@1 | 241 | class Resource: |
paulb@1 | 242 | |
paulb@1 | 243 | "A generic resource interface." |
paulb@1 | 244 | |
paulb@3 | 245 | def respond(self, trans): |
paulb@3 | 246 | |
paulb@3 | 247 | """ |
paulb@3 | 248 | An application-specific method which performs activities on the basis of |
paulb@3 | 249 | the transaction object 'trans'. |
paulb@3 | 250 | """ |
paulb@3 | 251 | |
paulb@3 | 252 | raise NotImplementedError, "respond" |
paulb@1 | 253 | |
paulb@1 | 254 | # vim: tabstop=4 expandtab shiftwidth=4 |