# This file contains classes that can be imported by any of the unit tests.

import sys ; sys.path.append('..')
import time
import threading

class DummyStdout(object):
    """ This class is a replacement for sys.stdout. All output is
    stored in this object's 'lines' instance variable.
    """
    def __init__(self):
        self.lines = []
        
    def flush(self):
        pass
    
    def write(self, msg):
        pass
    
from mremote import RemoteWrapperClient, RemoteWrapperServer

class MPdbTest(RemoteWrapperClient):
    """ This class provides a version of the MPdb class that is
    suitable for use in unit testing. All output is captured and
    stored in this object's 'lines' instance variable.
    """
    def __init__(self, mpdb, cmds=[]):
        """ The optional argument 'cmds' is a list specifying commands
        that this instance should interpret in it's 'cmdloop' method.
        """
        RemoteWrapperClient.__init__(self, mpdb)
        self.lines = []
        self.curframe = None
        self.cmdqueue = cmds
        self.botframe = None

    def msg_nocr(self, msg, out=None):
        self.lines.append(msg)


class Pdbserver(threading.Thread, RemoteWrapperServer):
    """ This class provides a fully functional pdbserver that runs
    in a separate thread. The optional argument 'addr' specifies a
    protocol and an address to use for pdbserver.
    """
    def __init__(self, mpdb, addr=None):
        RemoteWrapperServer.__init__(self, mpdb)
        threading.Thread.__init__(self)
        self._sys_argv = ['python', '-c', '"pass"']
        self.botframe = None
        self.curframe = None
        self.lines = []

        if not addr:
            addr = 'tcp localhost:8000'
        self.addr = addr

    def msg_nocr(self, msg, out=None):
        self.lines.append(msg)
        
    def run(self):
        self.do_pdbserver(self.addr)
        while True:
                self.cmdloop()
                if self._user_requested_quit:
                    break

class MPdbTestThread(threading.Thread, MPdbTest):
    """ This class provides a MPdbTest object that runs in a separate
    thread.
    """
    def __init__(self, cmds=[]):
        threading.Thread.__init__(self)
        MPdbTest.__init__(self, cmds)

from mproc import ProcessWrapper
        
class MPdbTestProc(ProcessWrapper):
    def __init__(self, mpdb):
        ProcessWrapper.__init__(self, mpdb)
        self.lines = []
        self.curframe = None

    def msg_nocr(self, msg, out=None):
        self.lines.append(msg)
                

        

    
