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