# asweigart/pyperclip architecture

> Architecture diagram of the GitHub repository asweigart/pyperclip, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-10-05). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/asweigart/pyperclip
- Repository: https://github.com/asweigart/pyperclip

## Overview

##### Project type and purpose
- This is an open-source, cross-platform Python library (“pyperclip”) that provides a simple API for copying to and pasting from the system clipboard.
- It’s not a full-stack app or microservice; it’s a language-level utility that abstracts away platform differences for clipboard I/O.

##### File structure
- Top-level files: setup (Pipfile, pyproject.toml), project metadata (README, LICENSE, CHANGES), docs/, tests/.
- src/pyperclip/: the library’s source code, including __init__.py (public API) and __main__.py (CLI entry point).
- tests/: one or more unit tests.
- docs/: Sphinx configuration and documentation sources.

##### Extract architectural insights from README
- The library exposes two main functions: copy(text) and paste().
- Internally, it selects a backend based on the OS:
- Windows: Win32 API via ctypes
- macOS: pbcopy / pbpaste subprocesses
- Linux: xclip or xsel subprocesses, with a fallback to qtpy/PyQt5
- No server or persistent storage; all operations are in‐process/OS calls.

##### Main components
a. Consumer Application
- Any Python script/module that invokes `import pyperclip` → `pyperclip.copy()` or `pyperclip.paste()`.
b. pyperclip Core (src/pyperclip)
- Public API layer (copy, paste)
- Platform detector (detects os.name or sys.platform)
- Backend dispatcher
c. Platform-specific Clipboard Backends
- Windows Backend: calls Win32 clipboard APIs via ctypes
- macOS Backend: subprocess calls to `/usr/bin/pbcopy` and `/usr/bin/pbpaste`
- Linux Backends: subprocess calls to `xclip` or `xsel`, or Qt clipboard via qtpy/PyQt5
d. Operating System
- Underlying clipboard service
- External command executables for Unix
e. Tests & Docs (supporting components)
- tests/test_pyperclip.py (unit tests)
- docs/ (Sphinx‐generated docs)

##### Relationships and interactions
- Consumer → pyperclip public API → platform detector → selects appropriate backend → backend issues OS call → OS clipboard service
- Optional: subprocess forks on Unix, ctypes calls on Windows.
- Tests invoke API directly to verify round-trip functionality.
- Documentation builds via Sphinx from docs/aign.

##### Key architectural patterns and principles
- Strategy Pattern: multiple backend implementations chosen at runtime.
- Facade Pattern: single unified API (copy, paste) hiding OS‐specific logic.
- Dependency Isolation: external dependencies (xclip, PyQt5) only required for certain platforms.

##### Technologies and external services
- Python standard library (ctypes, subprocess)
- External OS utilities: pbcopy/pbpaste, xclip, xsel
- Optional Python GUI toolkit via qtpy/PyQt5
- Sphinx (docs), pytest (tests), pipenv/pyproject.toml for packaging

## Diagram (Mermaid)

```mermaid
flowchart TD
    Consumer["Consumer Application"]:::support
    Tests["Unit Tests"]:::support
    Docs["Documentation"]:::support

    subgraph "pyperclip Library Internals"
        API["Public API\n(copy, paste)"]:::internals
        CLI["CLI Entry Point"]:::internals
        Detector["Platform Detector\n& Dispatcher"]:::internals
        subgraph "Backends"
            Win["Windows Backend\n(ctypes Win32 API)"]:::internals
            Mac["macOS Backend\n(pbcopy/pbpaste)"]:::internals
            Linux["Linux Backends\n(xclip/xsel/Qt)"]:::internals
        end
    end

    subgraph "OS & External Commands"
        OS["OS Clipboard Service"]:::externals
        ExtCmds["pbcopy / pbpaste\nxclip / xsel"]:::externals
    end

    Metadata["Project Metadata\n& Changelog"]:::support
    Packaging["Packaging & Dependencies"]:::support

    Consumer -->|"copy(text)"| API
    Consumer -->|"paste()"| API
    API --> Detector
    Detector -->|Windows| Win
    Detector -->|macOS| Mac
    Detector -->|Linux| Linux
    Win -->|"ctypes.OpenClipboard()"| OS
    Mac -->|"subprocess.run('pbcopy')"| ExtCmds
    Linux -->|"subprocess.run / Qt"| ExtCmds
    ExtCmds -->|"clipboard I/O"| OS
    OS -->|"return text"| API
    Tests --> API
    Docs --> Metadata
    Docs --> Packaging

    click API "https://github.com/asweigart/pyperclip/blob/master/src/pyperclip/__init__.py"
    click CLI "https://github.com/asweigart/pyperclip/blob/master/src/pyperclip/__main__.py"
    click Detector "https://github.com/asweigart/pyperclip/blob/master/src/pyperclip/__init__.py"
    click Win "https://github.com/asweigart/pyperclip/blob/master/src/pyperclip/__init__.py"
    click Mac "https://github.com/asweigart/pyperclip/blob/master/src/pyperclip/__init__.py"
    click Linux "https://github.com/asweigart/pyperclip/blob/master/src/pyperclip/__init__.py"
    click Tests "https://github.com/asweigart/pyperclip/blob/master/tests/test_pyperclip.py"
    click Docs "https://github.com/asweigart/pyperclip/tree/master/docs/"
    click Metadata "https://github.com/asweigart/pyperclip/blob/master/README.md"
    click Packaging "https://github.com/asweigart/pyperclip/blob/master/pyproject.toml"

    classDef internals fill:#f9f,stroke:#333,stroke-width:1px
    classDef externals fill:#ff9,stroke:#333,stroke-width:1px
    classDef support fill:#9ff,stroke:#333,stroke-width:1px
```
