为什么fuse不使用file_class中提供的类
我有一个基于熔丝文档中的 Xmp 示例的 python 熔丝项目。 我包含了一小段代码来展示它是如何工作的。 由于某种原因, get_file 确实被调用并创建了类,但不是从 get_file (file_class) 调用类上的熔断器 .read() ,熔断器继续调用 Dstorage.read() ,这违背了将读取函数移出该类的目的班级。
class Dstorage(Fuse, Distributor):
def get_file(self, server, path, flags, *mode):
pass
# This does some work and passes back an instance of
# a class very similar to XmpFile
def main(self, *a, **kw):
self.file_class = self.get_file
return Fuse.main(self, *a, **kw)
我的代码托管在启动板上,您可以使用此命令下载它。bzr co <a href="https://code.launchpad.net/~asa-ayers/+junk/dstorage" rel="nofollow noreferrer">https://code.launchpad.net/~asa-艾尔斯/+垃圾/dstorage
bzr 分支 lp:~asa-ayers/dstorage/trunk
解决方案:
我使用了一个代理类,它是我需要的类的子类,在构造函数中我获取了我需要的类的实例,并覆盖了所有代理的方法以简单地调用实例方法。
I have a python fuse project based on the Xmp example in the fuse documentation. I have included a small piece of the code to show how this works. For some reason get_file does get called and the class gets created, but instead of fuse calling .read() on the class from get_file (file_class) fuse keeps calling Dstorage.read() which defeats the purpose in moving the read function out of that class.
class Dstorage(Fuse, Distributor):
def get_file(self, server, path, flags, *mode):
pass
# This does some work and passes back an instance of
# a class very similar to XmpFile
def main(self, *a, **kw):
self.file_class = self.get_file
return Fuse.main(self, *a, **kw)
I have my code hosted on launchpad, you can download it with this command.bzr co https://code.launchpad.net/~asa-ayers/+junk/dstorage
bzr branch lp:~asa-ayers/dstorage/trunk
solution:
I used a proxy class that subclasses the one I needed and in the constructor I get the instance of the class I need and overwrite all of the proxy's methods to simply call the instance methods.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看 Fuse 类的代码(这是一个由创建方法代理的曲折小段落组成的迷宫),我看到了这一点(这是一个用于在
Fuse.MethodProxy._add_class_type
内部创建 setter 的闭包,第 865 行):当您执行
self.file_class = self.get_file
时,会使用self.get_file
调用它,这是一个绑定方法。 代理属性上的循环期望能够从您设置的类中获取属性,在包装它们后将它们放入其mdic
代理字典中,但它们不在那里,因为它是一个绑定方法,而不是类。 由于找不到它们,它会恢复在Dstorage
上调用它们。因此,长话短说,您不能使用返回实例(一种伪类)的可调用函数而不是这里的类,因为 Fuse 正在内省您设置的对象以查找它应该调用的方法。
您需要为
file_class
分配一个类 - 如果您需要引用回父实例,您可以使用它们在文档中显示的嵌套类技巧。Looking at the code of the Fuse class (which is a maze of twisty little passages creating method proxies), I see this bit (which is a closure used to create a setter inside
Fuse.MethodProxy._add_class_type
, line 865):When you do
self.file_class = self.get_file
, this gets called withself.get_file
, which is a bound method. The loop over proxied attributes is expecting to be able to get the attributes off the class you set, to put them into itsmdic
proxy dictionary after wrapping them, but they aren't there, because it's a bound method, rather than a class. Since it can't find them, it reverts to calling them onDstorage
.So, long story short, you can't use a callable that returns an instance (kind of a pseudo-class) instead of a class here, because Fuse is introspecting the object that you set to find the methods it should call.
You need to assign a class to
file_class
- if you need to refer back to the parent instance, you can use the nested class trick they show in the docs.