XSLTools

examples/Common/PEP241/__init__.py

320:cbb6702cb53d
2005-10-17 paulb [project @ 2005-10-17 16:20:17 by paulb] Simplified the naming of nodes.
     1 #!/usr/bin/env python     2      3 "A WebStack application for a PEP 241 repository."     4      5 import WebStack.Generic     6 import XSLForms.Resources     7 import XSLForms.Utils     8 import os     9     10 # Site map imports.    11     12 from WebStack.Resources.ResourceMap import MapResource    13 from WebStack.Resources.Static import DirectoryResource    14     15 # Resource classes.    16     17 class PEP241Resource(XSLForms.Resources.XSLFormsResource):    18     19     "A resource providing repository browsing."    20     21     resource_dir = os.path.join(os.path.split(__file__)[0], "Resources")    22     encoding = "utf-8"    23     template_resources = {    24         "pep241" : ("pep241_template.xhtml", "pep241_output.xsl")    25         }    26     init_resources = {    27         "pep241" : ("pep241_template.xhtml", "pep241_input.xsl")    28         }    29     document_resources = {    30         "categories" : "pep241_categories.xml"    31         }    32     in_page_resources = {    33         "platforms" : ("pep241_output_platforms.xsl", "platforms"),    34         "supported-platforms" : ("pep241_output_supported_platforms.xsl", "supported-platforms"),    35         "keywords" : ("pep241_output_keywords.xsl", "keywords"),    36         "authors" : ("pep241_output_authors.xsl", "authors"),    37         "dependencies" : ("pep241_output_dependencies.xsl", "dependencies")    38         }    39     40     def respond_to_form(self, trans, form):    41     42         """    43         Respond to a request having the given transaction 'trans' and the given    44         'form' information.    45         """    46     47         in_page_resource = self.get_in_page_resource(trans)    48         parameters = form.get_parameters()    49         documents = form.get_documents()    50         selectors = form.get_selectors()    51     52         # Ensure the presence of a document.    53     54         if documents.has_key("package"):    55             package = documents["package"]    56         else:    57             package = form.new_instance("package")    58     59         # Add and remove elements according to the selectors found.    60     61         XSLForms.Utils.add_elements(selectors.get("add_platform"), "platform", "platforms")    62         XSLForms.Utils.remove_elements(selectors.get("remove_platform"))    63         XSLForms.Utils.add_elements(selectors.get("add_supported_platform"), "supported-platform", "supported-platforms")    64         XSLForms.Utils.remove_elements(selectors.get("remove_supported_platform"))    65         XSLForms.Utils.add_elements(selectors.get("add_keyword"), "keyword", "keywords")    66         XSLForms.Utils.remove_elements(selectors.get("remove_keyword"))    67         XSLForms.Utils.add_elements(selectors.get("add_author"), "author", "authors")    68         XSLForms.Utils.remove_elements(selectors.get("remove_author"))    69         XSLForms.Utils.add_elements(selectors.get("add_dependency"), "dependency", "dependencies")    70         XSLForms.Utils.remove_elements(selectors.get("remove_dependency"))    71     72         # Send a response according to certain parameters.    73         # When exported, an XML version of the data is returned.    74     75         if parameters.has_key("export"):    76             trans.set_content_type(WebStack.Generic.ContentType("text/xml", self.encoding))    77             package.toStream(trans.get_response_stream(), trans.get_response_stream_encoding())    78     79         # When not exported, the data is transformed to produce a normal Web    80         # page.    81     82         else:    83     84             # Transform, adding enumerations/ranges.    85     86             input_xsl = self.prepare_initialiser("pep241")    87             categories_xml = self.prepare_document("categories")    88             package = self.get_result([input_xsl], package, references={"category" : categories_xml})    89     90             # Start the response.    91     92             trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", self.encoding))    93     94             # Ensure that an output stylesheet exists.    95     96             if in_page_resource in self.in_page_resources.keys():    97                 trans_xsl = self.prepare_fragment("pep241", in_page_resource)    98                 stylesheet_parameters = self.prepare_parameters(parameters)    99             else:   100                 trans_xsl = self.prepare_output("pep241")   101                 stylesheet_parameters = {}   102    103             # Complete the response.   104    105             self.send_output(trans, [trans_xsl], package, stylesheet_parameters)   106    107             #from XSLTools import XSLOutput   108             #import sys   109             #proc = XSLOutput.Processor([trans_xsl], parameters=stylesheet_parameters)   110             #proc.send_output(sys.stderr, "iso-8859-1", package)   111    112 # Site map initialisation.   113    114 def get_site():   115    116     "Return a simple Web site resource."   117    118     # Get the main resource and the directory used by the application.   119    120     pep241_resource = PEP241Resource()   121     directory = pep241_resource.resource_dir   122    123     # Make a simple Web site.   124    125     resource = MapResource({   126         # Static resources:   127         "styles" : DirectoryResource(os.path.join(directory, "styles"), {"css" : "text/css"}),   128         "scripts" : DirectoryResource(os.path.join(directory, "scripts"), {"js" : "text/javascript"}),   129         # In-page resources:   130         "platforms" : pep241_resource,   131         "supported-platforms" : pep241_resource,   132         "keywords" : pep241_resource,   133         "authors" : pep241_resource,   134         "dependencies" : pep241_resource,   135         # Main page:   136         "" : pep241_resource   137         })   138    139     return resource   140    141 # vim: tabstop=4 expandtab shiftwidth=4