comparison sat/tools/xml_tools.py @ 2625:a55a14c3cbf4

plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 21:15:34 +0200
parents 56f94936df1e
children c274201cea94
comparison
equal deleted inserted replaced
2624:56f94936df1e 2625:a55a14c3cbf4
49 49
50 def _dataFormField2XMLUIData(field, read_only=False): 50 def _dataFormField2XMLUIData(field, read_only=False):
51 """Get data needed to create an XMLUI's Widget from Wokkel's data_form's Field. 51 """Get data needed to create an XMLUI's Widget from Wokkel's data_form's Field.
52 52
53 The attribute field can be modified (if it's fixed and it has no value). 53 The attribute field can be modified (if it's fixed and it has no value).
54 @param field (data_form.Field): a field with attributes "value", "fieldType", "label" and "var" 54 @param field (data_form.Field): a field with attributes "value", "fieldType",
55 "label" and "var"
55 @param read_only (bool): if True and it makes sense, create a read only input widget 56 @param read_only (bool): if True and it makes sense, create a read only input widget
56 @return: a tuple (widget_type, widget_args, widget_kwargs) 57 @return: a tuple (widget_type, widget_args, widget_kwargs)
57 """ 58 """
58 widget_args = [field.value] 59 widget_args = [field.value]
59 widget_kwargs = {} 60 widget_kwargs = {}
747 @param items: list of widgets to add (just the first row) 748 @param items: list of widgets to add (just the first row)
748 @param columns: number of columns in this table, or None to autodetect 749 @param columns: number of columns in this table, or None to autodetect
749 @param selectable: one of: 750 @param selectable: one of:
750 'no': nothing is done 751 'no': nothing is done
751 'single': one row can be selected 752 'single': one row can be selected
752 @param auto_index: if True, indexes will be generated by frontends, starting from 0 753 @param auto_index: if True, indexes will be generated by frontends,
754 starting from 0
753 @return: created element 755 @return: created element
754 """ 756 """
755 assert selectable in ("no", "single") 757 assert selectable in ("no", "single")
756 if not items and columns is None: 758 if not items and columns is None:
757 raise exceptions.DataError(_("either items or columns need do be filled")) 759 raise exceptions.DataError(_("either items or columns need do be filled"))
1047 """ 1049 """
1048 1050
1049 @param xmlui 1051 @param xmlui
1050 @param options (list[option]): each option can be given as: 1052 @param options (list[option]): each option can be given as:
1051 - a single string if the label and the value are the same 1053 - a single string if the label and the value are the same
1052 - a tuple with a couple of string (value,label) if the label and the value differ 1054 - a tuple with a couple of string (value,label) if the label and the
1055 value differ
1053 @param selected (list[string]): list of the selected values 1056 @param selected (list[string]): list of the selected values
1054 @param styles (iterable[string]): flags to set the behaviour of the list 1057 @param styles (iterable[string]): flags to set the behaviour of the list
1055 can be: 1058 can be:
1056 - multi: multiple selection is allowed 1059 - multi: multiple selection is allowed
1057 - noselect: no selection is allowed 1060 - noselect: no selection is allowed
1058 useful when only the list itself is needed 1061 useful when only the list itself is needed
1059 - extensible: can be extended by user (i.e. new options can be added) 1062 - extensible: can be extended by user (i.e. new options can be added)
1060 - reducible: can be reduced by user (i.e. options can be removed) 1063 - reducible: can be reduced by user (i.e. options can be removed)
1061 - inline: hint that this list should be displayed on a single line (e.g. list of labels) 1064 - inline: hint that this list should be displayed on a single line
1065 (e.g. list of labels)
1062 @param name (string) 1066 @param name (string)
1063 @param parent 1067 @param parent
1064 """ 1068 """
1065 styles = set() if styles is None else set(styles) 1069 styles = set() if styles is None else set(styles)
1066 if styles is None: 1070 if styles is None:
1068 else: 1072 else:
1069 styles = set(styles) 1073 styles = set(styles)
1070 if u"noselect" in styles and (u"multi" in styles or selected): 1074 if u"noselect" in styles and (u"multi" in styles or selected):
1071 raise exceptions.DataError( 1075 raise exceptions.DataError(
1072 _( 1076 _(
1073 u'"multi" flag and "selected" option are not compatible with "noselect" flag' 1077 u'"multi" flag and "selected" option are not compatible with '
1078 u'"noselect" flag'
1074 ) 1079 )
1075 ) 1080 )
1076 if not options: 1081 if not options:
1077 # we can have no options if we get a submitted data form 1082 # we can have no options if we get a submitted data form
1078 # but we can't use submitted values directly, 1083 # but we can't use submitted values directly,
1208 1213
1209 1214
1210 class XMLUI(object): 1215 class XMLUI(object):
1211 """This class is used to create a user interface (form/window/parameters/etc) using SàT XML""" 1216 """This class is used to create a user interface (form/window/parameters/etc) using SàT XML"""
1212 1217
1213 def __init__( 1218 def __init__(self, panel_type="window", container="vertical", dialog_opt=None,
1214 self, 1219 title=None, submit_id=None, session_id=None):
1215 panel_type="window",
1216 container="vertical",
1217 dialog_opt=None,
1218 title=None,
1219 submit_id=None,
1220 session_id=None,
1221 ):
1222 """Init SàT XML Panel 1220 """Init SàT XML Panel
1223 1221
1224 @param panel_type: one of 1222 @param panel_type: one of
1225 - C.XMLUI_WINDOW (new window) 1223 - C.XMLUI_WINDOW (new window)
1226 - C.XMLUI_POPUP 1224 - C.XMLUI_POPUP
1227 - C.XMLUI_FORM (form, depend of the frontend, usually a panel with cancel/submit buttons) 1225 - C.XMLUI_FORM (form, depend of the frontend, usually a panel with
1226 cancel/submit buttons)
1228 - C.XMLUI_PARAM (parameters, presentation depend of the frontend) 1227 - C.XMLUI_PARAM (parameters, presentation depend of the frontend)
1229 - C.XMLUI_DIALOG (one common dialog, presentation depend of frontend) 1228 - C.XMLUI_DIALOG (one common dialog, presentation depend of frontend)
1230 @param container: disposition of elements, one of: 1229 @param container: disposition of elements, one of:
1231 - vertical: elements are disposed up to bottom 1230 - vertical: elements are disposed up to bottom
1232 - horizontal: elements are disposed left to right 1231 - horizontal: elements are disposed left to right
1233 - pairs: elements come on two aligned columns 1232 - pairs: elements come on two aligned columns
1234 (usually one for a label, the next for the element) 1233 (usually one for a label, the next for the element)
1235 - label: associations of one LabelWidget or EmptyWidget with an other widget 1234 - label: associations of one LabelWidget or EmptyWidget with an other widget
1236 similar to pairs but specialized in LabelWidget, and not necessarily arranged in 2 columns 1235 similar to pairs but specialized in LabelWidget,
1236 and not necessarily arranged in 2 columns
1237 - tabs: elemens are in categories with tabs (notebook) 1237 - tabs: elemens are in categories with tabs (notebook)
1238 @param dialog_opt: only used if panel_type == C.XMLUI_DIALOG. Dictionnary (string/string) where key can be: 1238 @param dialog_opt: only used if panel_type == C.XMLUI_DIALOG.
1239 Dictionnary (string/string) where key can be:
1239 - C.XMLUI_DATA_TYPE: type of dialog, value can be: 1240 - C.XMLUI_DATA_TYPE: type of dialog, value can be:
1240 - C.XMLUI_DIALOG_MESSAGE (default): an information/error message. Action of user is necessary to close the dialog. Usually the frontend display a classic popup 1241 - C.XMLUI_DIALOG_MESSAGE (default): an information/error message.
1241 - C.XMLUI_DIALOG_NOTE: like a C.XMLUI_DIALOG_MESSAGE, but action of user is not necessary to close, at frontend choice (it can be closed after a timeout). Usually the frontend display as a timed out notification 1242 Action of user is necessary to close the dialog.
1243 Usually the frontend display a classic popup.
1244 - C.XMLUI_DIALOG_NOTE: like a C.XMLUI_DIALOG_MESSAGE, but action of user
1245 is not necessary to close, at frontend choice (it can be closed after
1246 a timeout). Usually the frontend display as a timed out notification
1242 - C.XMLUI_DIALOG_CONFIRM: dialog with 2 choices (usualy "Ok"/"Cancel"). 1247 - C.XMLUI_DIALOG_CONFIRM: dialog with 2 choices (usualy "Ok"/"Cancel").
1243 returned data can contain: 1248 returned data can contain:
1244 - "answer": "true" if answer is "ok", "yes" or equivalent, "false" else 1249 - "answer": "true" if answer is "ok", "yes" or equivalent,
1250 "false" else
1245 - C.XLMUI_DIALOG_FILE: a file selection dialog 1251 - C.XLMUI_DIALOG_FILE: a file selection dialog
1246 returned data can contain: 1252 returned data can contain:
1247 - "cancelled": "true" if dialog has been cancelled, not present or "false" else 1253 - "cancelled": "true" if dialog has been cancelled, not present
1254 or "false" else
1248 - "path": path of the choosed file/dir 1255 - "path": path of the choosed file/dir
1249 - C.XMLUI_DATA_MESS: message shown in dialog 1256 - C.XMLUI_DATA_MESS: message shown in dialog
1250 - C.XMLUI_DATA_LVL: one of: 1257 - C.XMLUI_DATA_LVL: one of:
1251 - C.XMLUI_DATA_LVL_INFO (default): normal message 1258 - C.XMLUI_DATA_LVL_INFO (default): normal message
1252 - C.XMLUI_DATA_LVL_WARNING: attention of user is important 1259 - C.XMLUI_DATA_LVL_WARNING: attention of user is important
1253 - C.XMLUI_DATA_LVL_ERROR: something went wrong 1260 - C.XMLUI_DATA_LVL_ERROR: something went wrong
1254 - C.XMLUI_DATA_BTNS_SET: one of: 1261 - C.XMLUI_DATA_BTNS_SET: one of:
1255 - C.XMLUI_DATA_BTNS_SET_OKCANCEL (default): classical "OK" and "Cancel" set 1262 - C.XMLUI_DATA_BTNS_SET_OKCANCEL (default): classical "OK" and "Cancel"
1256 - C.XMLUI_DATA_BTNS_SET_YESNO: a translated "yes" for OK, and "no" for Cancel 1263 set
1264 - C.XMLUI_DATA_BTNS_SET_YESNO: a translated "yes" for OK, and "no" for
1265 Cancel
1257 - C.XMLUI_DATA_FILETYPE: only used for file dialogs, one of: 1266 - C.XMLUI_DATA_FILETYPE: only used for file dialogs, one of:
1258 - C.XMLUI_DATA_FILETYPE_FILE: a file path is requested 1267 - C.XMLUI_DATA_FILETYPE_FILE: a file path is requested
1259 - C.XMLUI_DATA_FILETYPE_DIR: a dir path is requested 1268 - C.XMLUI_DATA_FILETYPE_DIR: a dir path is requested
1260 - C.XMLUI_DATA_FILETYPE_DEFAULT: same as C.XMLUI_DATA_FILETYPE_FILE 1269 - C.XMLUI_DATA_FILETYPE_DEFAULT: same as C.XMLUI_DATA_FILETYPE_FILE
1261 1270
1262 @param title: title or default if None 1271 @param title: title or default if None
1263 @param submit_id: callback id to call for panel_type we can submit (form, param, dialog) 1272 @param submit_id: callback id to call for panel_type we can submit (form, param,
1264 @param session_id: use to keep a session attached to the dialog, must be returned by frontends 1273 dialog)
1274 @param session_id: use to keep a session attached to the dialog, must be
1275 returned by frontends
1265 @attribute named_widgets(dict): map from name to widget 1276 @attribute named_widgets(dict): map from name to widget
1266 """ 1277 """
1267 self._introspect() # FIXME: why doing that on each XMLUI ? should be done once 1278 self._introspect() # FIXME: why doing that on each XMLUI ? should be done once
1268 if panel_type not in [ 1279 if panel_type not in [
1269 C.XMLUI_WINDOW, 1280 C.XMLUI_WINDOW,
1503 1514
1504 xmlui.submit_id = host.registerCallback(onSubmit, with_data=True, one_shot=True) 1515 xmlui.submit_id = host.registerCallback(onSubmit, with_data=True, one_shot=True)
1505 return xmlui_d 1516 return xmlui_d
1506 1517
1507 1518
1508 def deferXMLUI( 1519 def deferXMLUI(host, xmlui, action_extra=None, security_limit=C.NO_SECURITY_LIMIT,
1509 host, 1520 chained=False, profile=C.PROF_KEY_NONE):
1510 xmlui,
1511 action_extra=None,
1512 security_limit=C.NO_SECURITY_LIMIT,
1513 chained=False,
1514 profile=C.PROF_KEY_NONE,
1515 ):
1516 """Create a deferred linked to XMLUI 1521 """Create a deferred linked to XMLUI
1517 1522
1518 @param xmlui(XMLUI): instance of the XMLUI 1523 @param xmlui(XMLUI): instance of the XMLUI
1519 Must be an XMLUI that you can submit, with submit_id set to '' 1524 Must be an XMLUI that you can submit, with submit_id set to ''
1520 @param profile: %(doc_profile)s 1525 @param profile: %(doc_profile)s
1536 profile=profile, 1541 profile=profile,
1537 ) 1542 )
1538 return xmlui_d 1543 return xmlui_d
1539 1544
1540 1545
1541 def deferDialog( 1546 def deferDialog(host, message, title=u"Please confirm", type_=C.XMLUI_DIALOG_CONFIRM,
1542 host, 1547 options=None, action_extra=None, security_limit=C.NO_SECURITY_LIMIT, chained=False,
1543 message, 1548 profile=C.PROF_KEY_NONE):
1544 title=u"Please confirm",
1545 type_=C.XMLUI_DIALOG_CONFIRM,
1546 options=None,
1547 action_extra=None,
1548 security_limit=C.NO_SECURITY_LIMIT,
1549 chained=False,
1550 profile=C.PROF_KEY_NONE,
1551 ):
1552 """Create a submitable dialog and manage it with a deferred 1549 """Create a submitable dialog and manage it with a deferred
1553 1550
1554 @param message(unicode): message to display 1551 @param message(unicode): message to display
1555 @param title(unicode): title of the dialog 1552 @param title(unicode): title of the dialog
1556 @param type(unicode): dialog type (C.XMLUI_DIALOG_*) 1553 @param type(unicode): dialog type (C.XMLUI_DIALOG_*)
1557 @param options(None, dict): if not None, will be used to update (extend) dialog_opt arguments of XMLUI 1554 @param options(None, dict): if not None, will be used to update (extend) dialog_opt
1555 arguments of XMLUI
1558 @param action_extra(None, dict): extra action to merge with xmlui 1556 @param action_extra(None, dict): extra action to merge with xmlui
1559 mainly used to add meta informations (see actionNew doc) 1557 mainly used to add meta informations (see actionNew doc)
1560 @param security_limit: %(doc_security_limit)s 1558 @param security_limit: %(doc_security_limit)s
1561 @param chained(bool): True if the Deferred result must be returned to the frontend 1559 @param chained(bool): True if the Deferred result must be returned to the frontend
1562 useful when backend is in a series of dialogs with an ui 1560 useful when backend is in a series of dialogs with an ui
1599 return u"" 1597 return u""
1600 1598
1601 def __call__(self, raw_xml, force_spaces=False, namespace=None): 1599 def __call__(self, raw_xml, force_spaces=False, namespace=None):
1602 """ 1600 """
1603 @param raw_xml(unicode): the raw XML 1601 @param raw_xml(unicode): the raw XML
1604 @param force_spaces (bool): if True, replace occurrences of '\n' and '\t' with ' '. 1602 @param force_spaces (bool): if True, replace occurrences of '\n' and '\t'
1605 @param namespace(unicode, None): if set, use this namespace for the wrapping element 1603 with ' '.
1604 @param namespace(unicode, None): if set, use this namespace for the wrapping
1605 element
1606 """ 1606 """
1607 # we need to wrap element in case 1607 # we need to wrap element in case
1608 # there is not a unique one on the top 1608 # there is not a unique one on the top
1609 if namespace is not None: 1609 if namespace is not None:
1610 raw_xml = u"<div xmlns='{}'>{}</div>".format(namespace, raw_xml) 1610 raw_xml = u"<div xmlns='{}'>{}</div>".format(namespace, raw_xml)