-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.py
257 lines (193 loc) · 9.96 KB
/
config.py
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from json import loads
class Config:
def __init__(self, config, database_config):
self.config = config
self.database_config = database_config
self.valid_relations = ['contains', 'contains_properly', 'covered_by', 'covers', 'crosses', 'disjoint', 'distance',
'distance_within', 'equals', 'hausdorff_distance', 'intersects', 'overlaps', 'touches', 'within']
self.check_config()
def check_config(self):
if 'source' not in self.config:
raise ConfigNotValidError("Config is missing source")
else:
if 'endpoint' not in self.config['source']:
raise ConfigNotValidError("Config is missing source endpoint")
if 'var' not in self.config['source']:
raise ConfigNotValidError("Config is missing source var")
else:
if 'uri' not in self.config['source']['var']:
raise ConfigNotValidError("Variable for uri not specified in source")
if 'shape' not in self.config['source']['var']:
raise ConfigNotValidError("Variable for shape not specified in source")
if 'rawquery' not in self.config['source']:
if 'graph' not in self.config['source']:
raise ConfigNotValidError("Config is missing source graph")
if 'property' not in self.config['source']:
raise ConfigNotValidError("Config is missing source property")
if 'offset' in self.config['source']:
if not isinstance(self.config['source']['offset'], int):
raise ConfigNotValidError("Source offset is not an integer")
if 'limit' in self.config['source']:
if not isinstance(self.config['source']['limit'], int):
raise ConfigNotValidError("Source limit is not an integer")
if 'chunksize' in self.config['source']:
if not isinstance(self.config['source']['chunksize'], int):
raise ConfigNotValidError("Source chunksize is not an integer")
if 'target' not in self.config:
raise ConfigNotValidError("Config is missing target")
else:
if 'endpoint' not in self.config['target']:
raise ConfigNotValidError("Config is missing target endpoint")
if 'var' not in self.config['target']:
raise ConfigNotValidError("Config is missing target var")
else:
if 'uri' not in self.config['target']['var']:
raise ConfigNotValidError("Variable for uri not specified in target")
if 'shape' not in self.config['target']['var']:
raise ConfigNotValidError("Variable for shape not specified in target")
if 'rawquery' not in self.config['target']:
if 'graph' not in self.config['target']:
raise ConfigNotValidError("Config is missing target graph")
if 'property' not in self.config['target']:
raise ConfigNotValidError("Config is missing target property")
if 'offset' in self.config['target']:
if not isinstance(self.config['target']['offset'], int):
raise ConfigNotValidError("Target offset is not an integer")
if 'limit' in self.config['target']:
if not isinstance(self.config['target']['limit'], int):
raise ConfigNotValidError("Target limit is not an integer")
if 'chunksize' in self.config['target']:
if not isinstance(self.config['target']['chunksize'], int):
raise ConfigNotValidError("Target chunksize is not an integer")
if 'measure' not in self.config:
raise ConfigNotValidError("Measure not specified")
else:
if 'relation' not in self.config['measure']:
raise ConfigNotValidError("Relation not specified")
else:
if self.config['measure']['relation'] not in self.valid_relations:
raise ConfigNotValidError("Relation not valid. Only the following relations are valid: {}".format(self.valid_relations))
elif self.config['measure']['relation'] == 'distance' or self.config['measure']['relation'] == 'distance_within' or self.config['measure']['relation'] == 'hausdorff_distance':
if 'threshold' not in self.config['measure']:
raise ConfigNotValidError("Config is missing measure threshold")
# Check database config
if 'database_name' not in self.database_config:
raise ConfigNotValidError("Database name not specified")
if 'database_user' not in self.database_config:
raise ConfigNotValidError("Database user not specified")
if 'database_password' not in self.database_config:
raise ConfigNotValidError("Database password not specified")
def get_chunksize(self, type):
if type != 'source' and type != 'target':
raise Exception("Wrong type (not source or target) specified")
if 'chunksize' in self.config[type]:
return self.config[type]['chunksize']
else:
return 1000
def get_database_name(self):
return self.database_config['database_name']
def get_database_user(self):
return self.database_config['database_user']
def get_database_password(self):
return self.database_config['database_password']
def get_database_host(self):
if 'database_host' in self.database_config:
return self.database_config['database_host']
else:
return 'localhost'
def get_database_port(self):
if 'database_port' in self.database_config:
return str(self.database_config['database_port'])
else:
return '5432'
def get_database_string(self):
return "host='{}' dbname='{}' user='{}' password='{}' port={}".format(self.get_database_host(), self.get_database_name(),
self.get_database_user(), self.get_database_password(),
self.get_database_port())
def get_endpoint(self, type):
if type != 'source' and type != 'target':
raise Exception("Wrong type (not source or target) specified")
return self.config[type]['endpoint']
def get_geo_coding(self, type):
if type != 'source' and type != 'target':
raise Exception("Wrong type (not source or target) specified")
if 'geo_coding' in self.config[type]:
return self.config[type]['geo_coding']
return False
def get_geometry(self, type):
if type != 'source' and type != 'target':
raise Exception("Wrong type (not source or target) specified")
return self.config[type]['geometry']
def get_graph(self, type):
if type != 'source' and type != 'target':
raise Exception("Wrong type (not source or target) specified")
return self.config[type]['graph']
def get_limit(self, type):
if type != 'source' and type != 'target':
raise Exception("Wrong type (not source or target) specified")
if 'limit' in self.config[type] and self.get_endpoint_type(type) == 'remote' :
return self.config[type]['limit']
else:
return -1
def get_relation(self):
return self.config['measure']['relation']
def get_offset(self, type):
if type != 'source' and type != 'target':
raise Exception("Wrong type (not source or target) specified")
if 'offset' in self.config[type] and self.get_endpoint_type(type) == 'remote':
return self.config[type]['offset']
else:
return 0
def get_output_format(self):
if 'output_format' in self.config:
return self.config['output_format']
else:
return None
def get_prefixes(self):
if 'prefixes' in self.config:
return self.config['prefixes']
else:
return None
def get_property(self, type):
if type != 'source' and type != 'target':
raise Exception("Wrong type (not source or target) specified")
return self.config[type]['property']
def get_rawquery(self, type):
if type != 'source' and type != 'target':
raise Exception("Wrong type (not source or target) specified")
if 'rawquery' in self.config[type]:
return self.config[type]['rawquery']
else:
return None
def get_restriction(self, type):
if type != 'source' and type != 'target':
raise Exception("Wrong type (not source or target) specified")
if 'restriction' in self.config[type]:
return self.config[type]['restriction']
else:
return None
def get_threshold(self):
return self.config['measure']['threshold']
def get_var_uri(self, type):
if type != 'source' and type != 'target':
raise Exception("Wrong type (not source or target) specified")
return self.config[type]['var']['uri']
def get_var_shape(self, type):
if type != 'source' and type != 'target':
raise Exception("Wrong type (not source or target) specified")
return self.config[type]['var']['shape']
def get_endpoint_type(self, type):
if self.config[type]['endpoint'].startswith('http'):
return 'remote' #0
elif self.config[type]['endpoint'].startswith('file'):
return 'local' #1
class ConfigNotValidError(Exception):
def __init__(self, error):
self.error = error
def __str__(self):
return(self.error)
def load_config(config_file_path):
with open(config_file_path, 'r') as config_file:
return loads(config_file.read())