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@1 | 33 | # Utility methods. |
paulb@1 | 34 | |
paulb@1 | 35 | def parse_content_type(self, content_type_field): |
paulb@1 | 36 | |
paulb@1 | 37 | """ |
paulb@1 | 38 | Determine the content type and charset from the supplied |
paulb@1 | 39 | 'content_type_field' string. |
paulb@1 | 40 | """ |
paulb@1 | 41 | |
paulb@1 | 42 | if content_type_field is None: |
paulb@1 | 43 | return ContentType(None, "iso-8859-1") |
paulb@1 | 44 | |
paulb@1 | 45 | t = content_type_field.split("; charset=") |
paulb@1 | 46 | if len(t) == 1: |
paulb@1 | 47 | return ContentType(t[0], "iso-8859-1") |
paulb@1 | 48 | else: |
paulb@1 | 49 | return ContentType(t[0], t[1]) |
paulb@1 | 50 | |
paulb@5 | 51 | def format_content_type(self, content_type): |
paulb@5 | 52 | |
paulb@5 | 53 | """ |
paulb@5 | 54 | Format the given 'content_type' object, producing a string suitable for |
paulb@5 | 55 | the response header field. |
paulb@5 | 56 | """ |
paulb@5 | 57 | |
paulb@5 | 58 | if content_type.charset: |
paulb@5 | 59 | field = "%s; charset=%s" % (content_type.content_type, content_type.charset) |
paulb@5 | 60 | else: |
paulb@5 | 61 | field = content_type.content_type |
paulb@5 | 62 | |
paulb@5 | 63 | # Make sure that only ASCII is used in the header. |
paulb@5 | 64 | |
paulb@5 | 65 | return field.encode("US-ASCII") |
paulb@5 | 66 | |
paulb@1 | 67 | def parse_content_preferences(self, accept_preference): |
paulb@1 | 68 | |
paulb@1 | 69 | """ |
paulb@1 | 70 | Returns the preferences as requested by the user agent. The preferences are |
paulb@1 | 71 | returned as a list of codes in the same order as they appeared in the |
paulb@1 | 72 | appropriate environment variable. In other words, the explicit weighting |
paulb@1 | 73 | criteria are ignored. |
paulb@1 | 74 | |
paulb@1 | 75 | As the 'accept_preference' parameter, values for language and charset |
paulb@1 | 76 | preferences are appropriate. |
paulb@1 | 77 | """ |
paulb@1 | 78 | |
paulb@1 | 79 | accept_defs = accept_preference.split(",") |
paulb@1 | 80 | accept_prefs = [] |
paulb@1 | 81 | for accept_def in accept_defs: |
paulb@1 | 82 | t = accept_def.split(";") |
paulb@1 | 83 | if len(t) >= 1: |
paulb@1 | 84 | accept_prefs.append(t[0].strip()) |
paulb@1 | 85 | return accept_prefs |
paulb@1 | 86 | |
paulb@1 | 87 | # Request-related methods. |
paulb@1 | 88 | |
paulb@1 | 89 | def get_request_stream(self): |
paulb@1 | 90 | |
paulb@1 | 91 | """ |
paulb@1 | 92 | A framework-specific method which returns the request stream for |
paulb@1 | 93 | the transaction. |
paulb@1 | 94 | """ |
paulb@1 | 95 | |
paulb@1 | 96 | raise NotImplementedError, "get_request_stream" |
paulb@1 | 97 | |
paulb@1 | 98 | def get_request_method(self): |
paulb@1 | 99 | |
paulb@1 | 100 | """ |
paulb@1 | 101 | A framework-specific method which gets the request method. |
paulb@1 | 102 | """ |
paulb@1 | 103 | |
paulb@1 | 104 | raise NotImplementedError, "get_request_method" |
paulb@1 | 105 | |
paulb@1 | 106 | def get_headers(self): |
paulb@1 | 107 | |
paulb@1 | 108 | """ |
paulb@1 | 109 | A framework-specific method which returns the request headers. |
paulb@1 | 110 | NOTE: Experimental, since framework support varies somewhat. |
paulb@1 | 111 | """ |
paulb@1 | 112 | |
paulb@1 | 113 | raise NotImplementedError, "get_headers" |
paulb@1 | 114 | |
paulb@1 | 115 | def get_content_type(self): |
paulb@1 | 116 | |
paulb@1 | 117 | """ |
paulb@1 | 118 | A framework-specific method which gets the content type specified on the |
paulb@1 | 119 | request, along with the charset employed. |
paulb@1 | 120 | """ |
paulb@1 | 121 | |
paulb@1 | 122 | raise NotImplementedError, "get_content_type" |
paulb@1 | 123 | |
paulb@1 | 124 | def get_content_charsets(self): |
paulb@1 | 125 | |
paulb@1 | 126 | """ |
paulb@1 | 127 | Returns the character set preferences. |
paulb@1 | 128 | """ |
paulb@1 | 129 | |
paulb@1 | 130 | raise NotImplementedError, "get_content_charsets" |
paulb@1 | 131 | |
paulb@1 | 132 | def get_content_languages(self): |
paulb@1 | 133 | |
paulb@1 | 134 | """ |
paulb@1 | 135 | A framework-specific method which extracts language information from |
paulb@1 | 136 | the transaction. |
paulb@1 | 137 | """ |
paulb@1 | 138 | |
paulb@1 | 139 | raise NotImplementedError, "get_content_languages" |
paulb@1 | 140 | |
paulb@5 | 141 | def get_path(self): |
paulb@5 | 142 | |
paulb@5 | 143 | """ |
paulb@5 | 144 | A framework-specific method which gets the entire path from the request. |
paulb@5 | 145 | """ |
paulb@5 | 146 | |
paulb@5 | 147 | raise NotImplementedError, "get_path" |
paulb@5 | 148 | |
paulb@1 | 149 | def get_path_info(self): |
paulb@1 | 150 | |
paulb@1 | 151 | """ |
paulb@1 | 152 | A framework-specific method which gets the "path info" (the part of the |
paulb@1 | 153 | URL after the resource name handling the current request) from the |
paulb@1 | 154 | request. |
paulb@1 | 155 | """ |
paulb@1 | 156 | |
paulb@1 | 157 | raise NotImplementedError, "get_path_info" |
paulb@1 | 158 | |
paulb@1 | 159 | def get_fields(self): |
paulb@1 | 160 | |
paulb@1 | 161 | """ |
paulb@1 | 162 | A framework-specific method which extracts the form fields from the |
paulb@1 | 163 | transaction. |
paulb@1 | 164 | """ |
paulb@1 | 165 | |
paulb@1 | 166 | raise NotImplementedError, "get_fields" |
paulb@1 | 167 | |
paulb@1 | 168 | def get_agent_information(self): |
paulb@1 | 169 | |
paulb@1 | 170 | """ |
paulb@1 | 171 | A framework-specific method which extracts agent information from |
paulb@1 | 172 | the transaction. |
paulb@1 | 173 | """ |
paulb@1 | 174 | |
paulb@1 | 175 | raise NotImplementedError, "get_agent_information" |
paulb@1 | 176 | |
paulb@1 | 177 | # Response-related methods. |
paulb@1 | 178 | |
paulb@1 | 179 | def get_response_stream(self): |
paulb@1 | 180 | |
paulb@1 | 181 | """ |
paulb@1 | 182 | A framework-specific method which returns the response stream for |
paulb@1 | 183 | the transaction. |
paulb@1 | 184 | """ |
paulb@1 | 185 | |
paulb@1 | 186 | raise NotImplementedError, "get_response_stream" |
paulb@1 | 187 | |
paulb@1 | 188 | def set_content_type(self, content_type): |
paulb@1 | 189 | |
paulb@1 | 190 | """ |
paulb@1 | 191 | A framework-specific method which sets the 'content_type' for the |
paulb@1 | 192 | response. |
paulb@1 | 193 | """ |
paulb@1 | 194 | |
paulb@1 | 195 | raise NotImplementedError, "set_content_type" |
paulb@1 | 196 | |
paulb@1 | 197 | class Resource: |
paulb@1 | 198 | |
paulb@1 | 199 | "A generic resource interface." |
paulb@1 | 200 | |
paulb@3 | 201 | def respond(self, trans): |
paulb@3 | 202 | |
paulb@3 | 203 | """ |
paulb@3 | 204 | An application-specific method which performs activities on the basis of |
paulb@3 | 205 | the transaction object 'trans'. |
paulb@3 | 206 | """ |
paulb@3 | 207 | |
paulb@3 | 208 | raise NotImplementedError, "respond" |
paulb@1 | 209 | |
paulb@1 | 210 | # vim: tabstop=4 expandtab shiftwidth=4 |