# legalize-dev/legalize-pipeline architecture

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

- Interactive diagram: https://gitdiagram.com/legalize-dev/legalize-pipeline
- Repository: https://github.com/legalize-dev/legalize-pipeline

## Overview

1. **Purpose and boundary**
   - This repository is the engine for `legalize.dev`: it ingests official legislation from many national sources, normalizes it, renders it as Markdown, and records legal changes as Git commits.
   - The main architectural boundary is between **country-specific ingestion** and **generic processing/output**.

2. **Primary runtime**
   - The project is a Python 3.12+ CLI package (`src/legalize/cli.py`) built and tested as a local tool.
   - Git is a core runtime dependency because the final product is not just files, but a version history with dated commits.

3. **Top-level pipeline**
   - `pipeline.py` orchestrates the lifecycle: **fetch → transform → store → commit**, with variants for bootstrap, daily updates, and reprocessing.
   - The pipeline works on a canonical internal model so that all countries follow the same downstream path even when their sources differ radically.

4. **Country-specific fetchers**
   - `src/legalize/fetcher/{country}/` contains the adapter for each jurisdiction.
   - Each country module usually has:
     - a **client** for the source system,
     - a **discovery** layer to enumerate available norms,
     - a **parser** to convert source format into the shared domain model.
   - Some countries need extra helper modules for source-specific behaviors such as bootstrapping, daily deltas, PDF extraction, catalogs, or reform reconstruction.

5. **External source integration**
   - The fetchers connect to heterogeneous public sources: APIs, HTML sites, XML feeds/dumps, PDFs, ZIP archives, SQLite dumps, and sitemap-like discovery mechanisms.
   - The architecture is source-driven rather than protocol-driven; each country module encapsulates how to talk to that legal publisher and how to interpret its publication model.

6. **Shared normalized model**
   - `models.py` defines the cross-country domain objects used after parsing: laws, metadata, blocks/sections, and reform-related structures.
   - This normalized model is what allows the rest of the system to be country-agnostic.

7. **Generic transformation layer**
   - `transformer/` converts parsed legal structures into Markdown and frontmatter.
   - `xml_parser.py` extracts logical blocks and version/reform timelines; `markdown.py` handles rendering; `frontmatter.py` adds metadata; `slug.py` maps a norm to its output path.
   - This is the main abstraction layer between source-specific parsing and Git output.

8. **Git as the output medium**
   - `committer/` turns transformed Markdown into repository history.
   - `git_ops.py` manages file writes and commits with historical dates, `message.py` standardizes commit messages, and `author.py` derives the committer identity from local Git config.
   - The architectural output is therefore a repository of law files where each legal change becomes a commit.

9. **State, cache, and intermediate storage**
   - `storage.py` persists fetched raw artifacts and parsed metadata under a local `data/` area before commit.
   - `state/store.py` tracks pipeline progress, last processed items, and run history so incremental jobs can resume safely and daily jobs can detect deltas.
   - `fetcher/cache.py` supports repeatable fetches and reduces unnecessary remote requests.

10. **Configuration and registry**
   - `config.py` and `config.yaml` define country-specific settings and operational parameters.
   - `countries.py` is the registry/dispatch boundary that maps a country code to the right fetcher implementation through lazy imports.
   - This keeps the pipeline generic while allowing each jurisdiction to carry its own source-specific configuration.

11. **Automation and operations**
   - GitHub Actions workflows drive scheduled and operational jobs: bootstrap, CI, daily updates, monthly updates, and backfills.
   - This repo is therefore not just a library; it is an automation-controlled ingestion system that can run unattended on a schedule.

12. **Testing and fixtures**
   - The `tests/fixtures/` tree contains captured source documents for many countries and formats, which is essential because correctness depends on parsing real-world legal source quirks.
   - The test suite is heavily parser-focused, plus integration tests for multi-country behavior, pipeline behavior, caching, and daily update flows.
   - For architecture diagrams, the most important test boundary is that each country adapter is independently validated against source snapshots before it reaches the shared transformer/committer path.

