Skip to content

Commit

Permalink
New features: sources can be provided at instantiation and creation a…
Browse files Browse the repository at this point in the history
…nd load happens immediately by default
  • Loading branch information
bast0006 committed Sep 7, 2021
1 parent 018e214 commit c5052a0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")

Expand Down
12 changes: 10 additions & 2 deletions configurave.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.


Expand All @@ -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()
Expand Down Expand Up @@ -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.
Expand All @@ -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": {}}
Expand Down
15 changes: 7 additions & 8 deletions tests/test_configurave.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_dupes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit c5052a0

Please sign in to comment.