Skip to content

Commit

Permalink
feat: INI support (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxb2 authored May 18, 2023
1 parent 2e2e09f commit 2ec9aa5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
4 changes: 4 additions & 0 deletions tests/config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[simple_app]
arg1 = stuff
opt1 = things
opt2 = nothing
8 changes: 8 additions & 0 deletions tests/test_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ def test_simple_example(simple_app):
(str(HERE.joinpath("config.json")), typer_config.json_conf_callback),
(str(HERE.joinpath("config.toml")), typer_config.toml_conf_callback),
(str(HERE.joinpath("config.env")), typer_config.dotenv_conf_callback),
(
str(HERE.joinpath("config.ini")),
typer_config.conf_callback_factory(
typer_config.loaders.subpath_loader(
typer_config.loaders.ini_loader, ["simple_app"]
)
),
),
]

# Test all the combinations of formats and extra parameters
Expand Down
30 changes: 30 additions & 0 deletions typer_config/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""
import json
import sys
from configparser import ConfigParser

from .__typing import (
ConfigDict,
Expand Down Expand Up @@ -212,3 +213,32 @@ def dotenv_loader(param_value: TyperParameterValue) -> ConfigDict:
conf: ConfigDict = dotenv.dotenv_values(stream=_file)

return conf


def ini_loader(param_value: TyperParameterValue) -> ConfigDict:
"""INI file loader
Note:
INI files must have sections at the top level.
You probably want to combine this with `subpath_loader`.
For example:
```py
ini_section_loader = subpath_loader(ini_loader, ["section"])
```
Args:
param_value (TyperParameterValue): path of INI file
Returns:
ConfigDict: dictionary loaded from file
"""

ini_parser = ConfigParser()
with open(param_value, "r", encoding="utf-8") as _file:
ini_parser.read_file(_file)

conf: ConfigDict = {
sect: dict(ini_parser.items(sect)) for sect in ini_parser.sections()
}

return conf

0 comments on commit 2ec9aa5

Please sign in to comment.