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