Nevow-traversal (转)

1Nevow Object Traversal
2======================
3
4*Object traversal* is the process Nevow uses to determine what object to use to
5render HTML for a particular URL. When an HTTP request comes in to the web
6server, the object publisher splits the URL into segments, and repeatedly calls
7methods which consume path segments and return objects which represent that
8path, until all segments have been consumed. At the core, the Nevow traversal
9API is very simple. However, it provides some higher level functionality layered
10on top of this to satisfy common use cases.
11
12* `Object Traversal Basics`_
13* `locateChild in depth`_
14* `childFactory method`_
15* `child_* methods and attributes`_
16* `Dots in child names`_
17* `children dictionary`_
18* `The default trailing slash handler`_
19* `ICurrentSegments and IRemainingSegments`_
20
21Object Traversal Basics
22-----------------------
23
24The *root resource* is the top-level object in the URL space; it conceptually
25represents the URI "/". The Nevow *object traversal* and *object publishing*
26machinery uses only two methods to locate an object suitable for publishing and
27to generate the HTML from it; these methods are described in the interface
28``nevow.inevow.IResource``::
29
30
31  class IResource(compy.Interface):
32      def locateChild(self, ctx, segments):
33          """Locate another object which can be adapted to IResource
34          Return a tuple of resource, path segments
35          """
36
37      def renderHTTP(self, ctx):
38          """Render a request
39          """
40
41``renderHTTP`` can be as simple as a method which simply returns a string of HTML.
42Let's examine what happens when object traversal occurs over a very simple root
43resource::
44
45  from zope.interface import implements
46
47  class SimpleRoot(object):
48      implements(inevow.IResource)
49
50      def locateChild(self, ctx, segments):
51          return self, ()
52
53      def renderHTTP(self, ctx):
54          return "Hello, world!"
55
56This resource, when passed as the root resource to ``appserver.NevowSite`` or
57``wsgi.createWSGIApplication``, will immediately return itself, consuming all path
58segments. This means that for every URI a user visits on a web server which is
59serving this root resource, the text "Hello, world!" will be rendered. Let's
60examine the value of ``segments`` for various values of URI:
61
62/foo/bar
63  ('foo', 'bar')
64
65/
66  ('', )
67
68/foo/bar/baz.html
69  ('foo', 'bar', 'baz.html')
70
71/foo/bar/directory/
72  ('foo', 'bar', 'directory', '')
73
74So we see that Nevow does nothing more than split the URI on the string '/' and
75pass these path segments to our application for consumption. Armed with these
76two methods alone, we already have enough information to write applications
77which service any form of URL imaginable in any way we wish. However, there are
78some common URL handling patterns which Nevow provides higher level support for.
79
80``locateChild`` in depth
81------------------------
82
83One common URL handling pattern involves parents which only know about their
84direct children. For example, a ``Directory`` object may only know about the
85contents of a single directory, but if it contains other directories, it does
86not know about the contents of them. Let's examine a simple ``Directory`` object
87which can provide directory listings and serves up objects for child directories
88and files::
89
90  from zope.interface import implements           
91
92  class Directory(object):
93      implements(inevow.IResource)
94
95      def __init__(self, directory):
96          self.directory = directory
97
98      def renderHTTP(self, ctx):
99          html = ['<ul>']
100          for child in os.listdir(self.directory):
101              fullpath = os.path.join(self.directory, child)
102              if os.path.isdir(fullpath):
103                  child += '/'
104              html.extend(['<li><a href="', child, '">', child, '</a></li>'])
105          html.append('</ul>')
106          return ''.join(html)
107
108      def locateChild(self, ctx, segments):
109          name = segments[0]
110          fullpath = os.path.join(self.directory, name)
111          if not os.path.exists(fullpath):
112              return None, () # 404
113
114          if os.path.isdir(fullpath):
115              return Directory(fullpath), segments[1:]
116          if os.path.isfile(fullpath):
117              return static.File(fullpath), segments[1:]
118
119Because this implementation of ``locateChild`` only consumed one segment and
120returned the rest of them (``segments[1:]``), the object traversal process will
121continue by calling ``locateChild`` on the returned resource and passing the
122partially-consumed segments. In this way, a directory structure of any depth can
123be traversed, and directory listings or file contents can be rendered for any
124existing directories and files.
125
126So, let us examine what happens when the URI "/foo/bar/baz.html" is traversed,
127where "foo" and "bar" are directories, and "baz.html" is a file.
128
129Directory('/').locateChild(ctx, ('foo', 'bar', 'baz.html'))
130    Returns Directory('/foo'), ('bar', 'baz.html')
131
132Directory('/foo').locateChild(ctx, ('bar', 'baz.html'))
133    Returns Directory('/foo/bar'), ('baz.html, )
134
135Directory('/foo/bar').locateChild(ctx, ('baz.html'))
136    Returns File('/foo/bar/baz.html'), ()
137
138No more segments to be consumed; ``File('/foo/bar/baz.html').renderHTTP(ctx)`` is
139called, and the result is sent to the browser.
140                       
141``childFactory`` method
142-----------------------
143
144Consuming one URI segment at a time by checking to see if a requested resource
145exists and returning a new object is a very common pattern. Nevow's default
146implementation of ``IResource``, ``nevow.rend.Page``, contains an implementation of
147``locateChild`` which provides more convenient hooks for implementing object
148traversal. One of these hooks is ``childFactory``. Let us imagine for the sake of
149example that we wished to render a tree of dictionaries. Our data structure
150might look something like this::
151
152    tree = dict(
153        one=dict(
154            foo=None,
155            bar=None),
156        two=dict(
157            baz=dict(
158                quux=None)))
159
160Given this data structure, the valid URIs would be:
161
162* /
163* /one
164* /one/foo
165* /one/bar
166* /two
167* /two/baz
168* /two/baz/quux
169
170Let us construct a ``rend.Page`` subclass which uses the default ``locateChild``
171implementation and overrides the ``childFactory`` hook instead::
172
173  class DictTree(rend.Page):
174      def __init__(self, dataDict):
175          self.dataDict = dataDict
176
177      def renderHTTP(self, ctx):
178          if self.dataDict is None:
179              return "Leaf"
180          html = ['<ul>']
181          for key in self.dataDict.keys():
182              html.extend(['<li><a href="', key, '">', key, '</a></li>'])
183          html.append('</ul>')
184          return ''.join(html)
185
186      def childFactory(self, ctx, name):
187          if name not in self.dataDict:
188              return rend.NotFound # 404
189          return DictTree(self.dataDict[name])
190
191As you can see, the ``childFactory`` implementation is considerably shorter than the
192equivalent ``locateChild`` implementation would have been.
193
194``child_*`` methods and attributes
195----------------------------------
196
197Often we may wish to have some hardcoded URLs which are not dynamically
198generated based on some data structure. For example, we might have an
199application which uses an external CSS stylesheet, an external JavaScript file,
200and a folder full of images. The ``rend.Page`` ``locateChild`` implementation provides a
201convenient way for us to express these relationships by using ``child``-prefixed
202methods::
203
204  class Linker(rend.Page):
205      def renderHTTP(self, ctx):
206          return """<html>
207    <head>
208      <link href="css" rel="stylesheet" />
209      <script type="text/javascript" src="scripts" />
210    <body>
211      <img src="images/logo.png" />
212    </body>
213  </html>"""
214
215      def child_css(self, ctx):
216          return static.File('/Users/dp/styles.css')
217
218      def child_scripts(self, ctx):
219          return static.File('/Users/dp/scripts.js')
220
221      def child_images(self, ctx):
222          return static.File('/Users/dp/images/')
223
224One thing you may have noticed is that all of the examples so far have returned
225new object instances whenever they were implementing a traversal API. However,
226there is no reason these instances cannot be shared. One could for example
227return a global resource instance, an instance which was previously inserted in
228a dict, or lazily create and cache dynamic resource instances on the fly. The
229``rend.Page`` ``locateChild`` implementation also provides a convenient way to express
230that one global resource instance should always be used for a particular url,
231the ``child``-prefixed attribute::
232
233  class FasterLinker(Linker):
234      child_css = static.File('/Users/dp/styles.css')
235      child_scripts = static.File('/Users/dp/scripts.js')
236      child_images = static.File('/Users/dp/images/')
237
238Dots in child names
239-------------------
240
241When a URL contains dots, which is quite common in normal URLs, it is simple
242enough to handle these URL segments in ``locateChild`` or ``childFactory`` -- one of the
243passed segments will simply be a string containing a dot. However, it is not
244immediately obvious how one would express a URL segment with a dot in it when
245using ``child``-prefixed methods. The solution is really quite simple::
246
247  class DotChildren(rend.Page):
248      return '<html><head><script type="text/javascript" src="scripts.js" /></head></html>'
249
250  setattr(DotChildren, 'child_scripts.js', static.File('/Users/dp/scripts.js'))
251
252The same technique could be used to install a child method with a dot in the
253name.
254
255children dictionary
256-------------------
257
258The final hook supported by the default implementation of locateChild is the
259``rend.Page.children`` dictionary::
260
261  class Main(rend.Page):
262      children = {
263          'people': People(),
264          'jobs': Jobs(),
265          'events': Events()}
266
267      def renderHTTP(self, ctx):
268          return """/
269<html>
270    <head>
271        <title>Our Site</title>
272    </head>
273    <body>
274        <p>bla bla bla</p>
275    </body>
276</html>"""
277
278
279Hooks are checked in the following order:
280
281  1. ``self.dictionary``
282  2. ``self.child_*``
283  3. ``self.childFactory``
284
285The default trailing slash handler
286----------------------------------
287
288When a URI which is being handled ends in a slash, such as when the '/' URI is
289being rendered or when a directory-like URI is being rendered, the string ''
290appears in the path segments which will be traversed. Again, handling this case
291is trivial inside either ``locateChild`` or ``childFactory``, but it may not be
292immediately obvious what ``child``-prefixed method or attribute will be looked up.
293The method or attribute name which will be used is simply ``child`` with a single
294trailing underscore.
295
296The ``rend.Page`` class provides an implementation of this method which can work in
297two different ways. If the attribute ``addSlash`` is True, the default trailing
298slash handler will return ``self``. In the case when ``addSlash`` is True, the default
299``rend.Page.renderHTTP`` implementation will simply perform a redirect which adds
300the missing slash to the URL.
301
302The default trailing slash handler also returns self if ``addSlash`` is false, but
303emits a warning as it does so. This warning may become an exception at some
304point in the future.
305
306``ICurrentSegments`` and ``IRemainingSegments``
307-----------------------------------------------
308
309During the object traversal process, it may be useful to discover which segments
310have already been handled and which segments are remaining to be handled. This
311information may be obtained from the ``context`` object which is passed to all the
312traversal APIs. The interfaces ``nevow.inevow.ICurrentSegments`` and
313``nevow.inevow.IRemainingSegments`` are used to retrieve this information. To
314retrieve a tuple of segments which have previously been consumed during object
315traversal, use this syntax::
316
317  segs = ICurrentSegments(ctx)
318
319The same is true of ``IRemainingSegments``. ``IRemainingSegments`` is the same value
320which is passed as ``segments`` to ``locateChild``, but may also be useful in the
321implementations of ``childFactory`` or a ``child``-prefixed method, where this
322information would not otherwise be available.
323 
324Conclusion
325==========
326
327Nevow makes it easy to handle complex URL hierarchies. The most basic object
328traversal interface, ``nevow.inevow.IResource.locateChild``, provides powerful and
329flexible control over the entire object traversal process. Nevow's canonical
330``IResource`` implementation, ``rend.Page``, also includes the convenience hooks
331``childFactory`` along with ``child``-prefixed method and attribute semantics to
332simplify common use cases.
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值