为什么fuse不使用file_class中提供的类

发布于 2024-07-09 06:16:56 字数 881 浏览 15 评论 0原文

我有一个基于熔丝文档中的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尐籹人 2024-07-16 06:16:56

查看 Fuse 类的代码(这是一个由创建方法代理的曲折小段落组成的迷宫),我看到了这一点(这是一个用于在 Fuse.MethodProxy._add_class_type 内部创建 setter 的闭包,第 865 行):

        def setter(self, xcls):

            setattr(self, type + '_class', xcls)

            for m in inits:
                self.mdic[m] = xcls

            for m in proxied:
                if hasattr(xcls, m):
                    self.mdic[m] = self.proxyclass(m)

当您执行 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):

        def setter(self, xcls):

            setattr(self, type + '_class', xcls)

            for m in inits:
                self.mdic[m] = xcls

            for m in proxied:
                if hasattr(xcls, m):
                    self.mdic[m] = self.proxyclass(m)

When you do self.file_class = self.get_file, this gets called with self.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 its mdic 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 on Dstorage.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文