frontends (xmlui): added whitelist argument:

when set whitelist indicate that only widgets (and their eventual labels) with names in this list are to be kept.
This option is mutually exclusive with ignore.
This commit is contained in:
Goffi 2018-03-29 09:03:19 +02:00
commit 25e6a8748d

View file

@ -21,7 +21,7 @@ from sat.core.i18n import _
from sat.core.log import getLogger
log = getLogger(__name__)
from sat_frontends.quick_frontend.constants import Const as C
from sat.core.exceptions import DataError
from sat.core import exceptions
class_map = {}
@ -271,7 +271,7 @@ class XMLUIBase(object):
self.host.actionManager(data, profile=profile)
def _isAttrSet(self, name, node):
"""Returnw widget boolean attribute status
"""Return widget boolean attribute status
@param name: name of the attribute (e.g. "read_only")
@param node: Node instance
@ -334,14 +334,16 @@ class ValueGetter(object):
class XMLUIPanel(XMLUIBase):
"""XMLUI Panel
New frontends can inherite this class to easily implement XMLUI
New frontends can inherit this class to easily implement XMLUI
@property widget_factory: factory to create frontend-specific widgets
@property dialog_factory: factory to create frontend-specific dialogs
"""
widget_factory = None
def __init__(self, host, parsed_dom, title=None, flags=None, callback=None, ignore=None, profile=C.PROF_KEY_NONE):
def __init__(self, host, parsed_dom, title=None, flags=None, callback=None, ignore=None, whitelist=None, profile=C.PROF_KEY_NONE):
"""
@param title(unicode, None): title of the
@property widgets(dict): widget name => widget map
@property widget_value(ValueGetter): retrieve widget value from it's name
"""
@ -353,6 +355,12 @@ class XMLUIPanel(XMLUIBase):
if ignore is None:
ignore = []
self._ignore = ignore
if whitelist is not None:
if ignore:
raise exceptions.InternalError('ignore and whitelist must not be used at the same time')
self._whitelist = whitelist
else:
self._whitelist = None
self.constructUI(parsed_dom)
def escape(self, name):
@ -405,7 +413,7 @@ class XMLUIPanel(XMLUIBase):
try:
columns = int(node.getAttribute('columns'))
except (TypeError, ValueError):
raise DataError("Invalid columns")
raise exceptions.DataError("Invalid columns")
selectable = node.getAttribute('selectable') or 'no'
auto_index = node.getAttribute('auto_index') == C.BOOL_TRUE
data = {'index': 0} if auto_index else None
@ -456,7 +464,7 @@ class XMLUIPanel(XMLUIBase):
elif node.nodeName == "widget":
name = node.getAttribute("name")
if name in self._ignore:
if name and (name in self._ignore or self._whitelist is not None and name not in self._whitelist):
# current widget is ignored, but there may be already a label
if CURRENT_LABEL in data:
# if so, we remove it from parent
@ -811,12 +819,15 @@ def registerClass(type_, class_):
class_map[type_] = class_
def create(host, xml_data, title=None, flags=None, dom_parse=None, dom_free=None, callback=None, ignore=None, profile=C.PROF_KEY_NONE):
def create(host, xml_data, title=None, flags=None, dom_parse=None, dom_free=None, callback=None, ignore=None, whitelist=None, profile=C.PROF_KEY_NONE):
"""
@param dom_parse: methode equivalent to minidom.parseString (but which must manage unicode), or None to use default one
@param dom_free: method used to free the parsed DOM
@param ignore(list[unicode], None): name of widgets to ignore
widgets with name in this list and their label will be ignored
@param whitelist(list[unicode], None): name of widgets to keep
when not None, only widgets in this list and their label will be kept
mutually exclusive with ignore
"""
if dom_parse is None:
from xml.dom import minidom
@ -841,6 +852,7 @@ def create(host, xml_data, title=None, flags=None, dom_parse=None, dom_free=None
flags = flags,
callback = callback,
ignore = ignore,
whitelist = whitelist,
profile = profile)
dom_free(parsed_dom)
return xmlui