paulb@30 | 1 | #!/usr/bin/env python |
paulb@30 | 2 | |
paulb@30 | 3 | "A WebStack application for a system configurator." |
paulb@30 | 4 | |
paulb@30 | 5 | import WebStack.Generic |
paulb@30 | 6 | import XSLForms.Fields |
paulb@30 | 7 | import XSLForms.Output |
paulb@30 | 8 | import XSLOutput |
paulb@30 | 9 | import libxml2dom |
paulb@30 | 10 | import os |
paulb@30 | 11 | |
paulb@30 | 12 | # NOTE: Move these functions into a common utility library. |
paulb@30 | 13 | |
paulb@30 | 14 | def add_elements(positions, element_name, element_parent_name=None): |
paulb@30 | 15 | if not positions: |
paulb@30 | 16 | return |
paulb@30 | 17 | for position in positions: |
paulb@30 | 18 | if element_parent_name: |
paulb@30 | 19 | parent_elements = position.xpath(element_parent_name) |
paulb@30 | 20 | if not parent_elements: |
paulb@30 | 21 | parent_element = position.ownerDocument.createElementNS(None, element_parent_name) |
paulb@30 | 22 | position.appendChild(parent_element) |
paulb@30 | 23 | else: |
paulb@30 | 24 | parent_element = parent_elements[0] |
paulb@30 | 25 | else: |
paulb@30 | 26 | parent_element = position |
paulb@30 | 27 | parent_element.appendChild(position.ownerDocument.createElementNS(None, element_name)) |
paulb@30 | 28 | |
paulb@30 | 29 | def remove_elements(positions): |
paulb@30 | 30 | if not positions: |
paulb@30 | 31 | return |
paulb@30 | 32 | for position in positions: |
paulb@30 | 33 | position.parentNode.removeChild(position) |
paulb@30 | 34 | |
paulb@30 | 35 | class ConfiguratorResource: |
paulb@30 | 36 | |
paulb@30 | 37 | "A resource providing a system configurator." |
paulb@30 | 38 | |
paulb@30 | 39 | resource_dir = os.path.join(os.path.split(__file__)[0], "Resources") |
paulb@30 | 40 | encoding = "utf-8" |
paulb@30 | 41 | |
paulb@30 | 42 | def respond(self, trans): |
paulb@30 | 43 | |
paulb@30 | 44 | # Produce stylesheet. |
paulb@30 | 45 | |
paulb@30 | 46 | if trans.get_path_info().split("/")[-1] == "styles.css": |
paulb@30 | 47 | trans.set_content_type(WebStack.Generic.ContentType("text/css", self.encoding)) |
paulb@30 | 48 | out = trans.get_response_stream() |
paulb@30 | 49 | f = open(os.path.join(self.resource_dir, "styles.css"), "rb") |
paulb@30 | 50 | out.write(f.read()) |
paulb@30 | 51 | f.close() |
paulb@30 | 52 | raise WebStack.Generic.EndOfResponse |
paulb@30 | 53 | |
paulb@30 | 54 | global XSLForms # NOTE: Strangely required to avoid UnboundLocalError! |
paulb@30 | 55 | method = trans.get_request_method() |
paulb@30 | 56 | |
paulb@30 | 57 | # Only obtain field information according to the stated method. |
paulb@30 | 58 | |
paulb@30 | 59 | if method == "GET": |
paulb@30 | 60 | fields = XSLForms.Fields.Fields(encoding="iso-8859-1", values_are_lists=1) |
paulb@30 | 61 | parameters = trans.get_fields_from_path() |
paulb@30 | 62 | documents = fields.make_documents(parameters.items()) |
paulb@30 | 63 | elif method == "POST": |
paulb@30 | 64 | fields = XSLForms.Fields.Fields(encoding=self.encoding, values_are_lists=1) |
paulb@30 | 65 | parameters = trans.get_fields_from_body(self.encoding) |
paulb@30 | 66 | documents = fields.make_documents(parameters.items()) |
paulb@33 | 67 | elif method == "PROPFIND": |
paulb@33 | 68 | dav_xsl = os.path.join(self.resource_dir, "config_dav.xsl") |
paulb@33 | 69 | database_xml = os.path.join(self.resource_dir, "config_database.xml") |
paulb@33 | 70 | database = libxml2dom.parse(database_xml) |
paulb@33 | 71 | |
paulb@33 | 72 | # Process the path. |
paulb@33 | 73 | |
paulb@33 | 74 | parts = trans.get_path_info().split("/") |
paulb@33 | 75 | if not parts[-1]: |
paulb@33 | 76 | del parts[-1] |
paulb@33 | 77 | |
paulb@33 | 78 | start = parts[-1] |
paulb@33 | 79 | path = "/".join(parts) |
paulb@33 | 80 | proc = XSLOutput.Processor([dav_xsl], parameters={ |
paulb@33 | 81 | "path" : path, |
paulb@33 | 82 | "start" : start |
paulb@33 | 83 | }) |
paulb@33 | 84 | |
paulb@33 | 85 | trans.set_response_code(207) |
paulb@33 | 86 | proc.send_output(trans.get_response_stream(), trans.get_response_stream_encoding(), |
paulb@33 | 87 | database) |
paulb@33 | 88 | raise WebStack.Generic.EndOfResponse |
paulb@30 | 89 | else: |
paulb@30 | 90 | trans.set_response_code(405) |
paulb@30 | 91 | raise WebStack.Generic.EndOfResponse |
paulb@30 | 92 | |
paulb@30 | 93 | # Ensure the presence of a document. |
paulb@30 | 94 | |
paulb@30 | 95 | if documents.has_key("configuration"): |
paulb@30 | 96 | configuration = documents["configuration"] |
paulb@30 | 97 | else: |
paulb@30 | 98 | configuration = fields.new_instance("configuration") |
paulb@30 | 99 | |
paulb@30 | 100 | # Add and remove elements according to the selectors found. |
paulb@30 | 101 | |
paulb@30 | 102 | selectors = fields.get_selectors(parameters.items(), documents) |
paulb@30 | 103 | add_elements(selectors.get("add-memory-unit"), "memory-unit") |
paulb@30 | 104 | remove_elements(selectors.get("remove-memory-unit")) |
paulb@30 | 105 | add_elements(selectors.get("add-storage-unit"), "storage-unit") |
paulb@30 | 106 | remove_elements(selectors.get("remove-storage-unit")) |
paulb@30 | 107 | add_elements(selectors.get("add-hard-disk"), "hard-disk") |
paulb@30 | 108 | remove_elements(selectors.get("remove-hard-disk")) |
paulb@30 | 109 | |
paulb@30 | 110 | # Send a response according to certain parameters. |
paulb@30 | 111 | # When exported, an XML version of the data is returned. |
paulb@30 | 112 | |
paulb@30 | 113 | if parameters.has_key("export"): |
paulb@30 | 114 | trans.set_content_type(WebStack.Generic.ContentType("text/xml", self.encoding)) |
paulb@30 | 115 | libxml2dom.toStream(configuration, trans.get_response_stream(), trans.get_response_stream_encoding()) |
paulb@30 | 116 | |
paulb@30 | 117 | # When not exported, the data is transformed to produce a normal Web |
paulb@30 | 118 | # page. |
paulb@30 | 119 | |
paulb@30 | 120 | else: |
paulb@30 | 121 | |
paulb@30 | 122 | # Transform, adding enumerations/ranges. |
paulb@30 | 123 | |
paulb@30 | 124 | database_xsl = os.path.join(self.resource_dir, "config_database.xsl") |
paulb@30 | 125 | database_xml = os.path.join(self.resource_dir, "config_database.xml") |
paulb@33 | 126 | proc = XSLOutput.Processor([database_xsl], references={"database" : database_xml}) |
paulb@30 | 127 | configuration = proc.get_result(configuration) |
paulb@30 | 128 | |
paulb@30 | 129 | # Start the response. |
paulb@30 | 130 | |
paulb@30 | 131 | trans.set_content_type(WebStack.Generic.ContentType("text/html", self.encoding)) |
paulb@30 | 132 | |
paulb@30 | 133 | # Ensure that an output stylesheet exists. |
paulb@30 | 134 | |
paulb@30 | 135 | trans_xsl = os.path.join(self.resource_dir, "config_output.xsl") |
paulb@30 | 136 | template_xml = os.path.join(self.resource_dir, "config_template.xhtml") |
paulb@30 | 137 | |
paulb@30 | 138 | if not os.path.exists(trans_xsl) or \ |
paulb@30 | 139 | os.path.getmtime(trans_xsl) < os.path.getmtime(template_xml): |
paulb@30 | 140 | |
paulb@30 | 141 | import XSLForms.Prepare |
paulb@30 | 142 | template_xml = os.path.join(self.resource_dir, "config_template.xhtml") |
paulb@30 | 143 | XSLForms.Prepare.make_stylesheet(template_xml, trans_xsl) |
paulb@30 | 144 | |
paulb@30 | 145 | # Complete the response. |
paulb@30 | 146 | |
paulb@30 | 147 | proc = XSLOutput.Processor([trans_xsl]) |
paulb@30 | 148 | proc.send_output(trans.get_response_stream(), trans.get_response_stream_encoding(), |
paulb@30 | 149 | configuration) |
paulb@30 | 150 | |
paulb@30 | 151 | # vim: tabstop=4 expandtab shiftwidth=4 |