Skip to content

Commit

Permalink
ConfigEntry
Browse files Browse the repository at this point in the history
  • Loading branch information
kmagusiak committed Dec 28, 2023
1 parent a93f0e1 commit 22f335a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
11 changes: 10 additions & 1 deletion alphaconf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from .frozendict import frozendict # noqa: F401 (expose)
from .internal.application import Application
from .internal.configuration import Configuration
from .internal.configuration import Configuration, Entry

__doc__ = """AlphaConf
Expand Down Expand Up @@ -50,6 +50,15 @@
get = _global_configuration.get


def setup_entry(typ: type[T], key: str = "", val=None) -> Entry[T]:
e = Entry(typ, val)
if val is None:
setup_configuration(typ)
else:
setup_configuration({key: val})
return e


def set_application(app: Application) -> None:
"""Setup the application globally
Expand Down
15 changes: 15 additions & 0 deletions alphaconf/internal/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import (
Any,
Dict,
Generic,
Iterable,
List,
MutableMapping,
Expand All @@ -31,6 +32,18 @@ class RaiseOnMissingType(Enum):
_cla_type = type


class Entry(Generic[T]):
def __init__(self, typ: type[T], key: Optional[str] = None) -> None:
self.typ = typ
self.key = key

def read(self) -> T:
# import alphaconf

# return alphaconf.get(self.key or self.typ, self.typ)
raise NotImplementedError


class Configuration:
c: DictConfig
__type_path: MutableMapping[Type, Optional[str]]
Expand Down Expand Up @@ -244,6 +257,8 @@ def __prepare_config(self, obj, path):
if not changed:
result = obj
return self.__prepare_dictconfig(OmegaConf.create(result), path, recursive=False)
if isinstance(obj, Entry):
return obj.value
return obj

def __prepare_pydantic(self, obj, path):
Expand Down
24 changes: 14 additions & 10 deletions example-simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,20 @@ class Opts(BaseModel):
exception: bool = Field(False, description="If set, raise an exception")


server_name = alphaconf.setup_entry(str, "server.name", "test_server")
server_user = alphaconf.setup_entry(str, "server.user", "${oc.env:USER}")


class CC(BaseModel):
name: str = "test_server"
user: str


cc = alphaconf.setup_entry(CC, "server")

# adding a default configuration
# these will be merged with the application
alphaconf.setup_configuration(Opts)
alphaconf.setup_configuration(
{
"server": {
"name": "test_server",
"user": "${oc.env:USER}",
}
}
)


def main():
Expand All @@ -32,8 +35,9 @@ def main():
# get the application name from the configuration
print('app:', alphaconf.get("application.name"))
# shortcut version to get a configuration value
print('server.name', alphaconf.get('server.name'))
print('server.user:', alphaconf.get('server.user'))
cc.read().user
print('server.name', server_name.read())
print('server.user:', server_user.read())

# you can set additional dynamic values in the logging
context_value = ['init']
Expand Down

0 comments on commit 22f335a

Please sign in to comment.