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