Configuration¤
Configuration is provided by the asab.Config
object, which you can access from any place in your code, without need of explicit initialization.
import asab
app = asab.Application() #(1)!
...
my_conf_value = asab.Config['section_name']['key1'] #(2)!
- Configuration is initialized during the application init-time.
- You can access the configuration values from anywhere in the code.
Based on ConfigParser object¤
asab.Config
is an instance of asab.config.ConfigParser
class, derived from the Python standard configparser.ConfigParser
.
The class implements a basic configuration language that provides a structure similar to what's found in Microsoft Windows INI files.
Basic usage:
This is an example of the configuration file. We hope that it helps you quickly understand what the rules are:
[section name]
key=value
keys can contain spaces = values can contain spaces
you can use = equal signs
as well as: colons
extra spaces are : removed
be careful: 'quotes are parsed as quotes'
[another section]
final answer = 42
are you sure = true
numerical values are held as: strings
booleans are held as: strings
use these functions: getint(), getfloat(), getboolean()
And this is how you access configuration values:
Be careful with comments:
Multiline configuration entry:
A multiline configuration entries are supported:
multiline_values = are
handled just fine as
long as they are indented
deeper than the first line
of a value
chorus: I'm a lumberjack, and I'm okay
I sleep all night and I work all day
However, there are some configurable options where the newline is used as a separator, see .
Loading configuration from a file¤
If a configuration file name is specified,the configuration is automatically loaded from a configuration file during the Application init-time. There are two ways to include a configuration file:
-
by using the
-c
command-line argument: -
by running the application with
ASAB_CONFIG
environment variable set:
Including other configuration files¤
You can specify one or more additional configuration files that are loaded and merged from a main configuration file:
Multiple paths are separated by os.pathsep
value, which is :
on Unix and ;
on Windows.
The path can be specified as a glob (e.g. use of *
and ?
wildcard characters),
it will be expanded by glob
module.
Warning
If the additional configuration files do not exist, the situation is ignored silently!
You can also use a multiline configuration entry:
Including ZooKeeper node in the configuration¤
The separator between 'include' items is a newline or space. This means that names of nodes in Zookeeper must not have the space character in them.
The ZooKeeper node can contain a configuration file in .conf, .json or .yaml format.
You can specify servers and path of the ZooKeeper node directly in the include option:
It is also possible to name only the node path in this section and use zookeeper configuration section to read the location of ZooKeeper servers. Using the environment variable ASAB_ZOOKEEPERS_SERVERS
is also a possible option.
Default values¤
This is how you can extend configuration default values:
asab.Config.add_defaults(
{
'section_name': {
'key1': 'value',
'key2': 'another value'
},
'other_section': {
'key3': 'value',
},
}
)
Warning
Only simple types (string
, int
and float
) are allowed in the
configuration values. Do not use complex types such as lists,
dictionaries, or objects because these are impossible to provide via
configuration files etc.
Environment variables¤
Environment variables found in values are automatically expanded.
There is a special environment variable ${THIS_DIR}
that is
expanded to a directory that contains a current configuration file.
It is useful in complex configurations that utilizes included configuration files etc.
Another environment variable ${HOSTNAME}
contains the
application hostname to be used, e.g. in logging file path.
Passwords¤
[passwords]
section in the configuration serves to securely store
passwords, which are then not shown publicly in the default API config
endpoint's output.
It is convenient for the user to store passwords at one place, so that they are not repeated in many sections of the config file(s).
Example
Reference¤
Environment variables¤
Name | Usage |
---|---|
ASAB_CONFIG |
Path to the custom configuration file with which ASAB app will be using |
ASAB_ZOOKEEPERS_SERVERS |
URL for Zookeeper node |
THIS_DIR |
Directory that contains a current configuration file |
HOSTNAME |
The application hostname |
asab.Config = ConfigParser(interpolation=_Interpolation())
module-attribute
¤
asab.config.ConfigParser
¤
Bases: ConfigParser
ConfigParser enhanced with new features such as adding default configuration, URL validation, automatic reading from Zookeeper etc.
Source code in asab/config.py
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 |
|
add_defaults(dictionary)
¤
Add defaults to a current configuration.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dictionary
|
dict
|
Arguments to be added to the default configuration. |
required |
Source code in asab/config.py
getmultiline(section, option, *, raw=False, vars=None, fallback=None, **kwargs)
¤
Get multiline data from config.
Examples:
>>> asab.Config.getmultiline("places", "visited")
["Praha", "Brno", "Pardubice", "Plzeň"]
>>> asab.Config.getmultiline("places", "unvisited")
[]
>>> asab.Config.getmultiline("places", "nonexisting", fallback=["Gottwaldov"])
["Gottwaldov"]
Source code in asab/config.py
getseconds(section, option, *, raw=False, vars=None, fallback=None, **kwargs)
¤
Get time data from config and convert time string into seconds with convert_to_seconds()
method.
The available units are:
y
- yearsM
- monthsw
- weeksd
- daysh
- hoursm
- minutess
- secondsms
- milliseconds
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
Time in seconds. |
Examples:
self.SleepTime = asab.Config["sleep"].getseconds("sleep_time")
self.AnotherSleepTime = asab.Config.getseconds("sleep", "another_sleep_time")
Source code in asab/config.py
geturl(section, option, *, raw=False, vars=None, fallback=None, scheme=None, **kwargs)
¤
Get URL from config and remove all leading and trailing whitespaces and trailing slashes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scheme
|
str | tuple
|
URL scheme(s) awaited. If |
None
|
Returns:
Type | Description |
---|---|
Validated URL. |
Raises:
Type | Description |
---|---|
ValueError
|
Scheme requirements are not met if set. |
Examples:
asab.Config["urls"].geturl("teskalabs", scheme="https")
asab.Config.geturl("urls", "github", scheme=None)
Source code in asab/config.py
asab.config.Configurable
¤
Bases: object
Custom object whose attributes can be loaded from the configuration.
Example
Source code in asab/config.py
asab.config.ConfigurableDict
¤
Bases: MutableMapping
A dictionary supplemented with custom methods for obtaining bools, seconds, urls etc.