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