summaryrefslogtreecommitdiff
path: root/docs/tutorials/lxmlgraph/step04
diff options
context:
space:
mode:
authorPaul Everitt <paul@agendaless.com>2008-07-24 17:14:16 +0000
committerPaul Everitt <paul@agendaless.com>2008-07-24 17:14:16 +0000
commit1a7b14de4bc89ef0fd162ef8e62d01926e42b54e (patch)
tree8d9ccc8187ac29eb80ac310c6f82c17d548cdf54 /docs/tutorials/lxmlgraph/step04
parentc1a6ef6f751a16b7a352bc3fdad13d4b5b73f212 (diff)
downloadpyramid-1a7b14de4bc89ef0fd162ef8e62d01926e42b54e.tar.gz
pyramid-1a7b14de4bc89ef0fd162ef8e62d01926e42b54e.tar.bz2
pyramid-1a7b14de4bc89ef0fd162ef8e62d01926e42b54e.zip
Add tutorial sections
Diffstat (limited to 'docs/tutorials/lxmlgraph/step04')
-rw-r--r--docs/tutorials/lxmlgraph/step04/myapp/__init__.py1
-rw-r--r--docs/tutorials/lxmlgraph/step04/myapp/configure.zcml11
-rw-r--r--docs/tutorials/lxmlgraph/step04/myapp/models.py41
-rw-r--r--docs/tutorials/lxmlgraph/step04/myapp/samplemodel.xml44
-rw-r--r--docs/tutorials/lxmlgraph/step04/myapp/views.py13
-rw-r--r--docs/tutorials/lxmlgraph/step04/myapp/xsltview.xsl69
-rw-r--r--docs/tutorials/lxmlgraph/step04/run.py8
7 files changed, 187 insertions, 0 deletions
diff --git a/docs/tutorials/lxmlgraph/step04/myapp/__init__.py b/docs/tutorials/lxmlgraph/step04/myapp/__init__.py
new file mode 100644
index 000000000..792d60054
--- /dev/null
+++ b/docs/tutorials/lxmlgraph/step04/myapp/__init__.py
@@ -0,0 +1 @@
+#
diff --git a/docs/tutorials/lxmlgraph/step04/myapp/configure.zcml b/docs/tutorials/lxmlgraph/step04/myapp/configure.zcml
new file mode 100644
index 000000000..1ba4c9155
--- /dev/null
+++ b/docs/tutorials/lxmlgraph/step04/myapp/configure.zcml
@@ -0,0 +1,11 @@
+<configure xmlns="http://namespaces.zope.org/zope"
+ xmlns:bfg="http://namespaces.repoze.org/bfg">
+
+ <include package="repoze.bfg" />
+
+ <bfg:view
+ for=".models.IMyModel"
+ view=".views.xslt_view"
+ />
+
+</configure>
diff --git a/docs/tutorials/lxmlgraph/step04/myapp/models.py b/docs/tutorials/lxmlgraph/step04/myapp/models.py
new file mode 100644
index 000000000..3c03de1a9
--- /dev/null
+++ b/docs/tutorials/lxmlgraph/step04/myapp/models.py
@@ -0,0 +1,41 @@
+from zope.interface import implements
+from zope.interface import Attribute
+from zope.interface import Interface
+from lxml import etree
+
+class IMyModel(Interface):
+ __name__ = Attribute('Name of the model instance')
+
+class BfgElement(etree.ElementBase):
+ """Handle access control and getitem behavior"""
+
+ implements(IMyModel)
+
+ @property
+ def __name__(self):
+ return self.xpath("@name")[0]
+
+ def __getitem__(self, child_name):
+ xp = "*[@name='%s']" % child_name
+ matches = self.xpath(xp)
+ if len(matches) == 0:
+ raise KeyError('No child found for %s' % child_name)
+ elif len(matches) > 1:
+ raise KeyError('More than one child for %s' % child_name)
+ else:
+ return matches[0]
+
+def get_root(environ):
+ # Setup the custom parser with our BfgElement behavior
+ parser_lookup = etree.ElementDefaultClassLookup(element=BfgElement)
+ parser = etree.XMLParser()
+ parser.set_element_class_lookup(parser_lookup)
+
+ # Now load the XML file
+ xmlstring = open("myapp/samplemodel.xml").read()
+ root = etree.XML(xmlstring, parser)
+
+ return root
+
+
+
diff --git a/docs/tutorials/lxmlgraph/step04/myapp/samplemodel.xml b/docs/tutorials/lxmlgraph/step04/myapp/samplemodel.xml
new file mode 100644
index 000000000..36ab335d2
--- /dev/null
+++ b/docs/tutorials/lxmlgraph/step04/myapp/samplemodel.xml
@@ -0,0 +1,44 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<site>
+ <title>My XMLGRAPH Website</title>
+ <document xml:id="index" name="index.html">
+ <title>Site Home Page</title>
+ <body>
+ <div xmlns="http://www.w3.org/1999/xhtml">
+ <p>Welcome to the site. We have lots to say.</p>
+ <p>Or, <em>maybe</em> not.</p>
+ </div>
+ </body>
+ </document>
+ <folder xml:id="n1" name="folder1">
+ <title>Folder One</title>
+ <document xml:id="n11" name="doc1">
+ <title>doc1 in folder1</title>
+ <body>
+ <div xmlns="http://www.w3.org/1999/xhtml">
+ <p>I am in an HTML <code>div</code> so I can do <strong>LOTS</strong> of
+ formatting.</p>
+ </div>
+ </body>
+ </document>
+ <document xml:id="n12" name="doc2">
+ <title>doc2 in folder1</title>
+ <body>
+ <div xmlns="http://www.w3.org/1999/xhtml">
+ <p>Keep this on one line.</p>
+ </div>
+ </body>
+ </document>
+ </folder>
+ <folder xml:id="n2" name="folder2">
+ <title>The Second Folder</title>
+ <document xml:id="n21" name="doc1">
+ <title>doc1 in folder2</title>
+ <body>
+ <div xmlns="http://www.w3.org/1999/xhtml">
+ <p>This is a special folder. It's folder 2!</p>
+ </div>
+ </body>
+ </document>
+ </folder>
+</site>
diff --git a/docs/tutorials/lxmlgraph/step04/myapp/views.py b/docs/tutorials/lxmlgraph/step04/myapp/views.py
new file mode 100644
index 000000000..fd8650e14
--- /dev/null
+++ b/docs/tutorials/lxmlgraph/step04/myapp/views.py
@@ -0,0 +1,13 @@
+from repoze.bfg.template import render_transform_to_response
+
+# Some constants
+XML_NAMESPACE='http://www.w3.org/XML/1998/namespace'
+XML_PREFIX= '{%s}' % XML_NAMESPACE
+
+def xslt_view(context, request):
+ # Grab the root of the tree, which should be a <site>
+ site = context.getroottree().getroot()
+ # Jot down which node we're sitting on as the <context>
+ contextid = "'%s'" % context.get(XML_PREFIX+'id')
+ return render_transform_to_response("xsltview.xsl", site,
+ contextid=contextid)
diff --git a/docs/tutorials/lxmlgraph/step04/myapp/xsltview.xsl b/docs/tutorials/lxmlgraph/step04/myapp/xsltview.xsl
new file mode 100644
index 000000000..2406987d0
--- /dev/null
+++ b/docs/tutorials/lxmlgraph/step04/myapp/xsltview.xsl
@@ -0,0 +1,69 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
+ <xsl:param name="contextid">n1</xsl:param>
+ <xsl:variable name="contextnode" select="id($contextid)"/>
+ <xsl:template match="/">
+ <html>
+ <head>
+ <title>
+ <xsl:value-of select="$contextnode/title"/>
+ </title>
+ </head>
+ <body>
+ <h2>
+ <xsl:value-of select="$contextnode/title"/>
+ </h2>
+ <xsl:apply-templates select="$contextnode"/>
+ <table border="1" cellpadding="6" cellspacing="0">
+ <tr>
+ <th>Type</th>
+ <th>@xml:id</th>
+ <th>@name</th>
+ <th>Parent Type</th>
+ <th>Parent @name</th>
+ </tr>
+ <tr>
+ <td>
+ <xsl:value-of select="name($contextnode)"/>
+ </td>
+ <td>
+ <xsl:value-of select="$contextnode/@xml:id"/>
+ </td>
+ <td>
+ <xsl:value-of select="$contextnode/@name"/>
+ </td>
+ <td>
+ <xsl:value-of select="name($contextnode/..)"/>
+ </td>
+ <td>
+ <xsl:value-of select="$contextnode/../@name"/>
+ </td>
+ </tr>
+ </table>
+ </body>
+ </html>
+ </xsl:template>
+ <xsl:template match="folder">
+ <p>
+ <em>Folders are special, they contain things.</em>
+ </p>
+ <xsl:if test="*[@xml:id]">
+ <h2>Folder Contents</h2>
+ <ul>
+ <xsl:for-each select="*[@xml:id]">
+ <li>
+ <a href="{../@name}/{@name}">
+ <xsl:value-of select="title"/>
+ </a>
+ </li>
+ </xsl:for-each>
+ </ul>
+ </xsl:if>
+ </xsl:template>
+ <xsl:template match="document">
+ <p>
+ <em>Documents contain text.</em>
+ </p>
+ <xsl:copy-of select="body/*"/>
+ </xsl:template>
+</xsl:stylesheet>
diff --git a/docs/tutorials/lxmlgraph/step04/run.py b/docs/tutorials/lxmlgraph/step04/run.py
new file mode 100644
index 000000000..1eac209dc
--- /dev/null
+++ b/docs/tutorials/lxmlgraph/step04/run.py
@@ -0,0 +1,8 @@
+from paste import httpserver
+
+from repoze.bfg import make_app
+from myapp.models import get_root
+import myapp
+
+app = make_app(get_root, myapp)
+httpserver.serve(app, host='0.0.0.0', port='5432')