# pylakey/pylogram architecture

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

- Interactive diagram: https://gitdiagram.com/pylakey/pylogram
- Repository: https://github.com/pylakey/pylogram

## Overview

##### Project type and purpose
- Pylogram is primarily a Python SDK/library offering an asynchronous MTProto API client for Telegram (users and bots).
- It embeds a “compiler” subproject that, at build time, consumes TL schema files (source templates, API definitions, error codes) and generates Python modules (raw objects, methods, types, errors, docs).

##### File structure
- Top-level directories:
- compiler/ – code‐generation engine (reads `.tl` and `.tsv` sources, applies templates, emits Python and docs)
- pylogram/ – runtime library (modules for connection, crypto, session, parsing, dispatching, methods, types, storage, handlers, utilities)
- tests/ – unit tests for filters, parser, file IDs, etc.
- Config and build scripts: hatch.toml, hatch_build.py, dev-requirements.txt, CI workflow `.github/workflows/build_and_publish.yml`.

##### README for additional insights
- Confirms async design, high-performance crypto (TgCrypto), type hints, event‐driven handlers, support for both user accounts and bot tokens.

##### Detail without overcomplication
- Interfaces/interactions, not internal method details.
- Build-time vs. runtime are visually separated (e.g., top vs. bottom or left vs. right).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Entities
    ClientApp["Client Application"]:::external
    TelegramServer["Telegram MTProto Server"]:::external

    %% Build-time Compiler
    subgraph "Build-time Compiler" 
        direction TB
        CompilerCore["Compiler Core"]:::compiler
        SourceTL["TL Schema Sources"]:::compiler
        SourceTSV["Error TSV Sources"]:::compiler
        TemplateAPI["API Jinja Templates"]:::compiler
        TemplateErr["Error Jinja Templates"]:::compiler
        DocsGen["Generated Docs"]:::compiler
        CompilerCore -->|reads| SourceTL
        CompilerCore -->|reads| SourceTSV
        CompilerCore -->|applies| TemplateAPI
        CompilerCore -->|applies| TemplateErr
        CompilerCore -->|outputs| DocsGen
    end

    click CompilerCore "https://github.com/pylakey/pylogram/blob/main/compiler/api/compiler.py"
    click CompilerCore "https://github.com/pylakey/pylogram/blob/main/compiler/errors/compiler.py"
    click DocsGen "https://github.com/pylakey/pylogram/blob/main/compiler/docs/compiler.py"
    click SourceTL "https://github.com/pylakey/pylogram/tree/main/compiler/api/source/"
    click SourceTSV "https://github.com/pylakey/pylogram/tree/main/compiler/errors/source/"
    click TemplateAPI "https://github.com/pylakey/pylogram/tree/main/compiler/api/template/"
    click TemplateErr "https://github.com/pylakey/pylogram/tree/main/compiler/errors/template/"

    %% Generated Code Output
    GeneratedRaw["pylogram/raw"]:::generated
    GeneratedTypes["pylogram/types"]:::generated
    GeneratedMethods["pylogram/methods"]:::generated
    GeneratedErrors["pylogram/errors"]:::generated

    click GeneratedRaw "https://github.com/pylakey/pylogram/tree/main/pylogram/raw/"
    click GeneratedTypes "https://github.com/pylakey/pylogram/tree/main/pylogram/types/"
    click GeneratedMethods "https://github.com/pylakey/pylogram/tree/main/pylogram/methods/"
    click GeneratedErrors "https://github.com/pylakey/pylogram/tree/main/pylogram/errors/"

    CompilerCore -->|generates| GeneratedRaw
    CompilerCore -->|generates| GeneratedTypes
    CompilerCore -->|generates| GeneratedMethods
    CompilerCore -->|generates| GeneratedErrors

    %% Runtime Library
    subgraph "Pylogram Runtime Library"
        direction TB

        %% API Layer
        ClientAPI["Client API"]:::api
        Filters["Filters & Decorators"]:::api
        Middleware["Middleware"]:::api

        click ClientAPI "https://github.com/pylakey/pylogram/blob/main/pylogram/client.py"
        click Filters "https://github.com/pylakey/pylogram/blob/main/pylogram/filters.py"
        click Middleware "https://github.com/pylakey/pylogram/blob/main/pylogram/middleware.py"
        click Filters "https://github.com/pylakey/pylogram/tree/main/pylogram/methods/decorators/"
        click HandlersDir "https://github.com/pylakey/pylogram/tree/main/pylogram/handlers/"

        %% Methods & Types
        MethodsTypes["Methods & Types"]:::runtime
        click MethodsTypes "https://github.com/pylakey/pylogram/tree/main/pylogram/methods/decorators/"
        click GeneratedMethods "https://github.com/pylakey/pylogram/tree/main/pylogram/methods/"
        click GeneratedTypes "https://github.com/pylakey/pylogram/tree/main/pylogram/types/"
        click GeneratedErrors "https://github.com/pylakey/pylogram/tree/main/pylogram/errors/"

        %% Connection & Transport
        ConnectionMgr["Connection Manager"]:::connection
        click ConnectionMgr "https://github.com/pylakey/pylogram/blob/main/pylogram/connection/connection.py"
        subgraph "Transport Layer (TCP)" 
            direction TB
            TCPBase["tcp.py"]:::transport
            TCPAbr["tcp_abridged.py"]:::transport
            TCPAbrO["tcp_abridged_o.py"]:::transport
            TCPFull["tcp_full.py"]:::transport
            TCPInt["tcp_intermediate.py"]:::transport
            TCPIntO["tcp_intermediate_o.py"]:::transport
        end
        click TCPBase "https://github.com/pylakey/pylogram/blob/main/pylogram/connection/transport/tcp/tcp.py"
        click TCPAbr "https://github.com/pylakey/pylogram/blob/main/pylogram/connection/transport/tcp/tcp_abridged.py"
        click TCPAbrO "https://github.com/pylakey/pylogram/blob/main/pylogram/connection/transport/tcp/tcp_abridged_o.py"
        click TCPFull "https://github.com/pylakey/pylogram/blob/main/pylogram/connection/transport/tcp/tcp_full.py"
        click TCPInt "https://github.com/pylakey/pylogram/blob/main/pylogram/connection/transport/tcp/tcp_intermediate.py"
        click TCPIntO "https://github.com/pylakey/pylogram/blob/main/pylogram/connection/transport/tcp/tcp_intermediate_o.py"

        %% Crypto
        CryptoMod["Crypto Module"]:::crypto
        click CryptoMod "https://github.com/pylakey/pylogram/blob/main/pylogram/crypto/mtproto.py"
        click AES "https://github.com/pylakey/pylogram/blob/main/pylogram/crypto/aes.py"
        click RSA "https://github.com/pylakey/pylogram/blob/main/pylogram/crypto/rsa.py"
        click Prime "https://github.com/pylakey/pylogram/blob/main/pylogram/crypto/prime.py"

        %% Parser
        ParserMod["Parser"]:::parser
        click ParserMod "https://github.com/pylakey/pylogram/blob/main/pylogram/parser/parser.py"
        click HTML "https://github.com/pylakey/pylogram/blob/main/pylogram/parser/html.py"
        click MD "https://github.com/pylakey/pylogram/blob/main/pylogram/parser/markdown.py"
        click PUtils "https://github.com/pylakey/pylogram/blob/main/pylogram/parser/utils.py"

        %% Session Manager
        SessionMgr["Session Manager"]:::session
        click SessionMgr "https://github.com/pylakey/pylogram/blob/main/pylogram/session/session.py"
        click Auth "https://github.com/pylakey/pylogram/blob/main/pylogram/session/auth.py"
        click MsgID "https://github.com/pylakey/pylogram/blob/main/pylogram/session/internals/msg_id.py"
        click SeqNo "https://github.com/pylakey/pylogram/blob/main/pylogram/session/internals/seq_no.py"
        click DC "https://github.com/pylakey/pylogram/blob/main/pylogram/session/internals/data_center.py"
        click MsgFactory "https://github.com/pylakey/pylogram/blob/main/pylogram/session/internals/msg_factory.py"

        %% Dispatcher & Handlers
        Dispatcher["Dispatcher"]:::dispatch
        HandlersDir["Handlers"]:::dispatch
        click Dispatcher "https://github.com/pylakey/pylogram/blob/main/pylogram/dispatcher.py"
        click HandlersDir "https://github.com/pylakey/pylogram/tree/main/pylogram/handlers/"

        %% Storage
        StorageMod["Storage Module"]:::storage
        click StorageMod "https://github.com/pylakey/pylogram/blob/main/pylogram/storage/storage.py"
        click MemStore "https://github.com/pylakey/pylogram/blob/main/pylogram/storage/memory_storage.py"
        click FileStore "https://github.com/pylakey/pylogram/blob/main/pylogram/storage/file_storage.py"
        click SQLiteStore "https://github.com/pylakey/pylogram/blob/main/pylogram/storage/sqlite_storage.py"

        %% Utilities
        UtilsMod["Utilities & Constants"]:::util
        click UtilsMod "https://github.com/pylakey/pylogram/blob/main/pylogram/utils.py"
        click Const "https://github.com/pylakey/pylogram/blob/main/pylogram/constants.py"
        click Enums "https://github.com/pylakey/pylogram/tree/main/pylogram/enums/"
        click Mime "https://github.com/pylakey/pylogram/blob/main/pylogram/mime_types.py"
        click FileID "https://github.com/pylakey/pylogram/blob/main/pylogram/file_id.py"
        click Peers "https://github.com/pylakey/pylogram/blob/main/pylogram/peers.py"
        click TVars "https://github.com/pylakey/pylogram/blob/main/pylogram/typevars.py"
    end

    %% Unit Tests
    Tests["Unit Tests"]:::test
    click Tests "https://github.com/pylakey/pylogram/tree/main/tests/filters/"
    click Tests "https://github.com/pylakey/pylogram/tree/main/tests/parser/"
    click Tests "https://github.com/pylakey/pylogram/blob/main/tests/test_file_id.py"

    %% Runtime Flow
    ClientApp -->|"uses"| ClientAPI
    ClientAPI -->|"invokes"| MethodsTypes
    MethodsTypes -->|"calls"| ConnectionMgr
    ConnectionMgr -->|"selects"| TCPBase
    TCPBase --> CryptoMod
    CryptoMod -->|encrypted| TelegramServer

    TelegramServer -->|response| CryptoMod
    CryptoMod --> ParserMod
    ParserMod --> Dispatcher
    Dispatcher --> HandlersDir
    HandlersDir --> ClientApp

    SessionMgr --> StorageMod
    ClientAPI --> SessionMgr

    %% Styling
    classDef external fill:#f9f,stroke:#333,stroke-width:1px;
    classDef compiler fill:#ccf,stroke:#333,stroke-width:1px,stroke-dasharray: 5 5;
    classDef generated fill:#eef,stroke:#333,stroke-width:1px;
    classDef api fill:#cfc,stroke:#333,stroke-width:1px;
    classDef connection fill:#fcf,stroke:#333,stroke-width:1px;
    classDef transport fill:#fcc,stroke:#333,stroke-width:1px;
    classDef crypto fill:#ffc,stroke:#333,stroke-width:1px;
    classDef parser fill:#cff,stroke:#333,stroke-width:1px;
    classDef session fill:#f9c,stroke:#333,stroke-width:1px;
    classDef dispatch fill:#c9f,stroke:#333,stroke-width:1px;
    classDef storage fill:#9cf,stroke:#333,stroke-width:1px;
    classDef util fill:#ccc,stroke:#333,stroke-width:1px;
    classDef test fill:#fdd,stroke:#333,stroke-width:1px;
```