Main entry points: [`src/legalize/pipeline.py`](https://github.com/legalize-dev/legalize-pipeline/blob/main/src/legalize/pipeline.py) (Pipeline), [`src/legalize/fetcher`](https://github.com/legalize-dev/legalize-pipeline/tree/main/src/legalize/fetcher) (Country fetchers), [`src/legalize/committer`](https://github.com/legalize-dev/legalize-pipeline/tree/main/src/legalize/committer) (Committer), [`src/legalize/countries.py`](https://github.com/legalize-dev/legalize-pipeline/blob/main/src/legalize/countries.py) (Country registry)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime"]
  node_cli["CLI<br/>python entrypoint<br/>[cli.py]"]
  node_pipeline["Pipeline<br/>orchestrator<br/>[pipeline.py]"]
  node_countries["Country registry<br/>dispatcher<br/>[countries.py]"]
end

subgraph group_ingestion["Ingestion"]
  node_base_fetcher["Fetcher base<br/>adapter base<br/>[base.py]"]
  node_fetch_cache["Fetch cache<br/>cache layer<br/>[cache.py]"]
  node_country_fetchers["Country fetchers<br/>jurisdiction adapters"]
  node_country_variants["Special flows<br/>country extensions"]
end

subgraph group_core["Core Model"]
  node_models["Domain model<br/>normalized schema<br/>[models.py]"]
  node_xml_parser["XML parser<br/>structure extractor<br/>[xml_parser.py]"]
  node_markdown["Markdown<br/>renderer<br/>[markdown.py]"]
  node_frontmatter["Frontmatter<br/>metadata renderer<br/>[frontmatter.py]"]
  node_slug["Slug map<br/>path mapper<br/>[slug.py]"]
end

subgraph group_output["Git Output"]
  node_committer["Committer<br/>git writer"]
  node_git_ops["Git ops<br/>git integration<br/>[git_ops.py]"]
  node_message["Commit msg<br/>message formatter<br/>[message.py]"]
  node_author["Author<br/>identity resolver<br/>[author.py]"]
end

subgraph group_ops["State and Ops"]
  node_storage["Storage<br/>artifact store<br/>[storage.py]"]
  node_state_store["State store<br/>run state<br/>[store.py]"]
  node_config["Config<br/>settings<br/>[config.py]"]
end

node_cli -->|"runs"| node_pipeline
node_pipeline -->|"dispatches"| node_countries
node_countries -->|"loads"| node_country_fetchers
node_country_fetchers -->|"implements"| node_base_fetcher
node_country_fetchers -->|"uses"| node_fetch_cache
node_country_fetchers -->|"parses to"| node_models
node_country_variants -.->|"extends"| node_country_fetchers
node_pipeline -->|"stores"| node_storage
node_pipeline -->|"tracks"| node_state_store
node_pipeline -->|"reads"| node_config
node_models -->|"feeds"| node_xml_parser
node_xml_parser -->|"renders"| node_markdown
node_markdown -->|"annotates"| node_frontmatter
node_frontmatter -->|"paths"| node_slug
node_slug -->|"hands off"| node_committer
node_committer -->|"writes"| node_git_ops
node_committer -->|"formats"| node_message
node_committer -->|"resolves"| node_author
node_pipeline -->|"finalizes"| node_committer
node_storage -.->|"supports"| node_state_store

click node_cli "https://github.com/legalize-dev/legalize-pipeline/blob/main/src/legalize/cli.py"
click node_pipeline "https://github.com/legalize-dev/legalize-pipeline/blob/main/src/legalize/pipeline.py"
click node_countries "https://github.com/legalize-dev/legalize-pipeline/blob/main/src/legalize/countries.py"
click node_base_fetcher "https://github.com/legalize-dev/legalize-pipeline/blob/main/src/legalize/fetcher/base.py"
click node_fetch_cache "https://github.com/legalize-dev/legalize-pipeline/blob/main/src/legalize/fetcher/cache.py"
click node_country_fetchers "https://github.com/legalize-dev/legalize-pipeline/tree/main/src/legalize/fetcher"
click node_country_variants "https://github.com/legalize-dev/legalize-pipeline/tree/main/src/legalize/fetcher"
click node_models "https://github.com/legalize-dev/legalize-pipeline/blob/main/src/legalize/models.py"
click node_xml_parser "https://github.com/legalize-dev/legalize-pipeline/blob/main/src/legalize/transformer/xml_parser.py"
click node_markdown "https://github.com/legalize-dev/legalize-pipeline/blob/main/src/legalize/transformer/markdown.py"
click node_frontmatter "https://github.com/legalize-dev/legalize-pipeline/blob/main/src/legalize/transformer/frontmatter.py"
click node_slug "https://github.com/legalize-dev/legalize-pipeline/blob/main/src/legalize/transformer/slug.py"
click node_committer "https://github.com/legalize-dev/legalize-pipeline/tree/main/src/legalize/committer"
click node_git_ops "https://github.com/legalize-dev/legalize-pipeline/blob/main/src/legalize/committer/git_ops.py"
click node_message "https://github.com/legalize-dev/legalize-pipeline/blob/main/src/legalize/committer/message.py"
click node_author "https://github.com/legalize-dev/legalize-pipeline/blob/main/src/legalize/committer/author.py"
click node_storage "https://github.com/legalize-dev/legalize-pipeline/blob/main/src/legalize/storage.py"
click node_state_store "https://github.com/legalize-dev/legalize-pipeline/blob/main/src/legalize/state/store.py"
click node_config "https://github.com/legalize-dev/legalize-pipeline/blob/main/src/legalize/config.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_pipeline,node_countries toneBlue
class node_base_fetcher,node_fetch_cache,node_country_fetchers,node_country_variants toneAmber
class node_models,node_xml_parser,node_markdown,node_frontmatter,node_slug toneMint
class node_committer,node_git_ops,node_message,node_author toneRose
class node_storage,node_state_store,node_config toneIndigo
```

## Components

### Runtime

Python CLI and orchestration

- [CLI](https://github.com/legalize-dev/legalize-pipeline/blob/main/src/legalize/cli.py): python entrypoint · Command-line interface for running the pipeline · `src/legalize/cli.py`
- [Pipeline](https://github.com/legalize-dev/legalize-pipeline/blob/main/src/legalize/pipeline.py): orchestrator · Coordinates fetch, transform, store, and commit flows · `src/legalize/pipeline.py`
- [Country registry](https://github.com/legalize-dev/legalize-pipeline/blob/main/src/legalize/countries.py): dispatcher · Maps country codes to fetcher implementations · `src/legalize/countries.py`

### Ingestion

Country-specific fetchers and source adapters

- [Fetcher base](https://github.com/legalize-dev/legalize-pipeline/blob/main/src/legalize/fetcher/base.py): adapter base · Shared fetcher interfaces and common behavior · `src/legalize/fetcher/base.py`
- [Fetch cache](https://github.com/legalize-dev/legalize-pipeline/blob/main/src/legalize/fetcher/cache.py): cache layer · Caches remote fetches and reduces repeated requests · `src/legalize/fetcher/cache.py`
- [Country fetchers](https://github.com/legalize-dev/legalize-pipeline/tree/main/src/legalize/fetcher): jurisdiction adapters · Per-country client, discovery, and parser implementations · `src/legalize/fetcher`
- [Special flows](https://github.com/legalize-dev/legalize-pipeline/tree/main/src/legalize/fetcher): country extensions · Bootstrap, daily, PDF, catalog, and reconstruction helpers · `src/legalize/fetcher`

### Core Model

Shared normalization and transformation

- [Domain model](https://github.com/legalize-dev/legalize-pipeline/blob/main/src/legalize/models.py): normalized schema · Cross-country laws, metadata, sections, and reform structures · `src/legalize/models.py`
- [XML parser](https://github.com/legalize-dev/legalize-pipeline/blob/main/src/legalize/transformer/xml_parser.py): structure extractor · Extracts blocks, versions, and reform timelines from legal sources · `src/legalize/transformer/xml_parser.py`
- [Markdown](https://github.com/legalize-dev/legalize-pipeline/blob/main/src/legalize/transformer/markdown.py): renderer · Renders normalized legal content to Markdown · `src/legalize/transformer/markdown.py`
- [Frontmatter](https://github.com/legalize-dev/legalize-pipeline/blob/main/src/legalize/transformer/frontmatter.py): metadata renderer · Adds document metadata for generated output · `src/legalize/transformer/frontmatter.py`
- [Slug map](https://github.com/legalize-dev/legalize-pipeline/blob/main/src/legalize/transformer/slug.py): path mapper · Maps a norm to its output path · `src/legalize/transformer/slug.py`

### Git Output

Commit and repository history

- [Committer](https://github.com/legalize-dev/legalize-pipeline/tree/main/src/legalize/committer): git writer · Turns transformed Markdown into repository history · `src/legalize/committer`
- [Git ops](https://github.com/legalize-dev/legalize-pipeline/blob/main/src/legalize/committer/git_ops.py): git integration · Writes files and creates dated commits · `src/legalize/committer/git_ops.py`
- [Commit msg](https://github.com/legalize-dev/legalize-pipeline/blob/main/src/legalize/committer/message.py): message formatter · Standardizes commit messages · `src/legalize/committer/message.py`
- [Author](https://github.com/legalize-dev/legalize-pipeline/blob/main/src/legalize/committer/author.py): identity resolver · Derives committer identity from local Git config · `src/legalize/committer/author.py`

### State and Ops

Storage, state, and config

- [Storage](https://github.com/legalize-dev/legalize-pipeline/blob/main/src/legalize/storage.py): artifact store · Persists raw fetched artifacts and parsed metadata under data/ · `src/legalize/storage.py`
- [State store](https://github.com/legalize-dev/legalize-pipeline/blob/main/src/legalize/state/store.py): run state · Tracks progress, last processed items, and run history · `src/legalize/state/store.py`
- [Config](https://github.com/legalize-dev/legalize-pipeline/blob/main/src/legalize/config.py): settings · Operational and country-specific configuration · `src/legalize/config.py`

## Connections

- CLI → Pipeline: runs · CLI invokes the orchestration pipeline
- Pipeline → Country registry: dispatches · Pipeline selects the country adapter
- Country registry → Country fetchers: loads · Registry resolves the jurisdiction module
- Country fetchers → Fetcher base: implements · Country adapters share the common fetcher boundary
- Country fetchers → Fetch cache: uses · Adapters reuse cached remote fetches
- Country fetchers → Domain model: parses to · Country parsers normalize source data into the shared model
- Special flows → Country fetchers: extends · Special helpers augment specific jurisdiction flows
- Pipeline → Storage: stores · Pipeline persists fetched artifacts and metadata
- Pipeline → State store: tracks · Pipeline records progress and resumable job state
- Pipeline → Config: reads · Pipeline uses runtime and country configuration
- Domain model → XML parser: feeds · Shared model is populated from parsed legal structure
- XML parser → Markdown: renders · Logical structure is converted into Markdown
- Markdown → Frontmatter: annotates · Generated content gets metadata headers
- Frontmatter → Slug map: paths · Metadata and identity determine output locations
- Slug map → Committer: hands off · Path mapping feeds the git output stage
- Committer → Git ops: writes · Committer performs file writes and commits
- Committer → Commit msg: formats · Committer standardizes history messages
- Committer → Author: resolves · Committer derives the Git author identity
- Pipeline → Committer: finalizes · Pipeline hands transformed output to Git history
- Storage → State store: supports · Stored artifacts and state together enable incremental reruns
