TAGS :Viewed: 4 - Published at: a few seconds ago

[ twisted command line protocol ]

I have a HistoricRecvLine that I use as an interface to a twisted.internet.protocol.ProcessProtocol class. I can get information from the HistoricRecvLine to the protocol without problems but I cannot seem to find the magic binding to go the other way: aka I want information from the protocol to be displayed in the CLI. To be clearer, I want to have stdout/stderr messages of the sub process created by the ProcessProtocol appear in the my CLI implementation of the HistoricRecvLine.

Anyone knows how to do that or can point me to the right direction?

Answer 1

Jean Paul Calderone in his comments offered a hint to a solution.

The command line has a reference to the protocol. In the protocol, create a method set_cli_write_func(self, func) that set the correct method for writing to the cli. When you construct the cli, call the function on the appropriate protocol with something like:

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

    :type message: String
    :param message: The message string.

So that this method is called form the protocol.

Note that this creates a kind of circular dependency so you need to check for that when objects get destroyed.