configaroo 0.4.2__py3-none-any.whl → 0.5.1__py3-none-any.whl

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.
configaroo/__init__.py CHANGED
@@ -20,4 +20,4 @@ __all__ = [
20
20
  "print_configuration",
21
21
  ]
22
22
 
23
- __version__ = "0.4.2"
23
+ __version__ = "0.5.1"
@@ -155,7 +155,7 @@ class Configuration(UserDict[str, Any]):
155
155
  self,
156
156
  model: type[BaseModel],
157
157
  prefix: str = "",
158
- types: type | UnionType = str | bool | int | float,
158
+ types: type | UnionType = str | bool | int | float, # pyright: ignore[reportArgumentType]
159
159
  ) -> Self:
160
160
  """Add environment variables to configuration based on the given model.
161
161
 
@@ -220,26 +220,46 @@ class Configuration(UserDict[str, Any]):
220
220
  }
221
221
 
222
222
 
223
- def print_configuration(config: Configuration | BaseModel, indent: int = 4) -> None:
223
+ def print_configuration(
224
+ config: Configuration | BaseModel, section: str | None = None, indent: int = 4
225
+ ) -> None:
224
226
  """Pretty print a configuration.
225
227
 
226
228
  If rich is installed, then a rich console is used for the printing.
227
229
  """
228
- return _print_dict_as_tree(
229
- config.model_dump() if isinstance(config, BaseModel) else config,
230
- indent=indent,
231
- _print=_get_rich_print(),
230
+ cfg = (
231
+ Configuration(config.model_dump()) if isinstance(config, BaseModel) else config
232
232
  )
233
+ if section is None:
234
+ _print, _escape = _get_rich_print()
235
+ return _print_dict_as_tree(cfg, indent=indent, _print=_print, _escape=_escape)
236
+
237
+ cfg_section = cfg.get(section)
238
+ if cfg_section is None:
239
+ message = f"'{type(cfg).__name__}' has no section '{section}'"
240
+ raise KeyError(message) from None
241
+
242
+ if isinstance(cfg_section, Configuration):
243
+ return print_configuration(cfg_section, indent=indent)
233
244
 
245
+ *_, key = section.split(".")
246
+ return print_configuration(Configuration({key: cfg_section}), indent=indent)
234
247
 
235
- def _get_rich_print() -> Callable[[str], None]: # pragma: no cover
236
- """Initialize a Rich console if Rich is installed, otherwise use built-in print."""
248
+
249
+ def _get_rich_print() -> tuple[
250
+ Callable[[str], None], Callable[[str], str]
251
+ ]: # pragma: no cover
252
+ """Initialize a Rich console if Rich is installed, otherwise use built-in print.
253
+
254
+ Include a function that can be used to escape markup.
255
+ """
237
256
  try:
238
257
  from rich.console import Console # noqa: PLC0415
258
+ from rich.markup import escape # noqa: PLC0415
239
259
 
240
- return Console().print
260
+ return Console().print, escape
241
261
  except ImportError:
242
- return print
262
+ return print, str
243
263
 
244
264
 
245
265
  def _print_dict_as_tree(
@@ -247,6 +267,7 @@ def _print_dict_as_tree(
247
267
  indent: int = 4,
248
268
  current_indent: int = 0,
249
269
  _print: Callable[[str], None] = print,
270
+ _escape: Callable[[str], str] = str,
250
271
  ) -> None:
251
272
  """Print a nested dictionary as a tree."""
252
273
  for key, value in data.items():
@@ -257,9 +278,11 @@ def _print_dict_as_tree(
257
278
  indent=indent,
258
279
  current_indent=current_indent + indent,
259
280
  _print=_print,
281
+ _escape=_escape,
260
282
  )
261
283
  else:
262
- _print(" " * current_indent + f"- {key}: {value!r}")
284
+ escaped_repr = _escape(repr(value))
285
+ _print(" " * current_indent + f"- {key}: {escaped_repr}")
263
286
 
264
287
 
265
288
  def find_pyproject_toml(
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: configaroo
3
- Version: 0.4.2
3
+ Version: 0.5.1
4
4
  Summary: Bouncy handling of configuration files
5
5
  Author-email: Geir Arne Hjelle <geirarne@gmail.com>
6
6
  Maintainer-email: Geir Arne Hjelle <geirarne@gmail.com>
@@ -33,9 +33,9 @@ Dynamic: license-file
33
33
  [![Python versions](https://img.shields.io/pypi/pyversions/configaroo.svg)](https://pypi.org/project/configaroo/)
34
34
  [![License](https://img.shields.io/pypi/l/configaroo.svg)](https://github.com/gahjelle/configaroo/blob/main/LICENSE)
35
35
  [![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
36
- [![Linted](https://github.com/gahjelle/configaroo/actions/workflows/lint.yml/badge.svg?branch=main)](https://github.com/gahjelle/configaroo/actions/workflows/lint.yml)
36
+ [![Linted with Ruff](https://github.com/gahjelle/configaroo/actions/workflows/lint.yml/badge.svg?branch=main)](https://github.com/gahjelle/configaroo/actions/workflows/lint.yml)
37
37
  [![Tested with Pytest](https://github.com/gahjelle/configaroo/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/gahjelle/configaroo/actions/workflows/test.yml)
38
- [![Type checked with mypy](https://img.shields.io/badge/type%20checked-mypy-green)](http://mypy-lang.org/)
38
+ [![Type checked with pyright](https://microsoft.github.io/pyright/img/pyright_badge.svg)](https://microsoft.github.io/pyright/)
39
39
 
40
40
  Configaroo is a light configuration package for Python that offers the following features:
41
41
 
@@ -1,12 +1,12 @@
1
- configaroo/__init__.py,sha256=rzq0iqqBuc--QB3yBM4dA6LYLAdLuviKwqeUzfcjCs0,477
2
- configaroo/configuration.py,sha256=1tGBLkyz703txcaGx49kWHMiVYgjTcERnikVQ69Pusc,10928
1
+ configaroo/__init__.py,sha256=vAghcz_xOnqTbFv0JlcHtG9gnff-BH2KsOfQEELVyos,477
2
+ configaroo/configuration.py,sha256=X_Wmw_NaY0vpGDZOTDI9aylqtgIDKKtLXl48KL2xd3M,11791
3
3
  configaroo/exceptions.py,sha256=GfLf3CLfHStiQjvdS7ZAtrKF9gmGL_8biFLayue6J0M,772
4
4
  configaroo/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
5
  configaroo/loaders/__init__.py,sha256=XQrFwCMWzQ71ykaZFPmYysDz12y_elPqxWhUMQCsq3s,1076
6
6
  configaroo/loaders/json.py,sha256=fT2Lg4hPM2BuwqrDsP7bcJlepAdmEh2iKU-YVK4KmIA,306
7
7
  configaroo/loaders/toml.py,sha256=jw9U78Lf-GMA8QmGIM8xMBqOhPaa8ITSMAhhN1ZNyng,256
8
- configaroo-0.4.2.dist-info/licenses/LICENSE,sha256=rdeT6Y5bm0MUaERso7HRWpPj37Y1RD5li2lIQaMNJjc,1090
9
- configaroo-0.4.2.dist-info/METADATA,sha256=i3TOmbmkG6e85-OGWqNK_sKC6CCBmjhlj8vtbreGVL8,2672
10
- configaroo-0.4.2.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
11
- configaroo-0.4.2.dist-info/top_level.txt,sha256=JVYICl1cWSjvSOZuZMYm976z9lnZaWtHVRSt373QCxg,11
12
- configaroo-0.4.2.dist-info/RECORD,,
8
+ configaroo-0.5.1.dist-info/licenses/LICENSE,sha256=rdeT6Y5bm0MUaERso7HRWpPj37Y1RD5li2lIQaMNJjc,1090
9
+ configaroo-0.5.1.dist-info/METADATA,sha256=pF6qO6hARbtFWyKkkssH9XuPTbJep18CBzxIhNQuOxU,2703
10
+ configaroo-0.5.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
11
+ configaroo-0.5.1.dist-info/top_level.txt,sha256=JVYICl1cWSjvSOZuZMYm976z9lnZaWtHVRSt373QCxg,11
12
+ configaroo-0.5.1.dist-info/RECORD,,