Mercurial > python-cmd2
changeset 67:a78dff1e7bca
prelim tests work; weirdness with xclip and shift-ins
author | catherine@Elli.myhome.westell.com |
---|---|
date | Mon, 23 Jun 2008 17:37:10 -0400 |
parents | f373aaa2390c |
children | e06961ebd035 |
files | cmd2.py |
diffstat | 1 files changed, 23 insertions(+), 15 deletions(-) [+] |
line wrap: on
line diff
--- a/cmd2.py Mon Jun 23 15:15:11 2008 -0400 +++ b/cmd2.py Mon Jun 23 17:37:10 2008 -0400 @@ -150,14 +150,16 @@ processed.ignore(pyparsing.sglQuotedString) processed.ignore(pyparsing.dblQuotedString) pattern = pyparsing.SkipTo(processed) + processed + pyparsing.restOfLine - def parser(txt): + def parser(self, txt): result = pattern.searchString(txt) if result: - return result[0][0], result[0][1:-1], result[0][-1] + return result[0][0].strip(), result[0][1:-1], result[0][-1].strip() else: return None return parser + + class Cmd(cmd.Cmd): caseInsensitive = True multilineCommands = [] @@ -207,11 +209,16 @@ """Lists single-key shortcuts available.""" result = "\n".join('%s: %s' % (sc[0], sc[1]) for sc in self.shortcuts.items()) self.stdout.write("Single-key shortcuts for other commands:\n%s\n" % (result)) - + commmand_terminator_finder = punctuationParser(terminators) output_destination_finder = punctuationParser(['>>', '>']) input_source_finder = punctuationParser(['<']) pipe_destination_finder = punctuationParser(['|']) + def strip_terminators(self, txt): + termination = self.commmand_terminator_finder(txt) + if termination: + txt = termination[0] + return txt def extractCommand(self, statement): try: @@ -241,7 +248,7 @@ The return value is a flag indicating whether interpretation of commands by the interpreter should stop. - """ + """ command, args = self.extractCommand(line) statement = originalStatement = ' '.join([command, args]) if (not assumeComplete) and (command in self.multilineCommands): @@ -259,16 +266,16 @@ pipeTo = self.pipe_destination_finder(statement) if pipeTo: - statement, pipeTo = pipeTo[0], [-1] + statement, pipeTo = pipeTo[0], pipeTo[-1] statekeeper = Statekeeper(self, ('stdout',)) - pipeTo = subprocess.Popen(PipeTo, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) - self.stdout = redirect.stdin + pipeTo = subprocess.Popen(pipeTo, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) + self.stdout = pipeTo.stdin else: # can't pipe output AND send it to a file - outputTo = self.outputTo_finder(statement) + outputTo = self.output_destination_finder(statement) if outputTo: statement, destination = outputTo[0], outputTo[-1] statekeeper = Statekeeper(self, ('stdout',)) - mode = ((output[1] == '>>') and 'a') or 'w' + mode = ((outputTo[1][0] == '>>') and 'a') or 'w' if destination: self.stdout = open(destination, mode) else: @@ -282,12 +289,12 @@ self.history.append(originalStatement) finally: if statekeeper: - if outputTo and not destination: + if pipeTo: + for result in pipeTo.communicate(): + statekeeper.stdout.write(result or '') + elif outputTo and not destination: self.stdout.seek(0) writeToPasteBuffer(self.stdout.read()) - elif pipeTo: # uh-oh. HUH? - for result in redirect.communicate(): - statekeeper.stdout.write(result or '') self.stdout.close() statekeeper.restore() @@ -377,7 +384,8 @@ line = '%s %s' % (shortcut, line[1:]) i, n = 0, len(line) while i < n and line[i] in self.identchars: i = i+1 - cmd, arg = line[:i], line[i:].strip().strip(self.terminators) + cmd, arg = line[:i], line[i:].strip() + arg = self.strip_terminators(arg) return cmd, arg, line def showParam(self, param): @@ -407,7 +415,7 @@ if paramName not in self.settable: raise NotSettableError currentVal = getattr(self, paramName) - val = cast(currentVal, val.strip(self.terminators)) + val = cast(currentVal, self.strip_terminators(val)) setattr(self, paramName, val) self.stdout.write('%s - was: %s\nnow: %s\n' % (paramName, currentVal, val)) except (ValueError, AttributeError, NotSettableError), e: