comparison src/tools/xml_tools.py @ 1472:c005c212b538

xmlui: minor docstring fixes
author Goffi <goffi@goffi.org>
date Tue, 18 Aug 2015 10:04:47 +0200
parents a13be5c22334
children 675e0e9f1653
comparison
equal deleted inserted replaced
1471:934e402c90bf 1472:c005c212b538
38 38
39 def _dataFormField2XMLUIData(field, read_only=False): 39 def _dataFormField2XMLUIData(field, read_only=False):
40 """Get data needed to create an XMLUI's Widget from Wokkel's data_form's Field. 40 """Get data needed to create an XMLUI's Widget from Wokkel's data_form's Field.
41 41
42 The attribute field can be modified (if it's fixed and it has no value). 42 The attribute field can be modified (if it's fixed and it has no value).
43
44 @param field (data_form.Field): a field with attributes "value", "fieldType", "label" and "var" 43 @param field (data_form.Field): a field with attributes "value", "fieldType", "label" and "var"
45 @param read_only (bool): if True and it makes sense, create a read only input widget 44 @param read_only (bool): if True and it makes sense, create a read only input widget
46 @return: a tuple (widget_type, widget_args, widget_kwargs) 45 @return: a tuple (widget_type, widget_args, widget_kwargs)
47 """ 46 """
48 widget_args = [field.value] 47 widget_args = [field.value]
233 232
234 233
235 def tupleList2dataForm(values): 234 def tupleList2dataForm(values):
236 """Convert a list of tuples (name, value) to a wokkel submit data form. 235 """Convert a list of tuples (name, value) to a wokkel submit data form.
237 236
238 @param values (list): list of tuples 237 @param values (list): list of tuples
239 @return: data_form.Form 238 @return: data_form.Form
240 """ 239 """
241 form = data_form.Form('submit') 240 form = data_form.Form('submit')
242 for value in values: 241 for value in values:
243 field = data_form.Field(var=value[0], value=value[1]) 242 field = data_form.Field(var=value[0], value=value[1])
447 """ 446 """
448 @param parent: AdvancedListContainer instance 447 @param parent: AdvancedListContainer instance
449 @param name: name of the container 448 @param name: name of the container
450 @param label: label to be displayed in columns 449 @param label: label to be displayed in columns
451 @param description: long descriptive text 450 @param description: long descriptive text
452
453 """ 451 """
454 assert(isinstance(parent, AdvancedListContainer)) 452 assert(isinstance(parent, AdvancedListContainer))
455 super(HeaderElement, self).__init__(parent.xmlui, parent) 453 super(HeaderElement, self).__init__(parent.xmlui, parent)
456 if name: 454 if name:
457 self.elem.setAttribute('name', name) 455 self.elem.setAttribute('name', name)
465 """ And Element which contains other ones and has a layout """ 463 """ And Element which contains other ones and has a layout """
466 type = None 464 type = None
467 465
468 def __init__(self, xmlui, parent=None): 466 def __init__(self, xmlui, parent=None):
469 """Create a container element 467 """Create a container element
468
470 @param xmlui: XMLUI instance 469 @param xmlui: XMLUI instance
471 @parent: parent element or None 470 @parent: parent element or None
472 """ 471 """
473 self.elem = xmlui.doc.createElement('container') 472 self.elem = xmlui.doc.createElement('container')
474 super(Container, self).__init__(xmlui, parent) 473 super(Container, self).__init__(xmlui, parent)
475 self.elem.setAttribute('type', self.type) 474 self.elem.setAttribute('type', self.type)
476 475
477 def getParentContainer(self): 476 def getParentContainer(self):
478 """ Return first parent container 477 """ Return first parent container
478
479 @return: parent container or None 479 @return: parent container or None
480
481 """ 480 """
482 current = self.parent 481 current = self.parent
483 while(not isinstance(current, (Container)) and 482 while(not isinstance(current, (Container)) and
484 current is not None): 483 current is not None):
485 current = current.parent 484 current = current.parent
509 new_container = container(self.xmlui, tab_elt) 508 new_container = container(self.xmlui, tab_elt)
510 self.xmlui.changeContainer(new_container) 509 self.xmlui.changeContainer(new_container)
511 510
512 def end(self): 511 def end(self):
513 """ Called when we have finished tabs 512 """ Called when we have finished tabs
513
514 change current container to first container parent 514 change current container to first container parent
515
516 """ 515 """
517 parent_container = self.getParentContainer() 516 parent_container = self.getParentContainer()
518 self.xmlui.changeContainer(parent_container) 517 self.xmlui.changeContainer(parent_container)
519 518
520 519
521 class AdvancedListContainer(Container): 520 class AdvancedListContainer(Container):
521 """A list which can contain other widgets, headers, etc"""
522 type = "advanced_list" 522 type = "advanced_list"
523 523
524 def __init__(self, xmlui, callback_id=None, name=None, headers=None, items=None, columns=None, selectable='no', auto_index=False, parent=None): 524 def __init__(self, xmlui, callback_id=None, name=None, headers=None, items=None, columns=None, selectable='no', auto_index=False, parent=None):
525 """Create an advanced list 525 """Create an advanced list
526
526 @param headers: optional headers information 527 @param headers: optional headers information
527 @param callback_id: id of the method to call when selection is done 528 @param callback_id: id of the method to call when selection is done
528 @param items: list of widgets to add (just the first row) 529 @param items: list of widgets to add (just the first row)
529 @param columns: number of columns in this table, or None to autodetect 530 @param columns: number of columns in this table, or None to autodetect
530 @param selectable: one of: 531 @param selectable: one of:
572 for item in items: 573 for item in items:
573 self.append(item) 574 self.append(item)
574 575
575 def setRowIndex(self, idx): 576 def setRowIndex(self, idx):
576 """ Set index for next row 577 """ Set index for next row
578
577 index are returned when a row is selected, in data's "index" key 579 index are returned when a row is selected, in data's "index" key
578 @param idx: string index to associate to the next row 580 @param idx: string index to associate to the next row
579
580 """ 581 """
581 self.next_row_idx = idx 582 self.next_row_idx = idx
582 583
583 def append(self, child): 584 def append(self, child):
584 if isinstance(child, RowElement): 585 if isinstance(child, RowElement):
588 self.current_row.append(child) 589 self.current_row.append(child)
589 self._item_idx += 1 590 self._item_idx += 1
590 591
591 def end(self): 592 def end(self):
592 """ Called when we have finished list 593 """ Called when we have finished list
594
593 change current container to first container parent 595 change current container to first container parent
594
595 """ 596 """
596 if self._item_idx % self._columns != 0: 597 if self._item_idx % self._columns != 0:
597 raise exceptions.DataError(_("Incorrect number of items in list")) 598 raise exceptions.DataError(_("Incorrect number of items in list"))
598 parent_container = self.getParentContainer() 599 parent_container = self.getParentContainer()
599 self.xmlui.changeContainer(parent_container) 600 self.xmlui.changeContainer(parent_container)
602 class Widget(Element): 603 class Widget(Element):
603 type = None 604 type = None
604 605
605 def __init__(self, xmlui, name=None, parent=None): 606 def __init__(self, xmlui, name=None, parent=None):
606 """Create an element 607 """Create an element
608
607 @param xmlui: XMLUI instance 609 @param xmlui: XMLUI instance
608 @param name: name of the element or None 610 @param name: name of the element or None
609 @param parent: parent element or None 611 @param parent: parent element or None
610 """ 612 """
611 self.elem = xmlui.doc.createElement('widget') 613 self.elem = xmlui.doc.createElement('widget')
617 def setInternalCallback(self, callback, fields, data_elts=None): 619 def setInternalCallback(self, callback, fields, data_elts=None):
618 """Set an internal UI callback when the widget value is changed. 620 """Set an internal UI callback when the widget value is changed.
619 621
620 The internal callbacks are NO callback ids, they are strings from 622 The internal callbacks are NO callback ids, they are strings from
621 a predefined set of actions that are running in the scope of XMLUI. 623 a predefined set of actions that are running in the scope of XMLUI.
622
623 @param callback (string): a value from: 624 @param callback (string): a value from:
624 - 'copy': process the widgets given in 'fields' two by two, by 625 - 'copy': process the widgets given in 'fields' two by two, by
625 copying the values of one widget to the other. Target widgets 626 copying the values of one widget to the other. Target widgets
626 of type List do not accept the empty value. 627 of type List do not accept the empty value.
627 - 'move': same than copy but moves the values if the source widget 628 - 'move': same than copy but moves the values if the source widget
657 value_elt.appendChild(text) 658 value_elt.appendChild(text)
658 self.elem.appendChild(value_elt) 659 self.elem.appendChild(value_elt)
659 660
660 661
661 class LabelWidget(Widget): 662 class LabelWidget(Widget):
662 """Used for one line blob of text, 663 """One line blob of text
663 most of time to display the desciption or name of the next widget 664
665 used most of time to display the desciption or name of the next widget
664 """ 666 """
665 type = 'label' 667 type = 'label'
666 668
667 def __init__(self, xmlui, label, name=None, parent=None): 669 def __init__(self, xmlui, label, name=None, parent=None):
668 super(LabelWidget, self).__init__(xmlui, name, parent) 670 super(LabelWidget, self).__init__(xmlui, name, parent)
684 class DividerWidget(Widget): 686 class DividerWidget(Widget):
685 type = 'divider' 687 type = 'divider'
686 688
687 def __init__(self, xmlui, style='line', name=None, parent=None): 689 def __init__(self, xmlui, style='line', name=None, parent=None):
688 """ Create a divider 690 """ Create a divider
691
689 @param xmlui: XMLUI instance 692 @param xmlui: XMLUI instance
690 @param style: one of: 693 @param style: one of:
691 - line: a simple line 694 - line: a simple line
692 - dot: a line of dots 695 - dot: a line of dots
693 - dash: a line of dashes 696 - dash: a line of dashes
703 706
704 ### Inputs ### 707 ### Inputs ###
705 708
706 709
707 class InputWidget(Widget): 710 class InputWidget(Widget):
708 """Input widget are widget which can accept user inputs, 711 """Widget which can accept user inputs
712
709 used mainly in forms 713 used mainly in forms
710 """ 714 """
711 def __init__(self, xmlui, name=None, parent=None, read_only=False): 715 def __init__(self, xmlui, name=None, parent=None, read_only=False):
712 super(InputWidget, self).__init__(xmlui, name, parent) 716 super(InputWidget, self).__init__(xmlui, name, parent)
713 if read_only: 717 if read_only:
769 class ButtonWidget(Widget): 773 class ButtonWidget(Widget):
770 type = 'button' 774 type = 'button'
771 775
772 def __init__(self, xmlui, callback_id, value=None, fields_back=None, name=None, parent=None): 776 def __init__(self, xmlui, callback_id, value=None, fields_back=None, name=None, parent=None):
773 """Add a button 777 """Add a button
778
774 @param callback_id: callback which will be called if button is pressed 779 @param callback_id: callback which will be called if button is pressed
775 @param value: label of the button 780 @param value: label of the button
776 @fields_back: list of names of field to give back when pushing the button 781 @fields_back: list of names of field to give back when pushing the button
777 @param name: name 782 @param name: name
778 @param parent: parent container 783 @param parent: parent container
1044 except KeyError: 1049 except KeyError:
1045 pass 1050 pass
1046 1051
1047 def _createContainer(self, container, parent=None, **kwargs): 1052 def _createContainer(self, container, parent=None, **kwargs):
1048 """Create a container element 1053 """Create a container element
1054
1049 @param type: container type (cf init doc) 1055 @param type: container type (cf init doc)
1050 @parent: parent element or None 1056 @parent: parent element or None
1051 """ 1057 """
1052 if container not in self._containers: 1058 if container not in self._containers:
1053 raise exceptions.DataError(_("Unknown container type [%s]") % container) 1059 raise exceptions.DataError(_("Unknown container type [%s]") % container)
1055 new_container = cls(self, parent=parent, **kwargs) 1061 new_container = cls(self, parent=parent, **kwargs)
1056 return new_container 1062 return new_container
1057 1063
1058 def changeContainer(self, container, **kwargs): 1064 def changeContainer(self, container, **kwargs):
1059 """Change the current container 1065 """Change the current container
1066
1060 @param container: either container type (container it then created), 1067 @param container: either container type (container it then created),
1061 or an Container instance""" 1068 or an Container instance"""
1062 if isinstance(container, basestring): 1069 if isinstance(container, basestring):
1063 self.current_container = self._createContainer(container, self.current_container.getParentContainer() or self.main_container, **kwargs) 1070 self.current_container = self._createContainer(container, self.current_container.getParentContainer() or self.main_container, **kwargs)
1064 else: 1071 else:
1100 # Misc other funtions 1107 # Misc other funtions
1101 1108
1102 1109
1103 class ElementParser(object): 1110 class ElementParser(object):
1104 """callable class to parse XML string into Element 1111 """callable class to parse XML string into Element
1112
1105 Found at http://stackoverflow.com/questions/2093400/how-to-create-twisted-words-xish-domish-element-entirely-from-raw-xml/2095942#2095942 1113 Found at http://stackoverflow.com/questions/2093400/how-to-create-twisted-words-xish-domish-element-entirely-from-raw-xml/2095942#2095942
1106 (c) Karl Anderson""" 1114 (c) Karl Anderson
1115 """
1107 1116
1108 def __call__(self, raw_xml, force_spaces=False): 1117 def __call__(self, raw_xml, force_spaces=False):
1109 """ 1118 """
1110 @param raw_xml(unicode): the raw XML 1119 @param raw_xml(unicode): the raw XML
1111 @param force_spaces: if True, replace occurrences of '\n' and '\t' with ' '. 1120 @param force_spaces: if True, replace occurrences of '\n' and '\t' with ' '.