-
Notifications
You must be signed in to change notification settings - Fork 12
/
output_file_parameter.py
386 lines (301 loc) · 16 KB
/
output_file_parameter.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# (c) 2016 Open Source Geospatial Foundation - all rights reserved
# (c) 2014 - 2015 Centre for Maritime Research and Experimentation (CMRE)
# (c) 2013 - 2014 German Aerospace Center (DLR)
# This code is licensed under the GPL 2.0 license, available at the root
# application directory.
__author__ = "Alessio Fabiani"
__copyright__ = "Copyright 2016 Open Source Geospatial Foundation - all rights reserved"
__license__ = "GPL"
import os
import tempfile
import path
import json
import uuid
# ############################################################################################################# #
# #
# Text/Plain Output Map Format #
# #
# ############################################################################################################# #
class OutputFileParameter(object):
def __init__(self, par_name, d, template_vars_for_param_types=None, wps_execution_shared_dir=None, uploader=None):
#{"type": "string", "description": "xml OAA output file", "filepath" : "%workdir\\\\output_file.xml" }
self._name=par_name
self._type=None
self._description = None
self._title = None
self._filepath = None
self._output_mime_type = None
self._wps_execution_shared_dir = wps_execution_shared_dir
self._uploader = uploader
self._backup_on_wps_execution_shared_dir = None
self._upload_data = None
self._upload_data_root = None
self._publish_as_layer = None
self._publish_layer_name = None
self._publish_default_style = None
self._publish_target_workspace = None
self._publish_metadata = None
for k,v in d.items():
if hasattr(self, "_" + k):
if template_vars_for_param_types != None and isinstance(v, basestring):
for var, val in template_vars_for_param_types.items():
if var in v:
v=v.replace("%" + var,val)
setattr(self, "_" + k, v)
self._filepath = path.path(self._filepath)
def get_name(self):
return self._name
def as_json_string(self):
#{"type": "string", "description": "A persons surname", "max": 1, "default": "Meier"}
res={}
attrib_to_convert = ['_type', '_description', '_title', '_output_mime_type', '_publish_as_layer', '_publish_layer_name', '_publish_default_style', '_publish_target_workspace']
attribute_list = [a for a in dir(self) if not a.startswith('__') and not callable(getattr(self, a))]
attribute_list_filtered = [x for x in attribute_list if x in attrib_to_convert]
for a in attribute_list_filtered:
res[a[1:]] = getattr(self, a)
return json.dumps(res)
def get_value(self):
if self._backup_on_wps_execution_shared_dir != None and self._backup_on_wps_execution_shared_dir.lower() == "true" and self._wps_execution_shared_dir != None:
unique_dirname = str(uuid.uuid4())
bkp_dir = path.path(self._wps_execution_shared_dir + "/" + unique_dirname)
bkp_dir.makedirs()
dst = bkp_dir.abspath() + "/" + self._filepath.basename()
self._filepath.copy(dst)
dst = path.path(dst)
return dst.text()
elif self._upload_data != None and self._upload_data.lower() == "true" and self._uploader != None:
unique_dirname = str(uuid.uuid4())
bkp_dir = path.path(tempfile.gettempdir() + '/' + unique_dirname)
bkp_dir.makedirs()
dst = bkp_dir.abspath() + '/' + self._filepath.basename()
self._filepath.copy(dst)
dst = path.path(dst)
src_path = os.path.abspath(os.path.join(dst.abspath(), os.pardir))
if self._upload_data_root:
unique_dirname = self._upload_data_root + '/' + unique_dirname
self._uploader.Upload(hostdir=unique_dirname, text='', binary='*.*', src=src_path)
return self._filepath.text()
else:
return self._filepath.text()
def get_type(self):
return "textual"
def get_description(self):
return self._description
def get_title(self):
return self._title
def get_output_mime_type(self):
return self._output_mime_type
def is_publish_as_layer(self):
return (self._publish_as_layer != None and self._publish_as_layer.lower() == "true")
def get_publish_layer_name(self):
return self._publish_layer_name
def get_publish_default_style(self):
return self._publish_default_style
def get_publish_target_workspace(self):
return self._publish_target_workspace
def get_metadata(self):
if self._publish_metadata != None:
metadata_file = path.path(self._publish_metadata)
if metadata_file.isfile():
return metadata_file.text()
return ' '
# ############################################################################################################# #
# #
# RAW File Output Map Format #
# #
# ############################################################################################################# #
class RawFileParameter(object):
def __init__(self, par_name, d, template_vars_for_param_types=None, wps_execution_shared_dir=None, uploader=None):
#{"type": "string", "description": "xml OAA output file", "filepath" : "%workdir\\\\output_file.xml" }
self._name=par_name
self._type=None
self._description = None
self._title = None
self._filepath = None
self._output_mime_type = None
self._wps_execution_shared_dir = wps_execution_shared_dir
self._uploader = uploader
self._backup_on_wps_execution_shared_dir = None
self._upload_data = None
self._upload_data_root = None
self._publish_as_layer = None
self._publish_layer_name = None
self._publish_default_style = None
self._publish_target_workspace = None
self._publish_metadata = None
for k,v in d.items():
if hasattr(self, "_" + k):
if template_vars_for_param_types != None and isinstance(v, basestring):
for var, val in template_vars_for_param_types.items():
if var in v:
v=v.replace("%" + var,val)
setattr(self, "_" + k, v)
self._filepath = path.path(self._filepath)
def get_name(self):
return self._name
def as_json_string(self):
#{"type": "string", "description": "A persons surname", "max": 1, "default": "Meier"}
res={}
attrib_to_convert = ['_type', '_description', '_title', '_output_mime_type', '_publish_as_layer', '_publish_layer_name', '_publish_default_style', '_publish_target_workspace']
attribute_list = [a for a in dir(self) if not a.startswith('__') and not callable(getattr(self, a))]
attribute_list_filtered = [x for x in attribute_list if x in attrib_to_convert]
for a in attribute_list_filtered:
res[a[1:]] = getattr(self, a)
return json.dumps(res)
def get_value(self):
if self._backup_on_wps_execution_shared_dir != None and self._backup_on_wps_execution_shared_dir.lower() == "true" and self._wps_execution_shared_dir != None:
unique_dirname = str(uuid.uuid4())
bkp_dir = path.path(self._wps_execution_shared_dir + "/" + unique_dirname)
bkp_dir.makedirs()
dst = bkp_dir.abspath() + "/" + self._filepath.basename()
self._filepath.copy(dst)
dst = path.path(dst)
return dst
elif self._upload_data != None and self._upload_data.lower() == "true" and self._uploader != None:
unique_dirname = str(uuid.uuid4())
if self._upload_data_root:
unique_dirname = self._upload_data_root + '/' + unique_dirname
src_path = os.path.abspath(os.path.join(self._filepath.abspath(), os.pardir))
basename = os.path.basename(self._filepath.abspath())
basename = os.path.splitext(basename)[0]
self._uploader.Upload(hostdir=unique_dirname, text='', binary=basename+'*.*', src=src_path)
return path.path(unique_dirname + "/" + self._filepath.basename())
else:
return self._filepath
def get_type(self):
return self._type
def get_description(self):
return self._description
def get_title(self):
return self._title
def get_output_mime_type(self):
return self._output_mime_type
def is_publish_as_layer(self):
return (self._publish_as_layer != None and self._publish_as_layer.lower() == "true")
def get_publish_layer_name(self):
return self._publish_layer_name
def get_publish_default_style(self):
return self._publish_default_style
def get_publish_target_workspace(self):
return self._publish_target_workspace
def get_metadata(self):
if self._publish_metadata != None:
metadata_file = path.path(self._publish_metadata)
if metadata_file.isfile():
return metadata_file.text()
return ' '
# ############################################################################################################# #
# #
# OWC Json Output Map Format #
# #
# ############################################################################################################# #
class OWCFileParameter(object):
def __init__(self, par_name, d, parameters_types_defs, template_vars_for_param_types=None, wps_execution_shared_dir=None, uploader=None):
#{"type": "string", "description": "xml OAA output file", "filepath" : "%workdir\\\\output_file.xml" }
self._name=par_name
self._type=None
self._description = None
self._title = None
self._output_mime_type = None
self._layers_to_publish = None
self._wps_execution_shared_dir = wps_execution_shared_dir
self._uploader = uploader
self._backup_on_wps_execution_shared_dir = None
self._upload_data = None
self._upload_data_root = None
self._publish_as_layer = "true"
self._publish_layer_name = None
self._publish_metadata = None
for k,v in d.items():
if hasattr(self, "_" + k):
if template_vars_for_param_types != None and isinstance(v, basestring):
for var, val in template_vars_for_param_types.items():
if var in v:
v=v.replace("%" + var,val)
setattr(self, "_" + k, v)
self._files_to_publish = ''
self._default_styles = ''
self._target_workspaces = ''
if self._layers_to_publish != None:
self._parameters_types_defs = parameters_types_defs
layer_names = self._layers_to_publish.split(';')
for name in layer_names:
if self._parameters_types_defs[name] != None:
publish_layer_name = self._parameters_types_defs[name].get('publish_layer_name')
publish_layer_name = publish_layer_name if publish_layer_name != None else ' '
publish_default_style = self._parameters_types_defs[name].get('publish_default_style')
publish_default_style = publish_default_style if publish_default_style != None else ' '
publish_target_workspace = self._parameters_types_defs[name].get('publish_target_workspace')
publish_target_workspace = publish_target_workspace if publish_target_workspace != None else ' '
self._files_to_publish += publish_layer_name + ";"
self._default_styles += publish_default_style + ";"
self._target_workspaces+= publish_target_workspace + ";"
def get_name(self):
return self._name
def as_json_string(self):
#{"type": "string", "description": "A persons surname", "max": 1, "default": "Meier"}
res={}
attrib_to_convert = ['_type', '_description', '_title', '_output_mime_type', '_publish_as_layer', '_publish_layer_name', '_files_to_publish', '_default_styles', '_target_workspaces']
attribute_list = [a for a in dir(self) if not a.startswith('__') and not callable(getattr(self, a))]
attribute_list_filtered = [x for x in attribute_list if x in attrib_to_convert]
for a in attribute_list_filtered:
res[a[1:]] = getattr(self, a)
return json.dumps(res)
def get_value(self):
if self._backup_on_wps_execution_shared_dir != None and self._backup_on_wps_execution_shared_dir.lower() == "true" and self._wps_execution_shared_dir != None:
unique_dirname = str(uuid.uuid4())
bkp_dir = path.path(self._wps_execution_shared_dir + "/" + unique_dirname)
bkp_dir.makedirs()
tokens = self._files_to_publish.split(';')
files_to_publish = ""
for token in tokens:
filepath = path.path(token)
dst = bkp_dir.abspath() + "/" + filepath.basename()
filepath.copy(dst)
dst = path.path(dst)
if len(files_to_publish) > 0:
files_to_publish = files_to_publish + ";"
files_to_publish = files_to_publish + dst.abspath()
return files_to_publish
elif self._upload_data != None and self._upload_data.lower() == "true" and self._uploader != None:
unique_dirname = str(uuid.uuid4())
bkp_dir = path.path(tempfile.gettempdir() + '/' + unique_dirname)
bkp_dir.makedirs()
tokens = self._files_to_publish.split(';')
if self._upload_data_root:
unique_dirname = self._upload_data_root + '/' + unique_dirname
files_to_publish = ""
for token in tokens:
filepath = path.path(token)
dst = bkp_dir.abspath() + '/' + filepath.basename()
filepath.copy(dst)
dst = path.path(dst)
if len(files_to_publish) > 0:
files_to_publish = files_to_publish + ';'
files_to_publish = files_to_publish + '/' + unique_dirname + '/' + filepath.basename()
self._uploader.Upload(hostdir=unique_dirname, text='', binary='*.*', src=bkp_dir.abspath())
return files_to_publish
else:
return self._files_to_publish
def get_type(self):
return self._type
def get_description(self):
return self._description
def get_title(self):
return self._title
def get_output_mime_type(self):
return self._output_mime_type
def is_publish_as_layer(self):
return (self._publish_as_layer != None and self._publish_as_layer.lower() == "true")
def get_publish_layer_name(self):
return self._publish_layer_name
def get_publish_default_style(self):
return self._default_styles
def get_publish_target_workspace(self):
return self._target_workspaces
def get_metadata(self):
if self._publish_metadata != None:
metadata_file = path.path(self._publish_metadata)
if metadata_file.isfile():
return metadata_file.text()
return ' '