|
45 | 45 | from optparse import make_option |
46 | 46 |
|
47 | 47 | import pyparsing |
| 48 | +import pyperclip |
48 | 49 |
|
49 | 50 | # next(it) gets next item of iterator it. This is a replacement for calling it.next() in Python 2 and next(it) in Py3 |
50 | 51 | from six import next |
@@ -302,148 +303,37 @@ def new_func(instance, arg): |
302 | 303 | return option_setup |
303 | 304 |
|
304 | 305 |
|
305 | | -# Prefix to use on all OSes when the appropriate library or CLI tool isn't installed for getting access to paste buffer |
306 | | -pastebufferr = """Redirecting to or from paste buffer requires %s |
307 | | -to be installed on operating system. |
308 | | -%s""" |
309 | | - |
310 | | -# Can we access the clipboard? |
311 | | -can_clip = False |
312 | | -if sys.platform == "win32": |
313 | | - # Running on Windows |
| 306 | +# Can we access the clipboard, always true on Windows and Mac, but only sometimes on Linux |
| 307 | +can_clip = True |
| 308 | +if sys.platform.startswith('linux'): |
314 | 309 | try: |
315 | | - # noinspection PyUnresolvedReferences |
316 | | - import win32clipboard |
317 | | - |
318 | | - def get_paste_buffer(): |
319 | | - """Get the contents of the clipboard for Windows OSes. |
320 | | -
|
321 | | - :return: str - contents of the clipboard |
322 | | - """ |
323 | | - win32clipboard.OpenClipboard(0) |
324 | | - try: |
325 | | - result = win32clipboard.GetClipboardData() |
326 | | - except TypeError: |
327 | | - result = '' # non-text |
328 | | - win32clipboard.CloseClipboard() |
329 | | - return result |
| 310 | + pyperclip.paste() |
| 311 | + except pyperclip.exceptions.PyperclipException: |
| 312 | + can_clip = False |
330 | 313 |
|
331 | | - def write_to_paste_buffer(txt): |
332 | | - """Paste text to the clipboard for Windows OSes. |
333 | 314 |
|
334 | | - :param txt: str - text to paste to the clipboard |
335 | | - """ |
336 | | - win32clipboard.OpenClipboard(0) |
337 | | - win32clipboard.EmptyClipboard() |
338 | | - win32clipboard.SetClipboardText(txt) |
339 | | - win32clipboard.CloseClipboard() |
340 | | - |
341 | | - can_clip = True |
342 | | - except ImportError: |
343 | | - # noinspection PyUnusedLocal |
344 | | - def get_paste_buffer(*args): |
345 | | - """For Windows OSes without the appropriate library installed to get text from clipboard, raise an exception. |
346 | | - """ |
347 | | - raise OSError(pastebufferr % ('pywin32', 'Download from http://sourceforge.net/projects/pywin32/')) |
348 | | - |
349 | | - write_to_paste_buffer = get_paste_buffer |
350 | | -elif sys.platform == 'darwin': |
351 | | - # Running on Mac OS X |
352 | | - try: |
353 | | - # Warning: subprocess.call() and subprocess.check_call() should never be called with stdout=PIPE or stderr=PIPE |
354 | | - # because the child process will block if it generates enough output to a pipe to fill up the OS pipe buffer. |
355 | | - # Starting with Python 3.5 there is a newer, safer API based on the run() function. |
356 | | - |
357 | | - # Python 3.3+ supports subprocess.DEVNULL, but that isn't defined for Python 2.7 |
358 | | - with open(os.devnull, 'w') as DEVNULL: |
359 | | - # test for pbcopy - AFAIK, should always be installed on MacOS |
360 | | - subprocess.check_call(['pbcopy', '-help'], stdin=subprocess.PIPE, stdout=DEVNULL, stderr=DEVNULL) |
361 | | - can_clip = True |
362 | | - except (subprocess.CalledProcessError, OSError, IOError): |
363 | | - pass |
364 | | - if can_clip: |
365 | | - def get_paste_buffer(): |
366 | | - """Get the contents of the clipboard for Mac OS X. |
367 | | -
|
368 | | - :return: str - contents of the clipboard |
369 | | - """ |
370 | | - pbcopyproc = subprocess.Popen('pbpaste', stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
371 | | - stderr=subprocess.PIPE) |
372 | | - stdout, stderr = pbcopyproc.communicate() |
373 | | - if six.PY3: |
374 | | - return stdout.decode() |
375 | | - else: |
376 | | - return stdout |
| 315 | +def get_paste_buffer(): |
| 316 | + """Get the contents of the clipboard / paste buffer. |
377 | 317 |
|
378 | | - def write_to_paste_buffer(txt): |
379 | | - """Paste text to the clipboard for Mac OS X. |
| 318 | + :return: str - contents of the clipboard |
| 319 | + """ |
| 320 | + pb_unicode = pyperclip.paste() |
380 | 321 |
|
381 | | - :param txt: str - text to paste to the clipboard |
382 | | - """ |
383 | | - pbcopyproc = subprocess.Popen('pbcopy', stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
384 | | - stderr=subprocess.PIPE) |
385 | | - if six.PY3: |
386 | | - pbcopyproc.communicate(txt.encode()) |
387 | | - else: |
388 | | - pbcopyproc.communicate(txt) |
| 322 | + if six.PY3: |
| 323 | + pb_str = pb_unicode |
389 | 324 | else: |
390 | | - # noinspection PyUnusedLocal |
391 | | - def get_paste_buffer(*args): |
392 | | - """For Mac OS X without the appropriate tool installed to get text from clipboard, raise an exception.""" |
393 | | - raise OSError(pastebufferr % ('pbcopy', |
394 | | - 'On MacOS X - error should not occur - part of the default installation')) |
395 | | - |
396 | | - write_to_paste_buffer = get_paste_buffer |
397 | | -else: |
398 | | - # Running on Linux |
399 | | - try: |
400 | | - with open(os.devnull, 'w') as DEVNULL: |
401 | | - subprocess.check_call(['uptime', '|', 'xclip'], stdin=subprocess.PIPE, stdout=DEVNULL, stderr=DEVNULL) |
402 | | - can_clip = True |
403 | | - except (subprocess.CalledProcessError, OSError, IOError): |
404 | | - pass # something went wrong with xclip and we cannot use it |
405 | | - if can_clip: |
406 | | - def get_paste_buffer(): |
407 | | - """Get the contents of the clipboard for Linux OSes. |
408 | | -
|
409 | | - :return: str - contents of the clipboard |
410 | | - """ |
411 | | - xclipproc = subprocess.Popen(['xclip', '-o', '-selection', 'clipboard'], stdin=subprocess.PIPE, |
412 | | - stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
413 | | - stdout, stderr = xclipproc.communicate() |
414 | | - if six.PY3: |
415 | | - return stdout.decode() |
416 | | - else: |
417 | | - return stdout |
| 325 | + import unicodedata |
| 326 | + pb_str = unicodedata.normalize('NFKD', pb_unicode).encode('ascii', 'ignore') |
418 | 327 |
|
419 | | - def write_to_paste_buffer(txt): |
420 | | - """Paste text to the clipboard for Linux OSes. |
| 328 | + return pb_str |
421 | 329 |
|
422 | | - :param txt: str - text to paste to the clipboard |
423 | | - """ |
424 | | - xclipproc = subprocess.Popen(['xclip', '-selection', 'clipboard'], stdin=subprocess.PIPE, |
425 | | - stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
426 | | - if six.PY3: |
427 | | - xclipproc.stdin.write(txt.encode()) |
428 | | - else: |
429 | | - xclipproc.stdin.write(txt) |
430 | | - xclipproc.stdin.close() |
431 | | - |
432 | | - # but we want it in both the "primary" and "mouse" clipboards |
433 | | - xclipproc = subprocess.Popen(['xclip'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
434 | | - stderr=subprocess.PIPE) |
435 | | - if six.PY3: |
436 | | - xclipproc.stdin.write(txt.encode()) |
437 | | - else: |
438 | | - xclipproc.stdin.write(txt) |
439 | | - xclipproc.stdin.close() |
440 | | - else: |
441 | | - # noinspection PyUnusedLocal |
442 | | - def get_paste_buffer(*args): |
443 | | - """For Linux without the appropriate tool installed to get text from clipboard, raise an exception.""" |
444 | | - raise OSError(pastebufferr % ('xclip', 'On Debian/Ubuntu, install with "sudo apt-get install xclip"')) |
445 | 330 |
|
446 | | - write_to_paste_buffer = get_paste_buffer |
| 331 | +def write_to_paste_buffer(txt): |
| 332 | + """Copy text to the clipboard / paste buffer. |
| 333 | +
|
| 334 | + :param txt: str - text to copy to the clipboard |
| 335 | + """ |
| 336 | + pyperclip.copy(txt) |
447 | 337 |
|
448 | 338 |
|
449 | 339 | class ParsedString(str): |
|
0 commit comments