pydantic-settings-file-envar


Namepydantic-settings-file-envar JSON
Version 0.1.0 PyPI version JSON
download
home_pagehttps://github.com/h4l/pydantic-settings-file-envar
SummaryLoad pydantic settings from files named by _FILE suffix environment variables
upload_time2023-09-23 08:40:36
maintainer
docs_urlNone
authorHal Blackburn
requires_python>=3.8,<4.0
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # pydantic-settings-file-envar

Support for loading [pydantic-settings] from files, using the `_FILE`
environment variable suffix pattern commonly used in container images,
especially official Docker images.

If you have a setting called `foo`, by default `pydantic-settings` will load it
from an environment variable `FOO`. With `pydantic-settings-file-envar`, if an
environment variable `FOO_FILE=/a/b/foo` is set, the secret's value will be
loaded from the file at `/a/b/foo`.

> Note that pydantic-settings
> [has built-in support for loading secrets from files](https://docs.pydantic.dev/latest/usage/pydantic_settings/#use-case-docker-secrets),
> so that may be all you need. It's different from the `_FILE` envar pattern in
> that it expects a specific directory to be specified, which contains
> appropriately-named secret files.

## Install

```Console
$ pip install pydantic-settings-file-envar
```

## Usage

To use `_FILE` envar settings, override the `settings_customise_sources()` class
method of your settings class to include the `FileSuffixEnvSettingsSource` this
package provides:

```Python
from pydantic_settings_file_envar import FileSuffixEnvSettingsSource

# ...

class ExampleSettings(BaseSettings):
    threshold: int
    launch_code: str

    @classmethod
    def settings_customise_sources(
        cls,
        settings_cls: Type[BaseSettings],
        init_settings: PydanticBaseSettingsSource,
        env_settings: PydanticBaseSettingsSource,
        dotenv_settings: PydanticBaseSettingsSource,
        file_secret_settings: PydanticBaseSettingsSource,
    ) -> Tuple[PydanticBaseSettingsSource, ...]:
        # Adding FileSuffixEnvSettingsSource enables _FILE envars
        return (init_settings, env_settings, FileSuffixEnvSettingsSource(settings_cls))
```

If you place it after the default `env_settings` source, `_FILE` environment
variable will only be used if a regular environment variable is not set. The
[pydantic-settings docs have more details on configuring settings sources](https://docs.pydantic.dev/latest/usage/pydantic_settings/#adding-sources).

## Example program & Docker image

The [./example](./example/) dir contains an example command-line program that
loads settings using regular or `_FILE` envars.

Try it like this:

```Console
$ cd example
$ poetry install --with local
$ poetry shell
$ echo -n secret-from-file > /tmp/secret
$ THRESHOLD=9000 LAUNCH_CODE_FILE=/tmp/secret file-envar-example
Loaded settings: threshold=9000 launch_code='secret-from-file'
```

Or the Docker image:

```Console
$ # build the example image
$ docker buildx bake example
[+] Building 5.2s (12/12) FINISHED                                                                         ...
 => => naming to docker.io/library/file-envar-example

$ echo -n secret-from-file > /tmp/secret
$ docker container run --rm -v /tmp:/secrets -e THRESHOLD=9000 -e LAUNCH_CODE_FILE=/secrets/secret file-envar-example
Loaded settings: threshold=9000 launch_code='secret-from-file'
```

[pydantic-settings]: https://github.com/pydantic/pydantic-settings

## Developing

See [docs/dev.md](docs/dev.md).


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/h4l/pydantic-settings-file-envar",
    "name": "pydantic-settings-file-envar",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8,<4.0",
    "maintainer_email": "",
    "keywords": "",
    "author": "Hal Blackburn",
    "author_email": "hwtb2@cam.ac.uk",
    "download_url": "https://files.pythonhosted.org/packages/f4/e8/9797ed3d8f83f6fe792dba994169134210f93fd9b110f3e128be1deaac33/pydantic_settings_file_envar-0.1.0.tar.gz",
    "platform": null,
    "description": "# pydantic-settings-file-envar\n\nSupport for loading [pydantic-settings] from files, using the `_FILE`\nenvironment variable suffix pattern commonly used in container images,\nespecially official Docker images.\n\nIf you have a setting called `foo`, by default `pydantic-settings` will load it\nfrom an environment variable `FOO`. With `pydantic-settings-file-envar`, if an\nenvironment variable `FOO_FILE=/a/b/foo` is set, the secret's value will be\nloaded from the file at `/a/b/foo`.\n\n> Note that pydantic-settings\n> [has built-in support for loading secrets from files](https://docs.pydantic.dev/latest/usage/pydantic_settings/#use-case-docker-secrets),\n> so that may be all you need. It's different from the `_FILE` envar pattern in\n> that it expects a specific directory to be specified, which contains\n> appropriately-named secret files.\n\n## Install\n\n```Console\n$ pip install pydantic-settings-file-envar\n```\n\n## Usage\n\nTo use `_FILE` envar settings, override the `settings_customise_sources()` class\nmethod of your settings class to include the `FileSuffixEnvSettingsSource` this\npackage provides:\n\n```Python\nfrom pydantic_settings_file_envar import FileSuffixEnvSettingsSource\n\n# ...\n\nclass ExampleSettings(BaseSettings):\n    threshold: int\n    launch_code: str\n\n    @classmethod\n    def settings_customise_sources(\n        cls,\n        settings_cls: Type[BaseSettings],\n        init_settings: PydanticBaseSettingsSource,\n        env_settings: PydanticBaseSettingsSource,\n        dotenv_settings: PydanticBaseSettingsSource,\n        file_secret_settings: PydanticBaseSettingsSource,\n    ) -> Tuple[PydanticBaseSettingsSource, ...]:\n        # Adding FileSuffixEnvSettingsSource enables _FILE envars\n        return (init_settings, env_settings, FileSuffixEnvSettingsSource(settings_cls))\n```\n\nIf you place it after the default `env_settings` source, `_FILE` environment\nvariable will only be used if a regular environment variable is not set. The\n[pydantic-settings docs have more details on configuring settings sources](https://docs.pydantic.dev/latest/usage/pydantic_settings/#adding-sources).\n\n## Example program & Docker image\n\nThe [./example](./example/) dir contains an example command-line program that\nloads settings using regular or `_FILE` envars.\n\nTry it like this:\n\n```Console\n$ cd example\n$ poetry install --with local\n$ poetry shell\n$ echo -n secret-from-file > /tmp/secret\n$ THRESHOLD=9000 LAUNCH_CODE_FILE=/tmp/secret file-envar-example\nLoaded settings: threshold=9000 launch_code='secret-from-file'\n```\n\nOr the Docker image:\n\n```Console\n$ # build the example image\n$ docker buildx bake example\n[+] Building 5.2s (12/12) FINISHED                                                                         ...\n => => naming to docker.io/library/file-envar-example\n\n$ echo -n secret-from-file > /tmp/secret\n$ docker container run --rm -v /tmp:/secrets -e THRESHOLD=9000 -e LAUNCH_CODE_FILE=/secrets/secret file-envar-example\nLoaded settings: threshold=9000 launch_code='secret-from-file'\n```\n\n[pydantic-settings]: https://github.com/pydantic/pydantic-settings\n\n## Developing\n\nSee [docs/dev.md](docs/dev.md).\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Load pydantic settings from files named by _FILE suffix environment variables",
    "version": "0.1.0",
    "project_urls": {
        "Homepage": "https://github.com/h4l/pydantic-settings-file-envar",
        "Repository": "https://github.com/h4l/pydantic-settings-file-envar"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e80927cc10eed43c15d52c09453e8d19ff97019037481f132974aaea6f44c5dc",
                "md5": "eeb3358cf84335e72fcff8e5becf280c",
                "sha256": "c50e811ff4b69254a3cdb5ce987589b2d97d576c7170c81747d10f93ee24459b"
            },
            "downloads": -1,
            "filename": "pydantic_settings_file_envar-0.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "eeb3358cf84335e72fcff8e5becf280c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 4565,
            "upload_time": "2023-09-23T08:40:34",
            "upload_time_iso_8601": "2023-09-23T08:40:34.511767Z",
            "url": "https://files.pythonhosted.org/packages/e8/09/27cc10eed43c15d52c09453e8d19ff97019037481f132974aaea6f44c5dc/pydantic_settings_file_envar-0.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f4e89797ed3d8f83f6fe792dba994169134210f93fd9b110f3e128be1deaac33",
                "md5": "c37b266a4c2518ad47a9747c47bef107",
                "sha256": "4976d10037ba1bc06fd574e2e37da0d73beb145886331752558ce958df2a480a"
            },
            "downloads": -1,
            "filename": "pydantic_settings_file_envar-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "c37b266a4c2518ad47a9747c47bef107",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 3854,
            "upload_time": "2023-09-23T08:40:36",
            "upload_time_iso_8601": "2023-09-23T08:40:36.000270Z",
            "url": "https://files.pythonhosted.org/packages/f4/e8/9797ed3d8f83f6fe792dba994169134210f93fd9b110f3e128be1deaac33/pydantic_settings_file_envar-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-23 08:40:36",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "h4l",
    "github_project": "pydantic-settings-file-envar",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "pydantic-settings-file-envar"
}
        
Elapsed time: 1.95608s