python - 扭曲的 python : the correct way to pass a kwarg through the component system to a factory

标签 python twisted keyword-argument

我需要使用 super 将 kwarg 传递给与 FingerFactoryFromService 等效的父类。

我知道我实际上是将 kwarg 传递给 IFingerFactory,因为这也是我传递最终在 init FingerFactoryFromService 中的服务的地方,我可以理解它在组件系统中的某个地方被绊倒了但我想不出其他办法。

我不断收到的错误是

exceptions.TypeError: 'test' is an invalid keyword argument for this function

我的 virtualenv 中的代码版本是:

pip (1.4.1)
setuptools (1.1.6)
Twisted (13.1.0)
wsgiref (0.1.2)
zope.interface (4.0.5)

这是手指教程中演示该问题的简化示例:

from twisted.protocols import basic

from twisted.application import internet, service
from twisted.internet import protocol, reactor, defer
from twisted.python import components
from zope.interface import Interface, implements  # @UnresolvedImport


class IFingerService(Interface):

    def getUser(user):  # @NoSelf
        """
        Return a deferred returning a string.
        """

    def getUsers():  # @NoSelf
        """
        Return a deferred returning a list of strings.
        """


class IFingerFactory(Interface):

    def getUser(user):  # @NoSelf
        """
        Return a deferred returning a string.
        """

    def buildProtocol(addr):  # @NoSelf
        """
        Return a protocol returning a string.
        """


def catchError(err):
    return "Internal error in server"


class FingerProtocol(basic.LineReceiver):

    def lineReceived(self, user):
        d = self.factory.getUser(user)
        d.addErrback(catchError)

        def writeValue(value):
            self.transport.write(value + '\r\n')
            self.transport.loseConnection()
        d.addCallback(writeValue)


class FingerService(service.Service):

    implements(IFingerService)

    def __init__(self, filename):
        self.filename = filename
        self.users = {}

    def _read(self):
        self.users.clear()
        for line in file(self.filename):
            user, status = line.split(':', 1)
            user = user.strip()
            status = status.strip()
            self.users[user] = status
        self.call = reactor.callLater(30, self._read)  # @UndefinedVariable

    def getUser(self, user):
        print user
        return defer.succeed(self.users.get(user, "No such user"))

    def getUsers(self):
        return defer.succeed(self.users.keys())

    def startService(self):
        self._read()
        service.Service.startService(self)

    def stopService(self):
        service.Service.stopService(self)
        self.call.cancel()


class FingerFactoryFromService(protocol.ServerFactory):

    implements(IFingerFactory)

    protocol = FingerProtocol

    #def __init__(self, srv):
    def __init__(self, srv, test=None):
        self.service = srv
        ## I need to call super here because my equivalent of ServerFactory requires 
        ## a kwarg but this cutdown example doesnt so I just assign it to a property
        # super(FingerFactoryFromService, self).__init__(test=test)
        self.test_thing = test or 'Default Something'

    def getUser(self, user):
        return self.service.getUser(user)

components.registerAdapter(FingerFactoryFromService,
                           IFingerService,
                           IFingerFactory)

application = service.Application('finger')
serviceCollection = service.IServiceCollection(application)

finger_service = FingerService('/etc/passwd')
finger_service.setServiceParent(serviceCollection)

#line_finger_factory = IFingerFactory(finger_service)
line_finger_factory = IFingerFactory(finger_service, test='Something')
line_finger_server = internet.TCPServer(1079, line_finger_factory)
line_finger_server.setServiceParent(serviceCollection)  

最佳答案

这与组件系统无关。您想要做的是重写 Factory 的 buildProtocol 方法,如下所示:

https://twistedmatrix.com/documents/current/core/howto/servers.html#auto9

关于python - 扭曲的 python : the correct way to pass a kwarg through the component system to a factory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19128517/

相关文章:

python - 在上下文中模拟计时以创建具有字段 DateTimeField 和 auto_now_add=True 的模型

python - Pandas groupby : apply vs agggregate with missing categories

python - 管理多个 Twisted 客户端连接

python - 监听多个端口

python - twisted reactor.spawnProcess 在 Windows 上获取无缓冲的标准输出

python - Jinja2 扩展多个关键字参数

python - 我无法使用 Exchangelib 读取交换日历

python - 接受整数作为 **kwargs 的键

flask - 使用 kwargs Flask-SQLAlchemy 更新表属性

python - Django:为什么 'Class' 对象不可迭代