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@79 | 6 | import XSLForms.Utils |
paulb@82 | 7 | import XSLForms.Resources |
paulb@30 | 8 | import XSLOutput |
paulb@30 | 9 | import os |
paulb@30 | 10 | |
paulb@82 | 11 | class ConfiguratorResource(XSLForms.Resources.XSLFormsResource): |
paulb@30 | 12 | |
paulb@30 | 13 | "A resource providing a system configurator." |
paulb@30 | 14 | |
paulb@30 | 15 | resource_dir = os.path.join(os.path.split(__file__)[0], "Resources") |
paulb@30 | 16 | encoding = "utf-8" |
paulb@84 | 17 | template_resources = { |
paulb@84 | 18 | "configuration" : ("config_template.xhtml", "config_output.xsl") |
paulb@84 | 19 | } |
paulb@79 | 20 | in_page_resources = { |
paulb@79 | 21 | "/cpu" : ("config_output_cpu.xsl", "cpu-node"), |
paulb@79 | 22 | "/memory" : ("config_output_memory.xsl", "memory-node"), |
paulb@79 | 23 | "/hard-disks" : ("config_output_harddisks.xsl", "hard-disks-node") |
paulb@79 | 24 | } |
paulb@79 | 25 | |
paulb@82 | 26 | def respond_to_form(self, trans, form): |
paulb@30 | 27 | |
paulb@82 | 28 | """ |
paulb@82 | 29 | Respond to a request having the given transaction 'trans' and the given |
paulb@82 | 30 | 'form' information. |
paulb@82 | 31 | """ |
paulb@30 | 32 | |
paulb@46 | 33 | path_info = trans.get_path_info() |
paulb@82 | 34 | parameters = form.get_parameters() |
paulb@82 | 35 | documents = form.get_documents() |
paulb@82 | 36 | selectors = form.get_selectors() |
paulb@30 | 37 | |
paulb@30 | 38 | # Ensure the presence of a document. |
paulb@30 | 39 | |
paulb@30 | 40 | if documents.has_key("configuration"): |
paulb@30 | 41 | configuration = documents["configuration"] |
paulb@30 | 42 | else: |
paulb@82 | 43 | configuration = form.new_instance("configuration") |
paulb@69 | 44 | #print "*", configuration.toString() |
paulb@30 | 45 | |
paulb@30 | 46 | # Add and remove elements according to the selectors found. |
paulb@30 | 47 | |
paulb@79 | 48 | XSLForms.Utils.add_elements(selectors.get("add-memory-unit"), "memory-unit") |
paulb@79 | 49 | XSLForms.Utils.remove_elements(selectors.get("remove-memory-unit")) |
paulb@79 | 50 | XSLForms.Utils.add_elements(selectors.get("add-storage-unit"), "storage-unit") |
paulb@79 | 51 | XSLForms.Utils.remove_elements(selectors.get("remove-storage-unit")) |
paulb@79 | 52 | XSLForms.Utils.add_elements(selectors.get("add-hard-disk"), "hard-disk") |
paulb@79 | 53 | XSLForms.Utils.remove_elements(selectors.get("remove-hard-disk")) |
paulb@30 | 54 | |
paulb@30 | 55 | # Send a response according to certain parameters. |
paulb@30 | 56 | # When exported, an XML version of the data is returned. |
paulb@30 | 57 | |
paulb@30 | 58 | if parameters.has_key("export"): |
paulb@30 | 59 | trans.set_content_type(WebStack.Generic.ContentType("text/xml", self.encoding)) |
paulb@66 | 60 | configuration.toStream(trans.get_response_stream(), trans.get_response_stream_encoding()) |
paulb@30 | 61 | |
paulb@30 | 62 | # When not exported, the data is transformed to produce a normal Web |
paulb@30 | 63 | # page. |
paulb@30 | 64 | |
paulb@30 | 65 | else: |
paulb@30 | 66 | |
paulb@30 | 67 | # Transform, adding enumerations/ranges. |
paulb@30 | 68 | |
paulb@30 | 69 | database_xsl = os.path.join(self.resource_dir, "config_database.xsl") |
paulb@30 | 70 | database_xml = os.path.join(self.resource_dir, "config_database.xml") |
paulb@33 | 71 | proc = XSLOutput.Processor([database_xsl], references={"database" : database_xml}) |
paulb@30 | 72 | configuration = proc.get_result(configuration) |
paulb@30 | 73 | |
paulb@30 | 74 | # Start the response. |
paulb@30 | 75 | |
paulb@71 | 76 | trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", self.encoding)) |
paulb@30 | 77 | |
paulb@51 | 78 | # Define the stylesheet parameters. |
paulb@51 | 79 | |
paulb@51 | 80 | stylesheet_parameters = {} |
paulb@51 | 81 | |
paulb@30 | 82 | # Ensure that an output stylesheet exists. |
paulb@30 | 83 | |
paulb@79 | 84 | if path_info in self.in_page_resources.keys(): |
paulb@84 | 85 | trans_xsl = self.prepare_fragment("configuration", path_info) |
paulb@63 | 86 | element_path = parameters.get("element-path", [""])[0] |
paulb@63 | 87 | stylesheet_parameters["element-path"] = element_path |
paulb@79 | 88 | else: |
paulb@84 | 89 | trans_xsl = self.prepare_output("configuration") |
paulb@84 | 90 | |
paulb@84 | 91 | # Add information essential for in-page requests. |
paulb@59 | 92 | |
paulb@51 | 93 | stylesheet_parameters["application-url"] = \ |
paulb@51 | 94 | "http://%s:%s%s" % (trans.get_server_name(), trans.get_server_port(), trans.get_path_without_query()) |
paulb@42 | 95 | |
paulb@30 | 96 | # Complete the response. |
paulb@30 | 97 | |
paulb@84 | 98 | self.send_output(trans, [trans_xsl], stylesheet_parameters, configuration) |
paulb@30 | 99 | |
paulb@79 | 100 | #import sys |
paulb@79 | 101 | #proc = XSLOutput.Processor([trans_xsl], parameters=stylesheet_parameters) |
paulb@79 | 102 | #proc.send_output(sys.stderr, "iso-8859-1", configuration) |
paulb@51 | 103 | |
paulb@30 | 104 | # vim: tabstop=4 expandtab shiftwidth=4 |