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@30 | 67 | else: |
paulb@30 | 68 | trans.set_response_code(405) |
paulb@30 | 69 | raise WebStack.Generic.EndOfResponse |
paulb@30 | 70 | |
paulb@30 | 71 | # Ensure the presence of a document. |
paulb@30 | 72 | |
paulb@30 | 73 | if documents.has_key("configuration"): |
paulb@30 | 74 | configuration = documents["configuration"] |
paulb@30 | 75 | else: |
paulb@30 | 76 | configuration = fields.new_instance("configuration") |
paulb@30 | 77 | |
paulb@30 | 78 | # Add and remove elements according to the selectors found. |
paulb@30 | 79 | |
paulb@30 | 80 | selectors = fields.get_selectors(parameters.items(), documents) |
paulb@30 | 81 | add_elements(selectors.get("add-memory-unit"), "memory-unit") |
paulb@30 | 82 | remove_elements(selectors.get("remove-memory-unit")) |
paulb@30 | 83 | add_elements(selectors.get("add-storage-unit"), "storage-unit") |
paulb@30 | 84 | remove_elements(selectors.get("remove-storage-unit")) |
paulb@30 | 85 | add_elements(selectors.get("add-hard-disk"), "hard-disk") |
paulb@30 | 86 | remove_elements(selectors.get("remove-hard-disk")) |
paulb@30 | 87 | |
paulb@30 | 88 | # Send a response according to certain parameters. |
paulb@30 | 89 | # When exported, an XML version of the data is returned. |
paulb@30 | 90 | |
paulb@30 | 91 | if parameters.has_key("export"): |
paulb@30 | 92 | trans.set_content_type(WebStack.Generic.ContentType("text/xml", self.encoding)) |
paulb@30 | 93 | libxml2dom.toStream(configuration, trans.get_response_stream(), trans.get_response_stream_encoding()) |
paulb@30 | 94 | |
paulb@30 | 95 | # When not exported, the data is transformed to produce a normal Web |
paulb@30 | 96 | # page. |
paulb@30 | 97 | |
paulb@30 | 98 | else: |
paulb@30 | 99 | |
paulb@30 | 100 | # Transform, adding enumerations/ranges. |
paulb@30 | 101 | |
paulb@30 | 102 | database_xsl = os.path.join(self.resource_dir, "config_database.xsl") |
paulb@30 | 103 | database_xml = os.path.join(self.resource_dir, "config_database.xml") |
paulb@30 | 104 | proc = XSLOutput.Processor([database_xsl], {"database" : database_xml}) |
paulb@30 | 105 | configuration = proc.get_result(configuration) |
paulb@30 | 106 | |
paulb@30 | 107 | # Start the response. |
paulb@30 | 108 | |
paulb@30 | 109 | trans.set_content_type(WebStack.Generic.ContentType("text/html", self.encoding)) |
paulb@30 | 110 | |
paulb@30 | 111 | # Ensure that an output stylesheet exists. |
paulb@30 | 112 | |
paulb@30 | 113 | trans_xsl = os.path.join(self.resource_dir, "config_output.xsl") |
paulb@30 | 114 | template_xml = os.path.join(self.resource_dir, "config_template.xhtml") |
paulb@30 | 115 | |
paulb@30 | 116 | if not os.path.exists(trans_xsl) or \ |
paulb@30 | 117 | os.path.getmtime(trans_xsl) < os.path.getmtime(template_xml): |
paulb@30 | 118 | |
paulb@30 | 119 | import XSLForms.Prepare |
paulb@30 | 120 | template_xml = os.path.join(self.resource_dir, "config_template.xhtml") |
paulb@30 | 121 | XSLForms.Prepare.make_stylesheet(template_xml, trans_xsl) |
paulb@30 | 122 | |
paulb@30 | 123 | # Complete the response. |
paulb@30 | 124 | |
paulb@30 | 125 | proc = XSLOutput.Processor([trans_xsl]) |
paulb@30 | 126 | proc.send_output(trans.get_response_stream(), trans.get_response_stream_encoding(), |
paulb@30 | 127 | configuration) |
paulb@30 | 128 | |
paulb@30 | 129 | # vim: tabstop=4 expandtab shiftwidth=4 |