comparison src/memory/params.py @ 1747:40b7f18ac704

memory (params): fixes loading user value and building params XML for jids_list type
author souliane <souliane@mailoo.org>
date Tue, 15 Dec 2015 17:43:36 +0100
parents 5ca3caefcf98
children d17772b0fe22
comparison
equal deleted inserted replaced
1746:0dfabe746ec4 1747:40b7f18ac704
369 if node.getAttribute('type') == 'bool': 369 if node.getAttribute('type') == 'bool':
370 return C.bool(value_to_use) 370 return C.bool(value_to_use)
371 if node.getAttribute('type') == 'int': 371 if node.getAttribute('type') == 'int':
372 return int(value_to_use) 372 return int(value_to_use)
373 elif node.getAttribute('type') == 'list': 373 elif node.getAttribute('type') == 'list':
374 assert not value_to_use # only accept <option selected...> and not <param value...> 374 if not value_to_use: # no user defined value, take default value from the XML
375 options = [option for option in node.childNodes if option.nodeName == 'option'] 375 options = [option for option in node.childNodes if option.nodeName == 'option']
376 selected = [option for option in options if option.getAttribute('selected') == 'true'] 376 selected = [option for option in options if option.getAttribute('selected') == 'true']
377 cat, param = node.parentNode.getAttribute('name'), node.getAttribute('name') 377 cat, param = node.parentNode.getAttribute('name'), node.getAttribute('name')
378 if len(selected) == 1: 378 if len(selected) == 1:
379 value_to_use = selected[0].getAttribute('value') 379 value_to_use = selected[0].getAttribute('value')
380 log.info(_("Unset parameter (%(cat)s, %(param)s) of type list will use the default option '%(value)s'") % 380 log.info(_("Unset parameter (%(cat)s, %(param)s) of type list will use the default option '%(value)s'") %
381 {'cat': cat, 'param': param, 'value': value_to_use}) 381 {'cat': cat, 'param': param, 'value': value_to_use})
382 return value_to_use 382 return value_to_use
383 if len(selected) == 0: 383 if len(selected) == 0:
384 log.error(_(u'Parameter (%(cat)s, %(param)s) of type list has no default option!') % {'cat': cat, 'param': param}) 384 log.error(_(u'Parameter (%(cat)s, %(param)s) of type list has no default option!') % {'cat': cat, 'param': param})
385 else: 385 else:
386 log.error(_(u'Parameter (%(cat)s, %(param)s) of type list has more than one default option!') % {'cat': cat, 'param': param}) 386 log.error(_(u'Parameter (%(cat)s, %(param)s) of type list has more than one default option!') % {'cat': cat, 'param': param})
387 raise exceptions.DataError 387 raise exceptions.DataError
388 elif node.getAttribute('type') == 'jids_list': 388 elif node.getAttribute('type') == 'jids_list':
389 assert not value_to_use # only accept <jid>...</jid> and not <param value...> 389 if value_to_use:
390 jids = [getText(jid_) for jid_ in node.getElementsByTagName("jid")] 390 jids = value_to_use.split('\t') # FIXME: it's not good to use tabs as separator !
391 else: # no user defined value, take default value from the XML
392 jids = [getText(jid_) for jid_ in node.getElementsByTagName("jid")]
391 to_delete = [] 393 to_delete = []
392 for idx, value in enumerate(jids): 394 for idx, value in enumerate(jids):
393 try: 395 try:
394 jids[idx] = jid.JID(value) 396 jids[idx] = jid.JID(value)
395 except (RuntimeError, jid.InvalidFormat, AttributeError): 397 except (RuntimeError, jid.InvalidFormat, AttributeError):
687 try: 689 try:
688 option.removeAttribute('selected') 690 option.removeAttribute('selected')
689 except NotFoundErr: 691 except NotFoundErr:
690 pass 692 pass
691 elif dest_params[name].getAttribute('type') == 'jids_list': 693 elif dest_params[name].getAttribute('type') == 'jids_list':
692 jids = [getText(jid_) for jid_ in dest_params[name].getElementsByTagName("jid")] 694 jids = profile_value.split('\t')
693 for jid_ in jids: 695 for jid_elt in dest_params[name].getElementsByTagName("jid"):
696 dest_params[name].removeChild(jid_elt) # remove all default
697 for jid_ in jids: # rebuilt the children with use values
694 try: 698 try:
695 jid.JID(jid_) 699 jid.JID(jid_)
696 except (RuntimeError, jid.InvalidFormat, AttributeError): 700 except (RuntimeError, jid.InvalidFormat, AttributeError):
697 log.warning(u"Incorrect jid value found in jids list: [{}]".format(jid_)) 701 log.warning(u"Incorrect jid value found in jids list: [{}]".format(jid_))
698 else: 702 else:
699 jid_elt = prof_xml.createElement('jid') 703 jid_elt = prof_xml.createElement('jid')
700 jid_elt.appendChild(prof_xml.createTextNode()) 704 jid_elt.appendChild(prof_xml.createTextNode(jid_))
701 dest_params[name].appendChild(jid_elt) 705 dest_params[name].appendChild(jid_elt)
702 else: 706 else:
703 dest_params[name].setAttribute('value', profile_value) 707 dest_params[name].setAttribute('value', profile_value)
704 if new_node: 708 if new_node:
705 prof_xml.documentElement.appendChild(dest_cat) 709 prof_xml.documentElement.appendChild(dest_cat)