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@88 | 6 | import XSLForms.Resources |
paulb@88 | 7 | import XSLForms.Utils |
paulb@5 | 8 | import XSLOutput |
paulb@5 | 9 | import os |
paulb@5 | 10 | |
paulb@88 | 11 | class PEP241Resource(XSLForms.Resources.XSLFormsResource): |
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@88 | 17 | template_resources = { |
paulb@88 | 18 | "pep241" : ("pep241_template.xhtml", "pep241_output.xsl") |
paulb@88 | 19 | } |
paulb@5 | 20 | |
paulb@88 | 21 | def respond_to_form(self, trans, form): |
paulb@5 | 22 | |
paulb@88 | 23 | """ |
paulb@88 | 24 | Respond to a request having the given transaction 'trans' and the given |
paulb@88 | 25 | 'form' information. |
paulb@88 | 26 | """ |
paulb@26 | 27 | |
paulb@88 | 28 | parameters = form.get_parameters() |
paulb@88 | 29 | documents = form.get_documents() |
paulb@88 | 30 | selectors = form.get_selectors() |
paulb@5 | 31 | |
paulb@5 | 32 | # Ensure the presence of a document. |
paulb@5 | 33 | |
paulb@5 | 34 | if documents.has_key("package"): |
paulb@5 | 35 | package = documents["package"] |
paulb@5 | 36 | else: |
paulb@88 | 37 | package = form.new_instance("package") |
paulb@5 | 38 | |
paulb@26 | 39 | # Add and remove elements according to the selectors found. |
paulb@26 | 40 | |
paulb@88 | 41 | XSLForms.Utils.add_elements(selectors.get("add_platform"), "platform", "platforms") |
paulb@88 | 42 | XSLForms.Utils.remove_elements(selectors.get("remove_platform")) |
paulb@88 | 43 | XSLForms.Utils.add_elements(selectors.get("add_supported_platform"), "supported-platform", "supported-platforms") |
paulb@88 | 44 | XSLForms.Utils.remove_elements(selectors.get("remove_supported_platform")) |
paulb@88 | 45 | XSLForms.Utils.add_elements(selectors.get("add_keyword"), "keyword", "keywords") |
paulb@88 | 46 | XSLForms.Utils.remove_elements(selectors.get("remove_keyword")) |
paulb@88 | 47 | XSLForms.Utils.add_elements(selectors.get("add_author"), "author", "authors") |
paulb@88 | 48 | XSLForms.Utils.remove_elements(selectors.get("remove_author")) |
paulb@88 | 49 | XSLForms.Utils.add_elements(selectors.get("add_dependency"), "dependency", "dependencies") |
paulb@88 | 50 | XSLForms.Utils.remove_elements(selectors.get("remove_dependency")) |
paulb@5 | 51 | |
paulb@26 | 52 | # Send a response according to certain parameters. |
paulb@26 | 53 | # When exported, an XML version of the data is returned. |
paulb@26 | 54 | |
paulb@26 | 55 | if parameters.has_key("export"): |
paulb@26 | 56 | trans.set_content_type(WebStack.Generic.ContentType("text/xml", self.encoding)) |
paulb@88 | 57 | package.toStream(trans.get_response_stream(), trans.get_response_stream_encoding()) |
paulb@18 | 58 | |
paulb@26 | 59 | # When not exported, the data is transformed to produce a normal Web |
paulb@26 | 60 | # page. |
paulb@26 | 61 | |
paulb@26 | 62 | else: |
paulb@18 | 63 | |
paulb@26 | 64 | # Transform, adding enumerations/ranges. |
paulb@5 | 65 | |
paulb@26 | 66 | categories_xsl = os.path.join(self.resource_dir, "pep241_categories.xsl") |
paulb@26 | 67 | categories_xml = os.path.join(self.resource_dir, "pep241_categories.xml") |
paulb@26 | 68 | proc = XSLOutput.Processor([categories_xsl], {"categories" : categories_xml}) |
paulb@26 | 69 | package = proc.get_result(package) |
paulb@5 | 70 | |
paulb@26 | 71 | # Start the response. |
paulb@26 | 72 | |
paulb@71 | 73 | trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", self.encoding)) |
paulb@5 | 74 | |
paulb@26 | 75 | # Ensure that an output stylesheet exists. |
paulb@5 | 76 | |
paulb@88 | 77 | trans_xsl = self.prepare_output("pep241") |
paulb@26 | 78 | |
paulb@26 | 79 | # Complete the response. |
paulb@26 | 80 | |
paulb@88 | 81 | self.send_output(trans, [trans_xsl], package) |
paulb@5 | 82 | |
paulb@5 | 83 | # vim: tabstop=4 expandtab shiftwidth=4 |