Skip to content

Commit 6363201

Browse files
code clean and refactor cmdrepl
1 parent a9af7ea commit 6363201

File tree

1 file changed

+28
-25
lines changed

1 file changed

+28
-25
lines changed

pyrepl/cmdrepl.py

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,28 @@
3535

3636
from __future__ import print_function
3737

38-
from pyrepl import completing_reader as cr, reader, completer
38+
from pyrepl import completer
3939
from pyrepl.completing_reader import CompletingReader as CR
4040
import cmd
4141

42+
4243
class CmdReader(CR):
4344
def collect_keymap(self):
4445
return super(CmdReader, self).collect_keymap() + (
4546
("\\M-\\n", "invalid-key"),
4647
("\\n", "accept"))
47-
48-
CR_init = CR.__init__
48+
4949
def __init__(self, completions):
50-
self.CR_init(self)
50+
super(CmdReader, self).__init__()
5151
self.completions = completions
5252

5353
def get_completions(self, stem):
5454
if len(stem) != self.pos:
5555
return []
56-
return sorted(set(s for s in self.completions
57-
if s.startswith(stem)))
56+
return sorted(set(s
57+
for s in self.completions
58+
if s.startswith(stem)))
59+
5860

5961
def replize(klass, history_across_invocations=1):
6062

@@ -71,26 +73,25 @@ def replize(klass, history_across_invocations=1):
7173
for s in completer.get_class_members(klass)
7274
if s.startswith("do_")]
7375

74-
if not issubclass(klass, cmd.Cmd):
75-
raise Exception
76+
assert issubclass(klass, cmd.Cmd)
7677
# if klass.cmdloop.im_class is not cmd.Cmd:
7778
# print "this may not work"
7879

79-
class CmdRepl(klass):
80-
k_init = klass.__init__
81-
82-
if history_across_invocations:
83-
_CmdRepl__history = []
84-
def __init__(self, *args, **kw):
85-
self.k_init(*args, **kw)
86-
self.__reader = CmdReader(completions)
87-
self.__reader.history = CmdRepl._CmdRepl__history
88-
self.__reader.historyi = len(CmdRepl._CmdRepl__history)
89-
else:
90-
def __init__(self, *args, **kw):
91-
self.k_init(*args, **kw)
92-
self.__reader = CmdReader(completions)
93-
80+
class MultiHist(object):
81+
__history = []
82+
83+
def __init__(self, *args, **kw):
84+
super(MultiHist, self).__init__(*args, **kw)
85+
self.__reader = CmdReader(completions)
86+
self.__reader.history = self.__history
87+
self.__reader.historyi = len(self.__history)
88+
89+
class SimpleHist(object):
90+
def __init__(self, *args, **kw):
91+
super(SimpleHist, self).__init__(*args, **kw)
92+
self.__reader = CmdReader(completions)
93+
94+
class CmdLoopMixin(object):
9495
def cmdloop(self, intro=None):
9596
self.preloop()
9697
if intro is not None:
@@ -113,6 +114,8 @@ def cmdloop(self, intro=None):
113114
stop = self.postcmd(stop, line)
114115
self.postloop()
115116

116-
CmdRepl.__name__ = "replize(%s.%s)"%(klass.__module__, klass.__name__)
117-
return CmdRepl
117+
hist = MultiHist if history_across_invocations else SimpleHist
118118

119+
class CmdRepl(hist, CmdLoopMixin, klass):
120+
__name__ = "replize(%s.%s)" % (klass.__module__, klass.__name__)
121+
return CmdRepl

0 commit comments

Comments
 (0)