-
Notifications
You must be signed in to change notification settings - Fork 0
/
example-simple.py
executable file
·66 lines (53 loc) · 1.94 KB
/
example-simple.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
import logging
from typing import Optional
from pydantic import BaseModel, Field
import alphaconf.cli
import alphaconf.logging_util
class Opts(BaseModel):
show: Optional[str] = Field(None, description="The name of the selection to show")
exception: bool = Field(False, description="If set, raise an exception")
# 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():
"""Simple demo of alphaconf"""
# 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'))
# you can set additional dynamic values in the logging
context_value = ['init']
alphaconf.logging_util.DynamicLogRecord.set_generator(lambda: context_value)
# you can log extra values with formatters such as json, try:
# ./example-simple.py logging.handlers.console.formatter=json
logging.info('The app is running...', extra={'other': 'othervalue'})
context_value = None
logging.info('Just a log')
# show configuration
value = alphaconf.get('show', str, default=None)
if value and (value := alphaconf.get(value, default=None)):
print(value)
# log an exception if we have it in the configuration
if alphaconf.get('exception', default=False):
try:
raise RuntimeError("Asked to raise something")
except Exception:
logging.error("Just log something", exc_info=True)
context_value = ['finished']
if __name__ == '__main__':
# running with explicit parameters
alphaconf.cli.run(
main,
name='example',
version='0.1',
)