memory (params): fixes loading user value and building params XML for jids_list type

This commit is contained in:
souliane 2015-12-15 17:43:36 +01:00
commit 4d3d6fb8b8

View file

@ -371,23 +371,25 @@ class Params(object):
if node.getAttribute('type') == 'int':
return int(value_to_use)
elif node.getAttribute('type') == 'list':
assert not value_to_use # only accept <option selected...> and not <param value...>
options = [option for option in node.childNodes if option.nodeName == 'option']
selected = [option for option in options if option.getAttribute('selected') == 'true']
cat, param = node.parentNode.getAttribute('name'), node.getAttribute('name')
if len(selected) == 1:
value_to_use = selected[0].getAttribute('value')
log.info(_("Unset parameter (%(cat)s, %(param)s) of type list will use the default option '%(value)s'") %
{'cat': cat, 'param': param, 'value': value_to_use})
return value_to_use
if len(selected) == 0:
log.error(_(u'Parameter (%(cat)s, %(param)s) of type list has no default option!') % {'cat': cat, 'param': param})
else:
log.error(_(u'Parameter (%(cat)s, %(param)s) of type list has more than one default option!') % {'cat': cat, 'param': param})
raise exceptions.DataError
if not value_to_use: # no user defined value, take default value from the XML
options = [option for option in node.childNodes if option.nodeName == 'option']
selected = [option for option in options if option.getAttribute('selected') == 'true']
cat, param = node.parentNode.getAttribute('name'), node.getAttribute('name')
if len(selected) == 1:
value_to_use = selected[0].getAttribute('value')
log.info(_("Unset parameter (%(cat)s, %(param)s) of type list will use the default option '%(value)s'") %
{'cat': cat, 'param': param, 'value': value_to_use})
return value_to_use
if len(selected) == 0:
log.error(_(u'Parameter (%(cat)s, %(param)s) of type list has no default option!') % {'cat': cat, 'param': param})
else:
log.error(_(u'Parameter (%(cat)s, %(param)s) of type list has more than one default option!') % {'cat': cat, 'param': param})
raise exceptions.DataError
elif node.getAttribute('type') == 'jids_list':
assert not value_to_use # only accept <jid>...</jid> and not <param value...>
jids = [getText(jid_) for jid_ in node.getElementsByTagName("jid")]
if value_to_use:
jids = value_to_use.split('\t') # FIXME: it's not good to use tabs as separator !
else: # no user defined value, take default value from the XML
jids = [getText(jid_) for jid_ in node.getElementsByTagName("jid")]
to_delete = []
for idx, value in enumerate(jids):
try:
@ -689,15 +691,17 @@ class Params(object):
except NotFoundErr:
pass
elif dest_params[name].getAttribute('type') == 'jids_list':
jids = [getText(jid_) for jid_ in dest_params[name].getElementsByTagName("jid")]
for jid_ in jids:
jids = profile_value.split('\t')
for jid_elt in dest_params[name].getElementsByTagName("jid"):
dest_params[name].removeChild(jid_elt) # remove all default
for jid_ in jids: # rebuilt the children with use values
try:
jid.JID(jid_)
except (RuntimeError, jid.InvalidFormat, AttributeError):
log.warning(u"Incorrect jid value found in jids list: [{}]".format(jid_))
else:
jid_elt = prof_xml.createElement('jid')
jid_elt.appendChild(prof_xml.createTextNode())
jid_elt.appendChild(prof_xml.createTextNode(jid_))
dest_params[name].appendChild(jid_elt)
else:
dest_params[name].setAttribute('value', profile_value)