1 #!/usr/bin/env python 2 3 "A WebStack application for a PEP 241 repository." 4 5 import WebStack.Generic 6 import XSLForms.Fields 7 import XSLForms.Output 8 import XSLOutput 9 import os 10 11 class PEP241Resource: 12 13 "A resource providing repository browsing." 14 15 resource_dir = os.path.join(os.path.split(__file__)[0], "Resources") 16 encoding = "utf-8" 17 18 def respond(self, trans): 19 20 # Produce stylesheet. 21 22 if trans.get_path_info().split("/")[-1] == "styles.css": 23 trans.set_content_type(WebStack.Generic.ContentType("text/css", self.encoding)) 24 out = trans.get_response_stream() 25 f = open(os.path.join(self.resource_dir, "styles.css"), "rb") 26 out.write(f.read()) 27 f.close() 28 raise WebStack.Generic.EndOfResponse 29 30 # Handle other requests. 31 32 global XSLForms # NOTE: Strangely required to avoid UnboundLocalError! 33 method = trans.get_request_method() 34 if method == "GET": 35 fields = XSLForms.Fields.Fields(encoding="iso-8859-1", values_are_lists=1) 36 documents = fields.make_documents(trans.get_fields_from_path().items()) 37 elif method == "POST": 38 fields = XSLForms.Fields.Fields(encoding=self.encoding, values_are_lists=1) 39 documents = fields.make_documents(trans.get_fields_from_body(self.encoding).items()) 40 else: 41 trans.set_response_code(405) 42 raise WebStack.Generic.EndOfResponse 43 44 # Ensure the presence of a document. 45 46 if documents.has_key("package"): 47 package = documents["package"] 48 else: 49 package = fields.new_instance("package") 50 51 import libxml2dom, sys 52 libxml2dom.toStream(package, sys.stderr) 53 54 # Start the response. 55 56 trans.set_content_type(WebStack.Generic.ContentType("text/html", self.encoding)) 57 58 # Ensure that an output stylesheet exists. 59 60 trans_xsl = os.path.join(self.resource_dir, "pep241_output.xsl") 61 template_xml = os.path.join(self.resource_dir, "pep241_template.xhtml") 62 63 if not os.path.exists(trans_xsl) or \ 64 os.path.getmtime(trans_xsl) < os.path.getmtime(template_xml): 65 66 import XSLForms.Prepare 67 template_xml = os.path.join(self.resource_dir, "pep241_template.xhtml") 68 XSLForms.Prepare.make_stylesheet(template_xml, trans_xsl) 69 70 # Complete the response. 71 72 proc = XSLOutput.Processor([trans_xsl]) 73 proc.send_output(trans.get_response_stream(), trans.get_response_stream_encoding(), 74 package) 75 76 # vim: tabstop=4 expandtab shiftwidth=4