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

43 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-07-23 02:26 -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 .._butler import Butler 

25from ..registry import CollectionType, MissingCollectionError 

26 

27 

28def collectionChain(repo, mode, parent, children, doc, flatten): 

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

30 

31 Parameters 

32 ---------- 

33 repo : `str` 

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

35 repo and its location. 

36 mode : `str` 

37 Update mode for this chain. Options are: 

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

39 ``children``. 

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

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

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

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

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

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

46 does not exist. 

47 parent: `str` 

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

49 does not exist already. 

50 children: iterable of `str` 

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

52 doc : `str` 

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

54 that will be associated with it. 

55 flatten : `str` 

56 If `True`, recursively flatten out any nested 

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

58 

59 Returns 

60 ------- 

61 chain : `tuple` of `str` 

62 The collections in the chain following this command. 

63 """ 

64 butler = Butler(repo, writeable=True) 

65 

66 # Every mode needs children except pop. 

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

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

69 

70 try: 

71 butler.registry.getCollectionType(parent) 

72 except MissingCollectionError: 

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

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

75 if not doc: 

76 doc = None 

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

78 else: 

79 raise RuntimeError( 

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

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

82 ) from None 

83 

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

85 

86 if mode == "redefine": 

87 # Given children are what we want. 

88 pass 

89 elif mode == "prepend": 

90 children = tuple(children) + tuple(current) 

91 elif mode == "extend": 

92 current.extend(children) 

93 children = current 

94 elif mode == "remove": 

95 for child in children: 

96 current.remove(child) 

97 children = current 

98 elif mode == "pop": 

99 if children: 

100 n_current = len(current) 

101 

102 def convert_index(i): 

103 """Convert negative index to positive.""" 

104 if i >= 0: 

105 return i 

106 return n_current + i 

107 

108 # For this mode the children should be integers. 

109 # Convert negative integers to positive ones to allow 

110 # sorting. 

111 children = [convert_index(int(child)) for child in children] 

112 

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

114 children = reversed(sorted(children)) 

115 

116 else: 

117 # Nothing specified, pop from the front of the chin. 

118 children = [0] 

119 

120 for i in children: 

121 current.pop(i) 

122 

123 children = current 

124 else: 

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

126 

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

128 

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