comparison cmd2.py @ 25:6948d1f9e4b4

missing xclip half-working
author catherine@localhost
date Fri, 16 May 2008 18:09:34 -0400
parents f9196cf5ef51
children 82dde2f60e46
comparison
equal deleted inserted replaced
24:f9196cf5ef51 25:6948d1f9e4b4
139 return newStatement, redirect, 'w' 139 return newStatement, redirect, 'w'
140 newStatement, redirect = self.parseRedirector(statement, '<', mustBeTerminated) 140 newStatement, redirect = self.parseRedirector(statement, '<', mustBeTerminated)
141 if redirect: 141 if redirect:
142 return newStatement, redirect, 'r' 142 return newStatement, redirect, 'r'
143 return statement, '', '' 143 return statement, '', ''
144 144
145 def pasteBufferProcess(self, mode='read'): 145 def pasteBufferProcess(self, mode='read'):
146 if mode == 'read': 146 if mode == 'read':
147 command = 'xclip -o -sel clip' 147 command = 'xc1ip -o -sel clip'
148 else: 148 else:
149 command = 'xclip -sel clip' 149 command = 'xclip -sel clip'
150 result = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) 150 errf = tempfile.TemporaryFile()
151 #TODO: need a better response to user who lacks xclip 151 result = subprocess.Popen(command, shell=True, stderr=errf, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
152 errf.seek(0)
153 if 'not found' in errf.read():
154 msg = """Redirecting to or from paste buffer requires xclip to be installed on operating system.
155 On Debian/Ubuntu, 'sudo apt-get install xclip' will install it."""
156 print msg
157 print '---'
158 errf.seek(0)
159 print errf.read()
160 print '---'
161 raise EnvironmentError, msg
152 return result 162 return result
153 163
154 def onecmd(self, line): 164 def onecmd(self, line):
155 """Interpret the argument as though it had been typed in response 165 """Interpret the argument as though it had been typed in response
156 to the prompt. 166 to the prompt.
164 command, args = self.extractCommand(line) 174 command, args = self.extractCommand(line)
165 statement = ' '.join([command, args]) 175 statement = ' '.join([command, args])
166 if command in self.multilineCommands: 176 if command in self.multilineCommands:
167 statement = self.finishStatement(statement) 177 statement = self.finishStatement(statement)
168 statekeeper = None 178 statekeeper = None
179 stop = 0
169 statement, redirect, mode = self.parseRedirectors(statement) 180 statement, redirect, mode = self.parseRedirectors(statement)
170 if redirect == self._TO_PASTE_BUFFER: 181 if redirect == self._TO_PASTE_BUFFER:
171 if mode in ('a', 'r'): 182 try:
172 clipcontents = self.pasteBufferProcess('read').stdout.read() 183 if mode in ('a', 'r'):
173 if mode in ('w', 'a'): 184 clipcontents = self.pasteBufferProcess('read').stdout.read()
174 statekeeper = Statekeeper(self, ('stdout',)) 185 if mode in ('w', 'a'):
175 self.stdout = self.pasteBufferProcess('write').stdin 186 statekeeper = Statekeeper(self, ('stdout',))
176 if mode == 'a': 187 self.stdout = self.pasteBufferProcess('write').stdin
177 self.stdout.write(clipcontents) 188 if mode == 'a':
178 else: 189 self.stdout.write(clipcontents)
179 statement = '%s %s' % (statement, clipcontents) 190 else:
191 statement = '%s %s' % (statement, clipcontents)
192 except EnvironmentError, e:
193 return 0
180 elif redirect: 194 elif redirect:
181 if mode in ('w','a'): 195 if mode in ('w','a'):
182 statekeeper = Statekeeper(self, ('stdout',)) 196 statekeeper = Statekeeper(self, ('stdout',))
183 self.stdout = open(redirect, mode) 197 self.stdout = open(redirect, mode)
184 else: 198 else: