Hide keyboard shortcuts

Hot-keys 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

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 .. import Butler, CollectionType 

25from ..registry import 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(f"Mode '{mode}' requires that the collection exists " 

80 f"but collection '{parent}' is not known to this registry") from None 

81 

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

83 

84 if mode == "redefine": 

85 # Given children are what we want. 

86 pass 

87 elif mode == "prepend": 

88 children = tuple(children) + tuple(current) 

89 elif mode == "extend": 

90 current.extend(children) 

91 children = current 

92 elif mode == "remove": 

93 for child in children: 

94 current.remove(child) 

95 children = current 

96 elif mode == "pop": 

97 if children: 

98 n_current = len(current) 

99 

100 def convert_index(i): 

101 """Convert negative index to positive.""" 

102 if i >= 0: 

103 return i 

104 return n_current + i 

105 

106 # For this mode the children should be integers. 

107 # Convert negative integers to positive ones to allow 

108 # sorting. 

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

110 

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

112 children = reversed(sorted(children)) 

113 

114 else: 

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

116 children = [0] 

117 

118 for i in children: 

119 current.pop(i) 

120 

121 children = current 

122 else: 

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

124 

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

126 

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