comparison libervia/frontends/tools/xmlui.py @ 4270:0d7bb4df2343

Reformatted code base using black.
author Goffi <goffi@goffi.org>
date Wed, 19 Jun 2024 18:44:57 +0200
parents 26b7ed2817da
children
comparison
equal deleted inserted replaced
4269:64a85ce8be70 4270:0d7bb4df2343
270 """Base class to construct SàT XML User Interface 270 """Base class to construct SàT XML User Interface
271 271
272 This class must not be instancied directly 272 This class must not be instancied directly
273 """ 273 """
274 274
275 def __init__(self, host, parsed_dom, title=None, flags=None, callback=None, 275 def __init__(
276 profile=C.PROF_KEY_NONE): 276 self,
277 host,
278 parsed_dom,
279 title=None,
280 flags=None,
281 callback=None,
282 profile=C.PROF_KEY_NONE,
283 ):
277 """Initialise the XMLUI instance 284 """Initialise the XMLUI instance
278 285
279 @param host: %(doc_host)s 286 @param host: %(doc_host)s
280 @param parsed_dom: main parsed dom 287 @param parsed_dom: main parsed dom
281 @param title: force the title, or use XMLUI one if None 288 @param title: force the title, or use XMLUI one if None
362 raise NotImplementedError 369 raise NotImplementedError
363 370
364 371
365 class ValueGetter(object): 372 class ValueGetter(object):
366 """dict like object which return values of widgets""" 373 """dict like object which return values of widgets"""
374
367 # FIXME: widget which can keep multiple values are not handled 375 # FIXME: widget which can keep multiple values are not handled
368 376
369 def __init__(self, widgets, attr="value"): 377 def __init__(self, widgets, attr="value"):
370 self.attr = attr 378 self.attr = attr
371 self.widgets = widgets 379 self.widgets = widgets
399 @property dialog_factory: factory to create frontend-specific dialogs 407 @property dialog_factory: factory to create frontend-specific dialogs
400 """ 408 """
401 409
402 widget_factory = None 410 widget_factory = None
403 411
404 def __init__(self, host, parsed_dom, title=None, flags=None, callback=None, 412 def __init__(
405 ignore=None, whitelist=None, profile=C.PROF_KEY_NONE): 413 self,
414 host,
415 parsed_dom,
416 title=None,
417 flags=None,
418 callback=None,
419 ignore=None,
420 whitelist=None,
421 profile=C.PROF_KEY_NONE,
422 ):
406 """ 423 """
407 424
408 @param title(unicode, None): title of the 425 @param title(unicode, None): title of the
409 @property widgets(dict): widget name => widget map 426 @property widgets(dict): widget name => widget map
410 @property widget_value(ValueGetter): retrieve widget value from it's name 427 @property widget_value(ValueGetter): retrieve widget value from it's name
447 def main_cont(self, value): 464 def main_cont(self, value):
448 if self._main_cont is not None: 465 if self._main_cont is not None:
449 raise ValueError(_("XMLUI can have only one main container")) 466 raise ValueError(_("XMLUI can have only one main container"))
450 self._main_cont = value 467 self._main_cont = value
451 468
452 def _parse_childs(self, _xmlui_parent, current_node, wanted=("container",), data=None): 469 def _parse_childs(
470 self, _xmlui_parent, current_node, wanted=("container",), data=None
471 ):
453 """Recursively parse childNodes of an element 472 """Recursively parse childNodes of an element
454 473
455 @param _xmlui_parent: widget container with '_xmlui_append' method 474 @param _xmlui_parent: widget container with '_xmlui_append' method
456 @param current_node: element from which childs will be parsed 475 @param current_node: element from which childs will be parsed
457 @param wanted: list of tag names that can be present in the childs to be SàT XMLUI 476 @param wanted: list of tag names that can be present in the childs to be SàT XMLUI
485 self._parse_childs( 504 self._parse_childs(
486 # FIXME: the "None" value for CURRENT_LABEL doesn't seem 505 # FIXME: the "None" value for CURRENT_LABEL doesn't seem
487 # used or even useful, it should probably be removed 506 # used or even useful, it should probably be removed
488 # and all "is not None" tests for it should be removed too 507 # and all "is not None" tests for it should be removed too
489 # to be checked for 0.8 508 # to be checked for 0.8
490 cont, node, ("widget", "container"), {CURRENT_LABEL: None} 509 cont,
510 node,
511 ("widget", "container"),
512 {CURRENT_LABEL: None},
491 ) 513 )
492 elif type_ == "advanced_list": 514 elif type_ == "advanced_list":
493 try: 515 try:
494 columns = int(node.getAttribute("columns")) 516 columns = int(node.getAttribute("columns"))
495 except (TypeError, ValueError): 517 except (TypeError, ValueError):
534 label = node.getAttribute("label") 556 label = node.getAttribute("label")
535 selected = C.bool(node.getAttribute("selected") or C.BOOL_FALSE) 557 selected = C.bool(node.getAttribute("selected") or C.BOOL_FALSE)
536 if not name or not "tabs_cont" in data: 558 if not name or not "tabs_cont" in data:
537 raise InvalidXMLUI 559 raise InvalidXMLUI
538 if self.type == "param": 560 if self.type == "param":
539 self._current_category = ( 561 self._current_category = name # XXX: awful hack because params need category and we don't keep parent
540 name
541 ) # XXX: awful hack because params need category and we don't keep parent
542 tab_cont = data["tabs_cont"] 562 tab_cont = data["tabs_cont"]
543 new_tab = tab_cont._xmlui_add_tab(label or name, selected) 563 new_tab = tab_cont._xmlui_add_tab(label or name, selected)
544 self._parse_childs(new_tab, node, ("widget", "container")) 564 self._parse_childs(new_tab, node, ("widget", "container"))
545 565
546 elif node.nodeName == "row": 566 elif node.nodeName == "row":
584 elif type_ == "label": 604 elif type_ == "label":
585 ctrl = self.widget_factory.createLabelWidget(_xmlui_parent, value) 605 ctrl = self.widget_factory.createLabelWidget(_xmlui_parent, value)
586 data[CURRENT_LABEL] = ctrl 606 data[CURRENT_LABEL] = ctrl
587 elif type_ == "hidden": 607 elif type_ == "hidden":
588 if name in self.hidden: 608 if name in self.hidden:
589 raise exceptions.ConflictError("Conflict on hidden value with " 609 raise exceptions.ConflictError(
590 "name {name}".format(name=name)) 610 "Conflict on hidden value with "
611 "name {name}".format(name=name)
612 )
591 self.hidden[name] = value 613 self.hidden[name] = value
592 continue 614 continue
593 elif type_ == "jid": 615 elif type_ == "jid":
594 ctrl = self.widget_factory.createJidWidget(_xmlui_parent, value) 616 ctrl = self.widget_factory.createJidWidget(_xmlui_parent, value)
595 elif type_ == "divider": 617 elif type_ == "divider":
1088 @param class_: the class to use to instanciate given type 1110 @param class_: the class to use to instanciate given type
1089 """ 1111 """
1090 # TODO: remove this method, as there are seme use cases where different XMLUI 1112 # TODO: remove this method, as there are seme use cases where different XMLUI
1091 # classes can be used in the same frontend, so a global value is not good 1113 # classes can be used in the same frontend, so a global value is not good
1092 assert type_ in (CLASS_PANEL, CLASS_DIALOG) 1114 assert type_ in (CLASS_PANEL, CLASS_DIALOG)
1093 log.warning("register_class for XMLUI is deprecated, please use partial with " 1115 log.warning(
1094 "xmlui.create and class_map instead") 1116 "register_class for XMLUI is deprecated, please use partial with "
1117 "xmlui.create and class_map instead"
1118 )
1095 if type_ in _class_map: 1119 if type_ in _class_map:
1096 log.debug(_("XMLUI class already registered for {type_}, ignoring").format( 1120 log.debug(
1097 type_=type_)) 1121 _("XMLUI class already registered for {type_}, ignoring").format(type_=type_)
1122 )
1098 return 1123 return
1099 1124
1100 _class_map[type_] = class_ 1125 _class_map[type_] = class_
1101 1126
1102 1127
1103 def create(host, xml_data, title=None, flags=None, dom_parse=None, dom_free=None, 1128 def create(
1104 callback=None, ignore=None, whitelist=None, class_map=None, 1129 host,
1105 profile=C.PROF_KEY_NONE): 1130 xml_data,
1131 title=None,
1132 flags=None,
1133 dom_parse=None,
1134 dom_free=None,
1135 callback=None,
1136 ignore=None,
1137 whitelist=None,
1138 class_map=None,
1139 profile=C.PROF_KEY_NONE,
1140 ):
1106 """ 1141 """
1107 @param dom_parse: methode equivalent to minidom.parseString (but which must manage unicode), or None to use default one 1142 @param dom_parse: methode equivalent to minidom.parseString (but which must manage unicode), or None to use default one
1108 @param dom_free: method used to free the parsed DOM 1143 @param dom_free: method used to free the parsed DOM
1109 @param ignore(list[unicode], None): name of widgets to ignore 1144 @param ignore(list[unicode], None): name of widgets to ignore
1110 widgets with name in this list and their label will be ignored 1145 widgets with name in this list and their label will be ignored
1111 @param whitelist(list[unicode], None): name of widgets to keep 1146 @param whitelist(list[unicode], None): name of widgets to keep
1112 when not None, only widgets in this list and their label will be kept 1147 when not None, only widgets in this list and their label will be kept
1113 mutually exclusive with ignore 1148 mutually exclusive with ignore
1114 """ 1149 """
1115 if class_map is None: 1150 if class_map is None:
1116 class_map = _class_map 1151 class_map = _class_map
1117 if dom_parse is None: 1152 if dom_parse is None:
1118 from xml.dom import minidom 1153 from xml.dom import minidom