-
Notifications
You must be signed in to change notification settings - Fork 2k
/
cli.py
127 lines (105 loc) · 3.29 KB
/
cli.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
# encoding: utf-8
import logging
from collections import defaultdict
import six
import click
import ckan.plugins as p
import ckan.cli as ckan_cli
from ckan.config.middleware import make_app
from ckan.cli import (
config_tool,
jobs,
datapusher,
front_end_build,
db, search_index, server,
profile,
asset,
datastore,
sysadmin,
translation,
dataset,
views,
plugin_info,
notify,
tracking,
minify,
less,
generate,
user
)
from ckan.cli import seed
log = logging.getLogger(__name__)
class CkanCommand(object):
def __init__(self, conf=None):
# Don't import `load_config` by itself, rather call it using
# module so that it can be patched during tests
self.config = ckan_cli.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)
if six.PY2:
ctx.meta["flask_app"] = ctx.obj.app.apps["flask_app"]._wsgi_app
else:
ctx.meta["flask_app"] = ctx.obj.app._wsgi_app
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
)
class CustomGroup(click.Group):
def get_command(self, ctx, name):
cmd = super(CustomGroup, self).get_command(ctx, name)
if not cmd:
ctx.forward(self)
cmd = super(CustomGroup, self).get_command(ctx, name)
return cmd
def format_commands(self, ctx, formatter):
super(CustomGroup, self).format_commands(ctx, formatter)
ctx.invoke(self)
ext_commands = defaultdict(list)
for subcommand in self.list_commands(ctx):
cmd = self.get_command(ctx, subcommand)
if cmd is None or not hasattr(cmd, u'_ckanext'):
continue
help = cmd.short_help or u''
ext_commands[cmd._ckanext].append((subcommand, help))
if ext_commands:
with formatter.section(u'Plugins'):
for ext, rows in ext_commands.items():
with formatter.section(ext):
formatter.write_dl(rows)
@click.group(cls=CustomGroup)
@click.help_option(u'-h', u'--help')
@click_config_option
def ckan(config, *args, **kwargs):
pass
ckan.add_command(jobs.jobs)
ckan.add_command(config_tool.config_tool)
ckan.add_command(front_end_build.front_end_build)
ckan.add_command(server.run)
ckan.add_command(profile.profile)
ckan.add_command(seed.seed)
ckan.add_command(db.db)
ckan.add_command(datapusher.datapusher)
ckan.add_command(search_index.search_index)
ckan.add_command(sysadmin.sysadmin)
ckan.add_command(asset.asset)
ckan.add_command(datastore.datastore)
ckan.add_command(translation.translation)
ckan.add_command(dataset.dataset)
ckan.add_command(views.views)
ckan.add_command(plugin_info.plugin_info)
ckan.add_command(notify.notify)
ckan.add_command(tracking.tracking)
ckan.add_command(minify.minify)
ckan.add_command(less.less)
ckan.add_command(generate.generate)
ckan.add_command(user.user)