/
configuration.py
executable file
·244 lines (198 loc) · 8.38 KB
/
configuration.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
"""
Reads the PyWPS configuration file
"""
# Author: Calin Ciociu
#
# License:
#
# Web Processing Service implementation
# Copyright (C) 2015 PyWPS Development Team, represented by Jachym Cepicky
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
import logging
import sys
import os
import tempfile
import pywps
from pywps._compat import PY2
if PY2:
import ConfigParser
else:
import configparser
CONFIG = None
LOGGER = logging.getLogger("PYWPS")
def get_config_value(section, option):
"""Get desired value from configuration files
:param section: section in configuration files
:type section: string
:param option: option in the section
:type option: string
:returns: value found in the configuration file
"""
if not CONFIG:
load_configuration()
value = ''
if CONFIG.has_section(section):
if CONFIG.has_option(section, option):
value = CONFIG.get(section, option)
# Convert Boolean string to real Boolean values
if value.lower() == "false":
value = False
elif value.lower() == "true":
value = True
return value
def load_configuration(cfgfiles=None):
"""Load PyWPS configuration from configuration files.
The later configuration file in the array overwrites configuration
from the first.
:param cfgfiles: list of configuration files
"""
global CONFIG
LOGGER.info('loading configuration')
if PY2:
CONFIG = ConfigParser.SafeConfigParser()
else:
CONFIG = configparser.ConfigParser()
LOGGER.debug('setting default values')
CONFIG.add_section('server')
CONFIG.set('server', 'encoding', 'utf-8')
CONFIG.set('server', 'language', 'en-US')
CONFIG.set('server', 'url', 'http://localhost/wps')
CONFIG.set('server', 'maxprocesses', '30')
CONFIG.set('server', 'maxsingleinputsize', '1mb')
CONFIG.set('server', 'maxrequestsize', '3mb')
CONFIG.set('server', 'temp_path', tempfile.gettempdir())
CONFIG.set('server', 'processes_path', '')
outputpath = tempfile.gettempdir()
CONFIG.set('server', 'outputurl', 'file:///%s' % outputpath)
CONFIG.set('server', 'outputpath', outputpath)
CONFIG.set('server', 'workdir', tempfile.gettempdir())
CONFIG.set('server', 'parallelprocesses', '2')
CONFIG.add_section('logging')
CONFIG.set('logging', 'file', '')
CONFIG.set('logging', 'level', 'DEBUG')
CONFIG.set('logging', 'database', 'sqlite:///:memory:')
CONFIG.set('logging', 'prefix', 'pywps_')
CONFIG.add_section('metadata:main')
CONFIG.set('metadata:main', 'identification_title', 'PyWPS Processing Service')
CONFIG.set('metadata:main', 'identification_abstract', 'PyWPS is an implementation of the Web Processing Service standard from the Open Geospatial Consortium. PyWPS is written in Python.')
CONFIG.set('metadata:main', 'identification_keywords', 'PyWPS,WPS,OGC,processing')
CONFIG.set('metadata:main', 'identification_keywords_type', 'theme')
CONFIG.set('metadata:main', 'identification_fees', 'NONE')
CONFIG.set('metadata:main', 'identification_accessconstraints', 'NONE')
CONFIG.set('metadata:main', 'provider_name', 'Organization Name')
CONFIG.set('metadata:main', 'provider_url', 'http://pywps.org/')
CONFIG.set('metadata:main', 'contact_name', 'Lastname, Firstname')
CONFIG.set('metadata:main', 'contact_position', 'Position Title')
CONFIG.set('metadata:main', 'contact_address', 'Mailing Address')
CONFIG.set('metadata:main', 'contact_city', 'City')
CONFIG.set('metadata:main', 'contact_stateorprovince', 'Administrative Area')
CONFIG.set('metadata:main', 'contact_postalcode', 'Zip or Postal Code')
CONFIG.set('metadata:main', 'contact_country', 'Country')
CONFIG.set('metadata:main', 'contact_phone', '+xx-xxx-xxx-xxxx')
CONFIG.set('metadata:main', 'contact_fax', '+xx-xxx-xxx-xxxx')
CONFIG.set('metadata:main', 'contact_email', 'Email Address')
CONFIG.set('metadata:main', 'contact_url', 'Contact URL')
CONFIG.set('metadata:main', 'contact_hours', 'Hours of Service')
CONFIG.set('metadata:main', 'contact_instructions', 'During hours of service. Off on weekends.')
CONFIG.set('metadata:main', 'contact_role', 'pointOfContact')
CONFIG.add_section('grass')
CONFIG.set('grass', 'gisbase', '')
if not cfgfiles:
cfgfiles = _get_default_config_files_location()
if isinstance(cfgfiles, str):
cfgfiles = [cfgfiles]
loaded_files = CONFIG.read(cfgfiles)
if loaded_files:
LOGGER.info('Configuration file(s) %s loaded', loaded_files)
else:
LOGGER.info('No configuration files loaded. Using default values')
_check_config()
def _check_config():
"""Check some configuration values
"""
global CONFIG
def checkdir(confid):
confvalue = get_config_value('server', confid)
if not os.path.isdir(confvalue):
LOGGER.warning('server->%s configuration value %s is not directory'
% (confid, confvalue))
if not os.path.isabs(confvalue):
LOGGER.warning(
'server->%s configuration value %s is not absolute path, making it absolute to %s' %\
(confid, confvalue, os.path.abspath(confvalue)))
CONFIG.set('server', confid, os.path.abspath(confvalue))
[checkdir(n) for n in ['workdir', 'outputpath']]
def _get_default_config_files_location():
"""Get the locations of the standard configuration files. These are
Unix/Linux:
1. `/etc/pywps.cfg`
2. `$HOME/.pywps.cfg`
Windows:
1. `pywps\\etc\\default.cfg`
Both:
1. `$PYWPS_CFG environment variable`
:returns: configuration files
:rtype: list of strings
"""
is_win32 = sys.platform == 'win32'
if is_win32:
LOGGER.debug('Windows based environment')
else:
LOGGER.debug('UNIX based environment')
if os.getenv("PYWPS_CFG"):
LOGGER.debug('using PYWPS_CFG environment variable')
# Windows or Unix
if is_win32:
PYWPS_INSTALL_DIR = os.path.abspath(os.path.join(os.getcwd(), os.path.dirname(sys.argv[0])))
cfgfiles = (os.getenv("PYWPS_CFG"))
else:
cfgfiles = (os.getenv("PYWPS_CFG"))
else:
LOGGER.debug('trying to estimate the default location')
# Windows or Unix
if is_win32:
PYWPS_INSTALL_DIR = os.path.abspath(os.path.join(os.getcwd(), os.path.dirname(sys.argv[0])))
cfgfiles = (os.path.join(PYWPS_INSTALL_DIR, "pywps", "etc", "pywps.cfg"))
else:
homePath = os.getenv("HOME")
if homePath:
cfgfiles = (os.path.join(pywps.__path__[0], "etc", "pywps.cfg"), "/etc/pywps.cfg",
os.path.join(os.getenv("HOME"), ".pywps.cfg"))
else:
cfgfiles = (os.path.join(pywps.__path__[0], "etc",
"pywps.cfg"), "/etc/pywps.cfg")
return cfgfiles
def get_size_mb(mbsize):
"""Get real size of given obeject
"""
size = mbsize.lower()
import re
units = re.compile("[gmkb].*")
newsize = float(re.sub(units, '', size))
if size.find("g") > -1:
newsize *= 1024
elif size.find("m") > -1:
newsize *= 1
elif size.find("k") > -1:
newsize /= 1024
else:
newsize *= 1
LOGGER.debug('Calculated real size of %s is %s', mbsize, newsize)
return newsize