From 1a7b14de4bc89ef0fd162ef8e62d01926e42b54e Mon Sep 17 00:00:00 2001 From: Paul Everitt Date: Thu, 24 Jul 2008 17:14:16 +0000 Subject: Add tutorial sections --- docs/tutorials/lxmlgraph/step02/myapp/__init__.py | 1 + .../lxmlgraph/step02/myapp/configure.zcml | 11 ++++++ docs/tutorials/lxmlgraph/step02/myapp/models.py | 41 ++++++++++++++++++++++ .../lxmlgraph/step02/myapp/samplemodel.xml | 5 +++ docs/tutorials/lxmlgraph/step02/myapp/views.py | 8 +++++ docs/tutorials/lxmlgraph/step02/run.py | 8 +++++ 6 files changed, 74 insertions(+) create mode 100644 docs/tutorials/lxmlgraph/step02/myapp/__init__.py create mode 100644 docs/tutorials/lxmlgraph/step02/myapp/configure.zcml create mode 100644 docs/tutorials/lxmlgraph/step02/myapp/models.py create mode 100644 docs/tutorials/lxmlgraph/step02/myapp/samplemodel.xml create mode 100644 docs/tutorials/lxmlgraph/step02/myapp/views.py create mode 100644 docs/tutorials/lxmlgraph/step02/run.py (limited to 'docs/tutorials/lxmlgraph/step02') diff --git a/docs/tutorials/lxmlgraph/step02/myapp/__init__.py b/docs/tutorials/lxmlgraph/step02/myapp/__init__.py new file mode 100644 index 000000000..792d60054 --- /dev/null +++ b/docs/tutorials/lxmlgraph/step02/myapp/__init__.py @@ -0,0 +1 @@ +# diff --git a/docs/tutorials/lxmlgraph/step02/myapp/configure.zcml b/docs/tutorials/lxmlgraph/step02/myapp/configure.zcml new file mode 100644 index 000000000..540526ec9 --- /dev/null +++ b/docs/tutorials/lxmlgraph/step02/myapp/configure.zcml @@ -0,0 +1,11 @@ + + + + + + + diff --git a/docs/tutorials/lxmlgraph/step02/myapp/models.py b/docs/tutorials/lxmlgraph/step02/myapp/models.py new file mode 100644 index 000000000..3c03de1a9 --- /dev/null +++ b/docs/tutorials/lxmlgraph/step02/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/step02/myapp/samplemodel.xml b/docs/tutorials/lxmlgraph/step02/myapp/samplemodel.xml new file mode 100644 index 000000000..83678e5e9 --- /dev/null +++ b/docs/tutorials/lxmlgraph/step02/myapp/samplemodel.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/docs/tutorials/lxmlgraph/step02/myapp/views.py b/docs/tutorials/lxmlgraph/step02/myapp/views.py new file mode 100644 index 000000000..205b4b163 --- /dev/null +++ b/docs/tutorials/lxmlgraph/step02/myapp/views.py @@ -0,0 +1,8 @@ +from webob import Response + +def my_hello_view(context, request): + response = Response('Hello to %s from %s @ %s' % ( + context.tag, + context.__name__, + request.environ['PATH_INFO'])) + return response diff --git a/docs/tutorials/lxmlgraph/step02/run.py b/docs/tutorials/lxmlgraph/step02/run.py new file mode 100644 index 000000000..1eac209dc --- /dev/null +++ b/docs/tutorials/lxmlgraph/step02/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') -- cgit v1.2.3