Coverage for python/lsst/daf/butler/registry/wildcards.py: 21%
207 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-02-22 03:05 -0800
« prev ^ index » next coverage.py v6.5.0, created at 2023-02-22 03:05 -0800
1# This file is part of daf_butler.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
21from __future__ import annotations
23__all__ = (
24 "CategorizedWildcard",
25 "CollectionWildcard",
26 "CollectionSearch",
27 "DatasetTypeWildcard",
28)
30import dataclasses
31import re
32from collections.abc import Callable, Iterable, Iterator, Mapping, Sequence
33from typing import Any
35from deprecated.sphinx import deprecated
36from lsst.utils.ellipsis import Ellipsis, EllipsisType
37from lsst.utils.iteration import ensure_iterable
38from pydantic import BaseModel
40from ..core import DatasetType
41from ..core.utils import globToRegex
42from ._exceptions import CollectionExpressionError, DatasetTypeExpressionError
45@dataclasses.dataclass
46class CategorizedWildcard:
47 """The results of preprocessing a wildcard expression to separate match
48 patterns from strings.
50 The `fromExpression` method should almost always be used to construct
51 instances, as the regular constructor performs no checking of inputs (and
52 that can lead to confusing error messages downstream).
53 """
55 @classmethod
56 def fromExpression(
57 cls,
58 expression: Any,
59 *,
60 allowAny: bool = True,
61 allowPatterns: bool = True,
62 coerceUnrecognized: Callable[[Any], tuple[str, Any] | str] | None = None,
63 coerceItemValue: Callable[[Any], Any] | None = None,
64 defaultItemValue: Any | None = None,
65 ) -> CategorizedWildcard | EllipsisType:
66 """Categorize a wildcard expression.
68 Parameters
69 ----------
70 expression
71 The expression to categorize. May be any of:
72 - `str` (including glob patterns if ``allowPatterns`` is `True`);
73 - `re.Pattern` (only if ``allowPatterns`` is `True`);
74 - objects recognized by ``coerceUnrecognized`` (if provided);
75 - two-element tuples of (`str`, value) where value is recognized
76 by ``coerceItemValue`` (if provided);
77 - a non-`str`, non-mapping iterable containing any of the above;
78 - the special value `...` (only if ``allowAny`` is `True`), which
79 matches anything;
80 - a mapping from `str` to a value are recognized by
81 ``coerceItemValue`` (if provided);
82 - a `CategorizedWildcard` instance (passed through unchanged if
83 it meets the requirements specified by keyword arguments).
84 allowAny: `bool`, optional
85 If `False` (`True` is default) raise `TypeError` if `...` is
86 encountered.
87 allowPatterns: `bool`, optional
88 If `False` (`True` is default) raise `TypeError` if a `re.Pattern`
89 is encountered, or if ``expression`` is a `CategorizedWildcard`
90 with `patterns` not empty.
91 coerceUnrecognized: `Callable`, optional
92 A callback that takes a single argument of arbitrary type and
93 returns either a `str` - appended to `strings` - or a `tuple` of
94 (`str`, `Any`) to be appended to `items`. This will be called on
95 objects of unrecognized type. Exceptions will be reraised as
96 `TypeError` (and chained).
97 coerceItemValue: `Callable`, optional
98 If provided, ``expression`` may be a mapping from `str` to any
99 type that can be passed to this function; the result of that call
100 will be stored instead as the value in ``self.items``.
101 defaultItemValue: `Any`, optional
102 If provided, combine this value with any string values encountered
103 (including any returned by ``coerceUnrecognized``) to form a
104 `tuple` and add it to `items`, guaranteeing that `strings` will be
105 empty. Patterns are never added to `items`.
107 Returns
108 -------
109 categorized : `CategorizedWildcard` or ``...``.
110 The struct describing the wildcard. ``...`` is passed through
111 unchanged.
113 Raises
114 ------
115 TypeError
116 Raised if an unsupported type is found in the expression.
117 """
118 assert expression is not None
119 # See if we were given ...; just return that if we were.
120 if expression is Ellipsis:
121 if not allowAny:
122 raise TypeError("This expression may not be unconstrained.")
123 return Ellipsis
124 if isinstance(expression, cls):
125 # This is already a CategorizedWildcard. Make sure it meets the
126 # reqs. implied by the kwargs we got.
127 if not allowPatterns and expression.patterns:
128 raise TypeError(
129 f"Regular expression(s) {expression.patterns} are not allowed in this context."
130 )
131 if defaultItemValue is not None and expression.strings:
132 if expression.items:
133 raise TypeError(
134 "Incompatible preprocessed expression: an ordered sequence of str is "
135 "needed, but the original order was lost in the preprocessing."
136 )
137 return cls(
138 strings=[],
139 patterns=expression.patterns,
140 items=[(k, defaultItemValue) for k in expression.strings],
141 )
142 elif defaultItemValue is None and expression.items:
143 if expression.strings:
144 raise TypeError(
145 "Incompatible preprocessed expression: an ordered sequence of items is "
146 "needed, but the original order was lost in the preprocessing."
147 )
148 return cls(strings=[k for k, _ in expression.items], patterns=expression.patterns, items=[])
149 else:
150 # Original expression was created with keyword arguments that
151 # were at least as restrictive as what we just got; pass it
152 # through.
153 return expression
155 # If we get here, we know we'll be creating a new instance.
156 # Initialize an empty one now.
157 self = cls(strings=[], patterns=[], items=[])
159 # If mappings are allowed, see if we were given a single mapping by
160 # trying to get items.
161 if coerceItemValue is not None:
162 rawItems = None
163 try:
164 rawItems = expression.items()
165 except AttributeError:
166 pass
167 if rawItems is not None:
168 for k, v in rawItems:
169 try:
170 self.items.append((k, coerceItemValue(v)))
171 except Exception as err:
172 raise TypeError(f"Could not coerce mapping value '{v}' for key '{k}'.") from err
173 return self
175 # Not ..., a CategorizedWildcard instance, or a mapping. Just
176 # process scalars or an iterable. We put the body of the loop inside
177 # a local function so we can recurse after coercion.
179 def process(element: Any, alreadyCoerced: bool = False) -> EllipsisType | None:
180 if isinstance(element, str):
181 if defaultItemValue is not None:
182 self.items.append((element, defaultItemValue))
183 return None
184 else:
185 # This returns a list but we know we only passed in
186 # single value.
187 converted = globToRegex(element)
188 if converted is Ellipsis:
189 return Ellipsis
190 element = converted[0]
191 # Let regex and ... go through to the next check
192 if isinstance(element, str):
193 self.strings.append(element)
194 return None
195 if allowPatterns and isinstance(element, re.Pattern):
196 self.patterns.append(element)
197 return None
198 if alreadyCoerced:
199 try:
200 k, v = element
201 except TypeError:
202 raise TypeError(
203 f"Object '{element!r}' returned by coercion function must be `str` or `tuple`."
204 ) from None
205 else:
206 self.items.append((k, v))
207 return None
208 if coerceItemValue is not None:
209 try:
210 k, v = element
211 except TypeError:
212 pass
213 else:
214 if not isinstance(k, str):
215 raise TypeError(f"Item key '{k}' is not a string.")
216 try:
217 v = coerceItemValue(v)
218 except Exception as err:
219 raise TypeError(f"Could not coerce tuple item value '{v}' for key '{k}'.") from err
220 self.items.append((k, v))
221 return None
222 if coerceUnrecognized is not None:
223 try:
224 # This should be safe but flake8 cant tell that the
225 # function will be re-declared next function call
226 process(coerceUnrecognized(element), alreadyCoerced=True) # noqa: F821
227 except Exception as err:
228 raise TypeError(f"Could not coerce expression element '{element!r}'.") from err
229 else:
230 extra = "."
231 if isinstance(element, re.Pattern):
232 extra = " and patterns are not allowed."
233 raise TypeError(f"Unsupported object in wildcard expression: '{element!r}'{extra}")
234 return None
236 for element in ensure_iterable(expression):
237 retval = process(element)
238 if retval is Ellipsis:
239 # One of the globs matched everything
240 if not allowAny:
241 raise TypeError("This expression may not be unconstrained.")
242 return Ellipsis
243 del process
244 return self
246 strings: list[str]
247 """Explicit string values found in the wildcard (`list` [ `str` ]).
248 """
250 patterns: list[re.Pattern]
251 """Regular expression patterns found in the wildcard
252 (`list` [ `re.Pattern` ]).
253 """
255 items: list[tuple[str, Any]]
256 """Two-item tuples that relate string values to other objects
257 (`list` [ `tuple` [ `str`, `Any` ] ]).
258 """
261@deprecated(
262 reason="Tuples of string collection names are now preferred. Will be removed after v26.",
263 version="v25.0",
264 category=FutureWarning,
265)
266class CollectionSearch(BaseModel, Sequence[str]):
267 """An ordered search path of collections.
269 The `fromExpression` method should almost always be used to construct
270 instances, as the regular constructor performs no checking of inputs (and
271 that can lead to confusing error messages downstream).
273 Parameters
274 ----------
275 collections : `tuple` [ `str` ]
276 Tuple of collection names, ordered from the first searched to the last
277 searched.
279 Notes
280 -----
281 A `CollectionSearch` is used to find a single dataset (or set of datasets
282 with different dataset types or data IDs) according to its dataset type and
283 data ID, giving preference to collections in the order in which they are
284 specified. A `CollectionWildcard` can be constructed from a broader range
285 of expressions but does not order the collections to be searched.
287 `CollectionSearch` is an immutable sequence of `str` collection names.
289 A `CollectionSearch` instance constructed properly (e.g. via
290 `fromExpression`) is a unique representation of a particular search path;
291 it is exactly the same internally and compares as equal to any
292 `CollectionSearch` constructed from an equivalent expression, regardless of
293 how different the original expressions appear.
294 """
296 __root__: tuple[str, ...]
298 @classmethod
299 def fromExpression(cls, expression: Any) -> CollectionSearch:
300 """Process a general expression to construct a `CollectionSearch`
301 instance.
303 Parameters
304 ----------
305 expression
306 May be:
307 - a `str` collection name;
308 - an iterable of `str` collection names;
309 - another `CollectionSearch` instance (passed through
310 unchanged).
312 Duplicate entries will be removed (preserving the first appearance
313 of each collection name).
314 Returns
315 -------
316 collections : `CollectionSearch`
317 A `CollectionSearch` instance.
318 """
319 # First see if this is already a CollectionSearch; just pass that
320 # through unchanged. This lets us standardize expressions (and turn
321 # single-pass iterators into multi-pass iterables) in advance and pass
322 # them down to other routines that accept arbitrary expressions.
323 if isinstance(expression, cls):
324 return expression
325 try:
326 wildcard = CategorizedWildcard.fromExpression(
327 expression,
328 allowAny=False,
329 allowPatterns=False,
330 )
331 except TypeError as err:
332 raise CollectionExpressionError(str(err)) from None
333 assert wildcard is not Ellipsis
334 assert not wildcard.patterns
335 assert not wildcard.items
336 deduplicated = []
337 for name in wildcard.strings:
338 if name not in deduplicated:
339 deduplicated.append(name)
340 return cls(__root__=tuple(deduplicated))
342 def explicitNames(self) -> Iterator[str]:
343 """Iterate over collection names that were specified explicitly."""
344 yield from self.__root__
346 def __iter__(self) -> Iterator[str]: # type: ignore
347 yield from self.__root__
349 def __len__(self) -> int:
350 return len(self.__root__)
352 def __getitem__(self, index: Any) -> str:
353 return self.__root__[index]
355 def __eq__(self, other: Any) -> bool:
356 if isinstance(other, CollectionSearch):
357 return self.__root__ == other.__root__
358 return False
360 def __str__(self) -> str:
361 return "[{}]".format(", ".join(self))
363 def __repr__(self) -> str:
364 return f"CollectionSearch({self.__root__!r})"
367@dataclasses.dataclass(frozen=True)
368class CollectionWildcard:
369 """A validated wildcard for collection names
371 The `from_expression` method should almost always be used to construct
372 instances, as the regular constructor performs no checking of inputs (and
373 that can lead to confusing error messages downstream).
375 Notes
376 -----
377 `CollectionWildcard` is expected to be rarely used outside of `Registry`
378 (which uses it to back several of its "query" methods that take general
379 expressions for collections), but it may occasionally be useful outside
380 `Registry` as a way to preprocess expressions that contain single-pass
381 iterators into a form that can be used to call those `Registry` methods
382 multiple times.
383 """
385 strings: tuple[str, ...] = ()
386 """An an ordered list of explicitly-named collections. (`tuple` [ `str` ]).
387 """
389 patterns: tuple[re.Pattern, ...] | EllipsisType = Ellipsis
390 """Regular expression patterns to match against collection names, or the
391 special value ``...`` indicating all collections.
393 `...` must be accompanied by ``strings=()``.
394 """
396 def __post_init__(self) -> None:
397 if self.patterns is Ellipsis and self.strings:
398 raise ValueError(
399 f"Collection wildcard matches any string, but still has explicit strings {self.strings}."
400 )
402 @classmethod
403 def from_expression(cls, expression: Any, require_ordered: bool = False) -> CollectionWildcard:
404 """Process a general expression to construct a `CollectionWildcard`
405 instance.
407 Parameters
408 ----------
409 expression
410 May be:
411 - a `str` collection name;
412 - an `re.Pattern` instance to match (with `re.Pattern.fullmatch`)
413 against collection names;
414 - any iterable containing any of the above;
415 - another `CollectionWildcard` instance (passed through
416 unchanged).
418 Duplicate collection names will be removed (preserving the first
419 appearance of each collection name).
420 require_ordered : `bool`, optional
421 If `True` (`False` is default) require the expression to be
422 ordered, and raise `CollectionExpressionError` if it is not.
424 Returns
425 -------
426 wildcard : `CollectionWildcard`
427 A `CollectionWildcard` instance.
429 Raises
430 ------
431 CollectionExpressionError
432 Raised if the patterns has regular expression, glob patterns, or
433 the ``...`` wildcard, and ``require_ordered=True``.
434 """
435 if isinstance(expression, cls):
436 return expression
437 if expression is Ellipsis:
438 return cls()
439 wildcard = CategorizedWildcard.fromExpression(
440 expression,
441 allowAny=True,
442 allowPatterns=True,
443 )
444 if wildcard is Ellipsis:
445 return cls()
446 result = cls(
447 strings=tuple(wildcard.strings),
448 patterns=tuple(wildcard.patterns),
449 )
450 if require_ordered:
451 result.require_ordered()
452 return result
454 @classmethod
455 def from_names(cls, names: Iterable[str]) -> CollectionWildcard:
456 """Construct from an iterable of explicit collection names.
458 Parameters
459 ----------
460 names : `Iterable` [ `str` ]
461 Iterable of collection names.
463 Returns
464 -------
465 wildcard : ~CollectionWildcard`
466 A `CollectionWildcard` instance. `require_ordered` is guaranteed
467 to succeed and return the given names in order.
468 """
469 return cls(strings=tuple(names), patterns=())
471 def require_ordered(self) -> tuple[str, ...]:
472 """Require that this wildcard contains no patterns, and return the
473 ordered tuple of names that it does hold.
475 Returns
476 -------
477 names : `tuple` [ `str` ]
478 Ordered tuple of collection names.
480 Raises
481 ------
482 CollectionExpressionError
483 Raised if the patterns has regular expression, glob patterns, or
484 the ``...`` wildcard.
485 """
486 if self.patterns:
487 raise CollectionExpressionError(
488 f"An ordered collection expression is required; got patterns {self.patterns}."
489 )
490 return self.strings
492 def __str__(self) -> str:
493 if self.patterns is Ellipsis:
494 return "..."
495 else:
496 terms = list(self.strings)
497 terms.extend(str(p) for p in self.patterns)
498 return "[{}]".format(", ".join(terms))
501@dataclasses.dataclass
502class DatasetTypeWildcard:
503 """A validated expression that resolves to one or more dataset types.
505 The `from_expression` method should almost always be used to construct
506 instances, as the regular constructor performs no checking of inputs (and
507 that can lead to confusing error messages downstream).
508 """
510 values: Mapping[str, DatasetType | None] = dataclasses.field(default_factory=dict)
511 """A mapping with `str` dataset type name keys and optional `DatasetType`
512 instances.
513 """
515 patterns: tuple[re.Pattern, ...] | EllipsisType = Ellipsis
516 """Regular expressions to be matched against dataset type names, or the
517 special value ``...`` indicating all dataset types.
519 Any pattern matching a dataset type is considered an overall match for
520 the expression.
521 """
523 @classmethod
524 def from_expression(cls, expression: Any) -> DatasetTypeWildcard:
525 """Construct an instance by analyzing the given expression.
527 Parameters
528 ----------
529 expression
530 Expression to analyze. May be any of the following:
532 - a `str` dataset type name;
533 - a `DatasetType` instance;
534 - a `re.Pattern` to match against dataset type names;
535 - an iterable whose elements may be any of the above (any dataset
536 type matching any element in the list is an overall match);
537 - an existing `DatasetTypeWildcard` instance;
538 - the special ``...`` ellipsis object, which matches any dataset
539 type.
541 Returns
542 -------
543 query : `DatasetTypeWildcard`
544 An instance of this class (new unless an existing instance was
545 passed in).
547 Raises
548 ------
549 DatasetTypeExpressionError
550 Raised if the given expression does not have one of the allowed
551 types.
552 """
553 if isinstance(expression, cls):
554 return expression
555 try:
556 wildcard = CategorizedWildcard.fromExpression(
557 expression, coerceUnrecognized=lambda d: (d.name, d)
558 )
559 except TypeError as err:
560 raise DatasetTypeExpressionError(f"Invalid dataset type expression: {expression!r}.") from err
561 if wildcard is Ellipsis:
562 return cls()
563 values: dict[str, DatasetType | None] = {}
564 for name in wildcard.strings:
565 values[name] = None
566 for name, item in wildcard.items:
567 if not isinstance(item, DatasetType):
568 raise DatasetTypeExpressionError(
569 f"Invalid value '{item}' of type {type(item)} in dataset type expression; "
570 "expected str, re.Pattern, DatasetType objects, iterables thereof, or '...'."
571 )
572 values[name] = item
573 return cls(values, patterns=tuple(wildcard.patterns))
575 def __str__(self) -> str:
576 if self.patterns is Ellipsis:
577 return "..."
578 else:
579 terms = list(self.values.keys())
580 terms.extend(str(p) for p in self.patterns)
581 return "[{}]".format(", ".join(terms))