-
Notifications
You must be signed in to change notification settings - Fork 2k
/
__init__.py
76 lines (57 loc) · 2.14 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# encoding: utf-8
import os
import click
import logging
from logging.config import fileConfig as loggingFileConfig
import ckan.plugins as p
from ckan.config.middleware import make_app
log = logging.getLogger(__name__)
def error_shout(exception):
click.secho(str(exception), fg=u'red', err=True)
class CkanCommand(object):
def __init__(self, conf=None):
self.config = load_config(conf)
self.app = make_app(self.config.global_conf, **self.config.local_conf)
def _init_ckan_config(ctx, param, value):
ctx.obj = CkanCommand(value)
for plugin in p.PluginImplementations(p.IClick):
for cmd in plugin.get_commands():
cmd._ckanext = plugin.name
ctx.command.add_command(cmd)
click_config_option = click.option(
u'-c',
u'--config',
default=None,
metavar=u'CONFIG',
help=u'Config file to use (default: development.ini)',
is_eager=True,
callback=_init_ckan_config
)
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)