confarg 0.0.1.dev2__tar.gz → 0.0.1.dev4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. confarg-0.0.1.dev4/PKG-INFO +415 -0
  2. confarg-0.0.1.dev4/README.md +402 -0
  3. {confarg-0.0.1.dev2 → confarg-0.0.1.dev4}/pyproject.toml +6 -1
  4. {confarg-0.0.1.dev2 → confarg-0.0.1.dev4}/src/confarg/__init__.py +60 -47
  5. {confarg-0.0.1.dev2 → confarg-0.0.1.dev4}/src/confarg/_callable.py +79 -65
  6. {confarg-0.0.1.dev2 → confarg-0.0.1.dev4}/src/confarg/_defaults.py +15 -15
  7. {confarg-0.0.1.dev2 → confarg-0.0.1.dev4}/src/confarg/_errors.py +9 -0
  8. {confarg-0.0.1.dev2 → confarg-0.0.1.dev4}/src/confarg/_files.py +74 -50
  9. confarg-0.0.1.dev4/src/confarg/_merge.py +363 -0
  10. confarg-0.0.1.dev4/src/confarg/_parse_cli.py +656 -0
  11. confarg-0.0.1.dev4/src/confarg/_parse_env.py +302 -0
  12. {confarg-0.0.1.dev2 → confarg-0.0.1.dev4}/src/confarg/_serialize.py +36 -20
  13. {confarg-0.0.1.dev2 → confarg-0.0.1.dev4}/src/confarg/_types.py +48 -5
  14. confarg-0.0.1.dev4/src/confarg/cli/__init__.py +15 -0
  15. confarg-0.0.1.dev4/src/confarg/cli/argparse/__init__.py +34 -0
  16. confarg-0.0.1.dev4/src/confarg/cli/argparse/_build.py +642 -0
  17. {confarg-0.0.1.dev2/src/confarg → confarg-0.0.1.dev4/src/confarg/cli/argparse}/_completion.py +63 -53
  18. confarg-0.0.1.dev4/src/confarg/cli/argparse/_namespace.py +267 -0
  19. confarg-0.0.1.dev4/src/confarg/cli/argparse/_register.py +196 -0
  20. confarg-0.0.1.dev4/src/confarg/cli/argparse/_spec.py +149 -0
  21. confarg-0.0.1.dev4/src/confarg/cli/click/__init__.py +16 -0
  22. confarg-0.0.1.dev4/src/confarg/cli/click/_completion.py +79 -0
  23. confarg-0.0.1.dev4/src/confarg/cli/click/_context.py +139 -0
  24. confarg-0.0.1.dev4/src/confarg/cli/click/_register.py +162 -0
  25. {confarg-0.0.1.dev2 → confarg-0.0.1.dev4}/src/confarg/dictexpr/_expressions.py +605 -566
  26. confarg-0.0.1.dev4/src/confarg/typedload/_coerce.py +208 -0
  27. {confarg-0.0.1.dev2 → confarg-0.0.1.dev4}/src/confarg/typedload/_construct.py +796 -685
  28. confarg-0.0.1.dev2/PKG-INFO +0 -9
  29. confarg-0.0.1.dev2/src/confarg/_argparse.py +0 -958
  30. confarg-0.0.1.dev2/src/confarg/_merge.py +0 -284
  31. confarg-0.0.1.dev2/src/confarg/_parse_cli.py +0 -507
  32. confarg-0.0.1.dev2/src/confarg/_parse_env.py +0 -279
  33. confarg-0.0.1.dev2/src/confarg/typedload/_coerce.py +0 -178
  34. {confarg-0.0.1.dev2 → confarg-0.0.1.dev4}/src/confarg/dictexpr/__init__.py +0 -0
  35. {confarg-0.0.1.dev2 → confarg-0.0.1.dev4}/src/confarg/typedload/__init__.py +0 -0
