-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from jieggii/next
Next
- Loading branch information
Showing
21 changed files
with
1,094 additions
and
618 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
""" | ||
This example demonstrates how to document the configuration class. | ||
""" | ||
|
||
from minicfg import Field, Minicfg, minicfg_name | ||
from minicfg.caster import IntCaster | ||
from minicfg.docs_generator import DocsGenerator | ||
from minicfg.provider import AbstractProvider | ||
|
||
|
||
class MockProvider(AbstractProvider): | ||
""" | ||
A custom mock provider. | ||
Used to simulate the environment variables. | ||
""" | ||
|
||
data = {"DATABASE_HOST": "example.com", "DATABASE_PORT": "5432", "EXTERNAL_API_KEY": "api_key"} | ||
|
||
def get(self, key: str) -> str | None: | ||
return self.data.get(key) | ||
|
||
|
||
class MyConfig(Minicfg): | ||
@minicfg_name("DATABASE") | ||
class Database(Minicfg): | ||
HOST = Field(default="localhost", description="database host") | ||
PORT = Field(caster=IntCaster(), description="database port") | ||
|
||
@minicfg_name("EXTERNAL_API") | ||
class ExternalAPI(Minicfg): | ||
KEY = Field(description="external API key") | ||
|
||
|
||
""" | ||
Try running `python docs.py` or `python -m minicfg docs.MyConfig --format=plaintext` and you should see the following output: | ||
MyConfig | ||
DATABASE | ||
- DATABASE_HOST: str = localhost # database host | ||
- DATABASE_PORT: int # database port | ||
EXTERNAL_API | ||
- EXTERNAL_API_KEY: str # external API key | ||
""" | ||
if __name__ == "__main__": | ||
config = MyConfig() # create a new instance of the config | ||
docs_generator = DocsGenerator(config) # create a new instance of the docs generator | ||
print(docs_generator.as_plaintext()) # print the documentation as plain text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
""" | ||
This example demonstrates how to attach a file field to another field. | ||
""" | ||
|
||
from minicfg import Field, Minicfg | ||
from minicfg.provider import AbstractProvider | ||
|
||
|
||
class MyProvider(AbstractProvider): | ||
""" | ||
A provider that reads the hostname from the /etc/hostname file. | ||
""" | ||
|
||
data = { | ||
"DATABASE_HOST_FILE": "/etc/hostname", | ||
} | ||
|
||
def get(self, key: str) -> str | None: | ||
return self.data.get(key) | ||
|
||
|
||
class MyConfig(Minicfg): | ||
""" | ||
My configuration class. | ||
""" | ||
|
||
""" | ||
A virtual DATABASE_HOST_FILE field will be also created and attached to the DATABASE_HOST field. | ||
- If only DATABASE_HOST_FILE is provided, the field value will be read from the file. | ||
- If only DATABASE_HOST is provided, the field value will be used directly from it. | ||
- If both DATABASE_HOST_FILE and DATABASE_HOST are provided, the value of DATABASE_HOST will be used. | ||
- If none of them are provided, the FieldValueNotProvidedError will be raised. | ||
""" | ||
DATABASE_HOST = Field(attach_file_field=True) | ||
|
||
|
||
""" | ||
Try running `python file_field.py` and you should see the following output: | ||
>>> config.DATABASE_HOST='<your hostname>' | ||
If you don't have a hostname file, the FileNotFound exception will be raised. | ||
""" | ||
if __name__ == "__main__": | ||
provider = MyProvider() # create a new instance of the custom provider | ||
|
||
config = MyConfig() # create a new instance of the config | ||
config.populate(provider) # populate the config using the custom provider | ||
|
||
print(f"{config.DATABASE_HOST=}") | ||
# >>> config.DATABASE_HOST='<your hostname>' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
""" | ||
This example demonstrates how to use prefixes with minicfg. | ||
""" | ||
|
||
from minicfg import Field, Minicfg, minicfg_name | ||
from minicfg.caster import IntCaster | ||
from minicfg.provider import AbstractProvider | ||
|
||
|
||
class MockProvider(AbstractProvider): | ||
""" | ||
A custom mock provider. | ||
Used to simulate the environment variables. | ||
""" | ||
|
||
data = { | ||
"SERVICE_DATABASE_HOST": "example.com", | ||
"SERVICE_EXTERNAL_API_KEY": "api_key", | ||
"SERVICE_EXTERNAL_API_USER_ID": "user123", | ||
} | ||
|
||
def get(self, key: str) -> str | None: | ||
return self.data.get(key) | ||
|
||
|
||
@minicfg_name("SERVICE") # <-- The prefix for the main config (will be inherited by nested configs). | ||
class MyConfig(Minicfg): | ||
""" | ||
My configuration class. | ||
""" | ||
|
||
@minicfg_name("DATABASE") # <-- The prefix for the nested config. | ||
class Database(Minicfg): | ||
HOST: str = Field() | ||
PORT: int = Field(default=5432, caster=IntCaster()) | ||
|
||
@minicfg_name("EXTERNAL_API") # <-- The prefix for the nested config. | ||
class ExternalAPI(Minicfg): | ||
KEY: str = Field(description="external API key") | ||
USER_ID: str = Field(description="external API user ID") | ||
|
||
|
||
""" | ||
Try running `python nesting.py` and you should see the following output: | ||
>>> config.Database.HOST='example.com' | ||
>>> config.Database.PORT=5432 | ||
>>> config.ExternalAPI.KEY='api_key' | ||
>>> config.ExternalAPI.USER_ID='user123' | ||
""" | ||
if __name__ == "__main__": | ||
provider = MockProvider() # create a new instance of the custom provider | ||
|
||
config = MyConfig() # create a new instance of the config | ||
config.populate(provider) # populate the config using the custom provider | ||
|
||
print(f"{config.Database.HOST=}") | ||
print(f"{config.Database.PORT=}") | ||
print(f"{config.ExternalAPI.KEY=}") | ||
print(f"{config.ExternalAPI.USER_ID=}") |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.