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 33 def respond_to_form(self, trans, form): 34 35 """ 36 Respond to a request having the given transaction 'trans' and the given 37 'form' information. 38 """ 39 40 parameters = form.get_parameters() 41 documents = form.get_documents() 42 selectors = form.get_selectors() 43 44 # Ensure the presence of a document. 45 46 if documents.has_key("package"): 47 package = documents["package"] 48 else: 49 package = form.new_instance("package") 50 51 # Add and remove elements according to the selectors found. 52 53 XSLForms.Utils.add_elements(selectors.get("add_platform"), "platform", "platforms") 54 XSLForms.Utils.remove_elements(selectors.get("remove_platform")) 55 XSLForms.Utils.add_elements(selectors.get("add_supported_platform"), "supported-platform", "supported-platforms") 56 XSLForms.Utils.remove_elements(selectors.get("remove_supported_platform")) 57 XSLForms.Utils.add_elements(selectors.get("add_keyword"), "keyword", "keywords") 58 XSLForms.Utils.remove_elements(selectors.get("remove_keyword")) 59 XSLForms.Utils.add_elements(selectors.get("add_author"), "author", "authors") 60 XSLForms.Utils.remove_elements(selectors.get("remove_author")) 61 XSLForms.Utils.add_elements(selectors.get("add_dependency"), "dependency", "dependencies") 62 XSLForms.Utils.remove_elements(selectors.get("remove_dependency")) 63 64 # Send a response according to certain parameters. 65 # When exported, an XML version of the data is returned. 66 67 if parameters.has_key("export"): 68 trans.set_content_type(WebStack.Generic.ContentType("text/xml", self.encoding)) 69 package.toStream(trans.get_response_stream(), trans.get_response_stream_encoding()) 70 71 # When not exported, the data is transformed to produce a normal Web 72 # page. 73 74 else: 75 76 # Transform, adding enumerations/ranges. 77 78 input_xsl = self.prepare_initialiser("pep241") 79 categories_xml = self.prepare_document("categories") 80 package = self.get_result([input_xsl], package, references={"category" : categories_xml}) 81 82 # Start the response. 83 84 trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", self.encoding)) 85 86 # Ensure that an output stylesheet exists. 87 88 trans_xsl = self.prepare_output("pep241") 89 stylesheet_parameters = {} 90 91 # Complete the response. 92 93 self.send_output(trans, [trans_xsl], package, stylesheet_parameters) 94 95 #from XSLTools import XSLOutput 96 #import sys 97 #proc = XSLOutput.Processor([trans_xsl], parameters=stylesheet_parameters) 98 #proc.send_output(sys.stderr, "iso-8859-1", package) 99 100 # Site map initialisation. 101 102 def get_site(): 103 104 "Return a simple Web site resource." 105 106 # Get the main resource and the directory used by the application. 107 108 pep241_resource = PEP241Resource() 109 directory = pep241_resource.resource_dir 110 111 # Make a simple Web site. 112 113 resource = MapResource({ 114 # Static resources: 115 "styles" : DirectoryResource(os.path.join(directory, "styles"), {"css" : "text/css"}), 116 # Main page: 117 "" : pep241_resource 118 }) 119 120 return resource 121 122 # vim: tabstop=4 expandtab shiftwidth=4