1.1 --- a/examples/Common/PEP241/__init__.py Thu Jul 14 22:53:44 2005 +0000
1.2 +++ b/examples/Common/PEP241/__init__.py Thu Jul 14 22:54:04 2005 +0000
1.3 @@ -3,98 +3,58 @@
1.4 "A WebStack application for a PEP 241 repository."
1.5
1.6 import WebStack.Generic
1.7 -import XSLForms.Fields
1.8 -import XSLForms.Output
1.9 +import XSLForms.Resources
1.10 +import XSLForms.Utils
1.11 import XSLOutput
1.12 -import libxml2dom
1.13 import os
1.14
1.15 -def add_elements(positions, element_name, element_parent_name=None):
1.16 - if not positions:
1.17 - return
1.18 - for position in positions:
1.19 - if element_parent_name:
1.20 - parent_elements = position.xpath(element_parent_name)
1.21 - if not parent_elements:
1.22 - parent_element = position.ownerDocument.createElementNS(None, element_parent_name)
1.23 - position.appendChild(parent_element)
1.24 - else:
1.25 - parent_element = parent_elements[0]
1.26 - else:
1.27 - parent_element = position
1.28 - parent_element.appendChild(position.ownerDocument.createElementNS(None, element_name))
1.29 -
1.30 -def remove_elements(positions):
1.31 - if not positions:
1.32 - return
1.33 - for position in positions:
1.34 - position.parentNode.removeChild(position)
1.35 -
1.36 -class PEP241Resource:
1.37 +class PEP241Resource(XSLForms.Resources.XSLFormsResource):
1.38
1.39 "A resource providing repository browsing."
1.40
1.41 resource_dir = os.path.join(os.path.split(__file__)[0], "Resources")
1.42 encoding = "utf-8"
1.43 -
1.44 - def respond(self, trans):
1.45 -
1.46 - # Produce stylesheet.
1.47 + template_resources = {
1.48 + "pep241" : ("pep241_template.xhtml", "pep241_output.xsl")
1.49 + }
1.50
1.51 - if trans.get_path_info().split("/")[-1] == "styles.css":
1.52 - trans.set_content_type(WebStack.Generic.ContentType("text/css", self.encoding))
1.53 - out = trans.get_response_stream()
1.54 - f = open(os.path.join(self.resource_dir, "styles.css"), "rb")
1.55 - out.write(f.read())
1.56 - f.close()
1.57 - raise WebStack.Generic.EndOfResponse
1.58 -
1.59 - # Handle other requests.
1.60 + def respond_to_form(self, trans, form):
1.61
1.62 - global XSLForms # NOTE: Strangely required to avoid UnboundLocalError!
1.63 - method = trans.get_request_method()
1.64 -
1.65 - # Only obtain field information according to the stated method.
1.66 + """
1.67 + Respond to a request having the given transaction 'trans' and the given
1.68 + 'form' information.
1.69 + """
1.70
1.71 - if method == "GET":
1.72 - fields = XSLForms.Fields.Fields(encoding="iso-8859-1", values_are_lists=1)
1.73 - parameters = trans.get_fields_from_path()
1.74 - documents = fields.make_documents(parameters.items())
1.75 - elif method == "POST":
1.76 - fields = XSLForms.Fields.Fields(encoding=self.encoding, values_are_lists=1)
1.77 - parameters = trans.get_fields_from_body(self.encoding)
1.78 - documents = fields.make_documents(parameters.items())
1.79 - else:
1.80 - trans.set_response_code(405)
1.81 - raise WebStack.Generic.EndOfResponse
1.82 + parameters = form.get_parameters()
1.83 + documents = form.get_documents()
1.84 + selectors = form.get_selectors()
1.85
1.86 # Ensure the presence of a document.
1.87
1.88 if documents.has_key("package"):
1.89 package = documents["package"]
1.90 else:
1.91 - package = fields.new_instance("package")
1.92 + package = form.new_instance("package")
1.93
1.94 # Add and remove elements according to the selectors found.
1.95
1.96 - selectors = fields.get_selectors(parameters.items(), documents)
1.97 - add_elements(selectors.get("add_platform"), "platform", "platforms")
1.98 - remove_elements(selectors.get("remove_platform"))
1.99 - add_elements(selectors.get("add_supported_platform"), "supported-platform", "supported-platforms")
1.100 - remove_elements(selectors.get("remove_supported_platform"))
1.101 - add_elements(selectors.get("add_keyword"), "keyword", "keywords")
1.102 - remove_elements(selectors.get("remove_keyword"))
1.103 - add_elements(selectors.get("add_author"), "author", "authors")
1.104 - remove_elements(selectors.get("remove_author"))
1.105 - add_elements(selectors.get("add_dependency"), "dependency", "dependencies")
1.106 - remove_elements(selectors.get("remove_dependency"))
1.107 + XSLForms.Utils.add_elements(selectors.get("add_platform"), "platform", "platforms")
1.108 + XSLForms.Utils.remove_elements(selectors.get("remove_platform"))
1.109 + XSLForms.Utils.add_elements(selectors.get("add_supported_platform"), "supported-platform", "supported-platforms")
1.110 + XSLForms.Utils.remove_elements(selectors.get("remove_supported_platform"))
1.111 + XSLForms.Utils.add_elements(selectors.get("add_keyword"), "keyword", "keywords")
1.112 + XSLForms.Utils.remove_elements(selectors.get("remove_keyword"))
1.113 + XSLForms.Utils.add_elements(selectors.get("add_author"), "author", "authors")
1.114 + XSLForms.Utils.remove_elements(selectors.get("remove_author"))
1.115 + XSLForms.Utils.add_elements(selectors.get("add_dependency"), "dependency", "dependencies")
1.116 + XSLForms.Utils.remove_elements(selectors.get("remove_dependency"))
1.117
1.118 # Send a response according to certain parameters.
1.119 # When exported, an XML version of the data is returned.
1.120
1.121 if parameters.has_key("export"):
1.122 trans.set_content_type(WebStack.Generic.ContentType("text/xml", self.encoding))
1.123 - libxml2dom.toStream(package, trans.get_response_stream(), trans.get_response_stream_encoding())
1.124 + package.toStream(trans.get_response_stream(), trans.get_response_stream_encoding())
1.125
1.126 # When not exported, the data is transformed to produce a normal Web
1.127 # page.
1.128 @@ -114,20 +74,10 @@
1.129
1.130 # Ensure that an output stylesheet exists.
1.131
1.132 - trans_xsl = os.path.join(self.resource_dir, "pep241_output.xsl")
1.133 - template_xml = os.path.join(self.resource_dir, "pep241_template.xhtml")
1.134 -
1.135 - if not os.path.exists(trans_xsl) or \
1.136 - os.path.getmtime(trans_xsl) < os.path.getmtime(template_xml):
1.137 -
1.138 - import XSLForms.Prepare
1.139 - template_xml = os.path.join(self.resource_dir, "pep241_template.xhtml")
1.140 - XSLForms.Prepare.make_stylesheet(template_xml, trans_xsl)
1.141 + trans_xsl = self.prepare_output("pep241")
1.142
1.143 # Complete the response.
1.144
1.145 - proc = XSLOutput.Processor([trans_xsl])
1.146 - proc.send_output(trans.get_response_stream(), trans.get_response_stream_encoding(),
1.147 - package)
1.148 + self.send_output(trans, [trans_xsl], package)
1.149
1.150 # vim: tabstop=4 expandtab shiftwidth=4