Usage¶
This page explains how to work with confkit and how the documentation is generated.
Descriptor Quickstart¶
from configparser import ConfigParser
from pathlib import Path
from confkit import Config
# 1. Configure confkit parser/file (normally app bootstrap)
parser = ConfigParser()
Config.set_parser(parser)
Config.set_file(Path("config.ini"))
# 2. Define your (class)variables with their default values
class AppConfig:
debug = Config(False)
port = Config(8080)
# 3. use them!
# (this value came from the .ini file!)
print(AppConfig.port)
Adding a New Data Type¶
- Subclass
BaseDataType[T] - Implement
convert(self, value: str) -> T - (Optional) override
__str__for serialization - Use via
Config(CustomType(default_value))
from configparser import ConfigParser
from pathlib import Path
from confkit import Config
from confkit.data_types import BaseDataType
# 1. Configure confkit parser/file (normally app bootstrap)
parser = ConfigParser()
Config.set_parser(parser)
Config.set_file(Path("config.ini"))
# 2. Define the custom converter, with a convert method (from str -> your_type)
class UpperString(BaseDataType[str]):
def convert(self, value: str) -> str: # str from INI -> normalized value
return str(value).upper()
def __str__(self, value: str): # value -> string for INI
return value.upper()
# 3. Use the custom datatype inside a config class
class CustomCfg:
shout_name = Config(UpperString("confkit"))
print(CustomCfg.shout_name) # -> CONFKIT
CustomCfg.shout_name = "mixedCase"
print(CustomCfg.shout_name) # -> MIXEDCASE
Full runnable example: examples/custom_data_type.py.
Optional Values¶
Either pass optional=True to Config(...) or wrap a data type in Optional(...).
from confkit.data_types import Optional, String
class Service:
api_key = Config("", optional=True) # primitive optional
token = Config(Optional(String(""))) # wrapped optional
Decorators Overview¶
Config.set(section, option, value)– always sets before callConfig.default(section, option, value)– sets only when missingConfig.with_setting(descriptor)– injects descriptor value by nameConfig.with_kwarg(section, option, name?, default?)– inject by strings
See also: the dedicated reference pages for cross-linked signatures.
Regenerating API Docs (pdoc + mkdocs)¶
The documentation is using both mkdocstrings and the mkdocs-pdoc-plugin for deep API symbol cross-references.
The api/ directory inside docs-mkdocs/ is produced by pdoc. Regenerate it after changing code-level docstrings or adding new public classes/functions.
uv run pdoc confkit -o docs-mkdocs/api --force
Key points:
--forceoverwrites existing output- The MkDocs plugin (configured in
mkdocs.ymlaspdoc: { api_path: api }) enables links like(pdoc:confkit.config.Config)inside Markdown - Reference pages in
reference/intentionally use those links for stable deep-links