Coverage for python/lsst/daf/butler/registry/_collectionType.py: 57%

Shortcuts on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

24 statements  

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/>. 

21 

22from __future__ import annotations 

23 

24__all__ = [ 

25 "CollectionType", 

26] 

27 

28import enum 

29from typing import FrozenSet, Optional, Iterable 

30 

31 

32class CollectionType(enum.IntEnum): 

33 """Enumeration used to label different types of collections. 

34 """ 

35 

36 RUN = 1 

37 """A ``RUN`` collection (also just called a 'run') is the initial 

38 collection a dataset is inserted into and the only one it can never be 

39 removed from. 

40 

41 Within a particular run, there may only be one dataset with a particular 

42 dataset type and data ID. 

43 """ 

44 

45 TAGGED = 2 

46 """Datasets can be associated with and removed from ``TAGGED`` collections 

47 arbitrarily. 

48 

49 Within a particular tagged collection, there may only be one dataset with 

50 a particular dataset type and data ID. 

51 """ 

52 

53 CHAINED = 3 

54 """A ``CHAINED`` collection is simply an ordered list of other collections 

55 to be searched. These may include other ``CHAINED`` collections. 

56 """ 

57 

58 CALIBRATION = 4 

59 """A ``CALIBRATION`` collection operates like a ``TAGGED`` collection, but 

60 also associates each dataset with a validity range as well. Queries 

61 against calibration collections must include a timestamp as an input. 

62 

63 It is difficult (perhaps impossible) to enforce a constraint that there be 

64 one dataset with a particular dataset type and data ID at any particular 

65 timestamp in the database, so higher-level tools that populate calibration 

66 collections are expected to maintain that invariant instead. 

67 """ 

68 

69 @classmethod 

70 def all(cls) -> FrozenSet[CollectionType]: 

71 """Return a `frozenset` containing all members. 

72 """ 

73 return frozenset(cls.__members__.values()) 

74 

75 @classmethod 

76 def from_name(cls, name: str) -> CollectionType: 

77 """Return the `CollectionType` given its name. 

78 

79 Parameters 

80 ---------- 

81 name : `str` 

82 Name of the collection type. Case insensitive. 

83 

84 Returns 

85 ------- 

86 collection_type : `CollectionType` 

87 The matching collection type. 

88 

89 Raises 

90 ------ 

91 KeyError 

92 Raised if the name does not match a collection type. 

93 """ 

94 name = name.upper() 

95 try: 

96 return CollectionType.__members__[name] 

97 except KeyError: 

98 raise KeyError(f"Collection type of '{name}' not known to Butler.") from None 

99 

100 @classmethod 

101 def from_names(cls, names: Optional[Iterable[str]]) -> FrozenSet[CollectionType]: 

102 """Return a `frozenset` containing the `CollectionType` instances 

103 corresponding to the names. 

104 

105 Parameters 

106 ---------- 

107 names : iterable of `str`, or `None` 

108 Names of collection types. Case insensitive. If `None` or empty, 

109 all collection types will be returned. 

110 

111 Returns 

112 ------- 

113 types : `frozenset` of `CollectionType` 

114 The matching types. 

115 

116 Raises 

117 ------ 

118 KeyError 

119 Raised if the name does not correspond to a collection type. 

120 """ 

121 if not names: 

122 return CollectionType.all() 

123 

124 # Convert to real collection types 

125 return frozenset({CollectionType.from_name(name) for name in names})