XSLTools

examples/Common/PEP241/__init__.py

204:0d474eee9121
2005-08-18 paulb [project @ 2005-08-18 15:57:49 by paulb] Moved XSLOutput and XMLTable into a new XSLTools package.
     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 from XSLTools import XSLOutput     9 import os    10     11 # Site map imports.    12     13 from WebStack.Resources.ResourceMap import MapResource    14 from WebStack.Resources.Static import DirectoryResource    15     16 # Resource classes.    17     18 class PEP241Resource(XSLForms.Resources.XSLFormsResource):    19     20     "A resource providing repository browsing."    21     22     resource_dir = os.path.join(os.path.split(__file__)[0], "Resources")    23     encoding = "utf-8"    24     template_resources = {    25         "pep241" : ("pep241_template.xhtml", "pep241_output.xsl")    26         }    27     28     def respond_to_form(self, trans, form):    29     30         """    31         Respond to a request having the given transaction 'trans' and the given    32         'form' information.    33         """    34     35         parameters = form.get_parameters()    36         documents = form.get_documents()    37         selectors = form.get_selectors()    38     39         # Ensure the presence of a document.    40     41         if documents.has_key("package"):    42             package = documents["package"]    43         else:    44             package = form.new_instance("package")    45     46         # Add and remove elements according to the selectors found.    47     48         XSLForms.Utils.add_elements(selectors.get("add_platform"), "platform", "platforms")    49         XSLForms.Utils.remove_elements(selectors.get("remove_platform"))    50         XSLForms.Utils.add_elements(selectors.get("add_supported_platform"), "supported-platform", "supported-platforms")    51         XSLForms.Utils.remove_elements(selectors.get("remove_supported_platform"))    52         XSLForms.Utils.add_elements(selectors.get("add_keyword"), "keyword", "keywords")    53         XSLForms.Utils.remove_elements(selectors.get("remove_keyword"))    54         XSLForms.Utils.add_elements(selectors.get("add_author"), "author", "authors")    55         XSLForms.Utils.remove_elements(selectors.get("remove_author"))    56         XSLForms.Utils.add_elements(selectors.get("add_dependency"), "dependency", "dependencies")    57         XSLForms.Utils.remove_elements(selectors.get("remove_dependency"))    58     59         # Send a response according to certain parameters.    60         # When exported, an XML version of the data is returned.    61     62         if parameters.has_key("export"):    63             trans.set_content_type(WebStack.Generic.ContentType("text/xml", self.encoding))    64             package.toStream(trans.get_response_stream(), trans.get_response_stream_encoding())    65     66         # When not exported, the data is transformed to produce a normal Web    67         # page.    68     69         else:    70     71             # Transform, adding enumerations/ranges.    72     73             categories_xsl = os.path.join(self.resource_dir, "pep241_categories.xsl")    74             categories_xml = os.path.join(self.resource_dir, "pep241_categories.xml")    75             proc = XSLOutput.Processor([categories_xsl], {"categories" : categories_xml})    76             package = proc.get_result(package)    77     78             # Start the response.    79     80             trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", self.encoding))    81     82             # Ensure that an output stylesheet exists.    83     84             trans_xsl = self.prepare_output("pep241")    85     86             # Complete the response.    87     88             self.send_output(trans, [trans_xsl], package)    89     90 # Site map initialisation.    91     92 def get_site():    93     94     "Return a simple Web site resource."    95     96     # Get the main resource and the directory used by the application.    97     98     pep241_resource = PEP241Resource()    99     directory = pep241_resource.resource_dir   100    101     # Make a simple Web site.   102    103     resource = MapResource({   104         # Static resources:   105         "styles" : DirectoryResource(os.path.join(directory, "styles"), {"css" : "text/css"}),   106         # Main page:   107         "" : pep241_resource   108         })   109    110     return resource   111    112 # vim: tabstop=4 expandtab shiftwidth=4