-
Notifications
You must be signed in to change notification settings - Fork 2k
/
__init__.py
55 lines (43 loc) · 1.6 KB
/
__init__.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
# encoding: utf-8
import os
import click
import logging
from logging.config import fileConfig as loggingFileConfig
log = logging.getLogger(__name__)
def error_shout(exception):
click.secho(str(exception), fg=u'red', err=True)
click_config_option = click.option(
u'-c',
u'--config',
default=None,
metavar=u'CONFIG',
help=u'Config file to use (default: development.ini)'
)
def load_config(ini_path=None):
from paste.deploy import appconfig
if ini_path:
if ini_path.startswith(u'~'):
ini_path = os.path.expanduser(ini_path)
filename = os.path.abspath(ini_path)
config_source = u'-c parameter'
elif os.environ.get(u'CKAN_INI'):
filename = os.environ.get(u'CKAN_INI')
config_source = u'$CKAN_INI'
else:
default_filename = u'development.ini'
filename = os.path.join(os.getcwd(), default_filename)
if not os.path.exists(filename):
# give really clear error message for this common situation
msg = u'ERROR: You need to specify the CKAN config (.ini) '\
u'file path.'\
u'\nUse the --config parameter or set environment ' \
u'variable CKAN_INI or have {}\nin the current directory.' \
.format(default_filename)
exit(msg)
if not os.path.exists(filename):
msg = u'Config file not found: %s' % filename
msg += u'\n(Given by: %s)' % config_source
exit(msg)
loggingFileConfig(filename)
log.info(u'Using configuration file {}'.format(filename))
return appconfig(u'config:' + filename)