summaryrefslogtreecommitdiff
path: root/pykolab/cli/conf/cmd_file_set_setting.py
blob: 9bea1bf4f0606e9623cfd1abd842798cdf36b66d (plain)
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
# -*- coding: utf-8 -*-
# Copyright 2010-2012 Kolab Systems AG (http://www.kolabsys.com)
#
# Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen a kolabsys.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 3 or, at your option, any later version
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

import sys
import time

import pykolab

from pykolab import confmgmt
from pykolab import constants
from pykolab.cli import commands
from pykolab.confmgmt.db import get_db
from pykolab.translate import _

log = pykolab.getLogger('pykolab.cli')
conf = pykolab.getConf()

def __init__():
    commands.register('file_set_setting', execute, group='conf', description=description())

def cli_options():
    my_option_group = conf.add_cli_parser_option_group(_("CLI Options"))
    my_option_group.add_option(
                '--file',
                dest    = "_file",
                action  = "store",
                default = None,
                metavar = "FILE",
                help    = _("Add file FILE")
            )

    my_option_group.add_option(
                '--role',
                dest    = "roles",
                action  = "append",
                default = [],
                metavar = "ROLE",
                help    = _("Exclusively associate the setting for file FILE with role ROLE. May be specified multiple times to associate the file setting with multiple roles.")
            )

    my_option_group.add_option(
                '--key',
                dest    = "key",
                action  = "store",
                default = None,
                metavar = "KEY",
                help    = _("The key or 'name' for the setting.")
            )

    my_option_group.add_option(
                '--value',
                dest    = "value",
                action  = "store",
                default = None,
                metavar = "VALUE",
                help    = _("Set to value VALUE.")
            )

    my_option_group.add_option(
                '--function',
                dest    = "function",
                action  = "store",
                default = None,
                metavar = "FUNCTION",
                help    = _("Set to function FUNCTION.")
            )

def description():
    return """Change the value or function for a managed setting with key KEY in file FILE."""

def execute(*args, **kw):
    if conf._file == None:
        print >> sys.stderr, _("Must specify a file.")
        sys.exit(1)

    if conf.key == None:
        print >> sys.stderr, _("Must specify a key.")
        sys.exit(1)

    if conf.value == None and conf.function == None:
        print >> sys.stderr, _("Must specify a value or a function to get the values with.")
        sys.exit(1)

    if not conf.value == None and not conf.function == None:
        print >> sys.stderr, _("Can only specify one of value or function.")
        sys.exit(1)

    _file = confmgmt.get_file(conf._file)

    if _file == None:
        print >> sys.stderr, _("File %s does not exist.") % (conf._file)
        sys.exit(1)

    if not conf.key in [x.key for x in _file.settings]:
        print >> sys.stderr, _("Setting with key %s is not managed in file %s") % (conf.key, conf._file)
        sys.exit(1)

    setting = confmgmt.get_setting(conf.key, conf._file)

    if setting == None:
        print >> sys.stderr, _("Setting with key %s is not managed in file %s") % (conf.key, conf._file)
        sys.exit(1)

    if not conf.value == None:
        setting.value = conf.value
    elif not conf.function == None:
        setting.function = conf.function

    confmgmt.commit()