comparison src/tools/xml_tools.py @ 1504:a2e4b976e707

core (xmlui): added helper method to easily manage dialogs with a deferred
author Goffi <goffi@goffi.org>
date Thu, 27 Aug 2015 17:59:22 +0200
parents c7c872a40e56
children 84250128e425
comparison
equal deleted inserted replaced
1503:f681788097ba 1504:a2e4b976e707
24 24
25 from xml.dom import minidom, NotFoundErr 25 from xml.dom import minidom, NotFoundErr
26 from wokkel import data_form 26 from wokkel import data_form
27 from twisted.words.xish import domish 27 from twisted.words.xish import domish
28 from twisted.words.protocols.jabber import jid 28 from twisted.words.protocols.jabber import jid
29 from twisted.internet import defer
29 from sat.core import exceptions 30 from sat.core import exceptions
30 from collections import OrderedDict 31 from collections import OrderedDict
31 32
32 33
33 """This library help manage XML used in SàT (parameters, registration, etc)""" 34 """This library help manage XML used in SàT (parameters, registration, etc)"""
1046 self.doc = impl.createDocument(None, "sat_xmlui", None) 1047 self.doc = impl.createDocument(None, "sat_xmlui", None)
1047 top_element = self.doc.documentElement 1048 top_element = self.doc.documentElement
1048 top_element.setAttribute("type", panel_type) 1049 top_element.setAttribute("type", panel_type)
1049 if title: 1050 if title:
1050 top_element.setAttribute("title", title) 1051 top_element.setAttribute("title", title)
1051 self.submit_id = submit_id 1052 self._submit_id = submit_id
1052 self.session_id = session_id 1053 self.session_id = session_id
1053 if panel_type == C.XMLUI_DIALOG: 1054 if panel_type == C.XMLUI_DIALOG:
1054 if dialog_opt is None: 1055 if dialog_opt is None:
1055 dialog_opt = {} 1056 dialog_opt = {}
1056 self._createDialog(dialog_opt) 1057 self._createDialog(dialog_opt)
1132 else: 1133 else:
1133 raise exceptions.DataError("session_id can't be empty") 1134 raise exceptions.DataError("session_id can't be empty")
1134 1135
1135 def _createDialog(self, dialog_opt): 1136 def _createDialog(self, dialog_opt):
1136 dialog_type = dialog_opt.setdefault(C.XMLUI_DATA_TYPE, C.XMLUI_DIALOG_MESSAGE) 1137 dialog_type = dialog_opt.setdefault(C.XMLUI_DATA_TYPE, C.XMLUI_DIALOG_MESSAGE)
1137 if dialog_type in [C.XMLUI_DIALOG_CONFIRM, C.XMLUI_DIALOG_FILE] and self.submit_id is None: 1138 if dialog_type in [C.XMLUI_DIALOG_CONFIRM, C.XMLUI_DIALOG_FILE] and self._submit_id is None:
1138 raise exceptions.InternalError(_("Submit ID must be filled for this kind of dialog")) 1139 raise exceptions.InternalError(_("Submit ID must be filled for this kind of dialog"))
1139 top_element = TopElement(self) 1140 top_element = TopElement(self)
1140 level = dialog_opt.get(C.XMLUI_DATA_LVL) 1141 level = dialog_opt.get(C.XMLUI_DATA_LVL)
1141 dialog_elt = DialogElement(top_element, dialog_type, level) 1142 dialog_elt = DialogElement(top_element, dialog_type, level)
1142 1143
1196 # Some sugar for XMLUI dialogs 1197 # Some sugar for XMLUI dialogs
1197 1198
1198 def note(message, title='', level=C.XMLUI_DATA_LVL_INFO): 1199 def note(message, title='', level=C.XMLUI_DATA_LVL_INFO):
1199 """Sugar to easily create a Note Dialog 1200 """Sugar to easily create a Note Dialog
1200 1201
1201 @param message: body of the note 1202 @param message(unicode): body of the note
1202 @param title: title of the note 1203 @param title(unicode): title of the note
1203 @param level: one of C.XMLUI_DATA_LVL_* 1204 @param level(unicode): one of C.XMLUI_DATA_LVL_*
1205 @return(XMLUI): instance of XMLUI
1204 """ 1206 """
1205 note_xmlui = XMLUI(C.XMLUI_DIALOG, dialog_opt={ 1207 note_xmlui = XMLUI(C.XMLUI_DIALOG, dialog_opt={
1206 C.XMLUI_DATA_TYPE: C.XMLUI_DIALOG_NOTE, 1208 C.XMLUI_DATA_TYPE: C.XMLUI_DIALOG_NOTE,
1207 C.XMLUI_DATA_MESS: message, 1209 C.XMLUI_DATA_MESS: message,
1208 C.XMLUI_DATA_LVL: level}, 1210 C.XMLUI_DATA_LVL: level},
1209 title=title 1211 title=title
1210 ) 1212 )
1211 return note_xmlui 1213 return note_xmlui
1212 1214
1215 def deferXMLUI(host, xmlui, profile):
1216 """Create a deferred linked to XMLUI
1217
1218 @param xmlui(XMLUI): instance of the XMLUI
1219 Must be an XMLUI that you can submit, with submit_id set to ''
1220 @param profile: %(doc_profile)s
1221 @return (data): a deferred which fire the data
1222 """
1223 assert xmlui._submit_id == '' # xmlui.submit_id can't be the empty string, but xmlui._submit_id must here
1224 xmlui_d = defer.Deferred()
1225
1226 def onSubmit(data, profile):
1227 xmlui_d.callback(data)
1228 return {}
1229
1230 xmlui.submit_id = host.registerCallback(onSubmit, with_data=True, one_shot=True)
1231 host.actionNew({'xmlui': xmlui.toXml()}, profile)
1232 return xmlui_d
1233
1234 def deferDialog(host, message, title=u'Please confirm', type_=C.XMLUI_DIALOG_CONFIRM, profile=None):
1235 """Create a submitable dialog and manage it with a deferred
1236
1237 @param message(unicode): message to display
1238 @param title(unicode): title of the dialog
1239 @param type(unicode): dialog type (C.XMLUI_DIALOG_*)
1240 @param profile: %(doc_profile)s
1241 @return (dict): Deferred dict
1242 """
1243 assert profile is not None
1244 dialog = XMLUI(C.XMLUI_DIALOG, title=title, dialog_opt = {'type': type_, 'message': message}, submit_id='')
1245 return deferXMLUI(host, dialog, profile)
1246
1247 def deferConfirm(*args, **kwargs):
1248 """call deferDialog and return a boolean instead of the whole data dict"""
1249 d = deferDialog(*args, **kwargs)
1250 d.addCallback(lambda data: C.bool(data['answer']))
1251 return d
1252
1213 # Misc other funtions 1253 # Misc other funtions
1214
1215 1254
1216 class ElementParser(object): 1255 class ElementParser(object):
1217 """callable class to parse XML string into Element 1256 """callable class to parse XML string into Element
1218 1257
1219 Found at http://stackoverflow.com/questions/2093400/how-to-create-twisted-words-xish-domish-element-entirely-from-raw-xml/2095942#2095942 1258 Found at http://stackoverflow.com/questions/2093400/how-to-create-twisted-words-xish-domish-element-entirely-from-raw-xml/2095942#2095942