93 lines
3.0 KiB
Python
Executable File
93 lines
3.0 KiB
Python
Executable File
#!/usr/bin/python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import argparse
|
|
from sys import exit
|
|
|
|
from pyeole import scriptargs
|
|
from pyeole.ansiprint import print_red
|
|
from pyeole.log import init_logging
|
|
from creole.var_loader import convert_value
|
|
from creole.loader import creole_loader, config_save_values
|
|
from tiramisu.error import PropertiesOptionError
|
|
from pyeole.ihm import only_root
|
|
|
|
only_root()
|
|
|
|
parser = argparse.ArgumentParser(description=u"Set Creole variable",
|
|
parents=[scriptargs.logging()])
|
|
parser.add_argument("--default", action="store_true", default=False,
|
|
help=u"remettre à la valeur par défaut")
|
|
parser.add_argument('variable', nargs=1,
|
|
help=u"Nom de variable Creole")
|
|
parser.add_argument('value', nargs='?',
|
|
help=u"Valeur de la variable Creole")
|
|
|
|
options = parser.parse_args()
|
|
|
|
if options.verbose:
|
|
# 'info' is outputed to stdout
|
|
options.log_level = u'warning'
|
|
if options.debug:
|
|
options.log_level = u'debug'
|
|
|
|
if options.default and options.value:
|
|
print_red("En cas de remise à la valeur par défaut, il ne faut pas spécifier de valeur")
|
|
exit(1)
|
|
|
|
if not options.default and options.value is None:
|
|
print_red("Veuiller spécifier la valeur")
|
|
exit(1)
|
|
|
|
def main():
|
|
log = init_logging(level=options.log_level)
|
|
try:
|
|
config = creole_loader(rw=True, owner='creoleset', load_extra=True)
|
|
var = options.variable[0]
|
|
if '.' in var:
|
|
if var.startswith('.'):
|
|
var = var[1:]
|
|
namespace = var.split('.')[0]
|
|
else:
|
|
namespace = 'creole'
|
|
var = config.find_first(byname=var, type_='path',
|
|
force_permissive=True)
|
|
if options.default:
|
|
homeconfig, name = config.cfgimpl_get_home_by_path(var)
|
|
homeconfig.__delattr__(name)
|
|
else:
|
|
option = config.unwrap_from_path(var)
|
|
value = options.value
|
|
if option.impl_is_multi():
|
|
values = []
|
|
for val in value.split('\n'):
|
|
values.append(convert_value(option, val))
|
|
value = values
|
|
else:
|
|
value = convert_value(option, value)
|
|
setattr(config, var, value)
|
|
config_save_values(config, namespace)
|
|
except PropertiesOptionError, err:
|
|
if options.debug:
|
|
log.debug(err, exc_info=True)
|
|
print_red(u"Erreur de propriété : {0}".format(err))
|
|
exit(1)
|
|
except ValueError, err:
|
|
if options.debug:
|
|
log.debug(err, exc_info=True)
|
|
print_red("Valeur invalide : {0}".format(err))
|
|
exit(1)
|
|
except AttributeError:
|
|
if options.debug:
|
|
log.debug("AttributeError", exc_info=True)
|
|
print_red("Nom de variable inconnue : {0}".format(options.variable[0]))
|
|
exit(1)
|
|
except Exception, err:
|
|
if options.debug:
|
|
log.debug(err, exc_info=True)
|
|
print_red("Erreur inconnue : {0}".format(err))
|
|
exit(1)
|
|
|
|
if __name__ == '__main__':
|
|
main()
|