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

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

# http://pyrocko.org - GPLv3 

# 

# The Pyrocko Developers, 21st Century 

# ---|P------/S----------~Lg---------- 

from __future__ import absolute_import 

import os 

import os.path as op 

from copy import deepcopy 

 

from . import util 

from .guts import Object, Float, String, load, dump, List, Dict, TBase, \ 

Tuple, StringChoice, Bool 

 

guts_prefix = 'pf' 

 

pyrocko_dir_tmpl = os.environ.get( 

'PYROCKO_DIR', 

os.path.join('~', '.pyrocko')) 

 

 

def make_conf_path_tmpl(name='config'): 

return op.join(pyrocko_dir_tmpl, '%s.pf' % name) 

 

 

default_phase_key_mapping = { 

'F1': 'P', 'F2': 'S', 'F3': 'R', 'F4': 'Q', 'F5': '?'} 

 

 

class BadConfig(Exception): 

pass 

 

 

class PathWithPlaceholders(String): 

'''Path, possibly containing placeholders.''' 

pass 

 

 

class VisibleLengthSetting(Object): 

class __T(TBase): 

def regularize_extra(self, val): 

if isinstance(val, list): 

return self.cls(key=val[0], value=val[1]) 

 

return val 

 

def to_save(self, val): 

return (val.key, val.value) 

 

def to_save_xml(self, val): 

raise NotImplementedError() 

 

key = String.T() 

value = Float.T() 

 

 

class ConfigBase(Object): 

@classmethod 

def default(cls): 

return cls() 

 

 

class SnufflerConfig(ConfigBase): 

visible_length_setting = List.T( 

VisibleLengthSetting.T(), 

default=[VisibleLengthSetting(key='Short', value=6000.), 

VisibleLengthSetting(key='Medium', value=20000.), 

VisibleLengthSetting(key='Long', value=60000.)]) 

phase_key_mapping = Dict.T( 

String.T(), String.T(), default=default_phase_key_mapping) 

demean = Bool.T(default=True) 

show_scale_ranges = Bool.T(default=False) 

show_scale_axes = Bool.T(default=False) 

trace_scale = String.T(default='individual_scale') 

show_boxes = Bool.T(default=True) 

clip_traces = Bool.T(default=True) 

first_start = Bool.T(default=True) 

 

def get_phase_name(self, key): 

return self.phase_key_mapping.get('F%s' % key, 'Undefined') 

 

 

class PyrockoConfig(ConfigBase): 

cache_dir = PathWithPlaceholders.T( 

default=os.path.join(pyrocko_dir_tmpl, 'cache')) 

earthradius = Float.T(default=6371.*1000.) 

fdsn_timeout = Float.T(default=None, optional=True) 

gf_store_dirs = List.T(PathWithPlaceholders.T()) 

gf_store_superdirs = List.T(PathWithPlaceholders.T()) 

topo_dir = PathWithPlaceholders.T( 

default=os.path.join(pyrocko_dir_tmpl, 'topo')) 

tectonics_dir = PathWithPlaceholders.T( 

default=os.path.join(pyrocko_dir_tmpl, 'tectonics')) 

geonames_dir = PathWithPlaceholders.T( 

default=os.path.join(pyrocko_dir_tmpl, 'geonames')) 

crustdb_dir = PathWithPlaceholders.T( 

default=os.path.join(pyrocko_dir_tmpl, 'crustdb')) 

gshhg_dir = PathWithPlaceholders.T( 

default=os.path.join(pyrocko_dir_tmpl, 'gshhg')) 

leapseconds_path = PathWithPlaceholders.T( 

default=os.path.join(pyrocko_dir_tmpl, 'leap-seconds.list')) 

leapseconds_url = String.T( 

default='https://www.ietf.org/timezones/data/leap-seconds.list') 

earthdata_credentials = Tuple.T( 

2, String.T(), 

optional=True) 

gui_toolkit = StringChoice.T( 

choices=['auto', 'qt4', 'qt5'], 

default='auto') 

 

 

config_cls = { 

'config': PyrockoConfig, 

'snuffler': SnufflerConfig 

} 

 

 

def fill_template(tmpl, config_type): 

tmpl = tmpl .format( 

module=('.' + config_type) if config_type != 'pyrocko' else '') 

return tmpl 

 

 

def expand(x): 

x = op.expanduser(op.expandvars(x)) 

return x 

 

 

def rec_expand(x): 

for prop, val in x.T.ipropvals(x): 

if prop.multivalued: 

if val is not None: 

for i, ele in enumerate(val): 

if isinstance(prop.content_t, PathWithPlaceholders.T): 

newele = expand(ele) 

if newele != ele: 

val[i] = newele 

 

elif isinstance(ele, Object): 

rec_expand(ele) 

else: 

if isinstance(prop, PathWithPlaceholders.T): 

newval = expand(val) 

if newval != val: 

setattr(x, prop.name, newval) 

 

elif isinstance(val, Object): 

rec_expand(val) 

 

 

def processed(config): 

config = deepcopy(config) 

rec_expand(config) 

return config 

 

 

def mtime(p): 

return os.stat(p).st_mtime 

 

 

g_conf_mtime = {} 

g_conf = {} 

 

 

def raw_config(config_name='config'): 

 

conf_path = expand(make_conf_path_tmpl(config_name)) 

 

if not op.exists(conf_path): 

g_conf[config_name] = config_cls[config_name].default() 

write_config(g_conf[config_name], config_name) 

 

conf_mtime_now = mtime(conf_path) 

if conf_mtime_now != g_conf_mtime.get(config_name, None): 

g_conf[config_name] = load(filename=conf_path) 

if not isinstance(g_conf[config_name], config_cls[config_name]): 

raise BadConfig('config file does not contain a ' 

'valid "%s" section.' % 

config_cls[config_name].__name__) 

 

g_conf_mtime[config_name] = conf_mtime_now 

 

return g_conf[config_name] 

 

 

def config(config_name='config'): 

return processed(raw_config(config_name)) 

 

 

def write_config(conf, config_name='config'): 

conf_path = expand(make_conf_path_tmpl(config_name)) 

util.ensuredirs(conf_path) 

dump(conf, filename=conf_path) 

 

 

override_gui_toolkit = None 

 

 

def effective_gui_toolkit(): 

return override_gui_toolkit or config().gui_toolkit