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

# encoding: utf-8 

from __future__ import print_function, division, absolute_import 

 

from os.path import join, exists, dirname, abspath, basename 

 

from .domain_objects import Site, Picture, Parameter, Parameters, SourceType 

from .errors import ConsistencyError 

from .logger import logger 

from .utils import iter_to_list 

from .yaml_stuff import parse_date, check_yaml_fields_exist, load_yaml_and_bunchify 

 

 

def _load_yaml(*pathes): 

 

path = join(*pathes) 

logger().info("load {}".format(path)) 

data = load_yaml_and_bunchify(path) 

if data is None: 

msg = "file {} is empty".format(path) 

logger().warn(msg) 

raise ConsistencyError(msg) 

return data 

 

 

def log(exceptions, rel_path): 

for e in exceptions: 

logger().warn("{}: {}".format(rel_path, e)) 

yield e 

 

 

def parse_site(landing_zone_folder, rel_path): 

""" Maksite sure that 'pictures' is always present 

""" 

 

path = join(landing_zone_folder, rel_path) 

try: 

site = _load_yaml(path) 

except ConsistencyError as e: 

yield e 

return 

 

if "pictures" not in site: 

site.pictures = [] 

 

site_folder = dirname(path) 

exceptions = check_site_yaml(site, rel_path) 

exceptions += check_pictures(site, site_folder) 

if exceptions: 

yield from log(exceptions, rel_path) 

return 

 

# we transform our site Bunch so that it corresponds to the data resp. dbo objects 

# attributes: 

 

folder = dirname(abspath(path)) 

 

for picture in site.pictures: 

path = join(folder, picture.path) 

with open(path, "rb") as fh: 

picture.data = fh.read() 

if "date" in picture: 

picture.date = parse_date(picture.date) 

else: 

picture.date = None 

 

picture.filename = basename(picture.path) 

del picture.path 

 

site.pictures = [Picture(p) for p in site.pictures] 

 

site.coord_x = site.coordinates.x 

site.coord_y = site.coordinates.y 

site.coord_z = site.coordinates.z 

del site.coordinates 

 

# now we can initialize our data object: 

yield Site(site, rel_path) 

 

 

def parse_parameters(landing_zone_folder, rel_path): 

 

try: 

parameters = _load_yaml(landing_zone_folder, rel_path) 

except ConsistencyError as e: 

yield e 

return 

 

logger().info("number of parameters found: {}".format(len(parameters))) 

 

result = [] 

for parameter in parameters: 

exceptions = check_parameters_yaml(parameter, rel_path) 

if exceptions: 

yield from log(exceptions, rel_path) 

continue 

 

# now we can initialize our data object: 

result.append(Parameter(parameter)) 

 

yield Parameters(result, rel_path) 

 

 

def parse_source_type(landing_zone_folder, rel_path): 

 

try: 

source_type = _load_yaml(landing_zone_folder, rel_path) 

except ConsistencyError as e: 

yield e 

return 

 

excs = check_source_type_yaml(source_type, rel_path) 

if excs: 

yield from log(excs, rel_path) 

return 

 

yield SourceType(source_type, rel_path) 

 

 

@iter_to_list 

def check_site_yaml(site, rel_path): 

check_attributes = ["name", "coordinates", "coordinates.x", "coordinates.y", "coordinates.z", 

"street", "city", "pictures"] 

yield from check_yaml_fields_exist(site, rel_path, check_attributes) 

 

 

@iter_to_list 

def check_pictures(site, site_folder): 

exceptions = [] 

for i, picture in enumerate(site.pictures): 

 

if "path" not in picture: 

yield ConsistencyError("picture no {} has no field 'path'".format(i + 1)) 

elif not picture.path: 

yield ConsistencyError("entry 'path' of picture no {} is empty".format(i + 1)) 

else: 

path = join(site_folder, picture.path) 

if not exists(path): 

yield ConsistencyError("picture {} does not exist".format(picture.path)) 

 

if "description" not in picture: 

yield ConsistencyError("picture no {} has no field 'description'".format(i + 1)) 

elif not picture.description: 

yield ConsistencyError("entry 'description' of picture no {} is empty".format(i + 1)) 

 

 

@iter_to_list 

def check_parameters_yaml(parameters, rel_path): 

check_attributes = ["name", "unit", "description"] 

yield from check_yaml_fields_exist(parameters, rel_path, check_attributes) 

if "name" in parameters and parameters.name is None: 

yield ConsistencyError("field 'name' has no value") 

if "unit" in parameters and parameters.unit is None: 

yield ConsistencyError("field 'unit' has no value") 

 

 

@iter_to_list 

def check_source_type_yaml(source_type, rel_path): 

for name in ("name", "description"): 

if name not in source_type: 

yield ConsistencyError("field {} is not set in {}".format(name, rel_path)) 

continue 

if not source_type[name]: 

yield ConsistencyError("field {} is empty in {}".format(name))