Metadata-Version: 2.4
Name: fluxconf
Version: 0.0.1
Summary: File-backed pydantic configuration with migration support
Project-URL: Repository, https://github.com/Greenroom-Robotics/fluxconf
Project-URL: Issues, https://github.com/Greenroom-Robotics/fluxconf/issues
Project-URL: Homepage, https://github.com/Greenroom-Robotics/fluxconf
Author-email: David Revay <david.revay@greenroomrobotics.com>
License: MIT License
        
        Copyright (c) 2026 Greenroom Robotics
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Keywords: config,config-io,fluxconf,migration,pydantic,pydantic-config,yaml-configuration
Classifier: Framework :: Pydantic :: 2
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: File Formats
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Python: >=3.10
Requires-Dist: pydantic>=2.12.3
Requires-Dist: pyyaml>=6.0
Provides-Extra: dev
Requires-Dist: codespell; extra == 'dev'
Requires-Dist: coverage; extra == 'dev'
Requires-Dist: mypy; extra == 'dev'
Requires-Dist: pre-commit; extra == 'dev'
Requires-Dist: pytest; extra == 'dev'
Requires-Dist: ruff; extra == 'dev'
Requires-Dist: types-pyyaml; extra == 'dev'
Provides-Extra: test
Requires-Dist: coverage; extra == 'test'
Requires-Dist: pytest; extra == 'test'
Description-Content-Type: text/markdown

# fluxconf

[![Tests](https://github.com/Greenroom-Robotics/fluxconf/actions/workflows/tests.yml/badge.svg)](https://github.com/Greenroom-Robotics/fluxconf/actions/workflows/tests.yml)
[![PyPI version](https://badge.fury.io/py/fluxconf.svg)](https://badge.fury.io/py/fluxconf)
![Supported versions](https://img.shields.io/badge/python-3.10+-blue.svg)
[![license](https://img.shields.io/github/license/Greenroom-Robotics/fluxconf.svg)](https://github.com/Greenroom-Robotics/fluxconf/blob/master/LICENSE)

File-backed Pydantic configuration with migration support.

## Installation

```sh
pip install fluxconf
```

or with [uv](https://docs.astral.sh/uv/):

```sh
uv add fluxconf
```

## Usage

### ConfigIO

`ConfigIO` is a generic base class for reading and writing YAML-backed Pydantic models. Subclass it, set `file_name` and `config_type`, and you get type-safe read/write with automatic migration support.

```python
from pydantic import BaseModel
from fluxconf import ConfigIO

class AppConfig(BaseModel):
    name: str = "my-app"
    debug: bool = False

class AppConfigIO(ConfigIO[AppConfig]):
    file_name = "app.yml"
    config_type = AppConfig

# Write
io = AppConfigIO("~/.config/my-app")
io.write(AppConfig(name="my-app", debug=True))

# Read
config = io.read()
```

### Migrations

Define migration functions keyed by the integer version they migrate **to**. Migrations run automatically on `read()` when the stored version is behind the latest known migration.

Inherit from `VersionedBaseModel` instead of Pydantic's `BaseModel` so that the `version` field is preserved when the config is written back to disk via `write()`:

```python
from fluxconf import ConfigIO, VersionedBaseModel

class AppConfig(VersionedBaseModel):
    name: str = "my-app"
    debug: bool = False

def migrate_to_v2(data: dict) -> dict:
    """Rename 'use_foo' → 'foo_enabled'."""
    if "use_foo" in data:
        data["foo_enabled"] = data.pop("use_foo")
    return data

class AppConfigIO(ConfigIO[AppConfig]):
    file_name = "app.yml"
    config_type = AppConfig
    migrations = {
        "2_rename_foo": migrate_to_v2,
    }
```

Migration keys are strings of the form `"N_description"` — the integer prefix determines ordering and is stored in the config file as `version`.

On `read()`, pending migrations are applied in version order, the result is written back to disk, and the parsed model is returned.

If a migration fails, `MigrationError` is raised with `last_successful_migration` (an `int`). If the stored version is **ahead** of all known migrations, a `ValueError` is raised immediately.

#### Directory-based migrations

For larger migration sets, point `migrations_dir` at a directory of individual `N_description.py` files instead of (or in addition to) the inline `migrations` dict:

```
myapp/migrations/
    1_rename_foo.py
    2_add_bar.py
    _helpers.py          # skipped (starts with _)
```

Each file must define a top-level `migrate(data: dict) -> dict` function:

```python
# myapp/migrations/1_rename_foo.py

def migrate(data: dict) -> dict:
    if "use_foo" in data:
        data["foo_enabled"] = data.pop("use_foo")
    return data
```

```python
from pathlib import Path
from fluxconf import ConfigIO

class AppConfigIO(ConfigIO[AppConfig]):
    file_name = "app.yml"
    config_type = AppConfig
    migrations_dir = Path(__file__).parent / "migrations"
```

Only `.py` files with an integer prefix are loaded. Files starting with `_` or without an integer prefix are silently skipped, so helper modules can live alongside migration files.

`migrations` and `migrations_dir` can be used together — fluxconf merges them, raising `ValueError` on key collisions.

## License

[MIT License](https://github.com/Greenroom-Robotics/fluxconf/blob/master/LICENSE)
