可以使用检查模块获取 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 行。
干杯悠闲