root / kamaki / cli / commands / config_cli.py @ 3fe56be4
History | View | Annotate | Download (3.8 kB)
1 |
# Copyright 2011-2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERaUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from kamaki.cli import command |
35 |
from kamaki.cli.argument import FlagArgument |
36 |
from kamaki.cli.commands import _command_init |
37 |
from kamaki.cli.command_tree import CommandTree |
38 |
|
39 |
config_cmds = CommandTree('config', 'Configuration commands') |
40 |
_commands = [config_cmds] |
41 |
|
42 |
about_options = '\nAbout options:\
|
43 |
\n syntax: [group.]option\
|
44 |
\n example: store.account\
|
45 |
\n special case: <option> is equivalent to global.<option>\
|
46 |
\n configuration file syntax:\
|
47 |
\n [group]\
|
48 |
\n option=value\
|
49 |
\n (more options can be set per group)'
|
50 |
|
51 |
|
52 |
@command(config_cmds)
|
53 |
class config_list(_command_init): |
54 |
"""List all configuration options
|
55 |
FAQ:
|
56 |
Q: I haven't set any options!
|
57 |
A: Defaults are used (override with /config set )
|
58 |
Q: There are more options than I have set
|
59 |
A: Default options remain if not explicitly replaced or deleted
|
60 |
"""
|
61 |
|
62 |
def main(self): |
63 |
for section in sorted(self.config.sections()): |
64 |
items = self.config.items(section)
|
65 |
for key, val in sorted(items): |
66 |
print('%s.%s = %s' % (section, key, val))
|
67 |
|
68 |
|
69 |
@command(config_cmds)
|
70 |
class config_get(_command_init): |
71 |
"""Show a configuration option
|
72 |
"""
|
73 |
|
74 |
__doc__ += about_options |
75 |
|
76 |
def main(self, option): |
77 |
section, sep, key = option.rpartition('.')
|
78 |
section = section or 'global' |
79 |
value = self.config.get(section, key)
|
80 |
if value:
|
81 |
print(value) |
82 |
|
83 |
|
84 |
@command(config_cmds)
|
85 |
class config_set(_command_init): |
86 |
"""Set a configuration option"""
|
87 |
|
88 |
__doc__ += about_options |
89 |
|
90 |
def main(self, option, value): |
91 |
section, sep, key = option.rpartition('.')
|
92 |
section = section or 'global' |
93 |
self.config.set(section, key, value)
|
94 |
self.config.write()
|
95 |
self.config.reload()
|
96 |
|
97 |
|
98 |
@command(config_cmds)
|
99 |
class config_delete(_command_init): |
100 |
"""Delete a configuration option
|
101 |
Default values are not removed by default. To alter this behavior in a
|
102 |
session, use --default.
|
103 |
"""
|
104 |
|
105 |
arguments = dict(
|
106 |
default=FlagArgument( |
107 |
'Remove default value as well (persists until end of sesion)',
|
108 |
'--default')
|
109 |
) |
110 |
|
111 |
def main(self, option): |
112 |
section, sep, key = option.rpartition('.')
|
113 |
section = section or 'global' |
114 |
self.config.remove_option(section, key, self['default']) |
115 |
self.config.write()
|
116 |
self.config.reload()
|