lsst-pex-config 30.0.0rc2__py3-none-any.whl → 30.0.0rc3__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.
lsst/pex/config/config.py CHANGED
@@ -38,6 +38,7 @@ __all__ = (
38
38
  import copy
39
39
  import importlib
40
40
  import io
41
+ import logging
41
42
  import math
42
43
  import numbers
43
44
  import os
@@ -47,9 +48,13 @@ import sys
47
48
  import tempfile
48
49
  import warnings
49
50
  from collections.abc import Mapping
51
+ from contextlib import contextmanager
52
+ from contextvars import ContextVar
50
53
  from types import GenericAlias
51
54
  from typing import Any, ForwardRef, Generic, TypeVar, cast, overload
52
55
 
56
+ from lsst.resources import ResourcePath, ResourcePathExpression
57
+
53
58
  # if YAML is not available that's fine and we simply don't register
54
59
  # the yaml representer since we know it won't be used.
55
60
  try:
@@ -74,6 +79,25 @@ else:
74
79
  YamlLoaders = ()
75
80
  doImport = None
76
81
 
82
+ _LOG = logging.getLogger(__name__)
83
+
84
+
85
+ # Tracks the current config directory for the current context.
86
+ _config_dir_stack: ContextVar[ResourcePath | None] = ContextVar("_config_dir_stack", default=None)
87
+
88
+
89
+ def _get_config_root() -> ResourcePath | None:
90
+ return _config_dir_stack.get()
91
+
92
+
93
+ @contextmanager
94
+ def _push_config_root(dirname: ResourcePath):
95
+ token = _config_dir_stack.set(dirname)
96
+ try:
97
+ yield
98
+ finally:
99
+ _config_dir_stack.reset(token)
100
+
77
101
 
78
102
  class _PexConfigGenericAlias(GenericAlias):
79
103
  """A Subclass of python's GenericAlias used in defining and instantiating
@@ -1171,15 +1195,51 @@ class Config(metaclass=ConfigMeta): # type: ignore
1171
1195
  e.add_note(f"No field of name {name} exists in config type {_typeStr(self)}")
1172
1196
  raise
1173
1197
 
1198
+ def _filename_to_resource(
1199
+ self, filename: ResourcePathExpression | None = None
1200
+ ) -> tuple[ResourcePath | None, str]:
1201
+ """Create resource path from filename.
1202
+
1203
+ Parameters
1204
+ ----------
1205
+ filename : `lsst.resources.ResourcePathExpression` or `None`
1206
+ The URI expression associated with this config. Can be `None`
1207
+ if no file URI is known.
1208
+
1209
+ Returns
1210
+ -------
1211
+ resource : `lsst.resources.ResourcePath` or `None`
1212
+ The resource version of the filename. Returns `None` if no filename
1213
+ was given or refers to unspecified value.
1214
+ file_string : `str`
1215
+ String form of the resource for use in ``__file__``
1216
+ """
1217
+ if filename is None or filename in ("?", "<unknown>"):
1218
+ return None, "<unknown>"
1219
+ base = _get_config_root()
1220
+ resource = ResourcePath(filename, forceAbsolute=True, forceDirectory=False, root=base)
1221
+
1222
+ # Preferred definition of __file__ is the full OS path. If a config
1223
+ # is loaded with a relative path it must be converted to the absolute
1224
+ # path to avoid confusion with later relative paths referenced inside
1225
+ # the config.
1226
+ if resource.scheme == "file":
1227
+ file_string = resource.ospath
1228
+ else:
1229
+ file_string = str(resource)
1230
+
1231
+ return resource, file_string
1232
+
1174
1233
  def load(self, filename, root="config"):
1175
1234
  """Modify this config in place by executing the Python code in a
1176
1235
  configuration file.
1177
1236
 
1178
1237
  Parameters
1179
1238
  ----------
1180
- filename : `str`
1181
- Name of the configuration file. A configuration file is Python
1182
- module.
1239
+ filename : `lsst.resources.ResourcePathExpression`
1240
+ Name of the configuration URI. A configuration file is a Python
1241
+ module. Since configuration files are Python code, remote URIs
1242
+ are not allowed.
1183
1243
  root : `str`, optional
1184
1244
  Name of the variable in file that refers to the config being
1185
1245
  overridden.
@@ -1199,9 +1259,21 @@ class Config(metaclass=ConfigMeta): # type: ignore
1199
1259
  lsst.pex.config.Config.saveToStream
1200
1260
  lsst.pex.config.Config.saveToString
1201
1261
  """
1202
- with open(filename) as f:
1203
- code = compile(f.read(), filename=filename, mode="exec")
1204
- self.loadFromString(code, root=root, filename=filename)
1262
+ resource, file_string = self._filename_to_resource(filename)
1263
+ if resource is None:
1264
+ # A filename is required.
1265
+ raise ValueError(f"Undefined URI provided to load command: {filename}.")
1266
+
1267
+ if resource.scheme not in ("file", "eups", "resource"):
1268
+ raise ValueError(f"Remote URI ({resource}) can not be used to load configurations.")
1269
+
1270
+ # Push the directory of the file we are now reading onto the stack
1271
+ # so that nested loads are relative to this file.
1272
+ with _push_config_root(resource.dirname()):
1273
+ _LOG.debug("Updating config from URI %s", str(resource))
1274
+ with resource.open("r") as f:
1275
+ code = compile(f.read(), filename=file_string, mode="exec")
1276
+ self._loadFromString(code, root=root, filename=file_string)
1205
1277
 
1206
1278
  def loadFromStream(self, stream, root="config", filename=None, extraLocals=None):
1207
1279
  """Modify this Config in place by executing the Python code in the
@@ -1224,7 +1296,8 @@ class Config(metaclass=ConfigMeta): # type: ignore
1224
1296
  Then this config's field ``myField`` is set to ``5``.
1225
1297
  filename : `str`, optional
1226
1298
  Name of the configuration file, or `None` if unknown or contained
1227
- in the stream. Used for error reporting.
1299
+ in the stream. Used for error reporting and to set ``__file__``
1300
+ variable in config.
1228
1301
  extraLocals : `dict` of `str` to `object`, optional
1229
1302
  Any extra variables to include in local scope when loading.
1230
1303
 
@@ -1244,7 +1317,7 @@ class Config(metaclass=ConfigMeta): # type: ignore
1244
1317
  """
1245
1318
  if hasattr(stream, "read"):
1246
1319
  if filename is None:
1247
- filename = getattr(stream, "name", "?")
1320
+ filename = getattr(stream, "name", "<unknown>")
1248
1321
  code = compile(stream.read(), filename=filename, mode="exec")
1249
1322
  else:
1250
1323
  code = stream
@@ -1268,9 +1341,11 @@ class Config(metaclass=ConfigMeta): # type: ignore
1268
1341
  config.myField = 5
1269
1342
 
1270
1343
  Then this config's field ``myField`` is set to ``5``.
1271
- filename : `str`, optional
1272
- Name of the configuration file, or `None` if unknown or contained
1273
- in the stream. Used for error reporting.
1344
+ filename : `lsst.resources.ResourcePathExpression`, optional
1345
+ URI of the configuration file, or `None` if unknown or contained
1346
+ in the stream. Used for error reporting and to set ``__file__``
1347
+ variable. Required to be set if the string config attempts to
1348
+ load other configs using either relative path or ``__file__``.
1274
1349
  extraLocals : `dict` of `str` to `object`, optional
1275
1350
  Any extra variables to include in local scope when loading.
1276
1351
 
@@ -1291,7 +1366,24 @@ class Config(metaclass=ConfigMeta): # type: ignore
1291
1366
  if filename is None:
1292
1367
  # try to determine the file name; a compiled string
1293
1368
  # has attribute "co_filename",
1294
- filename = getattr(code, "co_filename", "?")
1369
+ filename = getattr(code, "co_filename", "<unknown>")
1370
+
1371
+ resource, file_string = self._filename_to_resource(filename)
1372
+ if resource is None:
1373
+ # No idea where this config came from so no ability to deal with
1374
+ # relative paths. No reason to use context.
1375
+ self._loadFromString(code, root=root, filename=filename, extraLocals=extraLocals)
1376
+ else:
1377
+ # Push the directory of the file we are now reading onto the stack
1378
+ # so that nested loads are relative to this file.
1379
+ with _push_config_root(resource.dirname()):
1380
+ self._loadFromString(code, root=root, filename=file_string, extraLocals=extraLocals)
1381
+
1382
+ def _loadFromString(self, code, root="config", filename=None, extraLocals=None):
1383
+ """Update config from string.
1384
+
1385
+ Assumes relative directory path context has been setup by caller.
1386
+ """
1295
1387
  with RecordingImporter() as importer:
1296
1388
  globals = {"__file__": filename}
1297
1389
  local = {root: self}
@@ -192,7 +192,7 @@ class ConfigInstanceDict(collections.abc.Mapping[str, Config]):
192
192
  result._typemap = self._typemap
193
193
  if self._selection is not None:
194
194
  if self._field.multi:
195
- result._selection = SelectionSet(result._dict, self._selection._set)
195
+ result._selection = SelectionSet(self, self._selection._set)
196
196
  else:
197
197
  result._selection = self._selection
198
198
  return result
@@ -76,7 +76,7 @@ class ConfigDict(Dict[str, Config]):
76
76
  return type(self)(
77
77
  config,
78
78
  self._field,
79
- {k: v._copy() for k, v in self._dict.items()},
79
+ {k: v.copy() for k, v in self._dict.items()},
80
80
  at=None,
81
81
  label="copy",
82
82
  setHistory=False,
@@ -1,2 +1,2 @@
1
1
  __all__ = ["__version__"]
2
- __version__ = "30.0.0rc2"
2
+ __version__ = "30.0.0rc3"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: lsst-pex-config
3
- Version: 30.0.0rc2
3
+ Version: 30.0.0rc3
4
4
  Summary: A flexible configuration system using Python files.
5
5
  Author-email: Rubin Observatory Data Management <dm-admin@lists.lsst.org>
6
6
  License-Expression: BSD-3-Clause OR GPL-3.0-or-later
@@ -9,12 +9,12 @@ Keywords: lsst
9
9
  Classifier: Intended Audience :: Science/Research
10
10
  Classifier: Operating System :: OS Independent
11
11
  Classifier: Programming Language :: Python :: 3
12
- Classifier: Programming Language :: Python :: 3.10
12
+ Classifier: Programming Language :: Python :: 3.14
13
13
  Classifier: Programming Language :: Python :: 3.11
14
14
  Classifier: Programming Language :: Python :: 3.12
15
15
  Classifier: Programming Language :: Python :: 3.13
16
16
  Classifier: Topic :: Scientific/Engineering :: Astronomy
17
- Requires-Python: >=3.10.0
17
+ Requires-Python: >=3.11.0
18
18
  Description-Content-Type: text/x-rst
19
19
  License-File: COPYRIGHT
20
20
  License-File: LICENSE
@@ -22,6 +22,7 @@ License-File: gpl-v3.0.txt
22
22
  License-File: bsd_license.txt
23
23
  Requires-Dist: pyyaml>=5.1
24
24
  Requires-Dist: numpy>=1.17
25
+ Requires-Dist: lsst-resources
25
26
  Provides-Extra: test
26
27
  Requires-Dist: pytest>=3.2; extra == "test"
27
28
  Requires-Dist: pytest-openfiles>=0.5.0; extra == "test"
@@ -5,9 +5,9 @@ lsst/pex/config/_doNotImportMe.py,sha256=AYqqE8aIHH711hPcZJssplBsIIW4xCbz2oZqRNh
5
5
  lsst/pex/config/callStack.py,sha256=HxcH1QpYERsR68sBLs8jqX3Gj85e28vhCCjCy6lnVaE,5917
6
6
  lsst/pex/config/choiceField.py,sha256=S_ygw6coQaKNIstpQxKF-MMsX0agdEZtqZuEcK1EYJg,4277
7
7
  lsst/pex/config/comparison.py,sha256=2zbOdMD5aOC3ccI2NJSxtiI59wLMmssuP9oxdXBvzaE,6320
8
- lsst/pex/config/config.py,sha256=DPlN6tLVJawAaJPuxTjjDZQGYPnwWOv1cGq5myFoNQU,63028
9
- lsst/pex/config/configChoiceField.py,sha256=CbISBYgMuKEJyed2G6f8XEodL-98O3L2Haxc8Dx_s1I,24439
10
- lsst/pex/config/configDictField.py,sha256=gnVi52CHB3B0K-NNC3NUZDxi556pFJnVSF4-73JlJw4,12893
8
+ lsst/pex/config/config.py,sha256=bGlaQ9F5dtmXXbnlyzvUisT7UXn7SYBaZ4GS14LRANI,66935
9
+ lsst/pex/config/configChoiceField.py,sha256=gWBODt4Vbw7IcMTNLBBizWdXBXyJSCN0SRl8c2zafL4,24431
10
+ lsst/pex/config/configDictField.py,sha256=_OjAj8DSwyF8HPoIRjCvyw3Bt1IcP0GACdl-SHggR1M,12892
11
11
  lsst/pex/config/configField.py,sha256=9jSPSHO_9p-499SmJgvMWBbizAl4p1jTQrtlib1gXHo,11655
12
12
  lsst/pex/config/configurableField.py,sha256=-nYA54G6UJLVzB_lQgPRiCMQJ_yv9c-43uxyCJkH198,18995
13
13
  lsst/pex/config/convert.py,sha256=dBTLjkcoh3lM6FxtY9DV33iLOtD6V0PMni6O2b9K4Ww,2397
@@ -17,19 +17,19 @@ lsst/pex/config/listField.py,sha256=H0j1TR3jTxUM80vcBWmoNlCCjd1HFSP15UxiR56gBxY,
17
17
  lsst/pex/config/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
18
18
  lsst/pex/config/rangeField.py,sha256=VmfcAoLYRJIdIsnFQIFlVYKrmNnfGX6Ef9enEYkFTx8,5429
19
19
  lsst/pex/config/registry.py,sha256=JKy6kXOumSYDoOdHlg4oBGeQN7pmMSOgCJCXylEI6yM,15790
20
- lsst/pex/config/version.py,sha256=CrAQNQjb1NNXqaNvScI53U2GvkmrnpRtyOiy0bIrcl4,52
20
+ lsst/pex/config/version.py,sha256=Tt7y1Knxuicvk026_H8m0GXmxEFM8XEWyfbDoHcAxbs,52
21
21
  lsst/pex/config/wrap.py,sha256=t8s5KzN2kJ-oW8AJTZrz4xAl0Qtpvnqo9H6rQKRBK_s,12964
22
22
  lsst/pex/config/configurableActions/__init__.py,sha256=kwMbhFr3LirwEqzgHij7CxWF_xki3UYeajzveb4vpTI,1039
23
23
  lsst/pex/config/configurableActions/_configurableAction.py,sha256=KZiYYzIQHNeUhDxS1DMb_D9X28_LOzLyNXclaOW-ApY,2784
24
24
  lsst/pex/config/configurableActions/_configurableActionField.py,sha256=E1uVrGIvu64QDJL76sW3LlIrR30-NJs0ZQeofy-iVEs,5353
25
25
  lsst/pex/config/configurableActions/_configurableActionStructField.py,sha256=HL53MsLh1DaKmZBD3midrHcB8tbJ46lNBwerjne0qrg,17754
26
26
  lsst/pex/config/configurableActions/tests.py,sha256=aisHgzghfR4NFQD22NU2K5pNilCjAwcJTSMs0vK7dzI,3039
27
- lsst_pex_config-30.0.0rc2.dist-info/licenses/COPYRIGHT,sha256=J3bcuh9PTaTU9iNjxE-9THWEwoo5drYshgYvZVgUKoI,381
28
- lsst_pex_config-30.0.0rc2.dist-info/licenses/LICENSE,sha256=pRExkS03v0MQW-neNfIcaSL6aiAnoLxYgtZoFzQ6zkM,232
29
- lsst_pex_config-30.0.0rc2.dist-info/licenses/bsd_license.txt,sha256=7MIcv8QRX9guUtqPSBDMPz2SnZ5swI-xZMqm_VDSfxY,1606
30
- lsst_pex_config-30.0.0rc2.dist-info/licenses/gpl-v3.0.txt,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
31
- lsst_pex_config-30.0.0rc2.dist-info/METADATA,sha256=mJscy7ZVKy3me7ddU2jrehkQdhZWvt1gQ4J0jM2wYho,2206
32
- lsst_pex_config-30.0.0rc2.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
33
- lsst_pex_config-30.0.0rc2.dist-info/top_level.txt,sha256=eUWiOuVVm9wwTrnAgiJT6tp6HQHXxIhj2QSZ7NYZH80,5
34
- lsst_pex_config-30.0.0rc2.dist-info/zip-safe,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
35
- lsst_pex_config-30.0.0rc2.dist-info/RECORD,,
27
+ lsst_pex_config-30.0.0rc3.dist-info/licenses/COPYRIGHT,sha256=J3bcuh9PTaTU9iNjxE-9THWEwoo5drYshgYvZVgUKoI,381
28
+ lsst_pex_config-30.0.0rc3.dist-info/licenses/LICENSE,sha256=pRExkS03v0MQW-neNfIcaSL6aiAnoLxYgtZoFzQ6zkM,232
29
+ lsst_pex_config-30.0.0rc3.dist-info/licenses/bsd_license.txt,sha256=7MIcv8QRX9guUtqPSBDMPz2SnZ5swI-xZMqm_VDSfxY,1606
30
+ lsst_pex_config-30.0.0rc3.dist-info/licenses/gpl-v3.0.txt,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
31
+ lsst_pex_config-30.0.0rc3.dist-info/METADATA,sha256=FWwCQ97l9cvOytpuduZ_8A6GwnzyQwRX_YXFCSPS72I,2236
32
+ lsst_pex_config-30.0.0rc3.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
33
+ lsst_pex_config-30.0.0rc3.dist-info/top_level.txt,sha256=eUWiOuVVm9wwTrnAgiJT6tp6HQHXxIhj2QSZ7NYZH80,5
34
+ lsst_pex_config-30.0.0rc3.dist-info/zip-safe,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
35
+ lsst_pex_config-30.0.0rc3.dist-info/RECORD,,