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