def test_input_reset_at_EOF(self):
input = StringIO.StringIO("print test\nprint test2")
output = StringIO.StringIO()
cmd = self.simplecmd2(stdin=input, stdout=output)
cmd.use_rawinput = False
cmd.cmdloop()
self.assertMultiLineEqual(output.getvalue(),
("(Cmd) test\n"
"(Cmd) test2\n"
"(Cmd) *** Unknown syntax: EOF\n"))
input = StringIO.StringIO("print \n\n")
output = StringIO.StringIO()
cmd.stdin = input
cmd.stdout = output
cmd.cmdloop()
self.assertMultiLineEqual(output.getvalue(),
("(Cmd) \n"
"(Cmd) \n"
"(Cmd) *** Unknown syntax: EOF\n"))
python类Cmd()的实例源码
def pseudo_raw_input(self, prompt):
"""copied from cmd's cmdloop; like raw_input, but accounts for changed stdin, stdout"""
if self.use_rawinput:
try:
line = sm.input(prompt)
except EOFError:
line = 'EOF'
else:
self.stdout.write(prompt)
self.stdout.flush()
line = self.stdin.readline()
if not len(line):
line = 'EOF'
else:
if line[-1] == '\n': # this was always true in Cmd
line = line[:-1]
return line
def onecmd(self, line):
"""
Wrap error handling around cmd.Cmd.onecmd(). Might want to do
something kinder than showing a traceback, eventually.
"""
self.last_command_failed = False
try:
return cmd.Cmd.onecmd(self, line)
except SystemExit:
raise
except ExitArgparse, e:
if e.message is not None:
print e.message
self.last_command_failed = e.status != 0
return False
except BadCommandSyntax, e:
print e
except Exception:
traceback.print_exc()
self.last_command_failed = True
return False
def onecmd(self, line):
"""Interpret the argument as though it had been typed in response
to the prompt.
Checks whether this line is typed at the normal prompt or in
a breakpoint command list definition.
"""
if not self.commands_defining:
return cmd.Cmd.onecmd(self, line)
else:
return self.handle_command_def(line)
def __init__(self, profile=None):
cmd.Cmd.__init__(self)
self.prompt = "% "
self.stats = None
self.stream = sys.stdout
if profile is not None:
self.do_read(profile)
def __init__(self):
cmd.Cmd.__init__(self)
self.prompt = 'QUANTAXIS> ' # ????????
def __init__(self, pre_type, standard_client, mc_client=None):
cmd.Cmd.__init__(self)
self.client = standard_client
self.mc_client = mc_client
self.pre_type = pre_type
def __init__(self, env, quiet=False):
self.env = env
if not quiet:
self.intro = "RavelConsole: interactive console for Ravel.\n" \
"Configuration:\n" + self.env.pprint()
cmd.Cmd.__init__(self)
self.env.set_cli(self)
def do_help(self, arg):
"List available commands with 'help' or detailed help with 'help cmd'"
# extend to include loaded apps and their help methods
tokens = arg.split()
if len(tokens) > 0 and tokens[0] in self.env.loaded:
app = self.env.apps[tokens[0]]
if len(tokens) <= 1:
print app.description
app.console.do_help("")
else:
app.console.do_help(" ".join(tokens[1:]))
else:
cmd.Cmd.do_help(self, arg)
def completenames(self, text, *ignored):
"Add loaded application names/shortcuts to cmd name completions"
completions = cmd.Cmd.completenames(self, text, ignored)
apps = self.env.loaded.keys()
if not text:
completions.extend(apps)
else:
completions.extend([d for d in apps if d.startswith(text)])
return completions
def __init__(self, db, env, components):
"""db: a ravel.db.RavelDb instance
env: a ravel.env.Environment instance of the CLI's executing environment
components: list of the app's SQL components (tables, view, etc.)"""
self.db = db
self.env = env
self.components = components
cmd.Cmd.__init__(self)
def __init__(self, file_name, options):
cmd.Cmd.__init__(self)
self.units = []
self.file_name = file_name
self.env_list = [] # ??????
self.cmd_parser = False
self.options = options
def __init__(self, request, print_full_response=False):
cmd.Cmd.__init__(self)
self.request = request
self.print_full_response = print_full_response
self.prompt = PROMPT
def onecmd(self, line):
"""Interpret the argument as though it had been typed in response
to the prompt.
Checks whether this line is typed at the normal prompt or in
a breakpoint command list definition.
"""
if not self.commands_defining:
return cmd.Cmd.onecmd(self, line)
else:
return self.handle_command_def(line)
def __init__(self, profile=None):
cmd.Cmd.__init__(self)
self.prompt = "% "
self.stats = None
self.stream = sys.stdout
if profile is not None:
self.do_read(profile)
def __init__(self):
cmd.Cmd.__init__(self)
# Declaration of the variables.
self.__strategy = DepthStrategy()
self.clear()
# Declaration of the variables of the shell.
global wstream, colored_loaded
self.__milestone = 'Almost Ninja'
self.__version = '1.0'
self.__main_title = 'TURTLE v' + self.__version + ' (' + self.__milestone + ')'
self.__wstream = wstream
self.__colored_loaded = colored_loaded
self.__error_prefix = str(colortext(r'[ERROR] ', 'red', True))
self.__start_time = 0
self.__elapsed_time = 0
self.__stats = True
self.__default_exception_message = 'Unknown exception!'
self.use_rawinput = True
self.ruler = '='
self.default_file_name = 'new.clp'
self.prompt = str(colortext('TURTLE> ', 'green'))
self.intro = """\
__
.,-;-;-,. /'_\ """ + self.__main_title + """
_/_/_/_|_\_\) /
'-<_><_><_><_>=/\ An expert system shell inspired by CLIPS syntax
`/_/====/_/-'\_\\ Running on Python """ + str(platform.python_version()) + ' ' + str(platform.architecture()[0]) + """
'' '' ''
Usage: help to see online help
<Tab> to show commands
<Up-arrow>, <Down-arrow> to scroll through the history
<Ctrl-d>, quit or exit to leave
"""
def cmdloop(self, intro=None):
print('cmdloop({})'.format(intro))
return cmd.Cmd.cmdloop(self, intro)
def parseline(self, line):
print('parseline({!r}) =>'.format(line), end='')
ret = cmd.Cmd.parseline(self, line)
print(ret)
return ret
def onecmd(self, s):
print('onecmd({})'.format(s))
return cmd.Cmd.onecmd(self, s)
def emptyline(self):
print('emptyline()')
return cmd.Cmd.emptyline(self)