get_database_writer_username()   A
last analyzed

Complexity

Conditions 4

Size

Total Lines 12
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nop 0
dl 0
loc 12
rs 9.85
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
3
try:
4
    import logging
5
    import sys
6
    import configparser
7
    import sys
0 ignored issues
show
introduced by
Imports from package sys are not grouped
Loading history...
8
    import os
9
    from enum import Enum
10
except ImportError as err:
11
    logging.error('Error %s import module: %s', __name__, err)
12
    logging.exception('Exception occurred')
13
14
    sys.exit(128)
15
16
config = configparser.ConfigParser()
17
config.sections()
18
config.read("app.conf")
19
20
21
class Mode(Enum):
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
22
    matcher = 0
23
    server = 1
24
25
26
__mode = Mode.matcher
27
28
29
def set_mode(mode):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
30
    if not isinstance(mode, Mode):
31
        raise ValueError('Cannot set mode to %s', mode)
0 ignored issues
show
introduced by
Exception arguments suggest string formatting might be intended
Loading history...
32
33
    global __mode
0 ignored issues
show
Coding Style introduced by
Usage of the global statement should be avoided.

Usage of global can make code hard to read and test, its usage is generally not recommended unless you are dealing with legacy code.

Loading history...
Coding Style Naming introduced by
Constant name "__mode" doesn't conform to UPPER_CASE naming style ('([^\\W\\da-z][^\\Wa-z]*|__.*__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
34
    __mode = mode
35
36
37
if not config.has_section(__mode.name):
38
    logging.fatal('Config section missing for server %s', __mode.name)
39
    sys.exit(-1)
40
41
currentConfig = config[__mode.name]
42
43
KEY_DIRECTORY_OUTPUT = 'dir.output'
44
KEY_DIRECTORY_CACHE_URL = 'dir.cache_url'
45
KEY_DATABASE_TYPE = 'db.type'
46
KEY_DATABASE_WRITE_HOST = 'db.write.host'
47
KEY_DATABASE_WRITE_PORT = 'db.write.port'
48
KEY_DATABASE_WRITE_USERNAME = 'db.write.username'
49
KEY_DATABASE_WRITE_PASSWORD = 'db.write.password'
50
KEY_DATABASE_POI_DATABASE = 'db.poi.database'
51
KEY_GEO_DEFAULT_PROJECTION = 'geo.default.projection'
52
KEY_GEO_DEFAULT_POI_DISTANCE = 'geo.default.poi.distance'
53
KEY_GEO_DEFAULT_POI_UNSAFE_DISTANCE = 'geo.default.poi.unsafe.distance'
54
KEY_GEO_DEFAULT_POI_PERFECT_DISTANCE = 'geo.default.poi.perfect.distance'
55
KEY_GEO_DEFAULT_POI_ROAD_DISTANCE = 'geo.default.poi.road.distance'
56
KEY_GEO_AMENITY_ATM_POI_DISTANCE = 'geo.amenity.atm.poi.distance'
57
KEY_GEO_SHOP_CONVENIENCE_POI_DISTANCE = 'geo.shop.conveience.poi.distance'
58
KEY_GEO_AMENITY_POST_OFFICE_POI_DISTANCE = 'geo.amenity.post.office.poi.distance'
59
KEY_GEO_PREFER_OSM_POSTCODE = 'geo.prefer.osm.postcode'
60
KEY_GEO_ALTERNATIVE_OPENING_HOURS = 'geo.alternative.opening.hours'
61
KEY_GEO_ALTERNATIVE_OPENING_HOURS_TAG = 'geo.alternative.opening.hours.tag'
62
KEY_DOWNLOAD_VERIFY_LINK = 'download.verify.link'
63
KEY_DOWNLOAD_USE_CACHED_DATA = 'download.use.cached.data'
64
KEY_DATAPROVIDERS_MODULES_AVAILABLE = 'dataproviders.modules.available'
65
KEY_DATAPROVIDERS_MODULES_ENABLE = 'dataproviders.modules.enable'
66
67
68
def get_config(key):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
69
    if key in currentConfig:
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
70
        return currentConfig[key]
71
    else:
72
        return None
73
74
75
def get_config_bool(key):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
76
    return config.getboolean(__mode.name, key)
77
78
79
def get_config_int(key):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
80
    return config.getint(__mode.name, key)
81
82
83
def get_config_string(key):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
84
    return config.get(__mode.name, key)
85
86
87
def get_config_list(key):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
88
    return config.get(__mode.name, key).split(',')
89
90
91
def init_log():
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
92
    logging.config.fileConfig("log.conf")
93
94
95
def get_directory_output():
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
96
    setting = get_config_string(KEY_DIRECTORY_OUTPUT)
97
    env_setting = os.environ.get('OPM_DIRECTORY_OUTPUT')
98
    if env_setting is not None:
99
        return env_setting
100
    if setting is not None:
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
101
        return setting
102
    else:
103
        return '.'
104
105
106
def get_directory_cache_url():
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
107
    setting = get_config_string(KEY_DIRECTORY_CACHE_URL)
108
    env_setting = os.environ.get('OPM_DIRECTORY_CACHE_URL')
109
    if env_setting is not None:
110
        return env_setting
111
    if setting is not None:
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
112
        return setting
113
    else:
114
        return os.path.join('.', 'cache_url')
115
116
117
def get_database_type():
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
118
    setting = get_config_string(KEY_DATABASE_TYPE)
119
    env_setting = os.environ.get('OPM_DATABASE_TYPE')
120
    if env_setting:
121
        return env_setting
122
    if setting is not None:
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
123
        return setting
124
    else:
125
        return 'postgresql'
126
127
128
def get_database_writer_host():
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
129
    setting = get_config_string(KEY_DATABASE_WRITE_HOST)
130
    env_setting = os.environ.get('OPM_DATABASE_WRITE_HOST')
131
    if env_setting is not None:
132
        setting = env_setting
133
    if setting is not None:
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
134
        logging.info('Using "%s" for database host.', setting)
135
        return setting
136
    else:
137
        logging.info('Using localhost for database host.')
138
        return 'localhost'
139
140
141
def get_database_writer_port():
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
142
    setting = get_config_int(KEY_DATABASE_WRITE_PORT)
143
    env_setting = os.environ.get('OPM_DATABASE_WRITE_PORT')
144
    if env_setting is not None:
145
        return env_setting
146
    if setting is not None:
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
147
        return setting
148
    else:
149
        return '5432'
150
151
152
def get_database_writer_username():
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
Unused Code introduced by
Either all return statements in a function should return an expression, or none of them should.
Loading history...
153
    setting = get_config_string(KEY_DATABASE_WRITE_USERNAME)
154
    env_setting = os.environ.get('OPM_DATABASE_WRITE_USERNAME')
155
    if env_setting is not None:
156
        setting = env_setting
157
    if setting == 'poi':
158
        logging.warning(
159
            'Using default username. For security concerns please change default username in the config file and the database.')
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (128/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
160
        if None != setting:
0 ignored issues
show
introduced by
Comparison should be setting != None
Loading history...
161
            return setting
162
    else:
163
        return 'poi'
164
165
166
def get_database_writer_password():
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
167
    setting = get_config_string(KEY_DATABASE_WRITE_PASSWORD)
168
    env_setting = os.environ.get('OPM_DATABASE_WRITE_PASSWORD')
169
    if env_setting is not None:
170
        setting = env_setting
171
    if setting == 'poitest':
172
        logging.warning(
173
            'Using default password. For security concerns please change default password in the config file and the database.')
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (128/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
174
    if None != setting:
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
introduced by
Comparison should be setting != None
Loading history...
175
        return setting
176
    else:
177
        return 'poitest'
178
179
180
def get_database_poi_database():
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
181
    setting = get_config_string(KEY_DATABASE_POI_DATABASE)
182
    env_setting = os.environ.get('OPM_DATABASE_POI_DATABASE')
183
    if env_setting is not None:
184
        return env_setting
185
    if setting is not None:
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
186
        return setting
187
    else:
188
        return 'poi'
189
190
191
def get_geo_default_projection():
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
192
    setting = get_config_int(KEY_GEO_DEFAULT_PROJECTION)
193
    if setting is not None:
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
194
        return setting
195
    else:
196
        return 4326
197
198
199
def get_geo_default_poi_distance():
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
200
    setting = get_config_int(KEY_GEO_DEFAULT_POI_DISTANCE)
201
    if setting is not None:
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
202
        return setting
203
    else:
204
        return 70
205
206
207
def get_geo_default_poi_unsafe_distance():
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
208
    setting = get_config_int(KEY_GEO_DEFAULT_POI_UNSAFE_DISTANCE)
209
    if setting is not None:
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
210
        return setting
211
    else:
212
        return 5
213
214
215
def get_geo_default_poi_perfect_distance():
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
216
    setting = get_config_int(KEY_GEO_DEFAULT_POI_PERFECT_DISTANCE)
217
    if setting is not None:
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
218
        return setting
219
    else:
220
        return 300
221
222
223
def get_geo_default_poi_road_distance():
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
224
    setting = get_config_int(KEY_GEO_DEFAULT_POI_ROAD_DISTANCE)
225
    if setting is not None:
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
226
        return setting
227
    else:
228
        return 600
229
230
231
def get_geo_amenity_atm_poi_distance():
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
232
    setting = get_config_int(KEY_GEO_AMENITY_ATM_POI_DISTANCE)
233
    if setting is not None:
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
234
        return setting
235
    else:
236
        return 20
237
238
239
def get_geo_shop_poi_distance():
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
240
    setting = get_config_int(KEY_GEO_SHOP_CONVENIENCE_POI_DISTANCE)
241
    if setting is not None:
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
242
        return setting
243
    else:
244
        return 50
245
246
247
def get_geo_amenity_post_office_poi_distance():
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
248
    setting = get_config_int(KEY_GEO_AMENITY_POST_OFFICE_POI_DISTANCE)
249
    if setting is not None:
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
250
        return setting
251
    else:
252
        return 250
253
254
255
def get_geo_prefer_osm_postcode():
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
256
    setting = get_config_bool(KEY_GEO_PREFER_OSM_POSTCODE)
257
    if setting is not None:
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
258
        return setting
259
    else:
260
        return True
261
262
263
def get_geo_alternative_opening_hours():
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
264
    setting = get_config_bool(KEY_GEO_ALTERNATIVE_OPENING_HOURS)
265
    if setting is not None:
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
266
        return setting
267
    else:
268
        return False
269
270
271
def get_geo_alternative_opening_hours_tag():
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
272
    setting = get_config_string(KEY_GEO_ALTERNATIVE_OPENING_HOURS_TAG)
273
    if setting is not None:
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
274
        return setting
275
    else:
276
        return None
277
278
279
def get_download_verify_link():
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
280
    setting = get_config_bool(KEY_DOWNLOAD_VERIFY_LINK)
281
    if setting is not None:
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
282
        return setting
283
    else:
284
        return True
285
286
287
def get_download_use_cached_data():
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
288
    setting = get_config_bool(KEY_DOWNLOAD_USE_CACHED_DATA)
289
    if setting is not None:
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
290
        return setting
291
    else:
292
        return True
293
294
295
def get_dataproviders_modules_available():
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
296
    setting = get_config_list(KEY_DATAPROVIDERS_MODULES_AVAILABLE)
297
    env_setting = os.environ.get('OPM_DATAPROVIDERS_MODULES_AVAILABLE')
298
    if env_setting is not None:
299
        setting = env_setting
300
    if setting is not None:
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
301
        return setting
302
    else:
303
        return True
304
305
306
def get_dataproviders_modules_enable():
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
307
    setting = get_config_list(KEY_DATAPROVIDERS_MODULES_ENABLE)
308
    env_setting = os.environ.get('OPM_DATAPROVIDERS_MODULES_ENABLE')
309
    if env_setting is not None:
310
        setting = env_setting
311
    if setting is not None:
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
312
        return setting
313
    else:
314
        return True
315