paulb@81 | 1 | #!/usr/bin/env python |
paulb@81 | 2 | |
paulb@116 | 3 | """ |
paulb@116 | 4 | Resources for use with WebStack. |
paulb@116 | 5 | |
paulb@116 | 6 | Copyright (C) 2005 Paul Boddie <paul@boddie.org.uk> |
paulb@116 | 7 | |
paulb@116 | 8 | This library is free software; you can redistribute it and/or |
paulb@116 | 9 | modify it under the terms of the GNU Lesser General Public |
paulb@116 | 10 | License as published by the Free Software Foundation; either |
paulb@116 | 11 | version 2.1 of the License, or (at your option) any later version. |
paulb@116 | 12 | |
paulb@116 | 13 | This library is distributed in the hope that it will be useful, |
paulb@116 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@116 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
paulb@116 | 16 | Lesser General Public License for more details. |
paulb@116 | 17 | |
paulb@116 | 18 | You should have received a copy of the GNU Lesser General Public |
paulb@116 | 19 | License along with this library; if not, write to the Free Software |
paulb@116 | 20 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
paulb@116 | 21 | """ |
paulb@81 | 22 | |
paulb@81 | 23 | import WebStack.Generic |
paulb@81 | 24 | import XSLForms.Fields |
paulb@83 | 25 | import XSLForms.Prepare |
paulb@83 | 26 | import XSLForms.Output |
paulb@202 | 27 | from XSLTools import XSLOutput |
paulb@83 | 28 | import os |
paulb@81 | 29 | |
paulb@81 | 30 | class XSLFormsResource: |
paulb@81 | 31 | |
paulb@83 | 32 | """ |
paulb@83 | 33 | A generic XSLForms resource for use with WebStack. |
paulb@83 | 34 | |
paulb@83 | 35 | When overriding this class, define the following attributes appropriately: |
paulb@83 | 36 | |
paulb@83 | 37 | * template_resources - a dictionary mapping output identifiers to |
paulb@83 | 38 | (template_filename, output_filename) tuples, |
paulb@83 | 39 | indicating the template and stylesheet filenames |
paulb@83 | 40 | to be employed |
paulb@83 | 41 | |
paulb@83 | 42 | * in_page_resources - a dictionary mapping fragment identifiers to |
paulb@83 | 43 | (output_filename, node_identifier) tuples, |
paulb@83 | 44 | indicating the stylesheet filename to be |
paulb@83 | 45 | employed, along with the node identifier used in |
paulb@83 | 46 | the original template and output documents to |
paulb@83 | 47 | mark a region of those documents as the fragment |
paulb@83 | 48 | to be updated upon "in-page" requests |
paulb@83 | 49 | |
paulb@248 | 50 | * init_resources - a dictionary mapping initialiser/input |
paulb@248 | 51 | identifiers to (template_filename, |
paulb@248 | 52 | input_filename) tuples, indicating the template |
paulb@248 | 53 | and initialiser/input stylesheet filenames to be |
paulb@248 | 54 | employed |
paulb@248 | 55 | |
paulb@146 | 56 | * transform_resources - a dictionary mapping transform identifiers to |
paulb@146 | 57 | lists of stylesheet filenames for use with the |
paulb@146 | 58 | transformation methods |
paulb@146 | 59 | |
paulb@146 | 60 | * document_resources - a dictionary mapping document identifiers to |
paulb@146 | 61 | single filenames for use as source documents or |
paulb@146 | 62 | as references with the transformation methods |
paulb@146 | 63 | |
paulb@83 | 64 | * resource_dir - the absolute path of the directory in which |
paulb@83 | 65 | stylesheet resources are to reside |
paulb@83 | 66 | |
paulb@83 | 67 | All filenames shall be simple leafnames for files residing in the resource's |
paulb@83 | 68 | special resource directory 'resource_dir'. |
paulb@83 | 69 | |
paulb@83 | 70 | The following attributes may also be specified: |
paulb@83 | 71 | |
paulb@83 | 72 | * path_encoding - the assumed encoding of characters in request |
paulb@83 | 73 | paths |
paulb@83 | 74 | |
paulb@83 | 75 | * encoding - the assumed encoding of characters in request |
paulb@83 | 76 | bodies |
paulb@83 | 77 | """ |
paulb@81 | 78 | |
paulb@212 | 79 | path_encoding = "utf-8" |
paulb@81 | 80 | encoding = "utf-8" |
paulb@83 | 81 | template_resources = {} |
paulb@83 | 82 | in_page_resources = {} |
paulb@146 | 83 | transform_resources = {} |
paulb@146 | 84 | document_resources = {} |
paulb@83 | 85 | resource_dir = None |
paulb@81 | 86 | |
paulb@193 | 87 | def clean_parameters(self, parameters): |
paulb@81 | 88 | |
paulb@81 | 89 | """ |
paulb@193 | 90 | Workaround stray zero value characters from Konqueror in XMLHttpRequest |
paulb@193 | 91 | communications. |
paulb@81 | 92 | """ |
paulb@81 | 93 | |
paulb@193 | 94 | for name, values in parameters.items(): |
paulb@193 | 95 | new_values = [] |
paulb@193 | 96 | for value in values: |
paulb@193 | 97 | if value.endswith("\x00"): |
paulb@193 | 98 | new_values.append(value[:-1]) |
paulb@193 | 99 | else: |
paulb@193 | 100 | new_values.append(value) |
paulb@193 | 101 | parameters[name] = new_values |
paulb@81 | 102 | |
paulb@83 | 103 | def prepare_output(self, output_identifier): |
paulb@83 | 104 | |
paulb@83 | 105 | """ |
paulb@83 | 106 | Prepare the output stylesheets using the given 'output_identifier' to |
paulb@83 | 107 | indicate which templates and stylesheets are to be employed in the |
paulb@83 | 108 | production of output from the resource. |
paulb@83 | 109 | |
paulb@83 | 110 | The 'output_identifier' is used as a key to the 'template_resources' |
paulb@83 | 111 | dictionary attribute. |
paulb@146 | 112 | |
paulb@146 | 113 | Return the full path to the output stylesheet for use with 'send_output' |
paulb@146 | 114 | or 'get_result'. |
paulb@83 | 115 | """ |
paulb@83 | 116 | |
paulb@83 | 117 | template_filename, output_filename = self.template_resources[output_identifier] |
paulb@238 | 118 | output_path = os.path.abspath(os.path.join(self.resource_dir, output_filename)) |
paulb@238 | 119 | template_path = os.path.abspath(os.path.join(self.resource_dir, template_filename)) |
paulb@83 | 120 | XSLForms.Prepare.ensure_stylesheet(template_path, output_path) |
paulb@83 | 121 | return output_path |
paulb@83 | 122 | |
paulb@83 | 123 | def prepare_fragment(self, output_identifier, fragment_identifier): |
paulb@83 | 124 | |
paulb@83 | 125 | """ |
paulb@83 | 126 | Prepare the output stylesheets for the given 'output_identifier' and |
paulb@83 | 127 | 'fragment_identifier', indicating which templates and stylesheets are to |
paulb@83 | 128 | be employed in the production of output from the resource. |
paulb@83 | 129 | |
paulb@83 | 130 | The 'output_identifier' is used as a key to the 'template_resources' |
paulb@83 | 131 | dictionary attribute; the 'fragment_identifier' is used as a key to the |
paulb@83 | 132 | 'in_page_resources' dictionary attribute. |
paulb@146 | 133 | |
paulb@146 | 134 | Return the full path to the output stylesheet for use with 'send_output' |
paulb@146 | 135 | or 'get_result'. |
paulb@83 | 136 | """ |
paulb@83 | 137 | |
paulb@83 | 138 | output_path = self.prepare_output(output_identifier) |
paulb@83 | 139 | fragment_filename, node_identifier = self.in_page_resources[fragment_identifier] |
paulb@238 | 140 | fragment_path = os.path.abspath(os.path.join(self.resource_dir, fragment_filename)) |
paulb@83 | 141 | XSLForms.Prepare.ensure_stylesheet_fragment(output_path, fragment_path, node_identifier) |
paulb@83 | 142 | return fragment_path |
paulb@83 | 143 | |
paulb@292 | 144 | def prepare_parameters(self, parameters): |
paulb@292 | 145 | |
paulb@292 | 146 | """ |
paulb@292 | 147 | Prepare the stylesheet parameters from the given request 'parameters'. |
paulb@292 | 148 | This is most useful when preparing fragments for in-page update output. |
paulb@292 | 149 | """ |
paulb@292 | 150 | |
paulb@292 | 151 | element_path = parameters.get("element-path", [""])[0] |
paulb@292 | 152 | if element_path: |
paulb@292 | 153 | return {"element-path" : element_path} |
paulb@292 | 154 | else: |
paulb@292 | 155 | return {} |
paulb@292 | 156 | |
paulb@278 | 157 | def send_output(self, trans, stylesheet_filenames, document, stylesheet_parameters=None, |
paulb@278 | 158 | stylesheet_expressions=None, references=None): |
paulb@83 | 159 | |
paulb@83 | 160 | """ |
paulb@83 | 161 | Send the output from the resource to the user employing the transaction |
paulb@85 | 162 | 'trans', stylesheets having the given 'stylesheet_filenames', the |
paulb@146 | 163 | 'document' upon which the output will be based, the optional parameters |
paulb@278 | 164 | as defined in the 'stylesheet_parameters' dictionary, the optional |
paulb@278 | 165 | expressions are defined in the 'stylesheet_expressions' dictionary, and |
paulb@278 | 166 | the optional 'references' to external documents. |
paulb@146 | 167 | """ |
paulb@146 | 168 | |
paulb@256 | 169 | # Sanity check for the filenames list. |
paulb@256 | 170 | |
paulb@256 | 171 | if isinstance(stylesheet_filenames, str) or isinstance(stylesheet_filenames, unicode): |
paulb@256 | 172 | raise ValueError, stylesheet_filenames |
paulb@256 | 173 | |
paulb@278 | 174 | proc = XSLOutput.Processor(stylesheet_filenames, parameters=stylesheet_parameters, |
paulb@278 | 175 | expressions=stylesheet_expressions, references=references) |
paulb@146 | 176 | proc.send_output(trans.get_response_stream(), trans.get_response_stream_encoding(), |
paulb@146 | 177 | document) |
paulb@146 | 178 | |
paulb@278 | 179 | def get_result(self, stylesheet_filenames, document, stylesheet_parameters=None, |
paulb@278 | 180 | stylesheet_expressions=None, references=None): |
paulb@146 | 181 | |
paulb@146 | 182 | """ |
paulb@146 | 183 | Get the result of applying a transformation using stylesheets with the |
paulb@146 | 184 | given 'stylesheet_filenames', the 'document' upon which the result will |
paulb@146 | 185 | be based, the optional parameters as defined in the |
paulb@278 | 186 | 'stylesheet_parameters' dictionary, the optional parameters as defined |
paulb@278 | 187 | in the 'stylesheet_parameters' dictionaryand the optional 'references' |
paulb@278 | 188 | to external documents. |
paulb@83 | 189 | """ |
paulb@83 | 190 | |
paulb@256 | 191 | # Sanity check for the filenames list. |
paulb@256 | 192 | |
paulb@256 | 193 | if isinstance(stylesheet_filenames, str) or isinstance(stylesheet_filenames, unicode): |
paulb@256 | 194 | raise ValueError, stylesheet_filenames |
paulb@256 | 195 | |
paulb@278 | 196 | proc = XSLOutput.Processor(stylesheet_filenames, parameters=stylesheet_parameters, |
paulb@278 | 197 | expressions=stylesheet_expressions, references=references) |
paulb@146 | 198 | return proc.get_result(document) |
paulb@146 | 199 | |
paulb@248 | 200 | def prepare_initialiser(self, input_identifier): |
paulb@248 | 201 | |
paulb@248 | 202 | """ |
paulb@248 | 203 | Prepare an initialiser/input transformation using the given |
paulb@248 | 204 | 'input_identifier'. |
paulb@248 | 205 | |
paulb@248 | 206 | Return the full path to the input stylesheet for use with 'send_output' |
paulb@248 | 207 | or 'get_result'. |
paulb@248 | 208 | """ |
paulb@248 | 209 | |
paulb@248 | 210 | template_filename, input_filename = self.init_resources[input_identifier] |
paulb@248 | 211 | input_path = os.path.abspath(os.path.join(self.resource_dir, input_filename)) |
paulb@248 | 212 | template_path = os.path.abspath(os.path.join(self.resource_dir, template_filename)) |
paulb@248 | 213 | XSLForms.Prepare.ensure_input_stylesheet(template_path, input_path) |
paulb@248 | 214 | return input_path |
paulb@248 | 215 | |
paulb@146 | 216 | def prepare_transform(self, transform_identifier): |
paulb@146 | 217 | |
paulb@146 | 218 | """ |
paulb@146 | 219 | Prepare a transformation using the given 'transform_identifier'. |
paulb@146 | 220 | |
paulb@146 | 221 | Return a list of full paths to the output stylesheets for use with |
paulb@146 | 222 | 'send_output' or 'get_result'. |
paulb@146 | 223 | """ |
paulb@146 | 224 | |
paulb@146 | 225 | filenames = self.transform_resources[transform_identifier] |
paulb@146 | 226 | paths = [] |
paulb@146 | 227 | for filename in filenames: |
paulb@238 | 228 | paths.append(os.path.abspath(os.path.join(self.resource_dir, filename))) |
paulb@146 | 229 | return paths |
paulb@146 | 230 | |
paulb@146 | 231 | def prepare_document(self, document_identifier): |
paulb@146 | 232 | |
paulb@146 | 233 | """ |
paulb@146 | 234 | Prepare a document using the given 'document_identifier'. |
paulb@146 | 235 | |
paulb@146 | 236 | Return the full path of the document for use either as the source |
paulb@146 | 237 | document or as a reference with 'send_output' or 'get_result'. |
paulb@146 | 238 | """ |
paulb@146 | 239 | |
paulb@146 | 240 | filename = self.document_resources[document_identifier] |
paulb@238 | 241 | return os.path.abspath(os.path.join(self.resource_dir, filename)) |
paulb@83 | 242 | |
paulb@110 | 243 | def get_in_page_resource(self, trans): |
paulb@110 | 244 | |
paulb@110 | 245 | """ |
paulb@110 | 246 | Return the in-page resource being referred to in the given transaction |
paulb@110 | 247 | 'trans'. |
paulb@110 | 248 | """ |
paulb@110 | 249 | |
paulb@209 | 250 | return trans.get_path_info(self.path_encoding).split("/")[-1] |
paulb@110 | 251 | |
paulb@81 | 252 | def respond(self, trans): |
paulb@81 | 253 | |
paulb@81 | 254 | """ |
paulb@81 | 255 | Respond to the request described by the given transaction 'trans'. |
paulb@81 | 256 | """ |
paulb@81 | 257 | |
paulb@81 | 258 | # Only obtain field information according to the stated method. |
paulb@81 | 259 | |
paulb@81 | 260 | method = trans.get_request_method() |
paulb@110 | 261 | in_page_resource = self.get_in_page_resource(trans) |
paulb@81 | 262 | |
paulb@81 | 263 | # Handle typical request methods, processing request information. |
paulb@81 | 264 | |
paulb@81 | 265 | if method == "GET": |
paulb@81 | 266 | |
paulb@81 | 267 | # Get the fields from the request path (URL). |
paulb@209 | 268 | # NOTE: The encoding is actually redundant since WebStack produces |
paulb@209 | 269 | # NOTE: Unicode values. |
paulb@81 | 270 | |
paulb@81 | 271 | form = XSLForms.Fields.Form(encoding=self.path_encoding, values_are_lists=1) |
paulb@81 | 272 | parameters = trans.get_fields_from_path() |
paulb@81 | 273 | form.set_parameters(parameters) |
paulb@81 | 274 | |
paulb@81 | 275 | elif method == "POST": |
paulb@81 | 276 | |
paulb@81 | 277 | # Get the fields from the request body. |
paulb@209 | 278 | # NOTE: The encoding is actually redundant since WebStack produces |
paulb@209 | 279 | # NOTE: Unicode values. |
paulb@81 | 280 | |
paulb@81 | 281 | form = XSLForms.Fields.Form(encoding=self.encoding, values_are_lists=1) |
paulb@193 | 282 | parameters = trans.get_fields_from_body(self.encoding) |
paulb@81 | 283 | |
paulb@193 | 284 | # NOTE: Konqueror workaround. |
paulb@193 | 285 | self.clean_parameters(parameters) |
paulb@81 | 286 | |
paulb@81 | 287 | form.set_parameters(parameters) |
paulb@81 | 288 | |
paulb@81 | 289 | else: |
paulb@81 | 290 | |
paulb@81 | 291 | # Initialise empty containers. |
paulb@81 | 292 | |
paulb@81 | 293 | parameters = {} |
paulb@81 | 294 | documents = {} |
paulb@81 | 295 | |
paulb@81 | 296 | # Call an overridden method with the processed request information. |
paulb@81 | 297 | |
paulb@81 | 298 | self.respond_to_form(trans, form) |
paulb@81 | 299 | |
paulb@81 | 300 | def respond_to_form(self, trans, form): |
paulb@81 | 301 | |
paulb@81 | 302 | """ |
paulb@81 | 303 | Respond to the request described by the given transaction 'trans', using |
paulb@81 | 304 | the given 'form' object to conveniently retrieve field (request |
paulb@81 | 305 | parameter) information and structured form information (as DOM-style XML |
paulb@81 | 306 | documents). |
paulb@81 | 307 | """ |
paulb@81 | 308 | |
paulb@81 | 309 | trans.set_response_code(500) |
paulb@81 | 310 | raise WebStack.Generic.EndOfResponse |
paulb@81 | 311 | |
paulb@81 | 312 | # vim: tabstop=4 expandtab shiftwidth=4 |