paulb@81 | 1 | #!/usr/bin/env python |
paulb@81 | 2 | |
paulb@81 | 3 | "Resources for use with WebStack." |
paulb@81 | 4 | |
paulb@81 | 5 | import WebStack.Generic |
paulb@81 | 6 | import XSLForms.Fields |
paulb@83 | 7 | import XSLForms.Prepare |
paulb@83 | 8 | import XSLForms.Output |
paulb@83 | 9 | import XSLOutput |
paulb@83 | 10 | import os |
paulb@81 | 11 | |
paulb@81 | 12 | class XSLFormsResource: |
paulb@81 | 13 | |
paulb@83 | 14 | """ |
paulb@83 | 15 | A generic XSLForms resource for use with WebStack. |
paulb@83 | 16 | |
paulb@83 | 17 | When overriding this class, define the following attributes appropriately: |
paulb@83 | 18 | |
paulb@83 | 19 | * template_resources - a dictionary mapping output identifiers to |
paulb@83 | 20 | (template_filename, output_filename) tuples, |
paulb@83 | 21 | indicating the template and stylesheet filenames |
paulb@83 | 22 | to be employed |
paulb@83 | 23 | |
paulb@83 | 24 | * in_page_resources - a dictionary mapping fragment identifiers to |
paulb@83 | 25 | (output_filename, node_identifier) tuples, |
paulb@83 | 26 | indicating the stylesheet filename to be |
paulb@83 | 27 | employed, along with the node identifier used in |
paulb@83 | 28 | the original template and output documents to |
paulb@83 | 29 | mark a region of those documents as the fragment |
paulb@83 | 30 | to be updated upon "in-page" requests |
paulb@83 | 31 | |
paulb@83 | 32 | * resource_dir - the absolute path of the directory in which |
paulb@83 | 33 | stylesheet resources are to reside |
paulb@83 | 34 | |
paulb@83 | 35 | All filenames shall be simple leafnames for files residing in the resource's |
paulb@83 | 36 | special resource directory 'resource_dir'. |
paulb@83 | 37 | |
paulb@83 | 38 | The following attributes may also be specified: |
paulb@83 | 39 | |
paulb@83 | 40 | * path_encoding - the assumed encoding of characters in request |
paulb@83 | 41 | paths |
paulb@83 | 42 | |
paulb@83 | 43 | * encoding - the assumed encoding of characters in request |
paulb@83 | 44 | bodies |
paulb@83 | 45 | """ |
paulb@81 | 46 | |
paulb@81 | 47 | path_encoding = "iso-8859-1" |
paulb@81 | 48 | encoding = "utf-8" |
paulb@83 | 49 | template_resources = {} |
paulb@83 | 50 | in_page_resources = {} |
paulb@83 | 51 | resource_dir = None |
paulb@81 | 52 | |
paulb@81 | 53 | def get_fields_from_body(self, trans, encoding): |
paulb@81 | 54 | |
paulb@81 | 55 | """ |
paulb@81 | 56 | From the given transaction 'trans' and using the stated text 'encoding' |
paulb@81 | 57 | get the field values from the request body and return a dictionary |
paulb@81 | 58 | mapping field names to lists of such values. |
paulb@81 | 59 | """ |
paulb@81 | 60 | |
paulb@81 | 61 | text = trans.get_request_stream().read().decode(encoding) |
paulb@81 | 62 | parameters = {} |
paulb@81 | 63 | for text_line in text.split("\r\n"): |
paulb@81 | 64 | text_parts = text_line.split("=") |
paulb@81 | 65 | text_name, text_value = text_parts[0], "=".join(text_parts[1:]) |
paulb@81 | 66 | if not parameters.has_key(text_name): |
paulb@81 | 67 | parameters[text_name] = [] |
paulb@81 | 68 | # NOTE: Workaround from posted text. |
paulb@81 | 69 | if text_value[-1] == "\x00": |
paulb@81 | 70 | text_value = text_value[:-1] |
paulb@81 | 71 | parameters[text_name].append(text_value) |
paulb@81 | 72 | return parameters |
paulb@81 | 73 | |
paulb@83 | 74 | def prepare_output(self, output_identifier): |
paulb@83 | 75 | |
paulb@83 | 76 | """ |
paulb@83 | 77 | Prepare the output stylesheets using the given 'output_identifier' to |
paulb@83 | 78 | indicate which templates and stylesheets are to be employed in the |
paulb@83 | 79 | production of output from the resource. |
paulb@83 | 80 | |
paulb@83 | 81 | The 'output_identifier' is used as a key to the 'template_resources' |
paulb@83 | 82 | dictionary attribute. |
paulb@83 | 83 | """ |
paulb@83 | 84 | |
paulb@83 | 85 | template_filename, output_filename = self.template_resources[output_identifier] |
paulb@83 | 86 | output_path = os.path.join(self.resource_dir, output_filename) |
paulb@83 | 87 | template_path = os.path.join(self.resource_dir, template_filename) |
paulb@83 | 88 | XSLForms.Prepare.ensure_stylesheet(template_path, output_path) |
paulb@83 | 89 | return output_path |
paulb@83 | 90 | |
paulb@83 | 91 | def prepare_fragment(self, output_identifier, fragment_identifier): |
paulb@83 | 92 | |
paulb@83 | 93 | """ |
paulb@83 | 94 | Prepare the output stylesheets for the given 'output_identifier' and |
paulb@83 | 95 | 'fragment_identifier', indicating which templates and stylesheets are to |
paulb@83 | 96 | be employed in the production of output from the resource. |
paulb@83 | 97 | |
paulb@83 | 98 | The 'output_identifier' is used as a key to the 'template_resources' |
paulb@83 | 99 | dictionary attribute; the 'fragment_identifier' is used as a key to the |
paulb@83 | 100 | 'in_page_resources' dictionary attribute. |
paulb@83 | 101 | """ |
paulb@83 | 102 | |
paulb@83 | 103 | output_path = self.prepare_output(output_identifier) |
paulb@83 | 104 | fragment_filename, node_identifier = self.in_page_resources[fragment_identifier] |
paulb@83 | 105 | fragment_path = os.path.join(self.resource_dir, fragment_filename) |
paulb@83 | 106 | XSLForms.Prepare.ensure_stylesheet_fragment(output_path, fragment_path, node_identifier) |
paulb@83 | 107 | return fragment_path |
paulb@83 | 108 | |
paulb@83 | 109 | def send_output(self, trans, stylesheet_filenames, stylesheet_parameters, document): |
paulb@83 | 110 | |
paulb@83 | 111 | """ |
paulb@83 | 112 | Send the output from the resource to the user employing the transaction |
paulb@83 | 113 | 'trans', stylesheets having the given 'stylesheet_filenames', parameters |
paulb@83 | 114 | as defined in the 'stylesheet_parameters' dictionary, and the 'document' |
paulb@83 | 115 | upon which the output will be based. |
paulb@83 | 116 | """ |
paulb@83 | 117 | |
paulb@83 | 118 | proc = XSLOutput.Processor(stylesheet_filenames, parameters=stylesheet_parameters) |
paulb@83 | 119 | proc.send_output(trans.get_response_stream(), trans.get_response_stream_encoding(), |
paulb@83 | 120 | document) |
paulb@83 | 121 | |
paulb@81 | 122 | def respond(self, trans): |
paulb@81 | 123 | |
paulb@81 | 124 | """ |
paulb@81 | 125 | Respond to the request described by the given transaction 'trans'. |
paulb@81 | 126 | """ |
paulb@81 | 127 | |
paulb@81 | 128 | # Only obtain field information according to the stated method. |
paulb@81 | 129 | |
paulb@81 | 130 | method = trans.get_request_method() |
paulb@81 | 131 | path_info = trans.get_path_info() |
paulb@81 | 132 | |
paulb@81 | 133 | # Handle typical request methods, processing request information. |
paulb@81 | 134 | |
paulb@81 | 135 | if method == "GET": |
paulb@81 | 136 | |
paulb@81 | 137 | # Get the fields from the request path (URL). |
paulb@81 | 138 | |
paulb@81 | 139 | form = XSLForms.Fields.Form(encoding=self.path_encoding, values_are_lists=1) |
paulb@81 | 140 | parameters = trans.get_fields_from_path() |
paulb@81 | 141 | form.set_parameters(parameters) |
paulb@81 | 142 | |
paulb@81 | 143 | elif method == "POST": |
paulb@81 | 144 | |
paulb@81 | 145 | # Get the fields from the request body. |
paulb@81 | 146 | |
paulb@81 | 147 | form = XSLForms.Fields.Form(encoding=self.encoding, values_are_lists=1) |
paulb@81 | 148 | |
paulb@81 | 149 | # Handle requests for in-page updates. |
paulb@81 | 150 | |
paulb@81 | 151 | if path_info in self.in_page_resources.keys(): |
paulb@81 | 152 | parameters = self.get_fields_from_body(trans, self.encoding) |
paulb@81 | 153 | else: |
paulb@81 | 154 | parameters = trans.get_fields_from_body(self.encoding) |
paulb@81 | 155 | |
paulb@81 | 156 | # Get the XML representation of the request. |
paulb@81 | 157 | |
paulb@81 | 158 | form.set_parameters(parameters) |
paulb@81 | 159 | |
paulb@81 | 160 | else: |
paulb@81 | 161 | |
paulb@81 | 162 | # Initialise empty containers. |
paulb@81 | 163 | |
paulb@81 | 164 | parameters = {} |
paulb@81 | 165 | documents = {} |
paulb@81 | 166 | |
paulb@81 | 167 | # Call an overridden method with the processed request information. |
paulb@81 | 168 | |
paulb@81 | 169 | self.respond_to_form(trans, form) |
paulb@81 | 170 | |
paulb@81 | 171 | def respond_to_form(self, trans, form): |
paulb@81 | 172 | |
paulb@81 | 173 | """ |
paulb@81 | 174 | Respond to the request described by the given transaction 'trans', using |
paulb@81 | 175 | the given 'form' object to conveniently retrieve field (request |
paulb@81 | 176 | parameter) information and structured form information (as DOM-style XML |
paulb@81 | 177 | documents). |
paulb@81 | 178 | """ |
paulb@81 | 179 | |
paulb@81 | 180 | trans.set_response_code(500) |
paulb@81 | 181 | raise WebStack.Generic.EndOfResponse |
paulb@81 | 182 | |
paulb@81 | 183 | # vim: tabstop=4 expandtab shiftwidth=4 |