How legalize-dev/legalize-pipeline works
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: pipeline.py Pipeline · fetcher Country fetchers · committer Committer · countries.py Country registry
Components 19
Runtime
Python CLI and orchestration
- CLI · Command-line interface for running the pipeline
src/legalize/cli.py - Pipeline · Coordinates fetch, transform, store, and commit flows
src/legalize/pipeline.py - Country registry · Maps country codes to fetcher implementations
src/legalize/countries.py
Ingestion
Country-specific fetchers and source adapters
- Fetcher base · Shared fetcher interfaces and common behavior
src/legalize/fetcher/base.py - Fetch cache · Caches remote fetches and reduces repeated requests
src/legalize/fetcher/cache.py - Country fetchers · Per-country client, discovery, and parser implementations
src/legalize/fetcher - Special flows · Bootstrap, daily, PDF, catalog, and reconstruction helpers
src/legalize/fetcher
Core Model
Shared normalization and transformation
- Domain model · Cross-country laws, metadata, sections, and reform structures
src/legalize/models.py - XML parser · Extracts blocks, versions, and reform timelines from legal sources
src/legalize/transformer/xml_parser.py - Markdown · Renders normalized legal content to Markdown
src/legalize/transformer/markdown.py - Frontmatter · Adds document metadata for generated output
src/legalize/transformer/frontmatter.py - Slug map · Maps a norm to its output path
src/legalize/transformer/slug.py
Git Output
Commit and repository history
- Committer · Turns transformed Markdown into repository history
src/legalize/committer - Git ops · Writes files and creates dated commits
src/legalize/committer/git_ops.py - Commit msg · Standardizes commit messages
src/legalize/committer/message.py - Author · Derives committer identity from local Git config
src/legalize/committer/author.py
State and Ops
Storage, state, and config
- Storage · Persists raw fetched artifacts and parsed metadata under data/
src/legalize/storage.py - State store · Tracks progress, last processed items, and run history
src/legalize/state/store.py - Config · Operational and country-specific configuration
src/legalize/config.py
Connections 20
- 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