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

28 statements  

« prev     ^ index     » next       coverage.py v6.4.1, created at 2022-06-09 02:25 -0700

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, Iterable, Optional 

30 

31 

32class CollectionType(enum.IntEnum): 

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

34 

35 RUN = 1 

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

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

38 removed from. 

39 

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

41 dataset type and data ID. 

42 """ 

43 

44 TAGGED = 2 

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

46 arbitrarily. 

47 

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

49 a particular dataset type and data ID. 

50 """ 

51 

52 CHAINED = 3 

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

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

55 """ 

56 

57 CALIBRATION = 4 

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

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

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

61 

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

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

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

65 collections are expected to maintain that invariant instead. 

66 """ 

67 

68 @classmethod 

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

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

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

72 

73 @classmethod 

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

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

76 

77 Parameters 

78 ---------- 

79 name : `str` 

80 Name of the collection type. Case insensitive. 

81 

82 Returns 

83 ------- 

84 collection_type : `CollectionType` 

85 The matching collection type. 

86 

87 Raises 

88 ------ 

89 KeyError 

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

91 """ 

92 name = name.upper() 

93 try: 

94 return CollectionType.__members__[name] 

95 except KeyError: 

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

97 

98 @classmethod 

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

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

101 corresponding to the names. 

102 

103 Parameters 

104 ---------- 

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

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

107 all collection types will be returned. 

108 

109 Returns 

110 ------- 

111 types : `frozenset` of `CollectionType` 

112 The matching types. 

113 

114 Raises 

115 ------ 

116 KeyError 

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

118 """ 

119 if not names: 

120 return CollectionType.all() 

121 

122 # Convert to real collection types 

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