{"id":16854590,"url":"https://github.com/bswck/configzen","last_synced_at":"2025-03-22T06:31:02.776Z","repository":{"id":159567067,"uuid":"624086540","full_name":"bswck/configzen","owner":"bswck","description":"Manage configuration with pydantic.","archived":false,"fork":false,"pushed_at":"2024-04-14T00:55:26.000Z","size":1031,"stargazers_count":20,"open_issues_count":8,"forks_count":0,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-04-14T15:23:18.780Z","etag":null,"topics":["aiofiles","async","async-config","async-config-management","config","config-management","configuration","configuration-management","configuration-model","ini","json","pydantic","pydantic-configuration","pydantic-models","python","python-config","python-configuration","toml","yaml"],"latest_commit_sha":null,"homepage":"https://configzen.readthedocs.io","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bswck.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null},"funding":{"github":"bswck"}},"created_at":"2023-04-05T18:03:58.000Z","updated_at":"2024-04-22T21:31:51.793Z","dependencies_parsed_at":"2024-04-14T01:48:02.372Z","dependency_job_id":null,"html_url":"https://github.com/bswck/configzen","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bswck%2Fconfigzen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bswck%2Fconfigzen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bswck%2Fconfigzen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bswck%2Fconfigzen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bswck","download_url":"https://codeload.github.com/bswck/configzen/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244918500,"owners_count":20531682,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aiofiles","async","async-config","async-config-management","config","config-management","configuration","configuration-management","configuration-model","ini","json","pydantic","pydantic-configuration","pydantic-models","python","python-config","python-configuration","toml","yaml"],"created_at":"2024-10-13T13:56:03.823Z","updated_at":"2025-03-22T06:31:02.265Z","avatar_url":"https://github.com/bswck.png","language":"Python","funding_links":["https://github.com/sponsors/bswck","https://tidelift.com/badges/package/pypi/configzen","https://tidelift.com/subscription/pkg/pypi-configzen?utm_source=pypi-configzen\u0026utm_medium=readme","https://tidelift.com/subscription/pkg/pypi-configzen?utm_source=pypi-configzenutm_medium=referral\u0026utm_campaign=readme","https://tidelift.com/subscription/pkg/pypi-configzen?utm_source=pypi-configzen\u0026\u0026utm_medium=referral\u0026utm_campaign=readme","https://tidelift.com/subscription/pkg/pypi-configzen?utm_source=pypi-configzen\u0026utm_medium=referral\u0026utm_campaign=github","https://tidelift.com/security"],"categories":[],"sub_categories":[],"readme":"# \u003cdiv align=\"center\"\u003econfigzen\u003cbr\u003e[![skeleton](https://img.shields.io/badge/0.0.2rc–245–g8c04714-skeleton?label=%F0%9F%92%80%20skeleton-ci/skeleton-python\u0026labelColor=black\u0026color=grey\u0026link=https%3A//github.com/skeleton-ci/skeleton-python)](https://github.com/skeleton-ci/skeleton-python/tree/0.0.2rc-245-g8c04714) [![Supported Python versions](https://img.shields.io/pypi/pyversions/configzen.svg?logo=python\u0026label=Python)](https://pypi.org/project/configzen/) [![Package version](https://img.shields.io/pypi/v/configzen?label=PyPI)](https://pypi.org/project/configzen/)\u003c/div\u003e\n\n[![Tests](https://github.com/bswck/configzen/actions/workflows/test.yml/badge.svg)](https://github.com/bswck/configzen/actions/workflows/test.yml)\n[![Coverage](https://coverage-badge.samuelcolvin.workers.dev/bswck/configzen.svg)](https://coverage-badge.samuelcolvin.workers.dev/redirect/bswck/configzen)\n[![Lifted?](https://tidelift.com/badges/package/pypi/configzen)](https://tidelift.com/subscription/pkg/pypi-configzen?utm_source=pypi-configzen\u0026utm_medium=readme)\n\nManage configuration with pydantic.\n\n\u003e [!Warning]\n\u003e configzen is currently under huge refactoring to work with pydantic v2.\n\u003e The API is subject to change.\n\nWhile being built on top of [pydantic](https://docs.pydantic.dev/2.6/), configzen inherits most of its features, including\n[data validation](https://docs.pydantic.dev/2.6/concepts/models/#validation), [schema generation](https://docs.pydantic.dev/2.6/api/base_model/#pydantic.BaseModel.model_json_schema),\n[custom data types](https://docs.pydantic.dev/2.6/api/config/#pydantic.config.ConfigDict.arbitrary_types_allowed), good integration with [Rich](https://docs.pydantic.dev/2.6/integrations/rich/), and more.\n\nLearn more below.\n\n## Features\n\n### Managing Content\n\nHaving a YAML configuration file like this:\n\n```yaml\n# database.yml\nhost: 127.0.0.1\nport: 5432\nuser: postgres\n```\n\nYou can create a configzen configuration model for it like this:\n\n```python\n# model.py\nfrom ipaddress import IPv4Address, IPv6Address\n\nfrom configzen import BaseConfig, Field, ModelConfig\n\n\nclass DBConfig(BaseConfig):\n    host: IPv4Address | IPv6Address\n    port: int\n    user: str\n    password: str = Field(exclude=True)\n\n    model_config = ModelConfig(\n        config_source=\"database.yml\",\n        env_prefix=\"DB_\",\n    )\n\n\ndb_config = DBConfig.load()\n```\n\nAnd you can load your configuration from a file as well as from the environment variables\n`DB_HOST`, `DB_PORT`, `DB_USER` and `DB_PASSWORD`. Since `password` is a field created with\nthe option `exclude=True`, it will not be included in the configuration's exported data: that\nguarantees that your password does never leak into `database.yml` on save – but you may still pass it\nthrough an environment variable (here – the mentioned `DB_PASSWORD`). Secret files are also supported,\nsee [the pydantic documentation](https://docs.pydantic.dev/latest/usage/settings/#secret-support)\nfor more information.\n\n[pydantic](https://docs.pydantic.dev/latest/) will naturally take care of parsing and validating the loaded data.\nConfiguration models inherit from the `pydantic.BaseSettings` class, so you can use all of its features:\nschema generation, type conversion, validation, etc.\n\nThere are additional features brought to you by configzen worth checking out, though.\n\nYou can use the `db_config` object defined above to access the configuration values:\n\n```python\n\u003e\u003e\u003e db_config.host\nIPv4Address('127.0.0.1')\n```\n\nmodify them, if the pydantic model validation allows\nit ([`\u003cYour model\u003e.Config.validate_assignment`](https://docs.pydantic.dev/latest/usage/model_config/#options) will\nbe `True` by default):\n\n```python\n\u003e\u003e\u003e db_config.host = \"0.0.0.0\"\n\u003e\u003e\u003e db_config.host\nIPv4Address('0.0.0.0')\n```\n\nas well as reload particular values, without touching the rest of the configuration:\n\n```python\n\u003e\u003e\u003e db_config.at(DBConfig.port).reload()\n5432\n# `DBConfig.port` is a LinkedRoute object that ensures `port` of `DBConfig` exists!\n\u003e\u003e\u003e db_config\nDatabaseConfig(host=IPv4Address('0.0.0.0'), port=5432, user='postgres', password='password')\n\u003e\u003e\u003e db_config.at(DBConfig.host).reload()\nIPv4Address('127.0.0.1')\n\u003e\u003e\u003e db_config\nDatabaseConfig(host=IPv4Address('127.0.0.1'), port=5432, user='postgres', password='password')\n```\n\nor reload the whole configuration:\n\n```python\n\u003e\u003e\u003e db_config.port = 1234\n\u003e\u003e\u003e db_config.reload()\nDatabaseConfig(host=IPv4Address('127.0.0.1'), port=5432, user='postgres', password='password')\n```\n\nor save a particular value, without touching the rest of the configuration:\n\n```python\n\u003e\u003e\u003e db_config.host = \"0.0.0.0\"\n\u003e\u003e\u003e db_config.port = 443\n\u003e\u003e\u003e db_config\nDatabaseConfig(host=IPv4Address('0.0.0.0'), port=443, user='postgres', password='password')\n\u003e\u003e\u003e db_config.at(DBConfig.host).save()\n40\n\u003e\u003e\u003e db_config.reload()\nDatabaseConfig(host=IPv4Address('0.0.0.0'), port=5432, user='postgres', password='password')\n```\n\nor save the whole configuration:\n\n```python\n\u003e\u003e\u003e db_config.save()\n39\n```\n\n\n### Modular Configuration\n\n#### Wrapping Modules In-Place\n\nYou can wrap modules in-place with configuration models:\n\n\n1) Without writing a model class:\n\n```python\n# config.py\nfrom configzen import ConfigModule\n\n# Annotate config fields\nHOST: str = \"localhost\"\nPORT: int = 8000\n\nConfigModule.wrap_this_module()\n```\n\n2) With a model class:\n\n```python\n# config.py\nfrom configzen import ConfigModel\n\n# Annotations are optional\nHOST = \"localhost\"\nPORT = 8000\n\nclass AppConfig(ConfigModel):\n    HOST: str\n    PORT: int\n\nAppConfig.wrap_this_module()\n```\n\nNow values `HOST` and `PORT` will be validated as `str` and `int` data types, respectively:\n\n```python\n\u003e\u003e\u003e import config  # \u003cconfiguration module 'config' from 'config.py'\u003e\n\u003e\u003e\u003e config.HOST\n'localhost'\n\u003e\u003e\u003e config.PORT\n8000\n\u003e\u003e\u003e config.PORT = \"8000\"\n\u003e\u003e\u003e config.PORT\n8000\n\u003e\u003e\u003e config.PORT = \"abc\"\nTraceback (most recent call last):\n  ...\n```\n\n#### Wrapping Interchangeable Modules\n\nYou can wrap modules outside them with configuration models:\n\n```python\n# setup.py\nfrom configzen import ConfigModel\n\nclass AppConfig(ConfigModel):\n    HOST: str = \"localhost\"\n    PORT: int = 8000\n\nconfig_model = AppConfig.wrap_module(\"config\")\n```\n\n```py\n# config.py\nHOST: str = \"0.0.0.0\"\nPORT: int = 443\n```\n\n```python\n\u003e\u003e\u003e from setup import config_model\n\u003e\u003e\u003e config_model.HOST\n'0.0.0.0'\n\u003e\u003e\u003e config_model.PORT\n443\n\u003e\u003e\u003e config_model.PORT = \"8000\"\n\u003e\u003e\u003e config_model.PORT\n8000\n\u003e\u003e\u003e import config\n\u003e\u003e\u003e config.HOST\n'0.0.0.0'\n\u003e\u003e\u003e config.PORT\n8000\n```\n\n## Supported File Formats\n\nThe following table shows the supported file formats, their requirements, file extensions, and the backend libraries used to accomplish this goal.\n\n| File Format                                   | To use, install: | Recognized File Extension(s) | Backend Library                                                                                         |\n| --------------------------------------------- | ---------------- | ---------------------------- | ------------------------------------------------------------------------------------------------------- |\n| [JSON](https://en.wikipedia.org/wiki/JSON)    | -                | `json`                       | [json](https://docs.python.org/3/library/json.html) (standard library)                                  |\n| [TOML](https://en.wikipedia.org/wiki/TOML)    | -                | `toml`, `ini`, `cfg`, `conf` | [tomlkit](https://pypi.python.org/pypi/tomlkit)                                                               |\n| [YAML](https://yaml.org)                      | -                | `yaml`, `yml`                | [ruamel.yaml](https://pypi.python.org/pypi/ruamel.yaml) |\n| [Plist](https://en.wikipedia.org/wiki/XML)    | -                | `plist`                      | [plistlib](https://docs.python.org/3/library/plistlib.html) (standard library)                                    |\n\n# For Enterprise\n\n| [![Tidelift](https://nedbatchelder.com/pix/Tidelift_Logo_small.png)](https://tidelift.com/subscription/pkg/pypi-configzen?utm_source=pypi-configzenutm_medium=referral\u0026utm_campaign=readme) | [Available as part of the Tidelift Subscription.](https://tidelift.com/subscription/pkg/pypi-configzen?utm_source=pypi-configzen\u0026\u0026utm_medium=referral\u0026utm_campaign=readme)\u003cbr\u003eThis project and the maintainers of thousands of other packages are working with Tidelift to deliver one enterprise subscription that covers all of the open source you use. [Learn more here](https://tidelift.com/subscription/pkg/pypi-configzen?utm_source=pypi-configzen\u0026utm_medium=referral\u0026utm_campaign=github). |\n| - | - |\n\nTo report a security vulnerability, please use the\n[Tidelift security contact](https://tidelift.com/security).\u003cbr\u003e\nTidelift will coordinate the fix and disclosure.\n\n# Installation\nYou might simply install it with pip:\n\n```shell\npip install configzen\n```\n\nIf you use [Poetry](https://python-poetry.org/), then you might want to run:\n\n```shell\npoetry add configzen\n```\n\n## For Contributors\n[![Poetry](https://img.shields.io/endpoint?url=https://python-poetry.org/badge/v0.json)](https://python-poetry.org/)\n[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)\n[![Pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit\u0026logoColor=white)](https://github.com/pre-commit/pre-commit)\n\u003c!--\nThis section was generated from skeleton-ci/skeleton-python@0.0.2rc-245-g8c04714.\nInstead of changing this particular file, you might want to alter the template:\nhttps://github.com/skeleton-ci/skeleton-python/tree/0.0.2rc-245-g8c04714/project/README.md.jinja\n--\u003e\n\u003e [!Note]\n\u003e If you use Windows, it is highly recommended to complete the installation in the way presented below through [WSL2](https://learn.microsoft.com/en-us/windows/wsl/install).\n1.  Fork the [configzen repository](https://github.com/bswck/configzen) on GitHub.\n\n1.  [Install Poetry](https://python-poetry.org/docs/#installation).\u003cbr/\u003e\n    Poetry is an amazing tool for managing dependencies \u0026 virtual environments, building packages and publishing them.\n    You might use [pipx](https://github.com/pypa/pipx#readme) to install it globally (recommended):\n\n    ```shell\n    pipx install poetry\n    ```\n\n    \u003csub\u003eIf you encounter any problems, refer to [the official documentation](https://python-poetry.org/docs/#installation) for the most up-to-date installation instructions.\u003c/sub\u003e\n\n    Be sure to have Python 3.8 installed—if you use [pyenv](https://github.com/pyenv/pyenv#readme), simply run:\n\n    ```shell\n    pyenv install 3.8\n    ```\n\n1.  Clone your fork locally and install dependencies.\n\n    ```shell\n    git clone https://github.com/your-username/configzen path/to/configzen\n    cd path/to/configzen\n    poetry env use $(cat .python-version)\n    poetry install\n    ```\n\n    Next up, simply activate the virtual environment and install pre-commit hooks:\n\n    ```shell\n    poetry shell\n    pre-commit install\n    ```\n\nFor more information on how to contribute, check out [CONTRIBUTING.md](https://github.com/bswck/configzen/blob/HEAD/CONTRIBUTING.md).\u003cbr/\u003e\nAlways happy to accept contributions! ❤️\n\n# Legal Info\n© Copyright by Bartosz Sławecki ([@bswck](https://github.com/bswck)).\n\u003cbr /\u003eThis software is licensed under the terms of [GPL-3.0 License](https://github.com/bswck/configzen/blob/HEAD/LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbswck%2Fconfigzen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbswck%2Fconfigzen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbswck%2Fconfigzen/lists"}