@@ -0,0 +1,415 @@
1
+ Metadata-Version: 2.3
2
+ Name: confarg
3
+ Version: 0.0.1.dev4
4
+ Summary: A tool to manage complex, dynamic configurations.
5
+ Author: confarg
6
+ Author-email: confarg <280620574+confarg@users.noreply.github.com>
7
+ Requires-Dist: argcomplete>=3.0 ; extra == 'completion'
8
+ Requires-Python: >=3.12
9
+ Project-URL: Documentation, https://confarg.github.io/confarg
10
+ Project-URL: Repository, https://github.com/confarg/confarg
11
+ Provides-Extra: completion
12
+ Description-Content-Type: text/markdown
13
+
14
+ # A tool to manage complex configurations
15
+
16
+ > Load and resolve complex configurations from files, environment variables and command line arguments. Keep your favorite CLI library.
17
+
18
+
19
+ `confarg` is a Python library that helps you load your app configuration in a modular fashion from multiple sources: configuration files, environment variables, and command line arguments.
20
+
21
+ It can handle deeply nested configurations, type unions, derived classes, expressions and variable interpolation, configuration compositions, and can coexist with your favorite argument parser library such as `argparse`, `click`, `typer` or `cyclopts`.
22
+
23
+ If none of this makes sense to you, read along.
24
+
25
+ ## Keep your data structures and CLI
26
+
27
+ `confarg` is deliberately not a framework, but just a tool.
28
+
29
+ It does not offer custom data types or decorators, and does not own your CLI. Instead, it strives to play along with your own data structures and CLI framework, to make it easy to switch to it, or away from it.
30
+
31
+ The scope of `confarg` is limited to the deserialization and serialization of complex configurations. By limiting itself to those transient moments in the lifetime of your application, the footprint of `confarg` in your app is limited to a few lines of code.
32
+
33
+ ## Install
34
+
35
+ ```bash
36
+ pip install confarg
37
+ ```
38
+
39
+ Installing additional libraries such as `pyyaml` unlocks the support of extra configuration file formats.
40
+
41
+ ## Getting started
42
+
43
+ > All the examples presented in this section (and more) are available in the `examples/` folder.
44
+
45
+ Imagine that you have an app that depends on some parameters that you have collected into a `dataclass` like so:
46
+
47
+ ```python
48
+ @dataclass
49
+ class DBConfig:
50
+ host: str
51
+ port: int
52
+ name: str
53
+ ```
54
+
55
+ In your app, you use `confarg` to instantiate this configuration:
56
+
57
+ ```python
58
+ db_config = confarg.load(DBConfig)
59
+ ```
60
+
61
+ This allows you to construct a `DBConfig` object by collecting data from three possible sources.
62
+
63
+ 1. From a configuration file. By passing `--config <config_file>` to your app, `confarg` will load the content of the file and fill the `DBConfig` object. For example, a config file could look like so:
64
+
65
+ ```yaml
66
+ # config.yaml
67
+ host: example.com
68
+ port: 1234
69
+ name: mydb
70
+ ```
71
+
72
+ You would then call your application as
73
+
74
+ ```console notest
75
+ $ myapp.py --config config.yaml
76
+ DBConfig(host='example.com', port=1234, name='mydb')
77
+ ```
78
+
79
+ Configuration files in TOML and JSON formats are also supported.
80
+
81
+ > You can change the default `config` flag to something else using the `config_flag` parameter.
82
+
83
+ 2. From environment variables. You can declare
84
+
85
+ ```properties
86
+ MYAPP_HOST=example.com
87
+ MYAPP_PORT=1234
88
+ MYAPP_NAME=mydb
89
+ ```
90
+
91
+ for the same effect.
92
+
93
+ > Note that the environment variable prefix of your app should actually be passed to `confarg.load` like so:
94
+ >
95
+ > ```python
96
+ > db_config = confarg.load(DBConfig, env_prefix="MYAPP_")
97
+ > ```
98
+
99
+ 3. From command line arguments.
100
+
101
+ ```console notest
102
+ $ my_app --host example.com --port 1234 --name mydb
103
+ DBConfig(host='example.com', port=1234, name='mydb')
104
+ ```
105
+
106
+ ### Progressive build-up
107
+
108
+ The examples above presented different sources to feed your configuration. They are not mutually exclusive — in fact, they are intended to be used simultaneously.
109
+
110
+ Note that no one source needs to provide a complete configuration, as long as the configuration resulting from this progressive build-up is complete.
111
+
112
+ For example, taking our previous example, you could have a partial configuration file containing only host information,
113
+
114
+ ```yaml
115
+ # partial_config.yaml
116
+ host: example.com
117
+ port: 1234
118
+ ```
119
+
120
+ and provide the schema name from the command line:
121
+
122
+ ```console notest
123
+ $ myapp.py --config partial_config.yaml --name mydb
124
+ DBConfig(host='example.com', port=1234, name='mydb')
125
+ ```
126
+
127
+ ### Source precedence
128
+
129
+ Configuration data is read in the following order, later read overwriting existing data:
130
+
131
+ 1. configuration files are read first;
132
+ 2. then environment variables;
133
+ 3. finally, command line arguments.
134
+
135
+ This allows for surgical modifications of configuration files. For example, one could overwrite the schema configuration from our existing `full_config` from the command line like so:
136
+
137
+ ```console notest
138
+ $ # Overwrite the schema name defined in the config file from the command line
139
+ $ myapp.py --config config.yaml --name otherdb
140
+ DBConfig(host='example.com', port=1234, name='otherdb')
141
+ ```
142
+
143
+ ### Unions
144
+
145
+ Let's say your app needs to support SQLite databases. You now have two different, incompatible DB configurations:
146
+
147
+ ```python
148
+ @dataclass
149
+ class DBServerConfig:
150
+ host: str
151
+ port: int
152
+ name: str
153
+
154
+ @dataclass
155
+ class SQLiteConfig:
156
+ dbpath: str
157
+ ```
158
+
159
+ The DB configuration needs to be either one or the other, which we declare like so:
160
+
161
+ ```python
162
+ type DBConfig = SQLiteConfig | DBServerConfig
163
+ ```
164
+
165
+ `confarg` can handle this new union type and figure out which configuration is desired based on the arguments it got:
166
+
167
+ ```console notest
168
+ $ # Pass DBServerConfig parameters, and you get a DBServerConfig
169
+ $ myapp.py --host example.com --port 1234 --name mydb
170
+ DBServerConfig(host='example.com', port=1234, name='mydb')
171
+ $ # Pass SQLiteConfig parameters, and you get a SQLiteConfig
172
+ $ myapp.py --dbpath db.sqlite
173
+ SQLiteConfig(dbpath='db.sqlite')
174
+ ```
175
+
176
+ ### Disambiguation tags
177
+
178
+ For simple configurations, the above automatic disambiguation is enough and convenient.
179
+
180
+ In more complex configuration scenarios, this automatic disambiguation may not be not possible. For example, different configurations may share the exact same fields.
181
+
182
+ Even when disambiguation is possible, it may not be obvious to the human eye which object class should be return from the provided parameters.
183
+
184
+ Therefore, by necessity or for the sake of clarity, you can provide the class path of the required configuration by using the `class` tag, like so
185
+
186
+ ```console notest
187
+ $ # Explicitly ask for a SQLiteConfig
188
+ $ myapp.py --class myapp.SQLiteConfig --dbpath db.sqlite
189
+ SQLiteConfig(dbpath='db.sqlite')
190
+ ```
191
+
192
+ One example where it is necessary to provide the `class` path is to overwrite the configuration with a new class. Without it, command line arguments are added to the configuration, resulting in an invalid input.
193
+
194
+ ```console notest
195
+ $ # Config file contains a DBServerConfig
196
+ $ myapp.py --config db_server.yaml
197
+ DBServerConfig(host='example.com', port=1234, name='mydb')
198
+ $ # Fails: dbpath is not a DBServerConfig key
199
+ $ myapp.py --config db_server.yaml --dbpath db.sqlite
200
+ ...
201
+ $ # OK: using class signals overwrite existing DB config
202
+ $ myapp.py --config db_server.yaml --class myapp.SQLiteConfig --dbpath db.sqlite
203
+ SQLiteConfig(dbpath='db.sqlite')
204
+ ```
205
+
206
+ ### Inheritance
207
+
208
+ Another way to provide a flexible configuration is to derive akin configuration classes from a common base class.
209
+
210
+ ```python
211
+ @dataclass
212
+ class DBConfig:
213
+ pass
214
+
215
+ @dataclass
216
+ class DBServerConfig(DBConfig):
217
+ host: str
218
+ port: int
219
+ name: str
220
+
221
+ @dataclass
222
+ class SQLiteConfig(DBConfig):
223
+ dbpath: str
224
+ ```
225
+
226
+ This allows configurations to be easily extensible. Contrast with unions, where a class must be explicitly listed to be supported.
227
+
228
+ The downside is that the concrete class must be tagged, as `confarg` cannot discover classes derived from a given class.
229
+
230
+ ```console notest
231
+ $ # Fails: derived class not specified
232
+ $ uv run myapp.py --dbpath db.sqlite
233
+ ...
234
+ $ # OK: explicit class path provided
235
+ $ uv run myapp.py --dbpath db.sqlite --class myapp.SQLiteConfig
236
+ SQLiteConfig(dbpath='db.sqlite')
237
+ ```
238
+
239
+ ### Configuration hierarchies
240
+
241
+ The configurations discussed so far has been rather simple, composed of values grouped together in a `dataclass`. However, it needs not be. Configurations are generally deeply nested hierarchies, which `confarg` supports.
242
+
243
+ Let's say you want to add a log level to your application. You place it at the root level of a new `Config` object, along with the DB configuration, that is now one level down under the `db` key.
244
+
245
+ ```python
246
+ @dataclass
247
+ class Config:
248
+ db: DBConfig
249
+ log_level: Literal["DEBUG", "INFO", "WARNING", "ERROR"] = "INFO"
250
+ ```
251
+
252
+ You now parse your new top-level `Config` instead of `DBConfig`.
253
+
254
+ ```python
255
+ config = confarg.load(Config)
256
+ ```
257
+
258
+ Our DB configuration, which used to be the root configuration, is now located under the `db` key. This has the following impact.
259
+
260
+ For command line arguments, we follow the common convention of using dot-separated paths to address nested fields. Previous command line arguments for `DBConfig` are now prefixed by `db.`, like so:
261
+
262
+ ```console notest
263
+ $ myapp.py --db.class myapp.SQLiteConfig --db.dbpath db.sqlite
264
+ Config(db=SQLiteConfig(dbpath='db.sqlite'), log_level='INFO')
265
+ ```
266
+
267
+ The configuration file is also modified accordingly,
268
+
269
+ ```yaml
270
+ # config.yaml
271
+ db:
272
+ class: myapp.DBServerConfig
273
+ host: example.com
274
+ name: mydb
275
+ port: 1234
276
+ ```
277
+
278
+ and is used just like before:
279
+
280
+ ```console notest
281
+ $ myapp.py --config config.yaml
282
+ Config(db=DBServerConfig(host='example.com', port=1234, name='mydb'),
283
+ log_level='DEBUG')
284
+ ```
285
+
286
+ ### Leaf data type and type coercion
287
+
288
+ You may have noticed that the previous section introduced a `log_level` parameter that has two interesting features: first, it is not of a simple type (`str`, `int`, `float`, `bool` or `None`); second, it comes with a default value.
289
+
290
+ Default values are honored, and you may have noticed that we did not provide any value to `log_level`. You can of course override a default value.
291
+
292
+ As for leaf node data type, `confarg` coerces `Enum` and `Path` types as special exceptions to simple types. Other types are treated as classes and must follow the same rules.
293
+
294
+ ### Expressions and variable interpolation
295
+
296
+ Your application is becoming more complex by the day, and is now requiring a resources configuration.
297
+
298
+ ```python
299
+ @dataclass
300
+ class Resources:
301
+ cpu_count: int
302
+ memory_gb: int
303
+ max_heap_size_mb: int
304
+ ```
305
+
306
+ It is added to the global configuration under the `resources` key:
307
+
308
+ ```python
309
+ @dataclass
310
+ class Config:
311
+ db: DBConfig
312
+ resources: Resources
313
+ log_level: Literal["DEBUG", "INFO", "WARNING", "ERROR"] = "INFO"
314
+ ```
315
+
316
+ Your configuration file has become,
317
+
318
+ ```yaml
319
+ # config.yaml
320
+ db:
321
+ class: myapp.DBServerConfig
322
+ host: example.com
323
+ name: mydb
324
+ port: 1234
325
+
326
+ resources:
327
+ cpu_count: 4
328
+ memory_gb: 16
329
+ max_heap_size_mb: 131072
330
+ ```
331
+
332
+ This works fine. However, you want to better express the fact that `max_heap_size_mb` is chosen to be 80% of the host memory by default. To achieve this, you can write expressions relying on variable interpolation using the `${...}` syntax, like so:
333
+
334
+ ```yaml
335
+ # expression_config.yaml
336
+ db:
337
+ class: myapp.DBServerConfig
338
+ host: example.com
339
+ name: mydb
340
+ port: 1234
341
+
342
+ resources:
343
+ cpu_count: 4
344
+ memory_gb: 16
345
+ max_heap_size_mb: ${int(resources.memory_gb * 1024 * 0.8)}
346
+ ```
347
+
348
+ ```console notest
349
+ $ myapp.py --config expression_config.yaml
350
+ Config(db=SQLiteConfig(dbpath='db.sqlite'),
351
+ resources=Resources(cpu_count=4, memory_gb=16, max_heap_size_mb=13107),
352
+ log_level='INFO')
353
+ ```
354
+
355
+ Note that variable interpolation occurs after all configuration data is read. This means here that you can override `memory_gb` from the command line, and `max_heap_size_mb` will be adjusted accordingly, even though the expression is defined in the configuration file.
356
+
357
+ ```console notest
358
+ $ # Max heap is recomputed according to the expression in the config file
359
+ $ myapp.py --config expression_config.yaml --resources.memory_gb 8
360
+ Config(db=SQLiteConfig(dbpath='db.sqlite'),
361
+ resources=Resources(cpu_count=4, memory_gb=8, max_heap_size_mb=6553),
362
+ log_level='INFO')
363
+ ```
364
+
365
+ ### Building large configurations from parts
366
+
367
+ Large configurations are often made up of independent components, and as such, you may want to split them accordingly. It is easier to navigate, but it also makes it possible to reuse configuration parts and to build multiple complex configurations from the same set of atomic configuration components.
368
+
369
+ Some configuration components may even be generated automatically, in which case being able to isolate those parts from the rest is a must.
370
+
371
+ `confarg` lets you do this in different ways.
372
+
373
+ From the command line, the `--config` flag can be suffixed with a key path to load configurations there. For example,
374
+
375
+ ```console notest
376
+ # Load a config file specific to the `db` key
377
+ $ myapp.py --config.db db_config.yaml
378
+ Config(db=DBServerConfig(host='example.com', port=1234, name='mydb'), log_level='INFO')
379
+ ```
380
+
381
+ A similar pattern applies to environment variables:
382
+
383
+ ```console notest
384
+ $ MYAPP_CONFIG_DB=db_config.py myapp.py
385
+ Config(db=DBServerConfig(host='example.com', port=1234, name='mydb'), log_level='INFO')
386
+ ```
387
+
388
+ > Note that `db_config.yaml` does *not* contain the `db` key. It does not need to know the path it is loaded to.
389
+
390
+ In config files, you can load a configuration by specifying the special `__include__` key, followed by the path to the sub-configuration to load, like so:
391
+
392
+ ```yaml
393
+ # set everything under the `db` key from another file
394
+ db:
395
+ __include__: ./db_config.yaml
396
+ ```
397
+
398
+ The `__include__` keyword can also be used at the top-level, to create a new config that amends an existing config.
399
+
400
+ ```yaml
401
+ # start from this base configuration
402
+ __include__: base_config.yaml
403
+
404
+ # set or overwrite everything under the `db` key
405
+ db:
406
+ __include__: ./db_config.yaml
407
+ ```
408
+
409
+ ## Next steps
410
+
411
+ We have more than scratched the surface, and you should have enough knowledge to cover most of your needs.
412
+
413
+ Again, all of the examples above and more are in the `examples/` folder, which is a great way to discover and experiment with the library features.
414
+
415
+ A documentation is also currently being written at https://confarg.github.io/confarg/.