Coverage for python / lsst / daf / butler / remote_butler / _ref_utils.py: 0%

31 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-26 08:48 +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/>. 

27 

28from __future__ import annotations 

29 

30__all__ = ( 

31 "apply_storage_class_override", 

32 "normalize_dataset_type_name", 

33 "simplify_dataId", 

34) 

35 

36from pydantic import TypeAdapter 

37 

38from .._dataset_ref import DatasetRef 

39from .._dataset_type import DatasetType, get_dataset_type_name 

40from .._storage_class import StorageClass 

41from ..dimensions import DataCoordinate, DataId, DataIdValue, SerializedDataId 

42from .server_models import DatasetTypeName 

43 

44_SERIALIZED_DATA_ID_TYPE_ADAPTER = TypeAdapter(SerializedDataId) 

45 

46 

47def apply_storage_class_override( 

48 ref: DatasetRef, 

49 original_dataset_ref_or_type: DatasetRef | DatasetType | str, 

50 explicit_storage_class: StorageClass | str | None, 

51) -> DatasetRef: 

52 """Return a DatasetRef with its storage class overridden to match the 

53 StorageClass supplied by the user as input to one of the search functions. 

54 

55 Parameters 

56 ---------- 

57 ref : `DatasetRef` 

58 The ref to which we will apply the StorageClass override. 

59 original_dataset_ref_or_type : `DatasetRef` | `DatasetType` | `str` 

60 The ref or type that was input to the search, which may contain a 

61 storage class override. 

62 explicit_storage_class : `StorageClass` | `str` | `None` 

63 A storage class that the user explicitly requested as an override. 

64 """ 

65 if explicit_storage_class is not None: 

66 return ref.overrideStorageClass(explicit_storage_class) 

67 

68 # If the caller provided a DatasetRef or DatasetType, they may have 

69 # overridden the storage class on it, and we need to propagate that to the 

70 # output. 

71 dataset_type = _extract_dataset_type(original_dataset_ref_or_type) 

72 if dataset_type is not None: 

73 return ref.overrideStorageClass(dataset_type.storageClass) 

74 

75 return ref 

76 

77 

78def normalize_dataset_type_name(datasetTypeOrName: DatasetType | str) -> DatasetTypeName: 

79 """Convert DatasetType parameters in the format used by Butler methods 

80 to a standardized string name for the REST API. 

81 

82 Parameters 

83 ---------- 

84 datasetTypeOrName : `DatasetType` | `str` 

85 A DatasetType, or the name of a DatasetType. This union is a common 

86 parameter in many `Butler` methods. 

87 """ 

88 return DatasetTypeName(get_dataset_type_name(datasetTypeOrName)) 

89 

90 

91def simplify_dataId(dataId: DataId | None, kwargs: dict[str, DataIdValue]) -> SerializedDataId: 

92 """Take a generic Data ID and convert it to a serializable form. 

93 

94 Parameters 

95 ---------- 

96 dataId : `dict`, `None`, `DataCoordinate` 

97 The data ID to serialize. 

98 kwargs : `dict` 

99 Additional entries to augment or replace the values in ``dataId``. 

100 

101 Returns 

102 ------- 

103 data_id : `SerializedDataId` 

104 A serializable form. 

105 """ 

106 if dataId is None: 

107 dataId = {} 

108 elif isinstance(dataId, DataCoordinate): 

109 dataId = dataId.to_simple(minimal=True).dataId 

110 else: 

111 dataId = dict(dataId) 

112 

113 return _SERIALIZED_DATA_ID_TYPE_ADAPTER.validate_python(dataId | kwargs) 

114 

115 

116def _extract_dataset_type(datasetRefOrType: DatasetRef | DatasetType | str) -> DatasetType | None: 

117 """Return the DatasetType associated with the argument, or None if the 

118 argument is not an object that contains a DatasetType object. 

119 

120 Parameters 

121 ---------- 

122 datasetRefOrType : `DatasetRef` | `DatasetType` | `str` 

123 A DatasetRef, DatasetType, or name of a DatasetType. This union is a 

124 common parameter in many `Butler` methods. 

125 """ 

126 if isinstance(datasetRefOrType, DatasetType): 

127 return datasetRefOrType 

128 elif isinstance(datasetRefOrType, DatasetRef): 

129 return datasetRefOrType.datasetType 

130 else: 

131 return None