131 lines
4.0 KiB
Plaintext
131 lines
4.0 KiB
Plaintext
|
#!/usr/bin/python
|
|||
|
# -*- coding: utf-8 -*-
|
|||
|
|
|||
|
"""Get a creole variable value.
|
|||
|
"""
|
|||
|
|
|||
|
import sys
|
|||
|
import argparse
|
|||
|
|
|||
|
from creole.client import CreoleClient
|
|||
|
|
|||
|
from pyeole import scriptargs
|
|||
|
from pyeole.log import init_logging
|
|||
|
from pyeole.encode import normalize
|
|||
|
|
|||
|
_RETURN_VALUES = u"""Multiple values are separated with NEWLINE character '\\n',
|
|||
|
or SPACE character if several variables are displayed."""
|
|||
|
|
|||
|
parser = argparse.ArgumentParser(description=u"Get creole variable",
|
|||
|
epilog=_RETURN_VALUES,
|
|||
|
parents=[scriptargs.logging()])
|
|||
|
|
|||
|
parser.add_argument('variable', nargs='?',
|
|||
|
help=u"Nom de variable creole")
|
|||
|
parser.add_argument('default', nargs='?',
|
|||
|
help=u"Valeur par défaut si la variable n’existe pas")
|
|||
|
|
|||
|
incompatible_options = parser.add_mutually_exclusive_group()
|
|||
|
|
|||
|
incompatible_options.add_argument('--groups', action="store_true", default=False,
|
|||
|
help=u"Liste les groupes de conteneurs")
|
|||
|
|
|||
|
incompatible_options.add_argument('--list', action="store_true", default=False,
|
|||
|
help=u"Liste l'ensemble des variables creole")
|
|||
|
|
|||
|
incompatible_options.add_argument('--reload', action="store_true", default=False,
|
|||
|
help=u"Recharge toute la configuration creole")
|
|||
|
|
|||
|
incompatible_options.add_argument('--reload-eol', action="store_true", default=False,
|
|||
|
help=u"Recharge les valeurs de configuration 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'
|
|||
|
|
|||
|
def output(value, strip_master=False):
|
|||
|
"""
|
|||
|
formatage de l'affichage
|
|||
|
"""
|
|||
|
if isinstance(value, list):
|
|||
|
#FIXME: ['val1', None, 'val2']
|
|||
|
for val in value:
|
|||
|
if isinstance(val, dict):
|
|||
|
sys.stderr.write(u'{}\n'.format(val['err']))
|
|||
|
else:
|
|||
|
sys.stdout.write(u'{}\n'.format(val))
|
|||
|
elif isinstance(value, dict):
|
|||
|
# in case several keys/values are returned
|
|||
|
list_keys = value.keys()
|
|||
|
list_keys.sort()
|
|||
|
for var in list_keys:
|
|||
|
values = value[var]
|
|||
|
if isinstance(values, list):
|
|||
|
values_ = u''
|
|||
|
for val in values:
|
|||
|
if val and not isinstance(val, dict):
|
|||
|
values_ += u" {}".format(val)
|
|||
|
values = values_
|
|||
|
elif values is None:
|
|||
|
values = u''
|
|||
|
else:
|
|||
|
values = u'{}'.format(values)
|
|||
|
if strip_master:
|
|||
|
varname = var.split('.')[-1]
|
|||
|
else:
|
|||
|
varname = var
|
|||
|
sys.stdout.write(u'{}="{}"\n'.format(varname, values.strip()))
|
|||
|
elif value is None or value == u'':
|
|||
|
sys.stdout.write(u'\n')
|
|||
|
else:
|
|||
|
sys.stdout.write(u'{0}\n'.format(value))
|
|||
|
#return ret.rstrip('\n')
|
|||
|
|
|||
|
def main():
|
|||
|
"""Setup environnment and run templatisation.
|
|||
|
"""
|
|||
|
|
|||
|
try:
|
|||
|
log = init_logging(level=options.log_level)
|
|||
|
client = CreoleClient()
|
|||
|
var = options.variable
|
|||
|
if options.groups:
|
|||
|
output(client.get_groups())
|
|||
|
elif options.list:
|
|||
|
output(client.get_creole(), True)
|
|||
|
elif options.reload:
|
|||
|
client.reload_config()
|
|||
|
elif options.reload_eol:
|
|||
|
client.reload_eol()
|
|||
|
elif not var:
|
|||
|
raise Exception(u"Veuillez spécifier un nom de variable Creole")
|
|||
|
else:
|
|||
|
if options.default is not None:
|
|||
|
kwargs = {'default':options.default}
|
|||
|
else:
|
|||
|
kwargs = {}
|
|||
|
if '.' in var:
|
|||
|
output(client.get(var))
|
|||
|
else:
|
|||
|
output(client.get_creole(var, **kwargs))
|
|||
|
|
|||
|
except Exception, err:
|
|||
|
if options.debug:
|
|||
|
log.debug(normalize(err), exc_info=True)
|
|||
|
else:
|
|||
|
log.error(normalize(err))
|
|||
|
sys.exit(1)
|
|||
|
|
|||
|
sys.exit(0)
|
|||
|
|
|||
|
if __name__ == '__main__':
|
|||
|
#Fix #18701
|
|||
|
reload(sys)
|
|||
|
sys.setdefaultencoding('UTF8')
|
|||
|
main()
|
|||
|
|