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