cfgable 0.1.0__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.
cfgable/__init__.py ADDED
@@ -0,0 +1,75 @@
1
+ """cfgable: a reusable pydantic/dataclass-driven configuration framework.
2
+
3
+ Components declare a single ``config`` parameter and (optionally) inherit
4
+ ``ConfigurableBasis``; the ``InitConfigMeta`` metaclass assembles that config from
5
+ explicit objects, plain mappings, or flat keyword arguments (the shape Hydra's
6
+ ``instantiate`` passes), validating it once at construction.
7
+
8
+ The core exported here depends only on pydantic. The optional Hydra bridge lives
9
+ in ``cfgable.hydra_utils`` and is imported on demand, so ``import cfgable``
10
+ never pulls in hydra/omegaconf.
11
+
12
+ NOTE: do NOT add ``from .hydra_utils import ...`` to this module — it would make
13
+ hydra a hard dependency of the core. Import it explicitly where you need it.
14
+ """
15
+
16
+ from ._typing import (
17
+ AllConfigType,
18
+ BaseModelT,
19
+ ConfigType,
20
+ DataClassProto,
21
+ OtherCfgType,
22
+ T,
23
+ )
24
+ from .core import (
25
+ ConfigurableBasis,
26
+ InitConfigABCMeta,
27
+ InitConfigABCMixin,
28
+ InitConfigMeta,
29
+ InitConfigMixin,
30
+ InitConfigMixinBasis,
31
+ NoConfig,
32
+ dump_omegaconf,
33
+ dump_or_repr,
34
+ fetch_config,
35
+ )
36
+ from .enums import ReprEnum, StrEnum
37
+ from .frozen import ForceSetAttr, force_set_attr, force_validate_field, validate_field
38
+ from .reflection import (
39
+ get_full_class_name,
40
+ get_fully_qualified_class_name,
41
+ import_string,
42
+ )
43
+
44
+ __all__ = [
45
+ # typing
46
+ "ConfigType",
47
+ "AllConfigType",
48
+ "OtherCfgType",
49
+ "DataClassProto",
50
+ "BaseModelT",
51
+ "T",
52
+ # core
53
+ "NoConfig",
54
+ "InitConfigMeta",
55
+ "InitConfigABCMeta",
56
+ "InitConfigMixinBasis",
57
+ "InitConfigMixin",
58
+ "InitConfigABCMixin",
59
+ "ConfigurableBasis",
60
+ "dump_or_repr",
61
+ "dump_omegaconf",
62
+ "fetch_config",
63
+ # enums
64
+ "StrEnum",
65
+ "ReprEnum",
66
+ # frozen
67
+ "ForceSetAttr",
68
+ "force_set_attr",
69
+ "validate_field",
70
+ "force_validate_field",
71
+ # reflection
72
+ "import_string",
73
+ "get_fully_qualified_class_name",
74
+ "get_full_class_name",
75
+ ]
cfgable/_typing.py ADDED
@@ -0,0 +1,28 @@
1
+ """Shared typing primitives for the cfgable framework."""
2
+
3
+ from pathlib import Path
4
+ from typing import Any, Dict, Optional, Protocol, Type, TypeVar, Union
5
+
6
+ from pydantic import BaseModel
7
+ from typing_extensions import runtime_checkable
8
+
9
+ BaseModelT = TypeVar("BaseModelT", bound=BaseModel)
10
+ T = TypeVar("T")
11
+
12
+
13
+ @runtime_checkable
14
+ class DataClassProto(Protocol):
15
+ """Protocol for dataclass types."""
16
+
17
+ @classmethod
18
+ def __dataclass_fields__(cls) -> Dict[str, Any]: ...
19
+
20
+
21
+ # A config is a pydantic model or a dataclass instance.
22
+ ConfigType = Union[BaseModel, DataClassProto]
23
+ # Things that can be *resolved into* a config: a config type, a mapping of
24
+ # fields, or a path/dotted-string pointing at one.
25
+ OtherCfgType = Optional[
26
+ Union[Type[Union[DataClassProto, BaseModel]], Dict[str, Any], str, Path]
27
+ ]
28
+ AllConfigType = Union[ConfigType, OtherCfgType]
cfgable/_vendor.py ADDED
@@ -0,0 +1,19 @@
1
+ """Small vendored helpers, kept here to avoid extra runtime dependencies."""
2
+
3
+
4
+ def get_in(keys, coll, default=None, no_default=False):
5
+ """Return ``coll[k0][k1][...]`` following the sequence ``keys``.
6
+
7
+ Vendored equivalent of ``toolz.dicttoolz.get_in``. On a missing key/index
8
+ (``KeyError``, ``IndexError``, ``TypeError``) return ``default`` — unless
9
+ ``no_default`` is set, in which case the original exception propagates.
10
+ """
11
+ try:
12
+ out = coll
13
+ for key in keys:
14
+ out = out[key]
15
+ return out
16
+ except (KeyError, IndexError, TypeError):
17
+ if no_default:
18
+ raise
19
+ return default
cfgable/core.py ADDED
@@ -0,0 +1,393 @@
1
+ from abc import ABCMeta, abstractmethod
2
+ from dataclasses import asdict, replace, is_dataclass
3
+ from logging import getLogger
4
+ from typing import (
5
+ Union,
6
+ Optional,
7
+ Type,
8
+ Dict,
9
+ Any,
10
+ Literal,
11
+ Set,
12
+ List,
13
+ final,
14
+ get_type_hints,
15
+ )
16
+ from collections.abc import Mapping, Callable
17
+ from typing_extensions import get_args, Self
18
+ from pydantic import BaseModel, TypeAdapter
19
+ from pydantic_yaml import to_yaml_file
20
+ from pathlib import Path
21
+ from functools import cache
22
+ from weakref import WeakSet
23
+ from ._typing import (
24
+ DataClassProto, # noqa: F401 re-exported for backward-compat shims
25
+ ConfigType,
26
+ AllConfigType,
27
+ OtherCfgType, # noqa: F401 re-exported for backward-compat shims
28
+ )
29
+ from .reflection import import_string, get_fully_qualified_class_name
30
+ from copy import copy, deepcopy
31
+ from ._vendor import get_in
32
+ import inspect
33
+ import yaml
34
+ import json
35
+ import pickle
36
+
37
+
38
+ class NoConfig(BaseModel, frozen=True):
39
+ """A placeholder config class indicating no configuration is needed."""
40
+
41
+
42
+ class InitConfigMeta(type):
43
+ def __call__(cls, config: AllConfigType = None, **kwargs):
44
+ error_msg = """
45
+ If not provided as an arg, `config` must be annotated as a subclass of `ConfigType`
46
+ either in the `__init__` method or at the class scope. If a class truly does not
47
+ require any configuration, you can mark the type as `None` at the class scope."""
48
+ if isinstance(config, (str, Path)):
49
+ config = cls.config_from_file(config)
50
+ config_type = (
51
+ config
52
+ if isinstance(config, type)
53
+ else cls.resolve_config_type(cls)
54
+ if (config is None or isinstance(config, Mapping))
55
+ else type(config)
56
+ )
57
+ cls_path = None
58
+ if isinstance(config, Mapping):
59
+ cls_path = config.pop("_target_", cls_path)
60
+ cls_path = kwargs.pop("_target_", cls_path)
61
+ if config_type is None:
62
+ # try to get from _target_
63
+ if cls_path is not None:
64
+ config_type = import_string(cls_path)
65
+ # TODO: it is better to support more flexible config types
66
+ # e.g. dict, etc.
67
+ if not cls._check_config_type(config_type):
68
+ raise TypeError(f"{cls}" + error_msg + f" Got {config_type} instead.")
69
+ if not issubclass(config_type, BaseModel):
70
+ adapter = TypeAdapter(config_type)
71
+ if isinstance(config, Mapping):
72
+ # if (
73
+ # get_fully_qualified_class_name(config)
74
+ # == "omegaconf.dictconfig.DictConfig"
75
+ # ):
76
+ # from omegaconf import OmegaConf
77
+
78
+ # config = OmegaConf.to_object(config)
79
+ config.update(kwargs)
80
+ config = config_type(**config)
81
+ elif config is None or isinstance(config, type):
82
+ if issubclass(config_type, BaseModel):
83
+ config = config_type(**kwargs)
84
+ else:
85
+ config = adapter.validate_python(kwargs)
86
+ elif kwargs: # mainly used by yaml config, e.g. hydra
87
+ if isinstance(config, BaseModel):
88
+ config = config.model_copy(update=kwargs)
89
+ # re-validate
90
+ config = config.model_validate(config.model_dump(warnings="none"))
91
+ extra = kwargs.keys() - config_type.model_fields.keys()
92
+ if extra:
93
+ cls.get_logger().warning(
94
+ f"Extra fields {extra} found in config, which will be ignored."
95
+ )
96
+ else: # dataclass
97
+ config = replace(config, **kwargs)
98
+ config = adapter.validate_python(asdict(config))
99
+ # NOTE: since there may be arbitrary instances in the config,
100
+ # we should not deep copy the config here to avoid potential issues.
101
+ # if isinstance(config, BaseModel):
102
+ # cfg_copy = config.model_copy(deep=True)
103
+ # else:
104
+ # cfg_copy = deepcopy(config)
105
+ instance: "InitConfigMixinBasis" = super().__call__(config)
106
+ # instance.__config = cfg_copy
107
+ if not hasattr(instance, "config"):
108
+ instance.config = config
109
+ instance.config_post_init()
110
+ return instance
111
+
112
+ @staticmethod
113
+ def _check_config_type(config_type: Type) -> bool:
114
+ return isinstance(config_type, type) and issubclass(
115
+ config_type, get_args(ConfigType)
116
+ )
117
+
118
+ @classmethod
119
+ def get_logger(cls):
120
+ return getLogger(cls.__name__)
121
+
122
+ @staticmethod
123
+ @cache
124
+ def resolve_config_type(cls) -> Optional[Type]:
125
+ cfg_type = InitConfigMeta.get_annotation(cls, "config")
126
+ if cfg_type in (None, ...) or (getattr(cls, "config", object) in (None, ...)):
127
+ cfg_type = NoConfig
128
+ elif cfg_type is object:
129
+ cfg_type = get_type_hints(cls.__init__).get("config", None)
130
+ return cfg_type
131
+
132
+ @staticmethod
133
+ @cache
134
+ def get_annotation(cls, name: str) -> Optional[Any]:
135
+ return getattr(cls, "__annotations__", {}).get(name, object)
136
+
137
+ @staticmethod
138
+ def config_from_file(path: Union[str, Path]) -> AllConfigType:
139
+ path = Path(path)
140
+ if not path.exists():
141
+ raise FileNotFoundError(f"Config file {path} not found.")
142
+ if path.suffix == ".pkl":
143
+ with open(path, "rb") as f:
144
+ config = pickle.load(f)
145
+ else: # yaml or json
146
+ with open(path, "r") as f:
147
+ config = yaml.safe_load(f)
148
+ return config
149
+
150
+
151
+ class InitConfigABCMeta(InitConfigMeta, ABCMeta):
152
+ """A metaclass that combines InitConfigMeta and ABCMeta."""
153
+
154
+
155
+ class InitConfigMixinBasis:
156
+ """A mixin class for initializing with a config."""
157
+
158
+ def __init__(self, config: ConfigType) -> None:
159
+ self.config = config
160
+ """Initialize with a config. It is only used for type hinting in the current class;
161
+ subclasses can override it at will. Note that you should try to avoid directly modifying
162
+ the config variable internally, as this may lead to potential problems, such as inconsistent
163
+ behavior when the same config variable is used to instantiate the class multiple times. Unless
164
+ there is a special need, such as a class that might be specifically designed to modify external
165
+ configurations, it is best to update the copied variables and then reassign them to the instance.
166
+ Furthermore, for maximum security and faster value retrieval, the configuration class should be
167
+ set to frozen unless there is a clear need for modification. Conversely, due to this security
168
+ mechanism, in complex configuration chains, the original external configuration may not accurately
169
+ reflect the internal configuration of the instance. Therefore, when a configuration needs to be
170
+ referenced externally, this consistency issue should be considered. However, this should not be
171
+ seen as a drawback of this security mechanism, as it is an issue that must be considered in all
172
+ circumstances. On the contrary, it forces developers to make more proactive and reasonable designs.
173
+ It is undeniable that shallow copy operations incur additional time consumption, but this is usually
174
+ negligible."""
175
+
176
+ @classmethod
177
+ def get_logger(cls):
178
+ return getLogger(cls.__name__)
179
+
180
+ def dump(self, mode: Literal["python", "json"] = "python") -> Dict[str, Any]:
181
+ """Dump the config to a dictionary.
182
+ Args:
183
+ mode: The mode to dump the config. "python" for standard dict, "json" (only applicable when the config is the `BaseModel` of pydantic) for JSON serializable dict.
184
+ Returns:
185
+ A dictionary representing the config with a "_target_" key indicating the class path.
186
+ """
187
+ if isinstance(self.config, BaseModel):
188
+ config = self.config.model_dump(mode=mode)
189
+ else: # dataclass
190
+ config = asdict(self.config)
191
+ cls = self.__class__
192
+ config["_target_"] = f"{cls.__module__}.{cls.__qualname__}"
193
+ return config
194
+
195
+ def save_config(
196
+ self,
197
+ path: Union[str, Path],
198
+ mode: Optional[Literal["yaml", "json", "pickle"]] = None,
199
+ ) -> None:
200
+ """Save the config to a yaml file.
201
+ Args:
202
+ path: The file path to save the config.
203
+ mode: The mode to save the config. If None, it will be inferred from the file extension.
204
+ """
205
+ with open(path, "w") as f:
206
+ mode = Path(path).suffix.removeprefix(".") if mode is None else mode
207
+ if mode in ("yaml", "yml"):
208
+ if isinstance(self.config, BaseModel):
209
+ try:
210
+ to_yaml_file(f, self.config, add_comments=True)
211
+ except TypeError:
212
+ # Some pydantic-yaml versions don't accept `add_comments`
213
+ # (they forward it into model_dump_json). Fall back to a
214
+ # plain dump rather than failing the save.
215
+ to_yaml_file(f, self.config)
216
+ else: # dataclass
217
+ yaml.dump(asdict(self.config), f)
218
+ elif mode == "json":
219
+ json.dump(self.dump(mode="json"), f, indent=4)
220
+ elif mode in ("pickle", "pkl"):
221
+ pickle.dump(self.config, f)
222
+ else:
223
+ raise ValueError(f"Unsupported mode: {mode}")
224
+
225
+ def config_post_init(self) -> None:
226
+ """A hook to be called after the config is set."""
227
+
228
+ @final
229
+ def copy(
230
+ self, update: Optional[Mapping[str, Any]] = None, deep: bool = False
231
+ ) -> Self:
232
+ """Create a copy of the current instance with the same config."""
233
+ if isinstance(self.config, BaseModel):
234
+ config = self.config.model_copy(deep=deep, update=update)
235
+ else:
236
+ if update:
237
+ raise NotImplementedError(
238
+ "Update is only supported for BaseModel config."
239
+ )
240
+ method = copy if not deep else deepcopy
241
+ config = method(self.config)
242
+ return self.__class__(config)
243
+
244
+ def __repr__(self):
245
+ return f"{self.__module__}.{self.__class__.__qualname__}"
246
+
247
+
248
+ class InitConfigMixin(InitConfigMixinBasis, metaclass=InitConfigMeta):
249
+ """Mixin class for initializing with a config."""
250
+
251
+
252
+ class InitConfigABCMixin(InitConfigMixinBasis, metaclass=InitConfigABCMeta):
253
+ """Mixin class for initializing with a config and supporting abstract methods."""
254
+
255
+
256
+ class ConfigurableBasis(InitConfigABCMixin):
257
+ """A basis class for easy configuring."""
258
+
259
+ __instances: Set[Self] = WeakSet()
260
+
261
+ def config_post_init(self):
262
+ self._configured = False
263
+ self.__class__.__instances.add(self)
264
+
265
+ @final
266
+ def configure(self) -> bool:
267
+ if self._configured:
268
+ raise RuntimeError("Already configured")
269
+ itf_type = InitConfigMeta.get_annotation(self.__class__, "interface")
270
+ self.interface = self._create_interface(itf_type)
271
+ self._configured = self.on_configure()
272
+ return self._configured
273
+
274
+ @abstractmethod
275
+ def on_configure(self) -> bool:
276
+ """Callback to be called when configuring"""
277
+ raise NotImplementedError
278
+
279
+ @final
280
+ @property
281
+ def configured(self) -> bool:
282
+ return self._configured
283
+
284
+ @classmethod
285
+ def all_configure(cls) -> bool:
286
+ """Configure all instances of this class and its subclasses."""
287
+ for instance in cls.__instances:
288
+ if not instance.configured:
289
+ if not instance.configure():
290
+ cls.get_logger().error(f"Failed to configure instance: {instance}")
291
+ return False
292
+ return True
293
+
294
+ def _create_interface(self, class_type: Optional[Type]):
295
+ """Create the interface instance based on the config and the class type annotation.
296
+ The subclasses can override this method if needed.
297
+ """
298
+ if class_type is None:
299
+ return None
300
+ sig = inspect.signature(class_type)
301
+ if "config" in sig.parameters.keys():
302
+ interface = class_type(config=self.config)
303
+ else:
304
+ # convert the first level config to dict
305
+ if isinstance(self.config, BaseModel):
306
+ # dict(self.config) has some bugs
307
+ # so we use the following way
308
+ cfg_dict = {
309
+ k: getattr(self.config, k)
310
+ for k in self.config.__class__.model_fields.keys()
311
+ }
312
+ else: # dataclass
313
+ # TODO: error when using nested dataclass
314
+ cfg_dict = asdict(self.config)
315
+ com_keys = cfg_dict.keys() & sig.parameters.keys()
316
+ interface = class_type(**{key: cfg_dict[key] for key in com_keys})
317
+ return interface
318
+
319
+
320
+ def dump_omegaconf(obj: Any) -> Dict[str, Any]:
321
+ """Dump an omegaconf object to a dictionary.
322
+ Args:
323
+ obj: The omegaconf object to dump.
324
+ Returns:
325
+ A dictionary representing the omegaconf object.
326
+ Raises:
327
+ TypeError: If the object is not an omegaconf object.
328
+ """
329
+ from omegaconf import OmegaConf
330
+
331
+ if get_fully_qualified_class_name(obj) in {
332
+ "omegaconf.dictconfig.DictConfig",
333
+ "omegaconf.listconfig.ListConfig",
334
+ }:
335
+ return OmegaConf.to_object(obj)
336
+ raise TypeError("Not an omegaconf object.")
337
+
338
+
339
+ def dump_or_repr(
340
+ obj: Union[Any, ConfigurableBasis], handler: Optional[Callable] = None
341
+ ) -> Union[Dict[str, Any], str]:
342
+ """Dump the config if the object is a ConfigurableBasis, otherwise return the repr.
343
+ Args:
344
+ obj: The object to dump or repr.
345
+ Returns:
346
+ A dictionary representing the config or the repr string.
347
+ """
348
+ if callable(getattr(obj, "dump", None)):
349
+ try:
350
+ return obj.dump()
351
+ except Exception as e:
352
+ getLogger(f"{dump_or_repr.__name__}").error(f"Failed to dump config: {e}")
353
+ config = getattr(obj, "config", None)
354
+ if config is not None:
355
+ if isinstance(config, BaseModel):
356
+ return config.model_dump()
357
+ elif is_dataclass(config):
358
+ return asdict(config)
359
+ elif isinstance(config, dict):
360
+ return config
361
+ try:
362
+ return dump_omegaconf(obj)
363
+ except TypeError:
364
+ if handler is not None:
365
+ return handler(obj)
366
+ return repr(obj)
367
+
368
+
369
+ def fetch_config(
370
+ target: Union[Path, str, Mapping],
371
+ keys: Union[List[str], str] = "",
372
+ default=None,
373
+ no_default: bool = True,
374
+ ) -> Any:
375
+ """Fetch the config from a target mapping or a yaml file.
376
+ Args:
377
+ target: The target mapping or file path.
378
+ keys: The keys or a dot-separated key to the config. If empty, return the whole config.
379
+ default: The default value if the key is not found.
380
+ no_default: Whether to raise an error if the key is not found and no default is provided.
381
+ Returns:
382
+ The config object.
383
+ """
384
+ if isinstance(target, (str, Path)):
385
+ with open(target, "r") as f:
386
+ data = yaml.safe_load(f)
387
+ else:
388
+ data = target
389
+ if not keys:
390
+ return data
391
+ if isinstance(keys, str):
392
+ keys = keys.split(".")
393
+ return get_in(keys, data, default, no_default)
cfgable/enums.py ADDED
@@ -0,0 +1,44 @@
1
+ from enum import Enum
2
+
3
+
4
+ class ReprEnum(Enum):
5
+ """
6
+ Only changes the repr(), leaving str() and format() to the mixed-in type.
7
+ """
8
+
9
+
10
+ class StrEnum(str, ReprEnum):
11
+ """
12
+ Enum where members are also (and must be) strings
13
+ """
14
+
15
+ def __new__(cls, *values):
16
+ "values must already be of type `str`"
17
+ if len(values) > 3:
18
+ raise TypeError(f"too many arguments for str(): {values!r}")
19
+ if len(values) == 1:
20
+ # it must be a string
21
+ if not isinstance(values[0], str):
22
+ raise TypeError(f"{values[0]!r} is not a string")
23
+ if len(values) >= 2:
24
+ # check that encoding argument is a string
25
+ if not isinstance(values[1], str):
26
+ raise TypeError(f"encoding must be a string, not {values[1]!r}")
27
+ if len(values) == 3:
28
+ # check that errors argument is a string
29
+ if not isinstance(values[2], str):
30
+ raise TypeError("errors must be a string, not %r" % (values[2]))
31
+ value = str(*values)
32
+ member = str.__new__(cls, value)
33
+ member._value_ = value
34
+ return member
35
+
36
+ @staticmethod
37
+ def _generate_next_value_(name, start, count, last_values):
38
+ """
39
+ Return the lower-cased version of the member name.
40
+ """
41
+ return name.lower()
42
+
43
+ def __str__(self):
44
+ return self.value
cfgable/frozen.py ADDED
@@ -0,0 +1,62 @@
1
+ """Helpers for mutating otherwise-frozen pydantic models in a controlled way.
2
+
3
+ Frozen configs are the default in this framework (a component should not rewrite
4
+ the settings it was handed). Occasionally a component genuinely needs to update a
5
+ frozen config — e.g. it is specifically designed to derive and reassign values.
6
+ ``ForceSetAttr`` / ``force_set_attr`` make that explicit and localized rather than
7
+ dropping ``frozen`` altogether.
8
+ """
9
+
10
+ from functools import wraps
11
+ from typing import Any, Generic
12
+
13
+ from pydantic import BaseModel
14
+
15
+ from ._typing import BaseModelT
16
+
17
+
18
+ def validate_field(obj: BaseModel, name: str, value: Any):
19
+ """Validate a field value using the pydantic model's assignment validator."""
20
+ obj.__pydantic_validator__.validate_assignment(obj, name, value)
21
+
22
+
23
+ class ForceSetAttr(Generic[BaseModelT]):
24
+ """Context manager to temporarily allow setting attributes on frozen models."""
25
+
26
+ def __init__(self, obj: BaseModelT):
27
+ if not isinstance(obj, BaseModel):
28
+ raise TypeError("Only Pydantic BaseModel instances are supported.")
29
+ self._obj = obj
30
+
31
+ def __enter__(self) -> BaseModelT:
32
+ self._original_setattr = self._obj.__class__.__setattr__
33
+ self._obj.__class__.__setattr__ = self._setattr
34
+ return self._obj
35
+
36
+ def __exit__(self, exc_type, exc_val, exc_tb):
37
+ self._obj.__class__.__setattr__ = self._original_setattr
38
+
39
+ def _setattr(self, name, value):
40
+ obj = self._obj
41
+ config = obj.model_config
42
+ if config.get("frozen", False):
43
+ if config.get("validate_assignment", False):
44
+ validate_field(obj, name, value)
45
+ else:
46
+ object.__setattr__(obj, name, value)
47
+ else:
48
+ setattr(obj, name, value)
49
+
50
+
51
+ def force_set_attr(method):
52
+ """Decorator to force attribute setting on frozen pydantic models."""
53
+
54
+ @wraps(method)
55
+ def wrapper(self, *args, **kwargs):
56
+ with ForceSetAttr(self):
57
+ return method(self, *args, **kwargs)
58
+
59
+ return wrapper
60
+
61
+
62
+ force_validate_field = force_set_attr(validate_field)
cfgable/hydra_utils.py ADDED
@@ -0,0 +1,67 @@
1
+ """Optional Hydra bridge. Requires the ``[hydra]`` extra (hydra-core + omegaconf).
2
+
3
+ Importing this module pulls in hydra/omegaconf, so it is deliberately NOT imported
4
+ by ``cfgable/__init__.py`` — import it explicitly:
5
+ ``from cfgable.hydra_utils import init_hydra_config``.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import hydra
11
+ import os.path as osp
12
+ from omegaconf import DictConfig
13
+ from typing import Optional
14
+ from pathlib import Path
15
+
16
+
17
+ def relative_path_between(path1: Path, path2: Path) -> Path:
18
+ """Returns path1 relative to path2."""
19
+ path1 = Path(path1).absolute()
20
+ path2 = Path(path2).absolute()
21
+ try:
22
+ return path1.relative_to(path2)
23
+ except ValueError: # most likely because path1 is not a subpath of path2
24
+ common_parts = Path(osp.commonpath([path1, path2])).parts
25
+ return Path(
26
+ "/".join(
27
+ [".."] * (len(path2.parts) - len(common_parts))
28
+ + list(path1.parts[len(common_parts) :])
29
+ )
30
+ )
31
+
32
+
33
+ def init_hydra_config(
34
+ config_path: str, overrides: Optional[list[str]] = None
35
+ ) -> DictConfig:
36
+ """Initialize a Hydra config given only the path to the relevant config file.
37
+
38
+ For config resolution, it is assumed that the config file's parent is the Hydra config dir.
39
+ """
40
+ # TODO(alexander-soare): Resolve configs without Hydra initialization.
41
+ hydra.core.global_hydra.GlobalHydra.instance().clear()
42
+ # Hydra needs a path relative to this file.
43
+ hydra.initialize(
44
+ str(
45
+ relative_path_between(
46
+ Path(config_path).absolute().parent, Path(__file__).absolute().parent
47
+ )
48
+ ),
49
+ version_base="1.2",
50
+ )
51
+ cfg = hydra.compose(Path(config_path).stem, overrides)
52
+ return cfg
53
+
54
+
55
+ def hydra_instance(cfg: DictConfig):
56
+ return hydra.utils.instantiate(cfg)
57
+
58
+
59
+ def hydra_instance_from_config_path(config_path: str, params: dict = None):
60
+ config = init_hydra_config(config_path)
61
+ if params:
62
+ config.update(params)
63
+ return hydra_instance(config)
64
+
65
+
66
+ def hydra_instance_from_dict(config: dict):
67
+ return hydra_instance(DictConfig(config))
cfgable/reflection.py ADDED
@@ -0,0 +1,29 @@
1
+ """Class/string reflection helpers used by the cfgable framework."""
2
+
3
+ from inspect import isclass
4
+ from typing import Any, Type, Union
5
+
6
+ from pydantic import ImportString, validate_call
7
+
8
+ from ._typing import T
9
+
10
+
11
+ def get_fully_qualified_class_name(obj_or_cls) -> str:
12
+ """Return ``"module.QualName"`` for a class or an instance."""
13
+ if isinstance(obj_or_cls, type):
14
+ cls = obj_or_cls
15
+ else:
16
+ cls = type(obj_or_cls)
17
+ return f"{cls.__module__}.{cls.__qualname__}"
18
+
19
+
20
+ def get_full_class_name(obj: Union[Any, Type]) -> str:
21
+ """Return ``"module.QualName"`` for a class or an instance."""
22
+ cls = obj if isclass(obj) else obj.__class__
23
+ return f"{cls.__module__}.{cls.__qualname__}"
24
+
25
+
26
+ @validate_call
27
+ def import_string(import_path: ImportString[T]) -> T:
28
+ """Import and return the object at a dotted path (validated by pydantic)."""
29
+ return import_path
@@ -0,0 +1,143 @@
1
+ Metadata-Version: 2.4
2
+ Name: cfgable
3
+ Version: 0.1.0
4
+ Summary: Reusable pydantic/dataclass-driven configuration framework with an optional Hydra bridge.
5
+ Project-URL: Repository, https://github.com/OpenGHz/cfgable
6
+ Author: OpenGHz
7
+ License: MIT
8
+ License-File: LICENSE
9
+ Keywords: config,configuration,hydra,pydantic,settings
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Typing :: Typed
13
+ Requires-Python: >=3.9
14
+ Requires-Dist: pydantic-yaml
15
+ Requires-Dist: pydantic>=2
16
+ Requires-Dist: typing-extensions
17
+ Provides-Extra: cli
18
+ Requires-Dist: pydantic-settings; extra == 'cli'
19
+ Provides-Extra: dev
20
+ Requires-Dist: hydra-core; extra == 'dev'
21
+ Requires-Dist: omegaconf; extra == 'dev'
22
+ Requires-Dist: pytest; extra == 'dev'
23
+ Provides-Extra: hydra
24
+ Requires-Dist: hydra-core; extra == 'hydra'
25
+ Requires-Dist: omegaconf; extra == 'hydra'
26
+ Description-Content-Type: text/markdown
27
+
28
+ # cfgable
29
+
30
+ A small, reusable configuration framework for Python, built on
31
+ [pydantic](https://docs.pydantic.dev/). It lets every component declare a **single
32
+ `config` object** and be constructed uniformly — from Python, from a YAML file, or from
33
+ Hydra — with validation happening once at the boundary.
34
+
35
+ The core depends only on pydantic; the Hydra bridge is optional.
36
+
37
+ ## Install
38
+
39
+ ```bash
40
+ pip install cfgable # core
41
+ pip install "cfgable[hydra]" # + Hydra bridge (hydra-core, omegaconf)
42
+ ```
43
+
44
+ ## The idea
45
+
46
+ 1. A component's settings are a pydantic model (`*Config`), documented with attribute
47
+ docstrings.
48
+ 2. Every class takes **one** `config` parameter and reads fields off it.
49
+ 3. Inheriting `ConfigurableBasis` lets the same class be built from an explicit config,
50
+ a plain mapping, or **flat keyword arguments** — the shape Hydra's `instantiate`
51
+ passes — because the `InitConfigMeta` metaclass assembles and validates the config
52
+ for you, then calls `config_post_init()`.
53
+
54
+ ```python
55
+ from typing import Optional
56
+ from pydantic import BaseModel, ConfigDict, PositiveInt
57
+ from cfgable import ConfigurableBasis
58
+
59
+
60
+ class CameraConfig(BaseModel, frozen=True):
61
+ """Configuration for a camera."""
62
+
63
+ model_config = ConfigDict(use_attribute_docstrings=True, extra="forbid")
64
+
65
+ device: str = "/dev/video0"
66
+ """Video device path."""
67
+ fps: PositiveInt = 30
68
+ """Capture rate in frames per second."""
69
+ serial: Optional[str] = None
70
+ """Optional camera serial number for disambiguation."""
71
+
72
+
73
+ class Camera(ConfigurableBasis):
74
+ """A camera built from a single validated config."""
75
+
76
+ def __init__(self, config: CameraConfig):
77
+ self.config = config
78
+
79
+ def config_post_init(self):
80
+ super().config_post_init()
81
+ self._opened = False
82
+
83
+ def on_configure(self) -> bool:
84
+ self._opened = True
85
+ return True
86
+
87
+
88
+ # All three build the same object:
89
+ cam = Camera(CameraConfig(fps=60)) # explicit config
90
+ cam = Camera({"fps": 60}) # mapping
91
+ cam = Camera(fps=60) # flat kwargs (Hydra-style)
92
+ ```
93
+
94
+ ## With Hydra
95
+
96
+ Point `_target_` at the class and list the config fields as siblings; the metaclass turns
97
+ them into the `config` model:
98
+
99
+ ```yaml
100
+ # camera.yaml
101
+ _target_: my_pkg.Camera
102
+ fps: 60
103
+ device: /dev/video2
104
+ ```
105
+
106
+ ```python
107
+ from cfgable.hydra_utils import init_hydra_config, hydra_instance
108
+
109
+ cam = hydra_instance(init_hydra_config("camera.yaml"))
110
+ ```
111
+
112
+ For plain classes you can also nest the config under its own `_target_` so standard
113
+ `hydra.utils.instantiate` builds `Camera(config=CameraConfig(...))`.
114
+
115
+ ## Round-tripping
116
+
117
+ Because a component keeps its whole `config`, it can serialize back to the config that
118
+ would rebuild it:
119
+
120
+ ```python
121
+ cam.dump() # -> dict of fields + a "_target_" pointing at the class
122
+ cam.save_config("cam.yaml")
123
+ ```
124
+
125
+ ## What's in the box
126
+
127
+ - `ConfigurableBasis`, `InitConfigMixin`, `InitConfigABCMixin` — base classes for the
128
+ single-`config` construction protocol.
129
+ - `InitConfigMeta` / `InitConfigABCMeta` — the metaclass that assembles configs.
130
+ - `NoConfig` — placeholder for components that need no settings.
131
+ - `StrEnum`, `ReprEnum` — a string-enum backport (use this `StrEnum`, not `enum.StrEnum`,
132
+ for consistent behavior across Python 3.9–3.13).
133
+ - `ForceSetAttr` / `force_set_attr` — controlled mutation of otherwise-frozen configs.
134
+ - `import_string`, `get_fully_qualified_class_name`, `dump_or_repr`, `fetch_config`.
135
+ - `cfgable.hydra_utils` — `init_hydra_config`, `hydra_instance`,
136
+ `hydra_instance_from_dict`, `hydra_instance_from_config_path` (needs `[hydra]`).
137
+
138
+ `import cfgable` never imports Hydra — the bridge is loaded only when you import
139
+ `cfgable.hydra_utils`.
140
+
141
+ ## License
142
+
143
+ MIT.
@@ -0,0 +1,12 @@
1
+ cfgable/__init__.py,sha256=5xqkTfc6N736oSNJrEDigjM6hdKDvGbOhaNaKOb8H0U,1935
2
+ cfgable/_typing.py,sha256=tlwb39Uqx5rAuijMXHzlb_8ILnzdEr9XrrCGdTzv8dk,855
3
+ cfgable/_vendor.py,sha256=2WP0bQI8G7F96mX4AZuRvFbJqIWWA5cG9lSX075AwM8,656
4
+ cfgable/core.py,sha256=Kv89PEa35YM72CUi7hljG-MWxLV8czJH2GH-aDy-HiU,15285
5
+ cfgable/enums.py,sha256=gvbWZL2MqLRMRNeA1dbNGmTZ42QUnKhGV1kO4o7qEs0,1387
6
+ cfgable/frozen.py,sha256=OUPek9XLeTBkJ4XEhNSy-GtfIEOHnxrLt7moWcV6thA,2087
7
+ cfgable/hydra_utils.py,sha256=rOgpf7PK2L_lFjS8qLUoHeuhTMcgPsp3Zm-Oy2htpn4,2126
8
+ cfgable/reflection.py,sha256=xMmFoAAiE5zcYwq90aak7hTW-57cW6hkaSAOJUH-nX8,883
9
+ cfgable-0.1.0.dist-info/METADATA,sha256=lVsUTydh_rucrMhf2eNsx9erpEViYgnMZsQGrluhsf4,4676
10
+ cfgable-0.1.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
11
+ cfgable-0.1.0.dist-info/licenses/LICENSE,sha256=IK18Ynsk1Qfz9vu19VmMNNoce7f61SRRwrZA48PbOGc,1064
12
+ cfgable-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.27.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 OpenGHz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.