Mercurial > python-cmd2
comparison cmd2.py @ 354:7cd04727f7f7
redirect works even with print
author | catherine@dellzilla |
---|---|
date | Wed, 17 Feb 2010 12:08:42 -0500 |
parents | 5e3f918c41d8 |
children | 5972ae04515e |
comparison
equal
deleted
inserted
replaced
353:5e3f918c41d8 | 354:7cd04727f7f7 |
---|---|
181 win32clipboard.SetClipboardText(txt) | 181 win32clipboard.SetClipboardText(txt) |
182 win32clipboard.CloseClipboard() | 182 win32clipboard.CloseClipboard() |
183 except ImportError: | 183 except ImportError: |
184 def get_paste_buffer(*args): | 184 def get_paste_buffer(*args): |
185 raise OSError, pastebufferr % ('pywin32', 'Download from http://sourceforge.net/projects/pywin32/') | 185 raise OSError, pastebufferr % ('pywin32', 'Download from http://sourceforge.net/projects/pywin32/') |
186 setPasteBuffer = get_paste_buffer | 186 write_to_paste_buffer = get_paste_buffer |
187 else: | 187 else: |
188 can_clip = False | 188 can_clip = False |
189 try: | 189 try: |
190 subprocess.check_call('xclip -o -sel clip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) | 190 subprocess.check_call('xclip -o -sel clip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) |
191 can_clip = True | 191 can_clip = True |
212 xclipproc.stdin.write(txt) | 212 xclipproc.stdin.write(txt) |
213 xclipproc.stdin.close() | 213 xclipproc.stdin.close() |
214 else: | 214 else: |
215 def get_paste_buffer(*args): | 215 def get_paste_buffer(*args): |
216 raise OSError, pastebufferr % ('xclip', 'On Debian/Ubuntu, install with "sudo apt-get install xclip"') | 216 raise OSError, pastebufferr % ('xclip', 'On Debian/Ubuntu, install with "sudo apt-get install xclip"') |
217 setPasteBuffer = get_paste_buffer | 217 write_to_paste_buffer = get_paste_buffer |
218 writeToPasteBuffer = get_paste_buffer | |
219 | 218 |
220 pyparsing.ParserElement.setDefaultWhitespaceChars(' \t') | 219 pyparsing.ParserElement.setDefaultWhitespaceChars(' \t') |
221 | 220 |
222 class ParsedString(str): | 221 class ParsedString(str): |
223 def full_parsed_statement(self): | 222 def full_parsed_statement(self): |
785 raise EmptyStatement | 784 raise EmptyStatement |
786 return statement | 785 return statement |
787 | 786 |
788 def redirect_output(self, statement): | 787 def redirect_output(self, statement): |
789 if statement.parsed.pipeTo: | 788 if statement.parsed.pipeTo: |
790 self.kept_state = Statekeeper(self, ('stdout',)) | 789 self.kept_state = Statekeeper(self, ('stdout',)) |
790 self.kept_sys = Statekeeper(sys, ('stdout',)) | |
791 self.redirect = subprocess.Popen(statement.parsed.pipeTo, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) | 791 self.redirect = subprocess.Popen(statement.parsed.pipeTo, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) |
792 self.stdout = self.redirect.stdin | 792 sys.stdout = self.stdout = self.redirect.stdin |
793 elif statement.parsed.output: | 793 elif statement.parsed.output: |
794 self.kept_state = Statekeeper(self, ('stdout',)) | 794 self.kept_state = Statekeeper(self, ('stdout',)) |
795 self.kept_sys = Statekeeper(sys, ('stdout',)) | |
795 if statement.parsed.outputTo: | 796 if statement.parsed.outputTo: |
796 mode = 'w' | 797 mode = 'w' |
797 if statement.parsed.output == '>>': | 798 if statement.parsed.output == '>>': |
798 mode = 'a' | 799 mode = 'a' |
799 self.stdout = open(os.path.expanduser(statement.parsed.outputTo), mode) | 800 sys.stdout = self.stdout = open(os.path.expanduser(statement.parsed.outputTo), mode) |
800 else: | 801 else: |
801 self.stdout = tempfile.TemporaryFile() | 802 sys.stdout = self.stdout = tempfile.TemporaryFile() |
802 if statement.parsed.output == '>>': | 803 if statement.parsed.output == '>>': |
803 self.stdout.write(get_paste_buffer()) | 804 self.stdout.write(get_paste_buffer()) |
804 | 805 |
805 def restore_output(self, statement): | 806 def restore_output(self, statement): |
806 if self.kept_state: | 807 if self.kept_state: |
810 write_to_paste_buffer(self.stdout.read()) | 811 write_to_paste_buffer(self.stdout.read()) |
811 elif statement.parsed.pipeTo: | 812 elif statement.parsed.pipeTo: |
812 for result in self.redirect.communicate(): | 813 for result in self.redirect.communicate(): |
813 self.kept_state.stdout.write(result or '') | 814 self.kept_state.stdout.write(result or '') |
814 self.stdout.close() | 815 self.stdout.close() |
815 self.kept_state.restore() | 816 self.kept_state.restore() |
817 self.kept_sys.restore() | |
816 self.kept_state = None | 818 self.kept_state = None |
817 | 819 |
818 def onecmd(self, line): | 820 def onecmd(self, line): |
819 """Interpret the argument as though it had been typed in response | 821 """Interpret the argument as though it had been typed in response |
820 to the prompt. | 822 to the prompt. |