Coverage for python/lsst/daf/butler/script/collectionChain.py: 7%

44 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 02:10 -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 

24from collections.abc import Iterable 

25 

26from .._butler import Butler 

27from ..registry import CollectionType, MissingCollectionError 

28 

29 

30def collectionChain( 

31 repo: str, mode: str, parent: str, children: Iterable[str], doc: str | None, flatten: bool 

32) -> tuple[str, ...]: 

33 """Get the collections whose names match an expression. 

34 

35 Parameters 

36 ---------- 

37 repo : `str` 

38 URI to the location of the repo or URI to a config file describing the 

39 repo and its location. 

40 mode : `str` 

41 Update mode for this chain. Options are: 

42 'redefine': Create or modify ``parent`` to be defined by the supplied 

43 ``children``. 

44 'remove': Modify existing chain to remove ``children`` from it. 

45 'prepend': Add the given ``children`` to the beginning of the chain. 

46 'extend': Modify existing chain to add ``children`` to the end of it. 

47 'pop': Pop a numbered element off the chain. Defaults to popping 

48 the first element (0). ``children`` must be integers if given. 

49 Both 'prepend' and 'extend' are the same as 'redefine' if the chain 

50 does not exist. 

51 parent: `str` 

52 Name of the chained collection to update. Will be created if it 

53 does not exist already. 

54 children: iterable of `str` 

55 Names of the children to be included in the chain. 

56 doc : `str` 

57 If the chained collection is being created, the documentation string 

58 that will be associated with it. 

59 flatten : `bool` 

60 If `True`, recursively flatten out any nested 

61 `~CollectionType.CHAINED` collections in ``children`` first. 

62 

63 Returns 

64 ------- 

65 chain : `tuple` of `str` 

66 The collections in the chain following this command. 

67 """ 

68 butler = Butler(repo, writeable=True) 

69 

70 # Every mode needs children except pop. 

71 if not children and mode != "pop": 

72 raise RuntimeError(f"Must provide children when defining a collection chain in mode {mode}.") 

73 

74 try: 

75 butler.registry.getCollectionType(parent) 

76 except MissingCollectionError: 

77 # Create it -- but only if mode can work with empty chain. 

78 if mode in ("redefine", "extend", "prepend"): 

79 if not doc: 

80 doc = None 

81 butler.registry.registerCollection(parent, CollectionType.CHAINED, doc) 

82 else: 

83 raise RuntimeError( 

84 f"Mode '{mode}' requires that the collection exists " 

85 f"but collection '{parent}' is not known to this registry" 

86 ) from None 

87 

88 current = list(butler.registry.getCollectionChain(parent)) 

89 

90 if mode == "redefine": 

91 # Given children are what we want. 

92 pass 

93 elif mode == "prepend": 

94 children = tuple(children) + tuple(current) 

95 elif mode == "extend": 

96 current.extend(children) 

97 children = current 

98 elif mode == "remove": 

99 for child in children: 

100 current.remove(child) 

101 children = current 

102 elif mode == "pop": 

103 if children: 

104 n_current = len(current) 

105 

106 def convert_index(i: int) -> int: 

107 """Convert negative index to positive.""" 

108 if i >= 0: 

109 return i 

110 return n_current + i 

111 

112 # For this mode the children should be integers. 

113 # Convert negative integers to positive ones to allow 

114 # sorting. 

115 indices = [convert_index(int(child)) for child in children] 

116 

117 # Reverse sort order so we can remove from the end first 

118 indices = list(reversed(sorted(indices))) 

119 

120 else: 

121 # Nothing specified, pop from the front of the chain. 

122 indices = [0] 

123 

124 for i in indices: 

125 current.pop(i) 

126 

127 children = current 

128 else: 

129 raise ValueError(f"Unrecognized update mode: '{mode}'") 

130 

131 butler.registry.setCollectionChain(parent, children, flatten=flatten) 

132 

133 return tuple(butler.registry.getCollectionChain(parent))