diff --git a/README.md b/README.md index d1a6115..5ec2635 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ from typing import List from configurave import make_config, ConfigEntry as ce -@make_config(sources=["tests/test-config/readme.toml"]) +@make_config() class MyConfig: site_root: str = ce(comment="The root url the site should be mounted on") template_root: str = ce(comment="Directory under which templates should be found") @@ -29,8 +29,7 @@ class MyConfig: ) token: str = ce(comment="The discord auth token", secret=True) -config = MyConfig() -config.load() +config = MyConfig(sources=["tests/test-config/readme.toml"]) print(config.site_root, config.template_root, config.allowed_hosts[0], sep="\n") diff --git a/configurave.py b/configurave.py index 0269d6d..7a10aba 100644 --- a/configurave.py +++ b/configurave.py @@ -39,7 +39,6 @@ # TODO: json loader, ini loader, yaml loader # TODO: Optional[] support # TODO: Literal[] and enum support -# TODO: Move parsing from .load() and into __init__, taking sources then. # TODO: Accept file-like objects in sources list. @@ -51,6 +50,13 @@ class Config: _crve_sources: List[Source] loaded: bool = False + def __init__(self, sources: Optional[List[Source]] = None, load_now: bool = True): + if sources: + self._crve_sources.extend(sources) + + if load_now: + self.load() + def defaults_toml(self) -> str: """Generate a toml string containing comments and default configuration.""" doc = atoml.document() @@ -205,7 +211,7 @@ def validate_sources(sources: List[Source]) -> None: ) -def make_config(sources: List[Source]) -> Callable: +def make_config(sources: Optional[List[Source]] = None) -> Callable: """ Make the given class into a configurave configuration class. @@ -218,6 +224,8 @@ def make_config(sources: List[Source]) -> Callable: :param sources: a list of files or other special sources (such as ENV) to load configuration from :return: a decorator that converts the wrapped class """ + if not sources: + sources = [] def wrapper(class_: type) -> type: new_attributes = {"_crve_sources": sources, "_crve_configs": {}} diff --git a/tests/test_configurave.py b/tests/test_configurave.py index 1801116..846183f 100644 --- a/tests/test_configurave.py +++ b/tests/test_configurave.py @@ -38,12 +38,7 @@ def test_everything(): """We can create and load a configuration from our test config folder.""" # TODO: split this up and write better tests - @make_config( - sources=[ # in order of priority - "tests/test-config/config.toml", - "ENV", # Temporarily enabled, needs seperate optional dotenv test - ] - ) + @make_config() class Config: """The test configuration for configurave.""" @@ -53,8 +48,12 @@ class Config: " you could potentially decide upon using.", ) - c = Config() - c.load() + c = Config( + sources=[ # in order of priority + "tests/test-config/config.toml", + "ENV", # Temporarily enabled, needs seperate optional dotenv test + ] + ) assert "root_url" in str(c._crve_configs) assert c.root_url == "test url" diff --git a/tests/test_dupes.py b/tests/test_dupes.py index 6107b69..a3fb0c3 100644 --- a/tests/test_dupes.py +++ b/tests/test_dupes.py @@ -22,6 +22,6 @@ class Config: " you could potentially decide upon using.", ) - c = Config() + c = Config(load_now=False) with pytest.raises(atoml.exceptions.KeyAlreadyPresent): c.load()