XSLTools

Annotated examples/Common/PEP241/__init__.py

102:470103644a03
2005-07-16 paulb [project @ 2005-07-16 18:25:59 by paulb] Moved site map initialisation into the applications.
paulb@5 1
#!/usr/bin/env python
paulb@5 2
paulb@5 3
"A WebStack application for a PEP 241 repository."
paulb@5 4
paulb@5 5
import WebStack.Generic
paulb@88 6
import XSLForms.Resources
paulb@88 7
import XSLForms.Utils
paulb@5 8
import XSLOutput
paulb@5 9
import os
paulb@5 10
paulb@102 11
# Site map imports.
paulb@102 12
paulb@102 13
from WebStack.Resources.ResourceMap import MapResource
paulb@102 14
from WebStack.Resources.Static import DirectoryResource
paulb@102 15
paulb@102 16
# Resource classes.
paulb@102 17
paulb@88 18
class PEP241Resource(XSLForms.Resources.XSLFormsResource):
paulb@5 19
paulb@5 20
    "A resource providing repository browsing."
paulb@5 21
paulb@5 22
    resource_dir = os.path.join(os.path.split(__file__)[0], "Resources")
paulb@5 23
    encoding = "utf-8"
paulb@88 24
    template_resources = {
paulb@88 25
        "pep241" : ("pep241_template.xhtml", "pep241_output.xsl")
paulb@88 26
        }
paulb@5 27
paulb@88 28
    def respond_to_form(self, trans, form):
paulb@5 29
paulb@88 30
        """
paulb@88 31
        Respond to a request having the given transaction 'trans' and the given
paulb@88 32
        'form' information.
paulb@88 33
        """
paulb@26 34
paulb@88 35
        parameters = form.get_parameters()
paulb@88 36
        documents = form.get_documents()
paulb@88 37
        selectors = form.get_selectors()
paulb@5 38
paulb@5 39
        # Ensure the presence of a document.
paulb@5 40
paulb@5 41
        if documents.has_key("package"):
paulb@5 42
            package = documents["package"]
paulb@5 43
        else:
paulb@88 44
            package = form.new_instance("package")
paulb@5 45
paulb@26 46
        # Add and remove elements according to the selectors found.
paulb@26 47
paulb@88 48
        XSLForms.Utils.add_elements(selectors.get("add_platform"), "platform", "platforms")
paulb@88 49
        XSLForms.Utils.remove_elements(selectors.get("remove_platform"))
paulb@88 50
        XSLForms.Utils.add_elements(selectors.get("add_supported_platform"), "supported-platform", "supported-platforms")
paulb@88 51
        XSLForms.Utils.remove_elements(selectors.get("remove_supported_platform"))
paulb@88 52
        XSLForms.Utils.add_elements(selectors.get("add_keyword"), "keyword", "keywords")
paulb@88 53
        XSLForms.Utils.remove_elements(selectors.get("remove_keyword"))
paulb@88 54
        XSLForms.Utils.add_elements(selectors.get("add_author"), "author", "authors")
paulb@88 55
        XSLForms.Utils.remove_elements(selectors.get("remove_author"))
paulb@88 56
        XSLForms.Utils.add_elements(selectors.get("add_dependency"), "dependency", "dependencies")
paulb@88 57
        XSLForms.Utils.remove_elements(selectors.get("remove_dependency"))
paulb@5 58
paulb@26 59
        # Send a response according to certain parameters.
paulb@26 60
        # When exported, an XML version of the data is returned.
paulb@26 61
paulb@26 62
        if parameters.has_key("export"):
paulb@26 63
            trans.set_content_type(WebStack.Generic.ContentType("text/xml", self.encoding))
paulb@88 64
            package.toStream(trans.get_response_stream(), trans.get_response_stream_encoding())
paulb@18 65
paulb@26 66
        # When not exported, the data is transformed to produce a normal Web
paulb@26 67
        # page.
paulb@26 68
paulb@26 69
        else:
paulb@18 70
paulb@26 71
            # Transform, adding enumerations/ranges.
paulb@5 72
paulb@26 73
            categories_xsl = os.path.join(self.resource_dir, "pep241_categories.xsl")
paulb@26 74
            categories_xml = os.path.join(self.resource_dir, "pep241_categories.xml")
paulb@26 75
            proc = XSLOutput.Processor([categories_xsl], {"categories" : categories_xml})
paulb@26 76
            package = proc.get_result(package)
paulb@5 77
paulb@26 78
            # Start the response.
paulb@26 79
paulb@71 80
            trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", self.encoding))
paulb@5 81
paulb@26 82
            # Ensure that an output stylesheet exists.
paulb@5 83
paulb@88 84
            trans_xsl = self.prepare_output("pep241")
paulb@26 85
paulb@26 86
            # Complete the response.
paulb@26 87
paulb@88 88
            self.send_output(trans, [trans_xsl], package)
paulb@5 89
paulb@102 90
# Site map initialisation.
paulb@102 91
paulb@102 92
def get_site():
paulb@102 93
paulb@102 94
    "Return a simple Web site resource."
paulb@102 95
paulb@102 96
    # Get the main resource and the directory used by the application.
paulb@102 97
paulb@102 98
    pep241_resource = PEP241Resource()
paulb@102 99
    directory = pep241_resource.resource_dir
paulb@102 100
paulb@102 101
    # Make a simple Web site.
paulb@102 102
paulb@102 103
    resource = MapResource({
paulb@102 104
        # Static resources:
paulb@102 105
        "styles" : DirectoryResource(os.path.join(directory, "styles"), {"css" : "text/css"}),
paulb@102 106
        # Main page:
paulb@102 107
        "" : pep241_resource
paulb@102 108
        })
paulb@102 109
paulb@102 110
    return resource
paulb@102 111
paulb@5 112
# vim: tabstop=4 expandtab shiftwidth=4