ATLAS Offline Software
Loading...
Searching...
No Matches
PhysicsAnalysis/Columnar/ColumnarToolWrapperPython/python/config.py
Go to the documentation of this file.
1# Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
2#
3# @author Giordon Stark
4
5# TOML-based configuration loading for ColumnarToolWrapperPython.
6# Supports global defaults and per-tool-type overrides for rename_containers
7# and properties. Multiple instances of the same tool type are supported via
8# TOML array-of-tables: [[tool.TypeName]].
9
10from pathlib import Path
11
12try:
13 import tomllib
14except ImportError:
15 try:
16 import tomli as tomllib
17 except ImportError as exc:
18 raise ImportError(
19 "TOML support requires Python >= 3.11 or 'tomli' package. "
20 "Install with: pip install tomli"
21 ) from exc
22
23
24def load_config(path):
25 """Load a TOML configuration file.
26
27 Parameters
28 ----------
29 path:
30 Path to a TOML file (str or Path).
31
32 Returns
33 -------
34 dict
35 Raw parsed TOML dict. Tool entries are under ``cfg["tool"][TypeName]``
36 as a list (one entry per ``[[tool.TypeName]]`` block).
37 """
38 with Path(path).open("rb") as f:
39 return tomllib.load(f)
40
41
42def merge_tool_config(global_cfg, tool_entry):
43 """Merge global config with a single tool entry's overrides.
44
45 Parameters
46 ----------
47 global_cfg:
48 The ``cfg["global"]`` dict (may be empty).
49 tool_entry:
50 A single entry from ``cfg["tool"][TypeName]`` (one ``[[tool.TypeName]]``
51 block). May be empty or contain ``rename_containers`` / ``properties``.
52
53 Returns
54 -------
55 dict
56 Dict with keys ``"rename_containers"`` and ``"properties"``, each a
57 dict. Tool-level values override global values.
58 """
59 rename_containers = {**global_cfg.get("rename_containers", {}),
60 **tool_entry.get("rename_containers", {})}
61 properties = {**global_cfg.get("properties", {}),
62 **tool_entry.get("properties", {})}
63
64 return {"rename_containers": rename_containers, "properties": properties}