Coverage for python / lsst / daf / butler / remote_butler / _collection_args.py: 0%
41 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-18 08:42 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-18 08:42 +0000
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 software is dual licensed under the GNU General Public License and also
10# under a 3-clause BSD license. Recipients may choose which of these licenses
11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
12# respectively. If you choose the GPL option then the following text applies
13# (but note that there is still no warranty even if you opt for BSD instead):
14#
15# This program is free software: you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation, either version 3 of the License, or
18# (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program. If not, see <http://www.gnu.org/licenses/>.
28from __future__ import annotations
30import re
31from typing import NamedTuple, cast
33from lsst.utils.introspection import get_full_type_name
34from lsst.utils.iteration import ensure_iterable
36from .._dataset_type import DatasetType
37from ..registry import CollectionArgType
38from ..registry.wildcards import CollectionWildcard
39from .server_models import CollectionList
42def convert_collection_arg_to_glob_string_list(arg: CollectionArgType) -> CollectionList:
43 """Convert the collections argument used by many Butler/registry methods to
44 a format suitable for sending to Butler server.
46 Parameters
47 ----------
48 arg : `CollectionArgType`
49 Collection search pattern in many possible formats.
51 Returns
52 -------
53 glob_list : `CollectionList`
54 Collection search patterns normalized to a list of globs string.
56 Raises
57 ------
58 TypeError
59 If the search pattern provided by the user cannot be converted to a
60 glob string.
61 """
62 if arg is ...:
63 return CollectionList(["*"])
64 elif isinstance(arg, str):
65 return CollectionList([arg])
66 elif isinstance(arg, re.Pattern):
67 raise TypeError("RemoteButler does not support regular expressions as search patterns")
68 elif isinstance(arg, CollectionWildcard):
69 # In theory this could work, but CollectionWildcard eagerly converts
70 # any glob strings to regexes. So it would need some rework to
71 # preserve the original string inputs. CollectionWildcard has likely
72 # never been used in the wild by an end-user so this probably isn't
73 # worth figuring out.
74 raise TypeError("RemoteButler does not accept CollectionWildcard instances as search patterns")
75 else:
76 search = list(arg)
77 for item in search:
78 if not isinstance(item, str):
79 raise TypeError("RemoteButler only accepts strings and lists of strings as search patterns")
80 return CollectionList(cast(list[str], search))
83class DatasetTypeSearch(NamedTuple):
84 """Information needed to send a dataset type search expression to the
85 server.
86 """
88 search: list[str]
89 """List of glob strings to search, suitable for sending to Butler
90 server.
91 """
92 explicit_dataset_types: dict[str, DatasetType]
93 """Mapping from name to `DatasetType` instance for any items the user
94 provided as already-inflated DatasetType instances.
95 """
98def convert_dataset_type_arg_to_glob_string_list(arg: object) -> DatasetTypeSearch:
99 """Convert the dataset type search expression argument used by some
100 registry methods to a format suitable for sending to Butler server.
102 Parameters
103 ----------
104 arg : `typing.Any`
105 Dataset type search pattern in many possible formats.
107 Returns
108 -------
109 search_info : `DatasetTypeSearch`
110 Information needed to execute a dataset type search.
112 Raises
113 ------
114 TypeError
115 If a search pattern provided by the user cannot be converted to a
116 glob string.
117 """
118 if arg is ...:
119 return DatasetTypeSearch(search=["*"], explicit_dataset_types={})
121 search: list[str] = []
122 explicit_dataset_types: dict[str, DatasetType] = {}
123 for item in ensure_iterable(arg):
124 if isinstance(item, DatasetType):
125 search.append(item.name)
126 explicit_dataset_types[item.name] = item
127 elif isinstance(item, str):
128 search.append(item)
129 else:
130 raise TypeError(f"Search patterns must be string or DatasetType, not {get_full_type_name(item)}")
132 return DatasetTypeSearch(search=search, explicit_dataset_types=explicit_dataset_types)