comparison cmd2.py @ 46:27b45b33a574

pipe experiment
author catherine@cordelia
date Tue, 03 Jun 2008 08:01:16 -0400
parents 67cde3f501de
children 927bc07467de
comparison
equal deleted inserted replaced
45:67cde3f501de 46:27b45b33a574
101 try: 101 try:
102 subprocess.check_call('xclip -o -sel clip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) 102 subprocess.check_call('xclip -o -sel clip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
103 can_clip = True 103 can_clip = True
104 except AttributeError: # check_call not defined, Python < 2.5 104 except AttributeError: # check_call not defined, Python < 2.5
105 teststring = 'Testing for presence of xclip.' 105 teststring = 'Testing for presence of xclip.'
106 #import pdb; pdb.set_trace()
107 xclipproc = subprocess.Popen('xclip -sel clip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) 106 xclipproc = subprocess.Popen('xclip -sel clip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
108 xclipproc.stdin.write(teststring) 107 xclipproc.stdin.write(teststring)
109 xclipproc.stdin.close() 108 xclipproc.stdin.close()
110 xclipproc = subprocess.Popen('xclip -o -sel clip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) 109 xclipproc = subprocess.Popen('xclip -o -sel clip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
111 if xclipproc.stdout.read() == teststring: 110 if xclipproc.stdout.read() == teststring:
238 statement = ' '.join([command, args]) 237 statement = ' '.join([command, args])
239 if (not assumeComplete) and (command in self.multilineCommands): 238 if (not assumeComplete) and (command in self.multilineCommands):
240 statement = self.finishStatement(statement) 239 statement = self.finishStatement(statement)
241 statekeeper = None 240 statekeeper = None
242 stop = 0 241 stop = 0
243 statement, redirect, mode = self.parseRedirectors(statement) 242 statement, redirect = self.pipeFinder(statement)
244 if redirect == self._TO_PASTE_BUFFER: 243 if redirect:
245 try: 244 redirect = subprocess.Popen(redirect, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
246 clipcontents = getPasteBuffer() 245 redirect.stdin = self.stdout
247 if mode in ('w', 'a'): 246 stop = cmd.Cmd.onecmd(self, statement)
247 self.stdout.write(redirect.stdout.read())
248 return stop # didn't record in history
249 else:
250 statement, redirect, mode = self.parseRedirectors(statement)
251 if redirect == self._TO_PASTE_BUFFER:
252 try:
253 clipcontents = getPasteBuffer()
254 if mode in ('w', 'a'):
255 statekeeper = Statekeeper(self, ('stdout',))
256 self.stdout = tempfile.TemporaryFile()
257 if mode == 'a':
258 self.stdout.write(clipcontents)
259 else:
260 statement = '%s %s' % (statement, clipcontents)
261 except OSError, e:
262 print e
263 return 0
264 elif redirect:
265 if mode in ('w','a'):
248 statekeeper = Statekeeper(self, ('stdout',)) 266 statekeeper = Statekeeper(self, ('stdout',))
249 self.stdout = tempfile.TemporaryFile() 267 self.stdout = open(redirect, mode)
250 if mode == 'a':
251 self.stdout.write(clipcontents)
252 else: 268 else:
253 statement = '%s %s' % (statement, clipcontents) 269 statement = '%s %s' % (statement, self.fileimport(statement=statement, source=redirect))
254 except OSError, e:
255 print e
256 return 0
257 elif redirect:
258 if mode in ('w','a'):
259 statekeeper = Statekeeper(self, ('stdout',))
260 self.stdout = open(redirect, mode)
261 else:
262 statement = '%s %s' % (statement, self.fileimport(statement=statement, source=redirect))
263 stop = cmd.Cmd.onecmd(self, statement) 270 stop = cmd.Cmd.onecmd(self, statement)
264 try: 271 try:
265 if command not in self.excludeFromHistory: 272 if command not in self.excludeFromHistory:
266 self.history.append(statement) 273 self.history.append(statement) # or should we append the unmodified statement?
267 finally: 274 finally:
268 if statekeeper: 275 if statekeeper:
269 if redirect == self._TO_PASTE_BUFFER: 276 if redirect == self._TO_PASTE_BUFFER:
270 self.stdout.seek(0) 277 self.stdout.seek(0)
271 writeToPasteBuffer(self.stdout.read()) 278 writeToPasteBuffer(self.stdout.read())