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@18 | 9 | import libxml2dom |
paulb@5 | 10 | import os |
paulb@5 | 11 | |
paulb@14 | 12 | def add_elements(positions, element_name, element_parent_name=None): |
paulb@12 | 13 | if not positions: |
paulb@12 | 14 | return |
paulb@12 | 15 | for position in positions: |
paulb@12 | 16 | if element_parent_name: |
paulb@12 | 17 | parent_elements = position.xpath(element_parent_name) |
paulb@12 | 18 | if not parent_elements: |
paulb@12 | 19 | parent_element = position.ownerDocument.createElementNS(None, element_parent_name) |
paulb@12 | 20 | position.appendChild(parent_element) |
paulb@12 | 21 | else: |
paulb@12 | 22 | parent_element = parent_elements[0] |
paulb@12 | 23 | else: |
paulb@12 | 24 | parent_element = position |
paulb@12 | 25 | parent_element.appendChild(position.ownerDocument.createElementNS(None, element_name)) |
paulb@12 | 26 | |
paulb@14 | 27 | def remove_elements(positions): |
paulb@12 | 28 | if not positions: |
paulb@12 | 29 | return |
paulb@12 | 30 | for position in positions: |
paulb@12 | 31 | position.parentNode.removeChild(position) |
paulb@12 | 32 | |
paulb@5 | 33 | class PEP241Resource: |
paulb@5 | 34 | |
paulb@5 | 35 | "A resource providing repository browsing." |
paulb@5 | 36 | |
paulb@5 | 37 | resource_dir = os.path.join(os.path.split(__file__)[0], "Resources") |
paulb@5 | 38 | encoding = "utf-8" |
paulb@5 | 39 | |
paulb@5 | 40 | def respond(self, trans): |
paulb@5 | 41 | |
paulb@5 | 42 | # Produce stylesheet. |
paulb@5 | 43 | |
paulb@5 | 44 | if trans.get_path_info().split("/")[-1] == "styles.css": |
paulb@5 | 45 | trans.set_content_type(WebStack.Generic.ContentType("text/css", self.encoding)) |
paulb@5 | 46 | out = trans.get_response_stream() |
paulb@5 | 47 | f = open(os.path.join(self.resource_dir, "styles.css"), "rb") |
paulb@5 | 48 | out.write(f.read()) |
paulb@5 | 49 | f.close() |
paulb@5 | 50 | raise WebStack.Generic.EndOfResponse |
paulb@5 | 51 | |
paulb@5 | 52 | # Handle other requests. |
paulb@5 | 53 | |
paulb@5 | 54 | global XSLForms # NOTE: Strangely required to avoid UnboundLocalError! |
paulb@5 | 55 | method = trans.get_request_method() |
paulb@26 | 56 | |
paulb@26 | 57 | # Only obtain field information according to the stated method. |
paulb@26 | 58 | |
paulb@5 | 59 | if method == "GET": |
paulb@5 | 60 | fields = XSLForms.Fields.Fields(encoding="iso-8859-1", values_are_lists=1) |
paulb@26 | 61 | parameters = trans.get_fields_from_path() |
paulb@26 | 62 | documents = fields.make_documents(parameters.items()) |
paulb@5 | 63 | elif method == "POST": |
paulb@5 | 64 | fields = XSLForms.Fields.Fields(encoding=self.encoding, values_are_lists=1) |
paulb@26 | 65 | parameters = trans.get_fields_from_body(self.encoding) |
paulb@26 | 66 | documents = fields.make_documents(parameters.items()) |
paulb@5 | 67 | else: |
paulb@5 | 68 | trans.set_response_code(405) |
paulb@5 | 69 | raise WebStack.Generic.EndOfResponse |
paulb@5 | 70 | |
paulb@5 | 71 | # Ensure the presence of a document. |
paulb@5 | 72 | |
paulb@5 | 73 | if documents.has_key("package"): |
paulb@5 | 74 | package = documents["package"] |
paulb@5 | 75 | else: |
paulb@5 | 76 | package = fields.new_instance("package") |
paulb@5 | 77 | |
paulb@26 | 78 | # Add and remove elements according to the selectors found. |
paulb@26 | 79 | |
paulb@26 | 80 | selectors = fields.get_selectors(parameters.items(), documents) |
paulb@14 | 81 | add_elements(selectors.get("add_platform"), "platform", "platforms") |
paulb@14 | 82 | remove_elements(selectors.get("remove_platform")) |
paulb@14 | 83 | add_elements(selectors.get("add_supported_platform"), "supported-platform", "supported-platforms") |
paulb@14 | 84 | remove_elements(selectors.get("remove_supported_platform")) |
paulb@14 | 85 | add_elements(selectors.get("add_keyword"), "keyword", "keywords") |
paulb@14 | 86 | remove_elements(selectors.get("remove_keyword")) |
paulb@14 | 87 | add_elements(selectors.get("add_author"), "author", "authors") |
paulb@14 | 88 | remove_elements(selectors.get("remove_author")) |
paulb@14 | 89 | add_elements(selectors.get("add_dependency"), "dependency", "dependencies") |
paulb@14 | 90 | remove_elements(selectors.get("remove_dependency")) |
paulb@5 | 91 | |
paulb@26 | 92 | # Send a response according to certain parameters. |
paulb@26 | 93 | # When exported, an XML version of the data is returned. |
paulb@26 | 94 | |
paulb@26 | 95 | if parameters.has_key("export"): |
paulb@26 | 96 | trans.set_content_type(WebStack.Generic.ContentType("text/xml", self.encoding)) |
paulb@26 | 97 | libxml2dom.toStream(package, trans.get_response_stream(), trans.get_response_stream_encoding()) |
paulb@18 | 98 | |
paulb@26 | 99 | # When not exported, the data is transformed to produce a normal Web |
paulb@26 | 100 | # page. |
paulb@26 | 101 | |
paulb@26 | 102 | else: |
paulb@18 | 103 | |
paulb@26 | 104 | # Transform, adding enumerations/ranges. |
paulb@5 | 105 | |
paulb@26 | 106 | categories_xsl = os.path.join(self.resource_dir, "pep241_categories.xsl") |
paulb@26 | 107 | categories_xml = os.path.join(self.resource_dir, "pep241_categories.xml") |
paulb@26 | 108 | proc = XSLOutput.Processor([categories_xsl], {"categories" : categories_xml}) |
paulb@26 | 109 | package = proc.get_result(package) |
paulb@5 | 110 | |
paulb@26 | 111 | # Start the response. |
paulb@26 | 112 | |
paulb@71 | 113 | trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", self.encoding)) |
paulb@5 | 114 | |
paulb@26 | 115 | # Ensure that an output stylesheet exists. |
paulb@5 | 116 | |
paulb@26 | 117 | trans_xsl = os.path.join(self.resource_dir, "pep241_output.xsl") |
paulb@5 | 118 | template_xml = os.path.join(self.resource_dir, "pep241_template.xhtml") |
paulb@5 | 119 | |
paulb@26 | 120 | if not os.path.exists(trans_xsl) or \ |
paulb@26 | 121 | os.path.getmtime(trans_xsl) < os.path.getmtime(template_xml): |
paulb@5 | 122 | |
paulb@26 | 123 | import XSLForms.Prepare |
paulb@26 | 124 | template_xml = os.path.join(self.resource_dir, "pep241_template.xhtml") |
paulb@26 | 125 | XSLForms.Prepare.make_stylesheet(template_xml, trans_xsl) |
paulb@26 | 126 | |
paulb@26 | 127 | # Complete the response. |
paulb@26 | 128 | |
paulb@26 | 129 | proc = XSLOutput.Processor([trans_xsl]) |
paulb@26 | 130 | proc.send_output(trans.get_response_stream(), trans.get_response_stream_encoding(), |
paulb@26 | 131 | package) |
paulb@5 | 132 | |
paulb@5 | 133 | # vim: tabstop=4 expandtab shiftwidth=4 |