comparison cmd2.py @ 421:fc63f0aad022

Additional support for native pbcopy for clipboard redirect (">") on MacOSX.
author Jason Ledbetter <jasonbrent@gmail.com
date Wed, 22 Jun 2011 21:56:20 -0400
parents b6beae2a2f46
children 6ffa49335dcb
comparison
equal deleted inserted replaced
420:b6beae2a2f46 421:fc63f0aad022
150 class PasteBufferError(EnvironmentError): 150 class PasteBufferError(EnvironmentError):
151 if sys.platform[:3] == 'win': 151 if sys.platform[:3] == 'win':
152 errmsg = """Redirecting to or from paste buffer requires pywin32 152 errmsg = """Redirecting to or from paste buffer requires pywin32
153 to be installed on operating system. 153 to be installed on operating system.
154 Download from http://sourceforge.net/projects/pywin32/""" 154 Download from http://sourceforge.net/projects/pywin32/"""
155 elif sys.platform[:3] == 'dar':
156 # Use built in pbcopy on Mac OSX
157 pass
155 else: 158 else:
156 errmsg = """Redirecting to or from paste buffer requires xclip 159 errmsg = """Redirecting to or from paste buffer requires xclip
157 to be installed on operating system. 160 to be installed on operating system.
158 On Debian/Ubuntu, 'sudo apt-get install xclip' will install it.""" 161 On Debian/Ubuntu, 'sudo apt-get install xclip' will install it."""
159 def __init__(self): 162 def __init__(self):
180 win32clipboard.SetClipboardText(txt) 183 win32clipboard.SetClipboardText(txt)
181 win32clipboard.CloseClipboard() 184 win32clipboard.CloseClipboard()
182 except ImportError: 185 except ImportError:
183 def get_paste_buffer(*args): 186 def get_paste_buffer(*args):
184 raise OSError, pastebufferr % ('pywin32', 'Download from http://sourceforge.net/projects/pywin32/') 187 raise OSError, pastebufferr % ('pywin32', 'Download from http://sourceforge.net/projects/pywin32/')
188 write_to_paste_buffer = get_paste_buffer
189 elif sys.platform == 'darwin':
190 can_clip = False
191 try:
192 # test for pbcopy - AFAIK, should always be installed on MacOS
193 subprocess.check_call('pbcopy -help', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
194 can_clip = True
195 except (subprocess.CalledProcessError, OSError, IOError):
196 pass
197 if can_clip:
198 def get_paste_buffer():
199 pbcopyproc = subprocess.Popen('pbcopy -help', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
200 return pbcopyproc.stdout.read()
201 def write_to_paste_buffer(txt):
202 pbcopyproc = subprocess.Popen('pbcopy', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
203 pbcopyproc.communicate(txt.encode())
204 else:
205 def get_paste_buffer(*args):
206 raise OSError, pastebufferr % ('pbcopy', 'On MacOS X - error should not occur - part of the default installation')
185 write_to_paste_buffer = get_paste_buffer 207 write_to_paste_buffer = get_paste_buffer
186 else: 208 else:
187 can_clip = False 209 can_clip = False
188 try: 210 try:
189 subprocess.check_call('xclip -o -sel clip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE) 211 subprocess.check_call('xclip -o -sel clip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)