Mercurial > libervia-backend
diff sat_frontends/tools/xmlui.py @ 2624:56f94936df1e
code style reformatting using black
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 27 Jun 2018 20:14:46 +0200 |
parents | 9446f1ea9eac |
children | c274201cea94 |
line wrap: on
line diff
--- a/sat_frontends/tools/xmlui.py Wed Jun 27 07:51:29 2018 +0200 +++ b/sat_frontends/tools/xmlui.py Wed Jun 27 20:14:46 2018 +0200 @@ -19,15 +19,17 @@ 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 import exceptions class_map = {} -CLASS_PANEL = 'panel' -CLASS_DIALOG = 'dialog' -CURRENT_LABEL = 'current_label' +CLASS_PANEL = "panel" +CLASS_DIALOG = "dialog" +CURRENT_LABEL = "current_label" + class InvalidXMLUI(Exception): pass @@ -53,31 +55,37 @@ class Widget(object): """base Widget""" + pass class EmptyWidget(Widget): """Just a placeholder widget""" + pass class TextWidget(Widget): """Non interactive text""" + pass class LabelWidget(Widget): """Non interactive text""" + pass class JidWidget(Widget): """Jabber ID""" + pass class DividerWidget(Widget): """Separator""" + pass @@ -86,6 +94,7 @@ often called Edit in toolkits """ + pass @@ -94,11 +103,13 @@ often called Edit in toolkits """ + pass class PasswordWidget(Widget): """Input widget with require a masked string""" + pass @@ -106,6 +117,7 @@ """Input widget with require a long, possibly multilines string often called TextArea in toolkits """ + pass @@ -113,26 +125,31 @@ """Input widget with require a boolean value often called CheckBox in toolkits """ + pass class IntWidget(Widget): """Input widget with require an integer""" + pass class ButtonWidget(Widget): """A clickable widget""" + pass class ListWidget(Widget): """A widget able to show/choose one or several strings in a list""" + pass class JidsListWidget(Widget): """A widget able to show/choose one or several strings in a list""" + pass @@ -152,11 +169,13 @@ class PairsContainer(Container): """Widgets are disposed in rows of two (usually label/input)""" + pass class LabelContainer(Container): """Widgets are associated with label or empty widget""" + pass @@ -165,15 +184,19 @@ Often called Notebook in toolkits """ + pass + class VerticalContainer(Container): """Widgets are disposed vertically""" + pass class AdvancedListContainer(Container): """Widgets are disposed in rows with advaned features""" + pass @@ -206,11 +229,13 @@ class MessageDialog(Dialog): """Dialog with a OK/Cancel type configuration""" + pass class NoteDialog(Dialog): """Short message which doesn't need user confirmation to disappear""" + pass @@ -226,6 +251,7 @@ class FileDialog(Dialog): """Dialog with a OK/Cancel type configuration""" + pass @@ -235,7 +261,15 @@ This class must not be instancied directly """ - def __init__(self, host, parsed_dom, title=None, flags=None, callback=None, profile=C.PROF_KEY_NONE): + def __init__( + self, + host, + parsed_dom, + title=None, + flags=None, + callback=None, + profile=C.PROF_KEY_NONE, + ): """Initialise the XMLUI instance @param host: %(doc_host)s @@ -250,7 +284,7 @@ dialog closing or new xmlui to display, or other action (you can call host.actionManager) """ self.host = host - top=parsed_dom.documentElement + top = parsed_dom.documentElement self.session_id = top.getAttribute("session_id") or None self.submit_id = top.getAttribute("submit") or None self.xmlui_title = title or top.getAttribute("title") or u"" @@ -298,13 +332,17 @@ if self.submit_id is None: raise ValueError("Can't submit is self.submit_id is not set") if "session_id" in data: - raise ValueError("session_id must no be used in data, it is automaticaly filled with self.session_id if present") + raise ValueError( + "session_id must no be used in data, it is automaticaly filled with self.session_id if present" + ) if self.session_id is not None: data["session_id"] = self.session_id self._xmluiLaunchAction(self.submit_id, data) def _xmluiLaunchAction(self, action_id, data): - self.host.launchAction(action_id, data, callback=self.callback, profile=self.profile) + self.host.launchAction( + action_id, data, callback=self.callback, profile=self.profile + ) def _xmluiClose(self): """Close the window/popup/... where the constructor XMLUI is @@ -317,7 +355,7 @@ class ValueGetter(object): """dict like object which return values of widgets""" - def __init__(self, widgets, attr='value'): + def __init__(self, widgets, attr="value"): self.attr = attr self.widgets = widgets @@ -338,18 +376,31 @@ @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, whitelist=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 """ - super(XMLUIPanel, self).__init__(host, parsed_dom, title=title, flags=flags, callback=callback, profile=profile) + super(XMLUIPanel, self).__init__( + host, parsed_dom, title=title, flags=flags, callback=callback, profile=profile + ) self.ctrl_list = {} # input widget, used mainly for forms - self.widgets = {} # allow to access any named widgets + self.widgets = {} # allow to access any named widgets self.widget_value = ValueGetter(self.widgets) self._main_cont = None if ignore is None: @@ -357,7 +408,9 @@ self._ignore = ignore if whitelist is not None: if ignore: - raise exceptions.InternalError('ignore and whitelist must not be used at the same time') + raise exceptions.InternalError( + "ignore and whitelist must not be used at the same time" + ) self._whitelist = whitelist else: self._whitelist = None @@ -377,7 +430,7 @@ raise ValueError(_("XMLUI can have only one main container")) self._main_cont = value - def _parseChilds(self, _xmlui_parent, current_node, wanted = ('container',), data = None): + def _parseChilds(self, _xmlui_parent, current_node, wanted=("container",), data=None): """Recursively parse childNodes of an element @param _xmlui_parent: widget container with '_xmluiAppend' method @@ -389,82 +442,99 @@ if data is None: data = {} if wanted and not node.nodeName in wanted: - raise InvalidXMLUI('Unexpected node: [%s]' % node.nodeName) + raise InvalidXMLUI("Unexpected node: [%s]" % node.nodeName) if node.nodeName == "container": - type_ = node.getAttribute('type') - if _xmlui_parent is self and type_ != 'vertical': + type_ = node.getAttribute("type") + if _xmlui_parent is self and type_ != "vertical": # main container is not a VerticalContainer and we want one, so we create one to wrap it _xmlui_parent = self.widget_factory.createVerticalContainer(self) self.main_cont = _xmlui_parent if type_ == "tabs": cont = self.widget_factory.createTabsContainer(_xmlui_parent) - self._parseChilds(_xmlui_parent, node, ('tab',), {'tabs_cont': cont}) + self._parseChilds(_xmlui_parent, node, ("tab",), {"tabs_cont": cont}) elif type_ == "vertical": cont = self.widget_factory.createVerticalContainer(_xmlui_parent) - self._parseChilds(cont, node, ('widget', 'container')) + self._parseChilds(cont, node, ("widget", "container")) elif type_ == "pairs": cont = self.widget_factory.createPairsContainer(_xmlui_parent) - self._parseChilds(cont, node, ('widget', 'container')) + self._parseChilds(cont, node, ("widget", "container")) elif type_ == "label": cont = self.widget_factory.createLabelContainer(_xmlui_parent) - self._parseChilds(cont, node, ('widget', 'container'), {CURRENT_LABEL: None}) + self._parseChilds( + cont, node, ("widget", "container"), {CURRENT_LABEL: None} + ) elif type_ == "advanced_list": try: - columns = int(node.getAttribute('columns')) + columns = int(node.getAttribute("columns")) except (TypeError, ValueError): 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 - cont = self.widget_factory.createAdvancedListContainer(_xmlui_parent, columns, selectable) + selectable = node.getAttribute("selectable") or "no" + auto_index = node.getAttribute("auto_index") == C.BOOL_TRUE + data = {"index": 0} if auto_index else None + cont = self.widget_factory.createAdvancedListContainer( + _xmlui_parent, columns, selectable + ) callback_id = node.getAttribute("callback") or None if callback_id is not None: - if selectable == 'no': - raise ValueError("can't have selectable=='no' and callback_id at the same time") + if selectable == "no": + raise ValueError( + "can't have selectable=='no' and callback_id at the same time" + ) cont._xmlui_callback_id = callback_id cont._xmluiOnSelect(self.onAdvListSelect) - self._parseChilds(cont, node, ('row',), data) + self._parseChilds(cont, node, ("row",), data) else: log.warning(_("Unknown container [%s], using default one") % type_) cont = self.widget_factory.createVerticalContainer(_xmlui_parent) - self._parseChilds(cont, node, ('widget', 'container')) + self._parseChilds(cont, node, ("widget", "container")) try: xmluiAppend = _xmlui_parent._xmluiAppend - except (AttributeError, TypeError): # XXX: TypeError is here because pyjamas raise a TypeError instead of an AttributeError + except ( + AttributeError, + TypeError, + ): # XXX: TypeError is here because pyjamas raise a TypeError instead of an AttributeError if _xmlui_parent is self: self.main_cont = cont else: - raise Exception(_("Internal Error, container has not _xmluiAppend method")) + raise Exception( + _("Internal Error, container has not _xmluiAppend method") + ) else: xmluiAppend(cont) - elif node.nodeName == 'tab': - name = node.getAttribute('name') - label = node.getAttribute('label') - selected = C.bool(node.getAttribute('selected') or C.BOOL_FALSE) - if not name or not 'tabs_cont' in data: + elif node.nodeName == "tab": + name = node.getAttribute("name") + label = node.getAttribute("label") + selected = C.bool(node.getAttribute("selected") or C.BOOL_FALSE) + if not name or not "tabs_cont" in data: raise InvalidXMLUI - if self.type == 'param': - self._current_category = name #XXX: awful hack because params need category and we don't keep parent - tab_cont = data['tabs_cont'] + if self.type == "param": + self._current_category = ( + name + ) # XXX: awful hack because params need category and we don't keep parent + tab_cont = data["tabs_cont"] new_tab = tab_cont._xmluiAddTab(label or name, selected) - self._parseChilds(new_tab, node, ('widget', 'container')) + self._parseChilds(new_tab, node, ("widget", "container")) - elif node.nodeName == 'row': + elif node.nodeName == "row": try: - index = str(data['index']) + index = str(data["index"]) except KeyError: - index = node.getAttribute('index') or None + index = node.getAttribute("index") or None else: - data['index'] += 1 + data["index"] += 1 _xmlui_parent._xmluiAddRow(index) - self._parseChilds(_xmlui_parent, node, ('widget', 'container')) + self._parseChilds(_xmlui_parent, node, ("widget", "container")) elif node.nodeName == "widget": name = node.getAttribute("name") - if name and (name in self._ignore or self._whitelist is not None and name not in self._whitelist): + 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 @@ -475,79 +545,127 @@ if value_elt is not None: value = getText(value_elt) else: - value = node.getAttribute("value") if node.hasAttribute('value') else u'' - if type_=="empty": + value = ( + node.getAttribute("value") if node.hasAttribute("value") else u"" + ) + if type_ == "empty": ctrl = self.widget_factory.createEmptyWidget(_xmlui_parent) if CURRENT_LABEL in data: data[CURRENT_LABEL] = None - elif type_=="text": + elif type_ == "text": ctrl = self.widget_factory.createTextWidget(_xmlui_parent, value) - elif type_=="label": + elif type_ == "label": ctrl = self.widget_factory.createLabelWidget(_xmlui_parent, value) data[CURRENT_LABEL] = ctrl - elif type_=="jid": + elif type_ == "jid": ctrl = self.widget_factory.createJidWidget(_xmlui_parent, value) - elif type_=="divider": - style = node.getAttribute("style") or 'line' + elif type_ == "divider": + style = node.getAttribute("style") or "line" ctrl = self.widget_factory.createDividerWidget(_xmlui_parent, style) - elif type_=="string": - ctrl = self.widget_factory.createStringWidget(_xmlui_parent, value, self._isAttrSet("read_only", node)) - self.ctrl_list[name] = ({'type':type_, 'control':ctrl}) - elif type_=="jid_input": - ctrl = self.widget_factory.createJidInputWidget(_xmlui_parent, value, self._isAttrSet("read_only", node)) - self.ctrl_list[name] = ({'type':type_, 'control':ctrl}) - elif type_=="password": - ctrl = self.widget_factory.createPasswordWidget(_xmlui_parent, value, self._isAttrSet("read_only", node)) - self.ctrl_list[name] = ({'type':type_, 'control':ctrl}) - elif type_=="textbox": - ctrl = self.widget_factory.createTextBoxWidget(_xmlui_parent, value, self._isAttrSet("read_only", node)) - self.ctrl_list[name] = ({'type':type_, 'control':ctrl}) - elif type_=="bool": - ctrl = self.widget_factory.createBoolWidget(_xmlui_parent, value==C.BOOL_TRUE, self._isAttrSet("read_only", node)) - self.ctrl_list[name] = ({'type':type_, 'control':ctrl}) + elif type_ == "string": + ctrl = self.widget_factory.createStringWidget( + _xmlui_parent, value, self._isAttrSet("read_only", node) + ) + self.ctrl_list[name] = {"type": type_, "control": ctrl} + elif type_ == "jid_input": + ctrl = self.widget_factory.createJidInputWidget( + _xmlui_parent, value, self._isAttrSet("read_only", node) + ) + self.ctrl_list[name] = {"type": type_, "control": ctrl} + elif type_ == "password": + ctrl = self.widget_factory.createPasswordWidget( + _xmlui_parent, value, self._isAttrSet("read_only", node) + ) + self.ctrl_list[name] = {"type": type_, "control": ctrl} + elif type_ == "textbox": + ctrl = self.widget_factory.createTextBoxWidget( + _xmlui_parent, value, self._isAttrSet("read_only", node) + ) + self.ctrl_list[name] = {"type": type_, "control": ctrl} + elif type_ == "bool": + ctrl = self.widget_factory.createBoolWidget( + _xmlui_parent, + value == C.BOOL_TRUE, + self._isAttrSet("read_only", node), + ) + self.ctrl_list[name] = {"type": type_, "control": ctrl} elif type_ == "int": - ctrl = self.widget_factory.createIntWidget(_xmlui_parent, value, self._isAttrSet("read_only", node)) - self.ctrl_list[name] = ({'type':type_, 'control':ctrl}) + ctrl = self.widget_factory.createIntWidget( + _xmlui_parent, value, self._isAttrSet("read_only", node) + ) + self.ctrl_list[name] = {"type": type_, "control": ctrl} elif type_ == "list": - style = [] if node.getAttribute("multi") == 'yes' else ['single'] - for attr in (u'noselect', u'extensible', u'reducible', u'inline'): - if node.getAttribute(attr) == 'yes': + style = [] if node.getAttribute("multi") == "yes" else ["single"] + for attr in (u"noselect", u"extensible", u"reducible", u"inline"): + if node.getAttribute(attr) == "yes": style.append(attr) - _options = [(option.getAttribute("value"), option.getAttribute("label")) for option in node.getElementsByTagName("option")] - _selected = [option.getAttribute("value") for option in node.getElementsByTagName("option") if option.getAttribute('selected') == C.BOOL_TRUE] - ctrl = self.widget_factory.createListWidget(_xmlui_parent, _options, _selected, style) - self.ctrl_list[name] = ({'type': type_, 'control': ctrl}) + _options = [ + (option.getAttribute("value"), option.getAttribute("label")) + for option in node.getElementsByTagName("option") + ] + _selected = [ + option.getAttribute("value") + for option in node.getElementsByTagName("option") + if option.getAttribute("selected") == C.BOOL_TRUE + ] + ctrl = self.widget_factory.createListWidget( + _xmlui_parent, _options, _selected, style + ) + self.ctrl_list[name] = {"type": type_, "control": ctrl} elif type_ == "jids_list": style = [] jids = [getText(jid_) for jid_ in node.getElementsByTagName("jid")] - ctrl = self.widget_factory.createJidsListWidget(_xmlui_parent, jids, style) - self.ctrl_list[name] = ({'type': type_, 'control': ctrl}) - elif type_=="button": + ctrl = self.widget_factory.createJidsListWidget( + _xmlui_parent, jids, style + ) + self.ctrl_list[name] = {"type": type_, "control": ctrl} + elif type_ == "button": callback_id = node.getAttribute("callback") - ctrl = self.widget_factory.createButtonWidget(_xmlui_parent, value, self.onButtonPress) - ctrl._xmlui_param_id = (callback_id, [field.getAttribute('name') for field in node.getElementsByTagName("field_back")]) + ctrl = self.widget_factory.createButtonWidget( + _xmlui_parent, value, self.onButtonPress + ) + ctrl._xmlui_param_id = ( + callback_id, + [ + field.getAttribute("name") + for field in node.getElementsByTagName("field_back") + ], + ) else: - log.error(_("FIXME FIXME FIXME: widget type [%s] is not implemented") % type_) - raise NotImplementedError(_("FIXME FIXME FIXME: type [%s] is not implemented") % type_) + log.error( + _("FIXME FIXME FIXME: widget type [%s] is not implemented") + % type_ + ) + raise NotImplementedError( + _("FIXME FIXME FIXME: type [%s] is not implemented") % type_ + ) if name: self.widgets[name] = ctrl - if self.type == 'param' and type_ not in ('text', 'button'): + if self.type == "param" and type_ not in ("text", "button"): try: ctrl._xmluiOnChange(self.onParamChange) ctrl._param_category = self._current_category - except (AttributeError, TypeError): # XXX: TypeError is here because pyjamas raise a TypeError instead of an AttributeError - if not isinstance(ctrl, (EmptyWidget, TextWidget, LabelWidget, JidWidget)): + except ( + AttributeError, + TypeError, + ): # XXX: TypeError is here because pyjamas raise a TypeError instead of an AttributeError + if not isinstance( + ctrl, (EmptyWidget, TextWidget, LabelWidget, JidWidget) + ): log.warning(_("No change listener on [%s]") % ctrl) - elif type_ != 'text': + elif type_ != "text": callback = node.getAttribute("internal_callback") or None if callback: - fields = [field.getAttribute('name') for field in node.getElementsByTagName("internal_field")] + fields = [ + field.getAttribute("name") + for field in node.getElementsByTagName("internal_field") + ] cb_data = self.getInternalCallbackData(callback, node) ctrl._xmlui_param_internal = (callback, fields, cb_data) - if type_ == 'button': + if type_ == "button": ctrl._xmluiOnClick(self.onChangeInternal) else: ctrl._xmluiOnChange(self.onChangeInternal) @@ -560,7 +678,7 @@ data.pop(CURRENT_LABEL)._xmlui_for_name = name else: - raise NotImplementedError(_('Unknown tag [%s]') % node.nodeName) + raise NotImplementedError(_("Unknown tag [%s]") % node.nodeName) def constructUI(self, parsed_dom, post_treat=None): """Actually construct the UI @@ -569,12 +687,17 @@ @param post_treat: frontend specific treatments to do once the UI is constructed @return: constructed widget """ - top=parsed_dom.documentElement + top = parsed_dom.documentElement self.type = top.getAttribute("type") - if top.nodeName != "sat_xmlui" or not self.type in ['form', 'param', 'window', 'popup']: + if top.nodeName != "sat_xmlui" or not self.type in [ + "form", + "param", + "window", + "popup", + ]: raise InvalidXMLUI - if self.type == 'param': + if self.type == "param": self.param_changed = set() self._parseChilds(self, parsed_dom.documentElement) @@ -603,11 +726,14 @@ name = self.escape(wid._xmlui_name) value = wid._xmluiGetValue() data[name] = value - except (AttributeError, TypeError): # XXX: TypeError is here because pyjamas raise a TypeError instead of an AttributeError + except ( + AttributeError, + TypeError, + ): # XXX: TypeError is here because pyjamas raise a TypeError instead of an AttributeError pass idx = ctrl._xmluiGetSelectedIndex() if idx is not None: - data['index'] = idx + data["index"] = idx callback_id = ctrl._xmlui_callback_id if callback_id is None: log.info(_("No callback_id found")) @@ -627,10 +753,10 @@ for field in fields: escaped = self.escape(field) ctrl = self.ctrl_list[field] - if isinstance(ctrl['control'], ListWidget): - data[escaped] = u'\t'.join(ctrl['control']._xmluiGetSelectedValues()) + if isinstance(ctrl["control"], ListWidget): + data[escaped] = u"\t".join(ctrl["control"]._xmluiGetSelectedValues()) else: - data[escaped] = ctrl['control']._xmluiGetValue() + data[escaped] = ctrl["control"]._xmluiGetValue() self._xmluiLaunchAction(callback_id, data) def onChangeInternal(self, ctrl): @@ -641,8 +767,10 @@ @param ctrl: widget modified """ action, fields, data = ctrl._xmlui_param_internal - if action not in ('copy', 'move', 'groups_of_contact'): - raise NotImplementedError(_("FIXME: XMLUI internal action [%s] is not implemented") % action) + if action not in ("copy", "move", "groups_of_contact"): + raise NotImplementedError( + _("FIXME: XMLUI internal action [%s] is not implemented") % action + ) def copy_move(source, target): """Depending of 'action' value, copy or move from source to target.""" @@ -651,18 +779,18 @@ values = source._xmluiGetSelectedValues() else: values = [source._xmluiGetValue()] - if action == 'move': - source._xmluiSetValue('') + if action == "move": + source._xmluiSetValue("") values = [value for value in values if value] if values: target._xmluiAddValues(values, select=True) else: if isinstance(source, ListWidget): - value = u', '.join(source._xmluiGetSelectedValues()) + value = u", ".join(source._xmluiGetSelectedValues()) else: value = source._xmluiGetValue() - if action == 'move': - source._xmluiSetValue('') + if action == "move": + source._xmluiSetValue("") target._xmluiSetValue(value) def groups_of_contact(source, target): @@ -678,13 +806,13 @@ source = None for field in fields: - widget = self.ctrl_list[field]['control'] + widget = self.ctrl_list[field]["control"] if not source: source = widget continue - if action in ('copy', 'move'): + if action in ("copy", "move"): copy_move(source, widget) - elif action == 'groups_of_contact': + elif action == "groups_of_contact": groups_of_contact(source, widget) source = None @@ -700,16 +828,18 @@ # extract any kind of data structure from the 'internal_data' element. try: # data is stored in the first 'internal_data' element of the node - data_elts = node.getElementsByTagName('internal_data')[0].childNodes + data_elts = node.getElementsByTagName("internal_data")[0].childNodes except IndexError: return None data = {} - if action == 'groups_of_contact': # return a dict(key: string, value: list[string]) + if ( + action == "groups_of_contact" + ): # return a dict(key: string, value: list[string]) for elt in data_elts: - jid_s = elt.getAttribute('name') + jid_s = elt.getAttribute("name") data[jid_s] = [] for value_elt in elt.childNodes: - data[jid_s].append(value_elt.getAttribute('name')) + data[jid_s].append(value_elt.getAttribute("name")) return data def onFormSubmitted(self, ignore=None): @@ -721,15 +851,19 @@ for ctrl_name in self.ctrl_list: escaped = self.escape(ctrl_name) ctrl = self.ctrl_list[ctrl_name] - if isinstance(ctrl['control'], ListWidget): - selected_values.append((escaped, u'\t'.join(ctrl['control']._xmluiGetSelectedValues()))) + if isinstance(ctrl["control"], ListWidget): + selected_values.append( + (escaped, u"\t".join(ctrl["control"]._xmluiGetSelectedValues())) + ) else: - selected_values.append((escaped, ctrl['control']._xmluiGetValue())) + selected_values.append((escaped, ctrl["control"]._xmluiGetValue())) if self.submit_id is not None: data = dict(selected_values) self.submit(data) else: - log.warning(_("The form data is not sent back, the type is not managed properly")) + log.warning( + _("The form data is not sent back, the type is not managed properly") + ) self._xmluiClose() def onFormCancelled(self, ignore=None): @@ -739,7 +873,9 @@ data = {C.XMLUI_DATA_CANCELLED: C.BOOL_TRUE} self.submit(data) else: - log.warning(_("The form data is not sent back, the type is not managed properly")) + log.warning( + _("The form data is not sent back, the type is not managed properly") + ) self._xmluiClose() def onSaveParams(self, ignore=None): @@ -747,10 +883,10 @@ self.type must be param """ - assert self.type == 'param' + assert self.type == "param" for ctrl in self.param_changed: if isinstance(ctrl, ListWidget): - value = u'\t'.join(ctrl._xmluiGetSelectedValues()) + value = u"\t".join(ctrl._xmluiGetSelectedValues()) else: value = ctrl._xmluiGetValue() param_name = ctrl._xmlui_name.split(C.SAT_PARAM_SEPARATOR)[1] @@ -765,38 +901,69 @@ class XMLUIDialog(XMLUIBase): dialog_factory = None - def __init__(self, host, parsed_dom, title=None, flags=None, callback=None, ignore=None, whitelist=None, profile=C.PROF_KEY_NONE): - super(XMLUIDialog, self).__init__(host, parsed_dom, title=title, flags=flags, callback=callback, profile=profile) - top=parsed_dom.documentElement - dlg_elt = self._getChildNode(top, "dialog") + def __init__( + self, + host, + parsed_dom, + title=None, + flags=None, + callback=None, + ignore=None, + whitelist=None, + profile=C.PROF_KEY_NONE, + ): + super(XMLUIDialog, self).__init__( + host, parsed_dom, title=title, flags=flags, callback=callback, profile=profile + ) + top = parsed_dom.documentElement + dlg_elt = self._getChildNode(top, "dialog") if dlg_elt is None: raise ValueError("Invalid XMLUI: no Dialog element found !") dlg_type = dlg_elt.getAttribute("type") or C.XMLUI_DIALOG_MESSAGE try: mess_elt = self._getChildNode(dlg_elt, C.XMLUI_DATA_MESS) message = getText(mess_elt) - except (TypeError, AttributeError): # XXX: TypeError is here because pyjamas raise a TypeError instead of an AttributeError + except ( + TypeError, + AttributeError, + ): # XXX: TypeError is here because pyjamas raise a TypeError instead of an AttributeError message = "" level = dlg_elt.getAttribute(C.XMLUI_DATA_LVL) or C.XMLUI_DATA_LVL_INFO if dlg_type == C.XMLUI_DIALOG_MESSAGE: - self.dlg = self.dialog_factory.createMessageDialog(self, self.xmlui_title, message, level) + self.dlg = self.dialog_factory.createMessageDialog( + self, self.xmlui_title, message, level + ) elif dlg_type == C.XMLUI_DIALOG_NOTE: - self.dlg = self.dialog_factory.createNoteDialog(self, self.xmlui_title, message, level) + self.dlg = self.dialog_factory.createNoteDialog( + self, self.xmlui_title, message, level + ) elif dlg_type == C.XMLUI_DIALOG_CONFIRM: try: buttons_elt = self._getChildNode(dlg_elt, "buttons") - buttons_set = buttons_elt.getAttribute("set") or C.XMLUI_DATA_BTNS_SET_DEFAULT - except (TypeError, AttributeError): # XXX: TypeError is here because pyjamas raise a TypeError instead of an AttributeError + buttons_set = ( + buttons_elt.getAttribute("set") or C.XMLUI_DATA_BTNS_SET_DEFAULT + ) + except ( + TypeError, + AttributeError, + ): # XXX: TypeError is here because pyjamas raise a TypeError instead of an AttributeError buttons_set = C.XMLUI_DATA_BTNS_SET_DEFAULT - self.dlg = self.dialog_factory.createConfirmDialog(self, self.xmlui_title, message, level, buttons_set) + self.dlg = self.dialog_factory.createConfirmDialog( + self, self.xmlui_title, message, level, buttons_set + ) elif dlg_type == C.XMLUI_DIALOG_FILE: try: file_elt = self._getChildNode(dlg_elt, "file") filetype = file_elt.getAttribute("type") or C.XMLUI_DATA_FILETYPE_DEFAULT - except (TypeError, AttributeError): # XXX: TypeError is here because pyjamas raise a TypeError instead of an AttributeError + except ( + TypeError, + AttributeError, + ): # XXX: TypeError is here because pyjamas raise a TypeError instead of an AttributeError filetype = C.XMLUI_DATA_FILETYPE_DEFAULT - self.dlg = self.dialog_factory.createFileDialog(self, self.xmlui_title, message, level, filetype) + self.dlg = self.dialog_factory.createFileDialog( + self, self.xmlui_title, message, level, filetype + ) else: raise ValueError("Unknown dialog type [%s]" % dlg_type) @@ -819,7 +986,18 @@ class_map[type_] = class_ -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): +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 @@ -831,13 +1009,14 @@ """ if dom_parse is None: from xml.dom import minidom - dom_parse = lambda xml_data: minidom.parseString(xml_data.encode('utf-8')) + + dom_parse = lambda xml_data: minidom.parseString(xml_data.encode("utf-8")) dom_free = lambda parsed_dom: parsed_dom.unlink() else: dom_parse = dom_parse dom_free = dom_free or (lambda parsed_dom: None) parsed_dom = dom_parse(xml_data) - top=parsed_dom.documentElement + top = parsed_dom.documentElement ui_type = top.getAttribute("type") try: if ui_type != C.XMLUI_DIALOG: @@ -845,14 +1024,19 @@ else: cls = class_map[CLASS_DIALOG] except KeyError: - raise ClassNotRegistedError(_("You must register classes with registerClass before creating a XMLUI")) + raise ClassNotRegistedError( + _("You must register classes with registerClass before creating a XMLUI") + ) - xmlui = cls(host, parsed_dom, - title = title, - flags = flags, - callback = callback, - ignore = ignore, - whitelist = whitelist, - profile = profile) + xmlui = cls( + host, + parsed_dom, + title=title, + flags=flags, + callback=callback, + ignore=ignore, + whitelist=whitelist, + profile=profile, + ) dom_free(parsed_dom) return xmlui