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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

# 

# LSST Data Management System 

# 

# This product includes software developed by the 

# LSST Project (http://www.lsst.org/). 

# 

# See COPYRIGHT file at the top of the source tree. 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the LSST License Statement and 

# the GNU General Public License along with this program. If not, 

# see <https://www.lsstcorp.org/LegalNotices/>. 

# 

"""Utilities for working with YAML documents.""" 

 

from __future__ import print_function, division 

 

from collections import OrderedDict 

from copy import deepcopy 

import yaml 

 

__all__ = ['load_ordered_yaml', 'load_all_ordered_yaml'] 

 

 

def load_ordered_yaml(stream, **kwargs): 

"""Load a YAML document from a stream as an `~collections.OrderedDict`. 

 

Parameters 

---------- 

stream : 

A stream for a YAML file (made with `open` or `~io.StringIO`, for 

example). 

loader : optional 

A YAML loader class. Default is ``yaml.Loader``. 

object_pairs_hook : obj, optional 

Class that YAML key-value pairs are loaded into by the ``loader``. 

Default is `collections.OrderedDict`. 

 

Returns 

------- 

yaml_doc : `~collections.OrderedDict` 

The YAML document as an `~collections.OrderedDict` (or the type 

specified in ``object_pairs_hook``). 

""" 

OrderedLoader = _build_ordered_loader(**kwargs) 

return yaml.load(stream, OrderedLoader) 

 

 

def load_all_ordered_yaml(stream, **kwargs): 

"""Load all YAML documents from a stream as a `list` of 

`~collections.OrderedDict`\ s. 

 

Parameters 

---------- 

stream : 

A stream for a YAML file (made with `open` or `~io.StringIO`, for 

example). 

loader : optional 

A YAML loader class. Default is ``yaml.Loader``. 

object_pairs_hook : obj, optional 

Class that YAML key-value pairs are loaded into by the ``loader``. 

Default is `collections.OrderedDict`. 

 

Returns 

------- 

yaml_docs : `list` of `~collections.OrderedDict` 

The YAML documents as a `list` of `~collections.OrderedDict`\ s (or 

the type specified in ``object_pairs_hook``). 

""" 

OrderedLoader = _build_ordered_loader(**kwargs) 

return yaml.load_all(stream, OrderedLoader) 

 

 

def _build_ordered_loader(Loader=yaml.Loader, object_pairs_hook=OrderedDict): 

# Solution from http://stackoverflow.com/a/21912744 

 

class OrderedLoader(Loader): 

pass 

 

def construct_mapping(loader, node): 

loader.flatten_mapping(node) 

return object_pairs_hook(loader.construct_pairs(node)) 

 

OrderedLoader.add_constructor( 

yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, 

construct_mapping) 

 

return OrderedLoader 

 

 

def merge_documents(base_doc, new_doc): 

"""Merge the content of a dict-like object onto a base dict-like object, 

recursively following embedded dictionaries and lists. 

 

Parameters 

---------- 

base_doc : `dict`-like 

The base document. 

new_doc : `dict`-like 

The new document. Content from the new document are added to the 

base document. Matching keys from the new document will override 

the base document and new keys in the document are added to the 

base document. 

 

Returns 

------- 

merged_doc : `~collections.OrderedDict` 

The merged document. The contents of ``merged_doc`` are copies 

of originals in ``base_doc`` and ``new_doc``. 

 

Notes 

----- 

This function implements a key-value document merging algorithm 

design for specification YAML documents. The rules are: 

 

- Key-values from ``base_doc`` not present in ``new_doc`` are carried into 

``merged_doc``. 

 

- Key-values from ``new_doc`` not present in ``base_doc`` are carried into 

``merged_doc``. 

 

- If both ``new_doc`` and ``base_doc`` share a key and the value from 

**either** is a scalar (not a `dict` or `list`-type), the value from 

``new_doc`` is carried into ``merged_doc``. 

 

- If both ``new_doc`` and ``base_doc`` share a key and the value from 

**both** is a sequence (`list`-type) then the list items from the 

``new_doc`` are **appended** to the ``base_doc``\ 's list. 

 

- If both ``new_doc`` and ``base_doc`` share a key and the value from 

**both** is a mapping (`dict`-type) then the two values are 

merged by recursively calling this ``merge_documents`` function. 

""" 

# Create a copy so that the base doc is not mutated 

merged_doc = deepcopy(base_doc) 

 

for new_key, new_value in new_doc.items(): 

if new_key in merged_doc: 

# Deal with merge by created the 'merged_value' from base and new 

base_value = merged_doc[new_key] 

 

if isinstance(base_value, dict) and isinstance(new_value, dict): 

# Recursively merge these two dictionaries 

merged_value = merge_documents(base_value, new_value) 

 

elif isinstance(base_value, list) and isinstance(new_value, list): 

# Both are lists: merge by appending the new items to the end 

# of the base items. 

# Copy the base's list so we're not modify the input 

merged_value = deepcopy(base_value) 

merged_value.extend(deepcopy(new_value)) # modifies in-place 

 

else: 

# A scalar: just over-write the existing base value 

merged_value = deepcopy(new_value) 

 

# Done merging this key-value pair 

merged_doc[new_key] = merged_value 

 

else: 

# Add the new key that isn't over-writing merged_doc 

merged_doc[new_key] = deepcopy(new_value) 

 

return merged_doc