1

扭曲的网络文档说 getChild 必须以这种方式实现:

class Hello(Resource):
    isLeaf = True
    def getChild(self, name, request):
        if name == '':
            return self
        return Resource.getChild(self, name, request)$

据我了解,有一个对 getChild 方法的递归调用,但是谁(哪个类中的哪个方法?)负责从 name 中删除路径段?

谢谢 !

4

2 回答 2

2

可以使用检查模块获取 getChild(self, name, request) 的调用者。

import inspect
...
class Hello(Resource):
    #isLeaf = True  # This has to be left out, to ensure, getChild is called!
    def getChild(self, name, request):
        print inspect.stack()[2][1]
        return self

现在您将在标准输出中看到输出:

2012-09-17 11:16:24+0200 [HTTPChannel,0,127.0.0.1] getChildForRequest

如果您查看web/resource/Resource部分中的 API 文档并查看 Resource 的来源,您可以找到方法“ getChildForRequest ”(第 172 行),并找到一个弃用警告,上面写着“...使用模块级别 getChildForRequest.",这意味着查看模块级别以找到该函数(第 58 行)

在这里,模块级函数通过检查“isLeaf”并移动路径前和路径后元素来遍历路径元素。如果我们的资源具有“isLeaf”,则返回该资源,否则如果 request.postpath 存在且“isLeaf”为 false,则路径将四处移动并调用resource.getChildWithDefault,它本身会查找始终可用的资源(添加putChild 或简单地存在于 self.children dict) 中,如果找不到,它会调用“ getChild ”,它应该返回一个动态资源,或者它以 getChild 的默认返回结束,即:NoResource("No such child resource ") 在第 152 行

干杯悠闲

于 2012-09-17T09:37:32.523 回答
2

例如,URL /foo/bar/baz 通常是:

Resource.getChild('foo').getChild('bar').getChild('baz')

但是,如果“bar”返回的资源将 isLeaf 设置为 true,则永远不会对其进行 getChild 调用。

于 2012-02-13T11:40:57.670 回答