1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/examples/Common/Configurator/__init__.py Thu Dec 30 02:03:35 2004 +0000
1.3 @@ -0,0 +1,129 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"A WebStack application for a system configurator."
1.7 +
1.8 +import WebStack.Generic
1.9 +import XSLForms.Fields
1.10 +import XSLForms.Output
1.11 +import XSLOutput
1.12 +import libxml2dom
1.13 +import os
1.14 +
1.15 +# NOTE: Move these functions into a common utility library.
1.16 +
1.17 +def add_elements(positions, element_name, element_parent_name=None):
1.18 + if not positions:
1.19 + return
1.20 + for position in positions:
1.21 + if element_parent_name:
1.22 + parent_elements = position.xpath(element_parent_name)
1.23 + if not parent_elements:
1.24 + parent_element = position.ownerDocument.createElementNS(None, element_parent_name)
1.25 + position.appendChild(parent_element)
1.26 + else:
1.27 + parent_element = parent_elements[0]
1.28 + else:
1.29 + parent_element = position
1.30 + parent_element.appendChild(position.ownerDocument.createElementNS(None, element_name))
1.31 +
1.32 +def remove_elements(positions):
1.33 + if not positions:
1.34 + return
1.35 + for position in positions:
1.36 + position.parentNode.removeChild(position)
1.37 +
1.38 +class ConfiguratorResource:
1.39 +
1.40 + "A resource providing a system configurator."
1.41 +
1.42 + resource_dir = os.path.join(os.path.split(__file__)[0], "Resources")
1.43 + encoding = "utf-8"
1.44 +
1.45 + def respond(self, trans):
1.46 +
1.47 + # Produce stylesheet.
1.48 +
1.49 + if trans.get_path_info().split("/")[-1] == "styles.css":
1.50 + trans.set_content_type(WebStack.Generic.ContentType("text/css", self.encoding))
1.51 + out = trans.get_response_stream()
1.52 + f = open(os.path.join(self.resource_dir, "styles.css"), "rb")
1.53 + out.write(f.read())
1.54 + f.close()
1.55 + raise WebStack.Generic.EndOfResponse
1.56 +
1.57 + global XSLForms # NOTE: Strangely required to avoid UnboundLocalError!
1.58 + method = trans.get_request_method()
1.59 +
1.60 + # Only obtain field information according to the stated method.
1.61 +
1.62 + if method == "GET":
1.63 + fields = XSLForms.Fields.Fields(encoding="iso-8859-1", values_are_lists=1)
1.64 + parameters = trans.get_fields_from_path()
1.65 + documents = fields.make_documents(parameters.items())
1.66 + elif method == "POST":
1.67 + fields = XSLForms.Fields.Fields(encoding=self.encoding, values_are_lists=1)
1.68 + parameters = trans.get_fields_from_body(self.encoding)
1.69 + documents = fields.make_documents(parameters.items())
1.70 + else:
1.71 + trans.set_response_code(405)
1.72 + raise WebStack.Generic.EndOfResponse
1.73 +
1.74 + # Ensure the presence of a document.
1.75 +
1.76 + if documents.has_key("configuration"):
1.77 + configuration = documents["configuration"]
1.78 + else:
1.79 + configuration = fields.new_instance("configuration")
1.80 +
1.81 + # Add and remove elements according to the selectors found.
1.82 +
1.83 + selectors = fields.get_selectors(parameters.items(), documents)
1.84 + add_elements(selectors.get("add-memory-unit"), "memory-unit")
1.85 + remove_elements(selectors.get("remove-memory-unit"))
1.86 + add_elements(selectors.get("add-storage-unit"), "storage-unit")
1.87 + remove_elements(selectors.get("remove-storage-unit"))
1.88 + add_elements(selectors.get("add-hard-disk"), "hard-disk")
1.89 + remove_elements(selectors.get("remove-hard-disk"))
1.90 +
1.91 + # Send a response according to certain parameters.
1.92 + # When exported, an XML version of the data is returned.
1.93 +
1.94 + if parameters.has_key("export"):
1.95 + trans.set_content_type(WebStack.Generic.ContentType("text/xml", self.encoding))
1.96 + libxml2dom.toStream(configuration, trans.get_response_stream(), trans.get_response_stream_encoding())
1.97 +
1.98 + # When not exported, the data is transformed to produce a normal Web
1.99 + # page.
1.100 +
1.101 + else:
1.102 +
1.103 + # Transform, adding enumerations/ranges.
1.104 +
1.105 + database_xsl = os.path.join(self.resource_dir, "config_database.xsl")
1.106 + database_xml = os.path.join(self.resource_dir, "config_database.xml")
1.107 + proc = XSLOutput.Processor([database_xsl], {"database" : database_xml})
1.108 + configuration = proc.get_result(configuration)
1.109 +
1.110 + # Start the response.
1.111 +
1.112 + trans.set_content_type(WebStack.Generic.ContentType("text/html", self.encoding))
1.113 +
1.114 + # Ensure that an output stylesheet exists.
1.115 +
1.116 + trans_xsl = os.path.join(self.resource_dir, "config_output.xsl")
1.117 + template_xml = os.path.join(self.resource_dir, "config_template.xhtml")
1.118 +
1.119 + if not os.path.exists(trans_xsl) or \
1.120 + os.path.getmtime(trans_xsl) < os.path.getmtime(template_xml):
1.121 +
1.122 + import XSLForms.Prepare
1.123 + template_xml = os.path.join(self.resource_dir, "config_template.xhtml")
1.124 + XSLForms.Prepare.make_stylesheet(template_xml, trans_xsl)
1.125 +
1.126 + # Complete the response.
1.127 +
1.128 + proc = XSLOutput.Processor([trans_xsl])
1.129 + proc.send_output(trans.get_response_stream(), trans.get_response_stream_encoding(),
1.130 + configuration)
1.131 +
1.132 +# vim: tabstop=4 expandtab shiftwidth=4