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 | global XSLForms # NOTE: Strangely required to avoid UnboundLocalError! |
paulb@30 | 45 | |
paulb@30 | 46 | # Only obtain field information according to the stated method. |
paulb@30 | 47 | |
paulb@38 | 48 | method = trans.get_request_method() |
paulb@46 | 49 | path_info = trans.get_path_info() |
paulb@38 | 50 | |
paulb@30 | 51 | if method == "GET": |
paulb@30 | 52 | fields = XSLForms.Fields.Fields(encoding="iso-8859-1", values_are_lists=1) |
paulb@30 | 53 | parameters = trans.get_fields_from_path() |
paulb@30 | 54 | documents = fields.make_documents(parameters.items()) |
paulb@30 | 55 | elif method == "POST": |
paulb@30 | 56 | fields = XSLForms.Fields.Fields(encoding=self.encoding, values_are_lists=1) |
paulb@46 | 57 | if path_info == "/cpu": |
paulb@46 | 58 | text = trans.get_request_stream().read() |
paulb@46 | 59 | text_parts = text.split("=") |
paulb@46 | 60 | text_name, text_values = text_parts[0], ["=".join(text_parts[1:])] |
paulb@46 | 61 | parameters = {text_name : text_values} |
paulb@46 | 62 | else: |
paulb@46 | 63 | parameters = trans.get_fields_from_body(self.encoding) |
paulb@30 | 64 | documents = fields.make_documents(parameters.items()) |
paulb@46 | 65 | print "*", libxml2dom.toString(documents["configuration"]) |
paulb@30 | 66 | else: |
paulb@30 | 67 | trans.set_response_code(405) |
paulb@30 | 68 | raise WebStack.Generic.EndOfResponse |
paulb@30 | 69 | |
paulb@30 | 70 | # Ensure the presence of a document. |
paulb@30 | 71 | |
paulb@30 | 72 | if documents.has_key("configuration"): |
paulb@30 | 73 | configuration = documents["configuration"] |
paulb@30 | 74 | else: |
paulb@30 | 75 | configuration = fields.new_instance("configuration") |
paulb@30 | 76 | |
paulb@30 | 77 | # Add and remove elements according to the selectors found. |
paulb@30 | 78 | |
paulb@30 | 79 | selectors = fields.get_selectors(parameters.items(), documents) |
paulb@30 | 80 | add_elements(selectors.get("add-memory-unit"), "memory-unit") |
paulb@30 | 81 | remove_elements(selectors.get("remove-memory-unit")) |
paulb@30 | 82 | add_elements(selectors.get("add-storage-unit"), "storage-unit") |
paulb@30 | 83 | remove_elements(selectors.get("remove-storage-unit")) |
paulb@30 | 84 | add_elements(selectors.get("add-hard-disk"), "hard-disk") |
paulb@30 | 85 | remove_elements(selectors.get("remove-hard-disk")) |
paulb@30 | 86 | |
paulb@30 | 87 | # Send a response according to certain parameters. |
paulb@30 | 88 | # When exported, an XML version of the data is returned. |
paulb@30 | 89 | |
paulb@30 | 90 | if parameters.has_key("export"): |
paulb@30 | 91 | trans.set_content_type(WebStack.Generic.ContentType("text/xml", self.encoding)) |
paulb@30 | 92 | libxml2dom.toStream(configuration, trans.get_response_stream(), trans.get_response_stream_encoding()) |
paulb@30 | 93 | |
paulb@30 | 94 | # When not exported, the data is transformed to produce a normal Web |
paulb@30 | 95 | # page. |
paulb@30 | 96 | |
paulb@30 | 97 | else: |
paulb@30 | 98 | |
paulb@30 | 99 | # Transform, adding enumerations/ranges. |
paulb@30 | 100 | |
paulb@30 | 101 | database_xsl = os.path.join(self.resource_dir, "config_database.xsl") |
paulb@30 | 102 | database_xml = os.path.join(self.resource_dir, "config_database.xml") |
paulb@33 | 103 | proc = XSLOutput.Processor([database_xsl], references={"database" : database_xml}) |
paulb@30 | 104 | configuration = proc.get_result(configuration) |
paulb@30 | 105 | |
paulb@30 | 106 | # Start the response. |
paulb@30 | 107 | |
paulb@30 | 108 | trans.set_content_type(WebStack.Generic.ContentType("text/html", self.encoding)) |
paulb@30 | 109 | |
paulb@30 | 110 | # Ensure that an output stylesheet exists. |
paulb@30 | 111 | |
paulb@30 | 112 | trans_xsl = os.path.join(self.resource_dir, "config_output.xsl") |
paulb@30 | 113 | template_xml = os.path.join(self.resource_dir, "config_template.xhtml") |
paulb@30 | 114 | |
paulb@30 | 115 | if not os.path.exists(trans_xsl) or \ |
paulb@30 | 116 | os.path.getmtime(trans_xsl) < os.path.getmtime(template_xml): |
paulb@30 | 117 | |
paulb@30 | 118 | import XSLForms.Prepare |
paulb@30 | 119 | template_xml = os.path.join(self.resource_dir, "config_template.xhtml") |
paulb@30 | 120 | XSLForms.Prepare.make_stylesheet(template_xml, trans_xsl) |
paulb@30 | 121 | |
paulb@42 | 122 | # Define the parameters. |
paulb@42 | 123 | |
paulb@42 | 124 | parameters = { |
paulb@42 | 125 | "application-url" : "http://%s:%s%s" % (trans.get_server_name(), trans.get_server_port(), trans.get_path_without_query()) |
paulb@42 | 126 | } |
paulb@42 | 127 | |
paulb@30 | 128 | # Complete the response. |
paulb@30 | 129 | |
paulb@42 | 130 | proc = XSLOutput.Processor([trans_xsl], parameters=parameters) |
paulb@30 | 131 | proc.send_output(trans.get_response_stream(), trans.get_response_stream_encoding(), |
paulb@30 | 132 | configuration) |
paulb@30 | 133 | |
paulb@30 | 134 | # vim: tabstop=4 expandtab shiftwidth=4 |