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