1 #!/usr/bin/env python 2 3 "A WebStack application for a system configurator." 4 5 import WebStack.Generic 6 import XSLForms.Fields 7 import XSLForms.Output 8 import XSLOutput 9 import libxml2dom 10 import os 11 12 # NOTE: Move these functions into a common utility library. 13 14 def add_elements(positions, element_name, element_parent_name=None): 15 if not positions: 16 return 17 for position in positions: 18 if element_parent_name: 19 parent_elements = position.xpath(element_parent_name) 20 if not parent_elements: 21 parent_element = position.ownerDocument.createElementNS(None, element_parent_name) 22 position.appendChild(parent_element) 23 else: 24 parent_element = parent_elements[0] 25 else: 26 parent_element = position 27 parent_element.appendChild(position.ownerDocument.createElementNS(None, element_name)) 28 29 def remove_elements(positions): 30 if not positions: 31 return 32 for position in positions: 33 position.parentNode.removeChild(position) 34 35 class ConfiguratorResource: 36 37 "A resource providing a system configurator." 38 39 resource_dir = os.path.join(os.path.split(__file__)[0], "Resources") 40 encoding = "utf-8" 41 42 def respond(self, trans): 43 44 global XSLForms # NOTE: Strangely required to avoid UnboundLocalError! 45 46 # Only obtain field information according to the stated method. 47 48 method = trans.get_request_method() 49 path_info = trans.get_path_info() 50 51 if method == "GET": 52 fields = XSLForms.Fields.Fields(encoding="iso-8859-1", values_are_lists=1) 53 parameters = trans.get_fields_from_path() 54 documents = fields.make_documents(parameters.items()) 55 elif method == "POST": 56 fields = XSLForms.Fields.Fields(encoding=self.encoding, values_are_lists=1) 57 if path_info == "/cpu": 58 text = trans.get_request_stream().read() 59 text_parts = text.split("=") 60 text_name, text_values = text_parts[0], ["=".join(text_parts[1:])] 61 parameters = {text_name : text_values} 62 else: 63 parameters = trans.get_fields_from_body(self.encoding) 64 documents = fields.make_documents(parameters.items()) 65 print "*", libxml2dom.toString(documents["configuration"]) 66 else: 67 trans.set_response_code(405) 68 raise WebStack.Generic.EndOfResponse 69 70 # Ensure the presence of a document. 71 72 if documents.has_key("configuration"): 73 configuration = documents["configuration"] 74 else: 75 configuration = fields.new_instance("configuration") 76 77 # Add and remove elements according to the selectors found. 78 79 selectors = fields.get_selectors(parameters.items(), documents) 80 add_elements(selectors.get("add-memory-unit"), "memory-unit") 81 remove_elements(selectors.get("remove-memory-unit")) 82 add_elements(selectors.get("add-storage-unit"), "storage-unit") 83 remove_elements(selectors.get("remove-storage-unit")) 84 add_elements(selectors.get("add-hard-disk"), "hard-disk") 85 remove_elements(selectors.get("remove-hard-disk")) 86 87 # Send a response according to certain parameters. 88 # When exported, an XML version of the data is returned. 89 90 if parameters.has_key("export"): 91 trans.set_content_type(WebStack.Generic.ContentType("text/xml", self.encoding)) 92 libxml2dom.toStream(configuration, trans.get_response_stream(), trans.get_response_stream_encoding()) 93 94 # When not exported, the data is transformed to produce a normal Web 95 # page. 96 97 else: 98 99 # Transform, adding enumerations/ranges. 100 101 database_xsl = os.path.join(self.resource_dir, "config_database.xsl") 102 database_xml = os.path.join(self.resource_dir, "config_database.xml") 103 proc = XSLOutput.Processor([database_xsl], references={"database" : database_xml}) 104 configuration = proc.get_result(configuration) 105 106 # Start the response. 107 108 trans.set_content_type(WebStack.Generic.ContentType("text/html", self.encoding)) 109 110 # Ensure that an output stylesheet exists. 111 112 trans_xsl = os.path.join(self.resource_dir, "config_output.xsl") 113 template_xml = os.path.join(self.resource_dir, "config_template.xhtml") 114 115 if not os.path.exists(trans_xsl) or \ 116 os.path.getmtime(trans_xsl) < os.path.getmtime(template_xml): 117 118 import XSLForms.Prepare 119 template_xml = os.path.join(self.resource_dir, "config_template.xhtml") 120 XSLForms.Prepare.make_stylesheet(template_xml, trans_xsl) 121 122 # Define the parameters. 123 124 parameters = { 125 "application-url" : "http://%s:%s%s" % (trans.get_server_name(), trans.get_server_port(), trans.get_path_without_query()) 126 } 127 128 # Complete the response. 129 130 proc = XSLOutput.Processor([trans_xsl], parameters=parameters) 131 proc.send_output(trans.get_response_stream(), trans.get_response_stream_encoding(), 132 configuration) 133 134 # vim: tabstop=4 expandtab shiftwidth=4