Exemple #1
0
def replize(klass, history_across_invocations=1):

    """Return a subclass of the cmd.Cmd-derived klass that uses
    pyrepl instead of readline.

    Raises a ValueError if klass does not derive from cmd.Cmd.

    The optional history_across_invocations parameter (default 1)
    controls whether instances of the returned class share
    histories."""

    completions = [s[3:] for s in completer.get_class_members(klass) if s.startswith("do_")]

    if not issubclass(klass, cmd.Cmd):
        raise Exception
    #    if klass.cmdloop.im_class is not cmd.Cmd:
    #        print "this may not work"

    class CmdRepl(klass):
        k_init = klass.__init__

        if history_across_invocations:
            _CmdRepl__history = []

            def __init__(self, *args, **kw):
                self.k_init(*args, **kw)
                self.__reader = CmdReader(completions)
                self.__reader.history = CmdRepl._CmdRepl__history
                self.__reader.historyi = len(CmdRepl._CmdRepl__history)

        else:

            def __init__(self, *args, **kw):
                self.k_init(*args, **kw)
                self.__reader = CmdReader(completions)

        def cmdloop(self, intro=None):
            self.preloop()
            if intro is not None:
                self.intro = intro
            if self.intro:
                print self.intro
            stop = None
            while not stop:
                if self.cmdqueue:
                    line = self.cmdqueue[0]
                    del self.cmdqueue[0]
                else:
                    try:
                        self.__reader.ps1 = self.prompt
                        line = self.__reader.readline()
                    except EOFError:
                        line = "EOF"
                line = self.precmd(line)
                stop = self.onecmd(line)
                stop = self.postcmd(stop, line)
            self.postloop()

    CmdRepl.__name__ = "replize(%s.%s)" % (klass.__module__, klass.__name__)
    return CmdRepl
def replize(klass, console, history_across_invocations=1):

    """Return a subclass of the cmd.Cmd-derived klass that uses
    pyrepl instead of readline.

    Raises a ValueError if klass does not derive from cmd.Cmd.

    The optional history_across_invocations parameter (default 1)
    controls whether instances of the returned class share
    histories."""

    completions = [s[3:]
                   for s in completer.get_class_members(klass)
                   if s.startswith("do_")]

    if not issubclass(klass, cmd.Cmd):
        raise Exception
#    if klass.cmdloop.im_class is not cmd.Cmd:
#        print "this may not work"

    class CmdRepl(klass):
        k_init = klass.__init__

        if history_across_invocations:
            _CmdRepl__history = []
            def __init__(self, *args, **kw):
                self.k_init(*args, **kw)
                self.__reader = CmdReader(completions,console)
                self.__reader.history = CmdRepl._CmdRepl__history
                self.__reader.historyi = len(CmdRepl._CmdRepl__history)
        else:
            def __init__(self, *args, **kw):
                self.k_init(*args, **kw)
                self.__reader = CmdReader(completions,console)
        
        def cmdloop(self, intro=None):
            self.preloop()
            if intro is not None:
                self.intro = intro
            if self.intro:
                print self.intro
            stop = None
            while not stop:
                if self.cmdqueue:
                    line = self.cmdqueue[0]
                    del self.cmdqueue[0]
                else:
                    try:
                        self.__reader.ps1 = self.prompt
                        line = self.__reader.readline()
                    except EOFError:
                        line = "EOF"
                line = self.precmd(line)
                stop = self.onecmd(line)
                stop = self.postcmd(stop, line)
            self.postloop()

    CmdRepl.__name__ = "replize(%s.%s)"%(klass.__module__, klass.__name__)
    return CmdRepl
Exemple #3
0
def replize(klass, history_across_invocations=1):
    """Return a subclass of the cmd.Cmd-derived klass that uses
    pyrepl instead of readline.

    Raises a ValueError if klass does not derive from cmd.Cmd.

    The optional history_across_invocations parameter (default 1)
    controls whether instances of the returned class share
    histories."""

    completions = [
        s[3:] for s in completer.get_class_members(klass)
        if s.startswith("do_")
    ]

    assert issubclass(klass, cmd.Cmd)

    #    if klass.cmdloop.im_class is not cmd.Cmd:
    #        print "this may not work"

    class MultiHist(object):
        __history = []

        def __init__(self, *args, **kw):
            super(MultiHist, self).__init__(*args, **kw)
            self.__reader = CmdReader(completions)
            self.__reader.history = self.__history
            self.__reader.historyi = len(self.__history)

    class SimpleHist(object):
        def __init__(self, *args, **kw):
            super(SimpleHist, self).__init__(*args, **kw)
            self.__reader = CmdReader(completions)

    class CmdLoopMixin(object):
        def cmdloop(self, intro=None):
            self.preloop()
            if intro is not None:
                self.intro = intro
            if self.intro:
                print(self.intro)
            stop = None
            while not stop:
                if self.cmdqueue:
                    line = self.cmdqueue[0]
                    del self.cmdqueue[0]
                else:
                    try:
                        self.__reader.ps1 = self.prompt
                        line = self.__reader.readline()
                    except EOFError:
                        line = "EOF"
                line = self.precmd(line)
                stop = self.onecmd(line)
                stop = self.postcmd(stop, line)
            self.postloop()

    hist = MultiHist if history_across_invocations else SimpleHist

    class CmdRepl(hist, CmdLoopMixin, klass):
        __name__ = "replize(%s.%s)" % (klass.__module__, klass.__name__)

    return CmdRepl