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@204 | 8 | from XSLTools import XSLOutput |
paulb@30 | 9 | import os |
paulb@30 | 10 | |
paulb@99 | 11 | # Site map imports. |
paulb@99 | 12 | |
paulb@99 | 13 | from WebStack.Resources.ResourceMap import MapResource |
paulb@99 | 14 | from WebStack.Resources.Static import DirectoryResource |
paulb@99 | 15 | |
paulb@99 | 16 | # Resource classes. |
paulb@99 | 17 | |
paulb@82 | 18 | class ConfiguratorResource(XSLForms.Resources.XSLFormsResource): |
paulb@30 | 19 | |
paulb@30 | 20 | "A resource providing a system configurator." |
paulb@30 | 21 | |
paulb@30 | 22 | resource_dir = os.path.join(os.path.split(__file__)[0], "Resources") |
paulb@30 | 23 | encoding = "utf-8" |
paulb@84 | 24 | template_resources = { |
paulb@242 | 25 | "configuration" : ["config_template.xhtml", "config_output.xsl"] |
paulb@242 | 26 | } |
paulb@242 | 27 | transform_resources = { |
paulb@242 | 28 | "database" : ["config_database.xsl"] |
paulb@242 | 29 | } |
paulb@242 | 30 | document_resources = { |
paulb@242 | 31 | "database" : "config_database.xml" |
paulb@84 | 32 | } |
paulb@79 | 33 | in_page_resources = { |
paulb@113 | 34 | "cpu" : ("config_output_cpu.xsl", "cpu-node"), |
paulb@113 | 35 | "memory" : ("config_output_memory.xsl", "memory-node"), |
paulb@162 | 36 | "hard-disks" : ("config_output_harddisks.xsl", "hard-disks-node"), |
paulb@162 | 37 | "accessories" : ("config_output_accessories.xsl", "accessories-node") |
paulb@79 | 38 | } |
paulb@79 | 39 | |
paulb@82 | 40 | def respond_to_form(self, trans, form): |
paulb@30 | 41 | |
paulb@82 | 42 | """ |
paulb@82 | 43 | Respond to a request having the given transaction 'trans' and the given |
paulb@82 | 44 | 'form' information. |
paulb@82 | 45 | """ |
paulb@30 | 46 | |
paulb@113 | 47 | in_page_resource = self.get_in_page_resource(trans) |
paulb@82 | 48 | parameters = form.get_parameters() |
paulb@82 | 49 | documents = form.get_documents() |
paulb@82 | 50 | selectors = form.get_selectors() |
paulb@30 | 51 | |
paulb@30 | 52 | # Ensure the presence of a document. |
paulb@30 | 53 | |
paulb@30 | 54 | if documents.has_key("configuration"): |
paulb@30 | 55 | configuration = documents["configuration"] |
paulb@30 | 56 | else: |
paulb@82 | 57 | configuration = form.new_instance("configuration") |
paulb@30 | 58 | |
paulb@30 | 59 | # Add and remove elements according to the selectors found. |
paulb@30 | 60 | |
paulb@79 | 61 | XSLForms.Utils.add_elements(selectors.get("add-memory-unit"), "memory-unit") |
paulb@79 | 62 | XSLForms.Utils.remove_elements(selectors.get("remove-memory-unit")) |
paulb@79 | 63 | XSLForms.Utils.add_elements(selectors.get("add-storage-unit"), "storage-unit") |
paulb@79 | 64 | XSLForms.Utils.remove_elements(selectors.get("remove-storage-unit")) |
paulb@79 | 65 | XSLForms.Utils.add_elements(selectors.get("add-hard-disk"), "hard-disk") |
paulb@79 | 66 | XSLForms.Utils.remove_elements(selectors.get("remove-hard-disk")) |
paulb@30 | 67 | |
paulb@30 | 68 | # Send a response according to certain parameters. |
paulb@30 | 69 | # When exported, an XML version of the data is returned. |
paulb@30 | 70 | |
paulb@30 | 71 | if parameters.has_key("export"): |
paulb@30 | 72 | trans.set_content_type(WebStack.Generic.ContentType("text/xml", self.encoding)) |
paulb@66 | 73 | configuration.toStream(trans.get_response_stream(), trans.get_response_stream_encoding()) |
paulb@30 | 74 | |
paulb@30 | 75 | # When not exported, the data is transformed to produce a normal Web |
paulb@30 | 76 | # page. |
paulb@30 | 77 | |
paulb@30 | 78 | else: |
paulb@30 | 79 | |
paulb@30 | 80 | # Transform, adding enumerations/ranges. |
paulb@30 | 81 | |
paulb@242 | 82 | database_xsl = self.prepare_transform("database") |
paulb@242 | 83 | database_xml = self.prepare_document("database") |
paulb@242 | 84 | proc = XSLOutput.Processor(database_xsl, references={"database" : database_xml}) |
paulb@30 | 85 | configuration = proc.get_result(configuration) |
paulb@30 | 86 | |
paulb@30 | 87 | # Start the response. |
paulb@30 | 88 | |
paulb@71 | 89 | trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", self.encoding)) |
paulb@30 | 90 | |
paulb@51 | 91 | # Define the stylesheet parameters. |
paulb@51 | 92 | |
paulb@51 | 93 | stylesheet_parameters = {} |
paulb@51 | 94 | |
paulb@30 | 95 | # Ensure that an output stylesheet exists. |
paulb@30 | 96 | |
paulb@113 | 97 | if in_page_resource in self.in_page_resources.keys(): |
paulb@113 | 98 | trans_xsl = self.prepare_fragment("configuration", in_page_resource) |
paulb@63 | 99 | element_path = parameters.get("element-path", [""])[0] |
paulb@63 | 100 | stylesheet_parameters["element-path"] = element_path |
paulb@79 | 101 | else: |
paulb@84 | 102 | trans_xsl = self.prepare_output("configuration") |
paulb@84 | 103 | |
paulb@30 | 104 | # Complete the response. |
paulb@30 | 105 | |
paulb@87 | 106 | self.send_output(trans, [trans_xsl], configuration, stylesheet_parameters) |
paulb@30 | 107 | |
paulb@79 | 108 | #import sys |
paulb@79 | 109 | #proc = XSLOutput.Processor([trans_xsl], parameters=stylesheet_parameters) |
paulb@79 | 110 | #proc.send_output(sys.stderr, "iso-8859-1", configuration) |
paulb@51 | 111 | |
paulb@99 | 112 | # Site map initialisation. |
paulb@99 | 113 | |
paulb@99 | 114 | def get_site(): |
paulb@99 | 115 | |
paulb@99 | 116 | "Return a simple Web site resource." |
paulb@99 | 117 | |
paulb@99 | 118 | # Get the main resource and the directory used by the application. |
paulb@99 | 119 | |
paulb@99 | 120 | configurator_resource = ConfiguratorResource() |
paulb@99 | 121 | directory = configurator_resource.resource_dir |
paulb@99 | 122 | |
paulb@99 | 123 | # Make a simple Web site. |
paulb@99 | 124 | |
paulb@99 | 125 | resource = MapResource({ |
paulb@99 | 126 | # Static resources: |
paulb@99 | 127 | "styles" : DirectoryResource(os.path.join(directory, "styles"), {"css" : "text/css"}), |
paulb@99 | 128 | "scripts" : DirectoryResource(os.path.join(directory, "scripts"), {"js" : "text/javascript"}), |
paulb@99 | 129 | # Main page: |
paulb@99 | 130 | "" : configurator_resource, |
paulb@99 | 131 | # Fragments: |
paulb@99 | 132 | "cpu" : configurator_resource, |
paulb@99 | 133 | "memory" : configurator_resource, |
paulb@162 | 134 | "hard-disks" : configurator_resource, |
paulb@162 | 135 | "accessories" : configurator_resource |
paulb@99 | 136 | }) |
paulb@99 | 137 | |
paulb@99 | 138 | return resource |
paulb@99 | 139 | |
paulb@30 | 140 | # vim: tabstop=4 expandtab shiftwidth=4 |