# ready-dl/pyplayready architecture

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

- Interactive diagram: https://gitdiagram.com/ready-dl/pyplayready
- Repository: https://github.com/ready-dl/pyplayready

## Overview

Based on our analysis of the provided file tree and README, this project (“pyplayready”) is an open-source Python tool designed for handling DRM operations—specifically for interacting with and testing Microsoft Playready devices by generating and processing license challenges. The project is organized as a Python package with several clearly delineated submodules for distinct responsibilities, including cryptography, device handling, licensing, system utilities, and remote interactions.

##### Main Components
- Command-Line Interface & Entry Point: Although the README shows a “pyplayready” command usage and a main module (pyplayready/main.py), consider this as the starting point that orchestrates the overall workflow.
- Device Module: Represented by pyplayready/device (e.g., Device.load() and related “structs.py”), this component handles the creation, loading, validation, and export of DRM devices.
- Content Decryption Module (CDM): Found in pyplayready/cdm.py, represents the central encryption/decryption session management and DRM challenge generation/response parsing.
- Cryptography Utilities: The pyplayready/crypto submodule (including files for ECC keys and ElGamal) encapsulates cryptographic functions used in key generation and possibly signature validation.
- License Handling: Under pyplayready/license (with submodules for handling key formats like XML and xmrlicense), this part deals with license key extraction and formatting.
- Remote Operations: The remote submodule (pyplayready/remote) contains modules such as remotecdm.py and serve.py, likely used for exposing functionality over a network or integrating with external services.

##### Relationships and Interactions
- The CLI/Main Entry (pyplayready/main.py) initiates actions that call upon the Device module to load or create a DRM device (e.g., via create-device/test/export-device operations).
- The loaded device is then passed to the CDM, which manages a DRM session—opening a session, generating license challenges, and eventually parsing the license responses.
- The CDM component interacts with system utilities (e.g., PSSH generation) to format DRM-specific data for communication with the external license server.
- Cryptographic procedures in the crypto submodule support operations both in the CDM and license modules to securely handle keys and certificates.
- Remote modules might provide additional interfaces (perhaps for remote control or serving DRM functionality) that hook into the core functionalities.

##### Architectural Patterns and Design Principles
- Modularization: The project distinctly separates concerns into different submodules (device, cdm, crypto, license, system, remote), which is a form of a modular or layered architecture.
- Command-Line Driven Workflow: The CLI usage indicates a pipeline where input commands traverse through various modules to eventually reach external DRM services.
- Integration with External Services: The sample code snippet in the README shows interactions with an external license server using HTTP, meaning network communication is an integral part of the design.

##### Relevant Technologies, Frameworks, and Libraries
- Python is the language and package management is handled via pip (with pyproject.toml and requirements.txt indicating dependency management).
- Cryptographic operations likely leverage Python’s underlying libraries for ECC and ElGamal encryption.
- HTTP requests (using the “requests” library, as shown in the code snippet) are used for communication with DRM/Playready license services.

##### Specific Elements
- From CLI → Device Handler (for device creation/validation).
- From Device Handler → CDM (to initiate a DRM session).
- From CDM → System Utilities (to generate PSSH and related data).
- From CDM → External License Server (via HTTP request).
- From License Response → CDM (for parsing and key extraction).
- Support from the Crypto and License modules towards both CDM and Device components.
- Core business logic (e.g., CDM, Device Handler)
- Cryptographic functions (Crypto Utilities)
- Infrastructure/external communication (HTTP requests, Remote Interfaces)
- Utility modules (System Utilities)

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Core Workflow"
        CLI["CLI/Main"]:::cli
        Device["Device Handler"]:::device
        CDM["Content Decryption Module (CDM)"]:::cdm
        External["External License Server"]:::external
        License["License Handling"]:::license
    end

    subgraph "Support Modules"
        Crypto["Crypto Utilities"]:::crypto
        System["System Utilities (PSSH, Session)"]:::system
        Remote["Remote Operations"]:::remote
    end

    CLI -->|"initiates"| Device
    Device -->|"loads-device"| CDM
    CDM -->|"sends-license-request"| External
    External -->|"returns-license"| CDM
    CDM -->|"parses-license"| License
    CDM -->|"generates-PSSH"| System

    Crypto -->|"provides-crypto"| Device
    Crypto -->|"provides-crypto"| CDM
    Remote -->|"interfaces"| CDM

    click CLI "https://github.com/ready-dl/pyplayready/blob/main/pyplayready/main.py"
    click Device "https://github.com/ready-dl/pyplayready/tree/main/pyplayready/device"
    click CDM "https://github.com/ready-dl/pyplayready/blob/main/pyplayready/cdm.py"
    click Crypto "https://github.com/ready-dl/pyplayready/tree/main/pyplayready/crypto"
    click License "https://github.com/ready-dl/pyplayready/tree/main/pyplayready/license"
    click System "https://github.com/ready-dl/pyplayready/tree/main/pyplayready/system"
    click Remote "https://github.com/ready-dl/pyplayready/tree/main/pyplayready/remote"

    classDef cli fill:#F7B2AD,stroke:#333,stroke-width:2px;
    classDef device fill:#B2F7EF,stroke:#333,stroke-width:2px;
    classDef cdm fill:#FFD280,stroke:#333,stroke-width:2px;
    classDef crypto fill:#C1E1C1,stroke:#333,stroke-width:2px;
    classDef license fill:#FDFD96,stroke:#333,stroke-width:2px;
    classDef system fill:#ADD8E6,stroke:#333,stroke-width:2px;
    classDef remote fill:#DDBDF1,stroke:#333,stroke-width:2px;
    classDef external fill:#D3D3D3,stroke:#333,stroke-width:2px;
```
