Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / commands / config.py @ f724cd35

History | View | Annotate | Download (4.1 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, errors
37
from kamaki.cli.command_tree import CommandTree
38

    
39
config_cmds = CommandTree('config', 'Kamaki configurations')
40
_commands = [config_cmds]
41

    
42
about_options = '\nAbout options:\
43
    \n. syntax: [group.]option\
44
    \n. example: file.uuid\
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
    @errors.generic.all
63
    def _run(self):
64
        for section in sorted(self.config.sections()):
65
            items = self.config.items(section)
66
            for key, val in sorted(items):
67
                print('%s.%s = %s' % (section, key, val))
68

    
69
    def main(self):
70
        self._run()
71

    
72

    
73
@command(config_cmds)
74
class config_get(_command_init):
75
    """Show a configuration option"""
76

    
77
    __doc__ += about_options
78

    
79
    @errors.generic.all
80
    def _run(self, option):
81
        section, sep, key = option.rpartition('.')
82
        section = section or 'global'
83
        value = self.config.get(section, key)
84
        if value:
85
            print(value)
86

    
87
    def main(self, option):
88
        self._run(option)
89

    
90

    
91
@command(config_cmds)
92
class config_set(_command_init):
93
    """Set a configuration option"""
94

    
95
    __doc__ += about_options
96

    
97
    @errors.generic.all
98
    def _run(self, option, value):
99
        section, sep, key = option.rpartition('.')
100
        section = section or 'global'
101
        self.config.set(section, key, value)
102
        self.config.write()
103
        self.config.reload()
104

    
105
    def main(self, option, value):
106
        self._run(option, value)
107

    
108

    
109
@command(config_cmds)
110
class config_delete(_command_init):
111
    """Delete a configuration option
112
    Default values are not removed by default. To alter this behavior in a
113
    session, use --default.
114
    """
115

    
116
    arguments = dict(
117
        default=FlagArgument(
118
            'Remove default value as well (persists until end of session)',
119
            '--default')
120
    )
121

    
122
    @errors.generic.all
123
    def _run(self, option):
124
        section, sep, key = option.rpartition('.')
125
        section = section or 'global'
126
        self.config.remove_option(section, key, self['default'])
127
        self.config.write()
128
        self.config.reload()
129

    
130
    def main(self, option):
131
        self._run(option)