Settings¶
This reference documents how Monkay loads, caches, overrides, and evaluates settings objects.
Settings Sources¶
Monkay.settings can be configured from:
- a dotted path (
"module:attribute"), - a settings class,
- a callable returning a settings source,
- an already-instantiated settings object.
When settings resolve to a class/path, Monkay caches the parsed object. Callable sources can be used for dynamic forwarding patterns.
Evaluate Settings¶
evaluate_settings(...) processes optional preload and extension lists from the
current settings object.
Parameters¶
on_conflict: extension conflict policy ("error","keep","replace")ignore_import_errors: returnFalseinstead of raising on settings import/unset errorsignore_preload_import_errors: continue when preload imports failonetime: skip re-evaluation when settings already evaluated in current context
Example¶
Forwarding Settings¶
Forwarding allows a package to delegate settings to another package.
Child package¶
import os
from monkay import Monkay
monkay = Monkay(
globals(),
settings_path=lambda: os.environ.get("MONKAY_CHILD_SETTINGS", "foo.test:example") or "",
)
Main package¶
import os
import child
from monkay import Monkay
monkay = Monkay(
globals(),
settings_path=lambda: os.environ.get("MONKAY_MAIN_SETTINGS", "foo.test:example") or "",
)
# because monkay.settings is an instance it uses not a cache for the settings
child.monkay.settings = lambda: monkay.settings
Lazy Settings Setup¶
import os
from dataclasses import dataclass
from monkay import Monkay
@dataclass
class Settings:
env: str
@dataclass
class ProductionSettings(Settings):
env: str = "production"
@dataclass
class DebugSettings(Settings):
env: str = "debug"
def lazy_loader():
# Lazy setup based on environment variables
if not os.environ.get("DEBUG"):
return os.environ.get("MONKAY_MAIN_SETTINGS", "foo.test:example") or ""
elif os.environ.get("PERFORMANCE"):
# must be class to be cached
return ProductionSettings
else:
# not a class, will evaluated always on access
return DebugSettings()
monkay = Monkay(
globals(),
# Required for initializing settings feature
settings_path=lazy_loader,
)
# Now the settings are applied
monkay.evaluate_settings()
Multi-Stage Resolution¶
from monkay import Monkay
monkay = Monkay(
globals(),
# Required for initializing settings feature
settings_path="",
)
def find_settings():
for path in ["a.settings", "b.settings.develop"]:
monkay.settings = path
if monkay.evaluate_settings(ignore_import_errors=True):
break
Temporary Settings Overrides¶
Use with_settings(...) for context-local overrides:
Use with_settings(None) to temporarily disable override and access the base
settings definition.
Clearing Settings Cache¶
Settings Preloads and Extensions¶
settings_preloads_namepoints to a list of preload stringssettings_extensions_namepoints to extension objects/factories/classes
These are only evaluated when evaluate_settings(...) is called.
Error Behavior¶
- Disabled/unset settings access raises
UnsetError. - Unknown
on_conflictvalues raiseValueError. - Import failures raise
ImportErrorunless suppressed withignore_import_errors=True.