# HG changeset patch # User paulb # Date 1121379863 0 # Node ID 32a48ea3b9561ab3b232e0f909114d9f2e9cc847 # Parent 541381504c4db99c877e09b31c0be2694e5e1742 [project @ 2005-07-14 22:24:23 by paulb] Added stylesheet preparation and usage to the WebStack resource class, along with documentation of various presentation-related attributes. diff -r 541381504c4d -r 32a48ea3b956 XSLForms/Resources.py --- a/XSLForms/Resources.py Thu Jul 14 21:39:35 2005 +0000 +++ b/XSLForms/Resources.py Thu Jul 14 22:24:23 2005 +0000 @@ -4,13 +4,51 @@ import WebStack.Generic import XSLForms.Fields +import XSLForms.Prepare +import XSLForms.Output +import XSLOutput +import os class XSLFormsResource: - "A generic XSLForms resource for use with WebStack." + """ + A generic XSLForms resource for use with WebStack. + + When overriding this class, define the following attributes appropriately: + + * template_resources - a dictionary mapping output identifiers to + (template_filename, output_filename) tuples, + indicating the template and stylesheet filenames + to be employed + + * in_page_resources - a dictionary mapping fragment identifiers to + (output_filename, node_identifier) tuples, + indicating the stylesheet filename to be + employed, along with the node identifier used in + the original template and output documents to + mark a region of those documents as the fragment + to be updated upon "in-page" requests + + * resource_dir - the absolute path of the directory in which + stylesheet resources are to reside + + All filenames shall be simple leafnames for files residing in the resource's + special resource directory 'resource_dir'. + + The following attributes may also be specified: + + * path_encoding - the assumed encoding of characters in request + paths + + * encoding - the assumed encoding of characters in request + bodies + """ path_encoding = "iso-8859-1" encoding = "utf-8" + template_resources = {} + in_page_resources = {} + resource_dir = None def get_fields_from_body(self, trans, encoding): @@ -33,14 +71,60 @@ parameters[text_name].append(text_value) return parameters + def prepare_output(self, output_identifier): + + """ + Prepare the output stylesheets using the given 'output_identifier' to + indicate which templates and stylesheets are to be employed in the + production of output from the resource. + + The 'output_identifier' is used as a key to the 'template_resources' + dictionary attribute. + """ + + template_filename, output_filename = self.template_resources[output_identifier] + output_path = os.path.join(self.resource_dir, output_filename) + template_path = os.path.join(self.resource_dir, template_filename) + XSLForms.Prepare.ensure_stylesheet(template_path, output_path) + return output_path + + def prepare_fragment(self, output_identifier, fragment_identifier): + + """ + Prepare the output stylesheets for the given 'output_identifier' and + 'fragment_identifier', indicating which templates and stylesheets are to + be employed in the production of output from the resource. + + The 'output_identifier' is used as a key to the 'template_resources' + dictionary attribute; the 'fragment_identifier' is used as a key to the + 'in_page_resources' dictionary attribute. + """ + + output_path = self.prepare_output(output_identifier) + fragment_filename, node_identifier = self.in_page_resources[fragment_identifier] + fragment_path = os.path.join(self.resource_dir, fragment_filename) + XSLForms.Prepare.ensure_stylesheet_fragment(output_path, fragment_path, node_identifier) + return fragment_path + + def send_output(self, trans, stylesheet_filenames, stylesheet_parameters, document): + + """ + Send the output from the resource to the user employing the transaction + 'trans', stylesheets having the given 'stylesheet_filenames', parameters + as defined in the 'stylesheet_parameters' dictionary, and the 'document' + upon which the output will be based. + """ + + proc = XSLOutput.Processor(stylesheet_filenames, parameters=stylesheet_parameters) + proc.send_output(trans.get_response_stream(), trans.get_response_stream_encoding(), + document) + def respond(self, trans): """ Respond to the request described by the given transaction 'trans'. """ - global XSLForms # NOTE: Strangely required to avoid UnboundLocalError! - # Only obtain field information according to the stated method. method = trans.get_request_method()