summaryrefslogtreecommitdiff
path: root/pykolab/cli/conf/cmd_file_add_setting.py
diff options
context:
space:
mode:
Diffstat (limited to 'pykolab/cli/conf/cmd_file_add_setting.py')
-rw-r--r--pykolab/cli/conf/cmd_file_add_setting.py127
1 files changed, 127 insertions, 0 deletions
diff --git a/pykolab/cli/conf/cmd_file_add_setting.py b/pykolab/cli/conf/cmd_file_add_setting.py
new file mode 100644
index 0000000..e90e2e8
--- /dev/null
+++ b/pykolab/cli/conf/cmd_file_add_setting.py
@@ -0,0 +1,127 @@
+# -*- 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_add_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 """Add 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 of 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 conf.key in [x.key for x in _file.settings]:
+ print >> sys.stderr, _("Setting with key %s already managed in file %s") % (conf.key, conf._file)
+ sys.exit(1)
+
+ setting = confmgmt.add_setting(conf.key, conf.value, conf.function)
+
+ _file.settings.append(setting)
+
+ for role_name in conf.roles:
+ role = confmgmt.get_role(role_name)
+
+ if role == None:
+ log.warning(_("Role %s does not exist.") % (role_name))
+ continue
+
+ setting.roles.append(role)
+
+ confmgmt.commit()