MCPcopy
hub / github.com/asweigart/pyperclip / determine_clipboard

Function determine_clipboard

src/pyperclip/__init__.py:497–565  ·  view source on GitHub ↗

Determine the OS/platform and set the copy() and paste() functions accordingly.

()

Source from the content-addressed store, hash-verified

495
496# Automatic detection of clipboard mechanisms and importing is done in determine_clipboard():
497def determine_clipboard():
498 '''
499 Determine the OS/platform and set the copy() and paste() functions
500 accordingly.
501 '''
502
503 global Foundation, AppKit, qtpy, PyQt5
504
505 # Setup for the CYGWIN platform:
506 if 'cygwin' in platform.system().lower(): # Cygwin has a variety of values returned by platform.system(), such as 'CYGWIN_NT-6.1'
507 # FIXME: pyperclip currently does not support Cygwin,
508 # see https://github.com/asweigart/pyperclip/issues/55
509 if os.path.exists('/dev/clipboard'):
510 warnings.warn('Pyperclip\'s support for Cygwin is not perfect, see https://github.com/asweigart/pyperclip/issues/55')
511 return init_dev_clipboard_clipboard()
512
513 # Setup for the WINDOWS platform:
514 elif os.name == 'nt' or platform.system() == 'Windows':
515 return init_windows_clipboard()
516
517 if platform.system() == 'Linux' and os.path.isfile('/proc/version'):
518 with open('/proc/version', 'r') as f:
519 if "microsoft" in f.read().lower():
520 return init_wsl_clipboard()
521
522 # Setup for the MAC OS X platform:
523 if os.name == 'mac' or platform.system() == 'Darwin':
524 try:
525 import Foundation # check if pyobjc is installed
526 import AppKit
527 except ImportError:
528 return init_osx_pbcopy_clipboard()
529 else:
530 return init_osx_pyobjc_clipboard()
531
532 # Setup for the LINUX platform:
533
534 if os.getenv("WAYLAND_DISPLAY") and _executable_exists("wl-copy") and _executable_exists("wl-paste"):
535 return init_wl_clipboard()
536
537 # `import PyQt4` sys.exit()s if DISPLAY is not in the environment.
538 # Thus, we need to detect the presence of $DISPLAY manually
539 # and not load PyQt4 if it is absent.
540 elif os.getenv("DISPLAY"):
541 if _executable_exists("xclip"):
542 # Note: 2024/06/18 Google Trends shows xclip as more popular than xsel.
543 return init_xclip_clipboard()
544 if _executable_exists("xsel"):
545 return init_xsel_clipboard()
546 if _executable_exists("klipper") and _executable_exists("qdbus"):
547 return init_klipper_clipboard()
548
549 try:
550 # qtpy is a small abstraction layer that lets you write
551 # applications using a single api call to either PyQt or PySide.
552 # https://pypi.python.org/pypi/QtPy
553 import qtpy # check if qtpy is installed
554 return init_qt_clipboard()

Callers 2

lazy_load_stub_copyFunction · 0.85
lazy_load_stub_pasteFunction · 0.85

Calls 11

init_windows_clipboardFunction · 0.85
init_wsl_clipboardFunction · 0.85
init_wl_clipboardFunction · 0.85
init_xclip_clipboardFunction · 0.85
init_xsel_clipboardFunction · 0.85
init_klipper_clipboardFunction · 0.85
init_qt_clipboardFunction · 0.85
init_no_clipboardFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…