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@1 | 22 | def __init__(self, content_type, charset): |
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@1 | 51 | def parse_content_preferences(self, accept_preference): |
paulb@1 | 52 | |
paulb@1 | 53 | """ |
paulb@1 | 54 | Returns the preferences as requested by the user agent. The preferences are |
paulb@1 | 55 | returned as a list of codes in the same order as they appeared in the |
paulb@1 | 56 | appropriate environment variable. In other words, the explicit weighting |
paulb@1 | 57 | criteria are ignored. |
paulb@1 | 58 | |
paulb@1 | 59 | As the 'accept_preference' parameter, values for language and charset |
paulb@1 | 60 | preferences are appropriate. |
paulb@1 | 61 | """ |
paulb@1 | 62 | |
paulb@1 | 63 | accept_defs = accept_preference.split(",") |
paulb@1 | 64 | accept_prefs = [] |
paulb@1 | 65 | for accept_def in accept_defs: |
paulb@1 | 66 | t = accept_def.split(";") |
paulb@1 | 67 | if len(t) >= 1: |
paulb@1 | 68 | accept_prefs.append(t[0].strip()) |
paulb@1 | 69 | return accept_prefs |
paulb@1 | 70 | |
paulb@1 | 71 | # Request-related methods. |
paulb@1 | 72 | |
paulb@1 | 73 | def get_request_stream(self): |
paulb@1 | 74 | |
paulb@1 | 75 | """ |
paulb@1 | 76 | A framework-specific method which returns the request stream for |
paulb@1 | 77 | the transaction. |
paulb@1 | 78 | """ |
paulb@1 | 79 | |
paulb@1 | 80 | raise NotImplementedError, "get_request_stream" |
paulb@1 | 81 | |
paulb@1 | 82 | def get_request_method(self): |
paulb@1 | 83 | |
paulb@1 | 84 | """ |
paulb@1 | 85 | A framework-specific method which gets the request method. |
paulb@1 | 86 | """ |
paulb@1 | 87 | |
paulb@1 | 88 | raise NotImplementedError, "get_request_method" |
paulb@1 | 89 | |
paulb@1 | 90 | def get_headers(self): |
paulb@1 | 91 | |
paulb@1 | 92 | """ |
paulb@1 | 93 | A framework-specific method which returns the request headers. |
paulb@1 | 94 | NOTE: Experimental, since framework support varies somewhat. |
paulb@1 | 95 | """ |
paulb@1 | 96 | |
paulb@1 | 97 | raise NotImplementedError, "get_headers" |
paulb@1 | 98 | |
paulb@1 | 99 | def get_content_type(self): |
paulb@1 | 100 | |
paulb@1 | 101 | """ |
paulb@1 | 102 | A framework-specific method which gets the content type specified on the |
paulb@1 | 103 | request, along with the charset employed. |
paulb@1 | 104 | """ |
paulb@1 | 105 | |
paulb@1 | 106 | raise NotImplementedError, "get_content_type" |
paulb@1 | 107 | |
paulb@1 | 108 | def get_content_charsets(self): |
paulb@1 | 109 | |
paulb@1 | 110 | """ |
paulb@1 | 111 | Returns the character set preferences. |
paulb@1 | 112 | """ |
paulb@1 | 113 | |
paulb@1 | 114 | raise NotImplementedError, "get_content_charsets" |
paulb@1 | 115 | |
paulb@1 | 116 | def get_content_languages(self): |
paulb@1 | 117 | |
paulb@1 | 118 | """ |
paulb@1 | 119 | A framework-specific method which extracts language information from |
paulb@1 | 120 | the transaction. |
paulb@1 | 121 | """ |
paulb@1 | 122 | |
paulb@1 | 123 | raise NotImplementedError, "get_content_languages" |
paulb@1 | 124 | |
paulb@1 | 125 | def get_path_info(self): |
paulb@1 | 126 | |
paulb@1 | 127 | """ |
paulb@1 | 128 | A framework-specific method which gets the "path info" (the part of the |
paulb@1 | 129 | URL after the resource name handling the current request) from the |
paulb@1 | 130 | request. |
paulb@1 | 131 | """ |
paulb@1 | 132 | |
paulb@1 | 133 | raise NotImplementedError, "get_path_info" |
paulb@1 | 134 | |
paulb@1 | 135 | def get_fields(self): |
paulb@1 | 136 | |
paulb@1 | 137 | """ |
paulb@1 | 138 | A framework-specific method which extracts the form fields from the |
paulb@1 | 139 | transaction. |
paulb@1 | 140 | """ |
paulb@1 | 141 | |
paulb@1 | 142 | raise NotImplementedError, "get_fields" |
paulb@1 | 143 | |
paulb@1 | 144 | def get_agent_information(self): |
paulb@1 | 145 | |
paulb@1 | 146 | """ |
paulb@1 | 147 | A framework-specific method which extracts agent information from |
paulb@1 | 148 | the transaction. |
paulb@1 | 149 | """ |
paulb@1 | 150 | |
paulb@1 | 151 | raise NotImplementedError, "get_agent_information" |
paulb@1 | 152 | |
paulb@1 | 153 | # Response-related methods. |
paulb@1 | 154 | |
paulb@1 | 155 | def get_response_stream(self): |
paulb@1 | 156 | |
paulb@1 | 157 | """ |
paulb@1 | 158 | A framework-specific method which returns the response stream for |
paulb@1 | 159 | the transaction. |
paulb@1 | 160 | """ |
paulb@1 | 161 | |
paulb@1 | 162 | raise NotImplementedError, "get_response_stream" |
paulb@1 | 163 | |
paulb@1 | 164 | def set_content_type(self, content_type): |
paulb@1 | 165 | |
paulb@1 | 166 | """ |
paulb@1 | 167 | A framework-specific method which sets the 'content_type' for the |
paulb@1 | 168 | response. |
paulb@1 | 169 | """ |
paulb@1 | 170 | |
paulb@1 | 171 | raise NotImplementedError, "set_content_type" |
paulb@1 | 172 | |
paulb@1 | 173 | class Resource: |
paulb@1 | 174 | |
paulb@1 | 175 | "A generic resource interface." |
paulb@1 | 176 | |
paulb@3 | 177 | def respond(self, trans): |
paulb@3 | 178 | |
paulb@3 | 179 | """ |
paulb@3 | 180 | An application-specific method which performs activities on the basis of |
paulb@3 | 181 | the transaction object 'trans'. |
paulb@3 | 182 | """ |
paulb@3 | 183 | |
paulb@3 | 184 | raise NotImplementedError, "respond" |
paulb@1 | 185 | |
paulb@1 | 186 | # vim: tabstop=4 expandtab shiftwidth=4 |