【问题标题】:twisted command line protocol扭曲的命令行协议
【发布时间】:2013-05-17 09:20:51
【问题描述】:

我有一个HistoricRecvLine,用作twisted.internet.protocol.ProcessProtocol 类的接口。我可以毫无问题地从HistoricRecvLine 获取协议的信息,但我似乎无法找到另一种方式的魔法绑定:我希望协议中的信息显示在 CLI 中。更清楚地说,我想让 ProcessProtocol 创建的子进程的 stdout/stderr 消息出现在我的 HistoricRecvLine 的 CLI 实现中。

任何人都知道该怎么做,或者可以指出正确的方向吗?

【问题讨论】:

  • 没有任何魔法。信息使用相同的机制向任一方向流动:使用数据调用方法。您能否更具体地说明您遇到的问题?
  • @Jean-PaulCalderone:问题已编辑,是否更有意义?
  • 并非如此。是什么阻止您从 ProcessProtocol 的方法调用处理 CLI 的协议上的方法,该方法具有您想要显示的数据?
  • @Jean-PaulCalderone 因为我看不到要调用的 哪个 方法! CLI 和 ProcessProtocol 是独立的并传递给twisted.conch.insults.insults.ServerProtocol。 CLI 可以看到协议,但看不到 vis-ver-ca。
  • 为什么不能让协议“看到”CLI? theProtocol.setCLI(theCLI) 应该这样做。

标签: python twisted


【解决方案1】:

Jean Paul Calderone 在他的 cmets 中提供了解决方案的提示。

命令行有对协议的引用。在协议中,创建一个方法set_cli_write_func(self, func),设置正确的写入cli的方法。当你构建 cli 时,在适当的协议上调用函数,如下所示:

def lpr(self, message): 
    """Print a message to the screen.

    :type message: String
    :param message: The message string.
    """
    self.terminal.nextLine()
    self.terminal.write(_format_message(message))
    self.terminal.nextLine()
    self.drawInputLine()

所以这个方法是从协议中调用的。

请注意,这会产生一种循环依赖,因此您需要在对象被销毁时检查它。

【讨论】:

    猜你喜欢
    • 2011-05-17
    • 2012-09-23
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多