comparison cmd2.py @ 350:1c91655d05f8

still working out redirection
author catherine@Drou
date Tue, 16 Feb 2010 20:51:47 -0500
parents 6116360f6e03
children 5e3f918c41d8
comparison
equal deleted inserted replaced
349:6116360f6e03 350:1c91655d05f8
784 raise EmptyStatement 784 raise EmptyStatement
785 return statement 785 return statement
786 786
787 def redirect_output(self, statement): 787 def redirect_output(self, statement):
788 if statement.parsed.pipeTo: 788 if statement.parsed.pipeTo:
789 redirect = subprocess.Popen(statement.parsed.pipeTo, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) 789 self.kept_state = Statekeeper(self, ('stdout',))
790 self.kept_state = Statekeeper(self, ('stdout',)) 790 self.redirect = subprocess.Popen(statement.parsed.pipeTo, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
791 self.stdout = redirect.stdin 791 self.stdout = self.redirect.stdin
792 elif statement.parsed.output: 792 elif statement.parsed.output:
793 self.kept_state = Statekeeper(self, ('stdout',)) 793 self.kept_state = Statekeeper(self, ('stdout',))
794 if statement.parsed.outputTo: 794 if statement.parsed.outputTo:
795 mode = 'w' 795 mode = 'w'
796 if statement.parsed.output == '>>': 796 if statement.parsed.output == '>>':
797 mode = 'a' 797 mode = 'a'
798 self.stdout = open(os.path.expanduser(statement.parsed.outputTo), mode) 798 self.stdout = open(os.path.expanduser(statement.parsed.outputTo), mode)
799 else: 799 else:
800 self.kept_state = Statekeeper(self, ('stdout',))
801 self.stdout = tempfile.TemporaryFile() 800 self.stdout = tempfile.TemporaryFile()
802 if statement.parsed.output == '>>': 801 if statement.parsed.output == '>>':
803 self.stdout.write(get_paste_buffer()) 802 self.stdout.write(get_paste_buffer())
804 803
805 def restore_output(self, statement): 804 def restore_output(self, statement):
806 if self.kept_state: 805 if self.kept_state:
807 if statement.parsed.output and not statement.parsed.outputTo: 806 if statement.parsed.output:
808 self.stdout.seek(0) 807 if not statement.parsed.outputTo:
809 try: 808 self.stdout.seek(0)
810 write_to_paste_buffer(self.stdout.read()) 809 write_to_paste_buffer(self.stdout.read())
811 except Exception, e:
812 self.perror(e)
813 elif statement.parsed.pipeTo: 810 elif statement.parsed.pipeTo:
814 for result in redirect.communicate(): 811 for result in self.redirect.communicate():
815 self.kept_state.stdout.write(result or '') 812 self.kept_state.stdout.write(result or '')
816 self.stdout.close() 813 self.stdout.close()
817 self.kept_state.restore() 814 self.kept_state.restore()
818 self.kept_state = None 815 self.kept_state = None
819 816