Mercurial > libervia-backend
diff src/tools/xml_tools.py @ 590:56531f9e9ac7
Fix pep8 support in src/tools.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Fri, 18 Jan 2013 17:55:35 +0100 |
parents | beaf6bec2fcd |
children | 84a6e83157c2 |
line wrap: on
line diff
--- a/src/tools/xml_tools.py Fri Jan 18 17:55:35 2013 +0100 +++ b/src/tools/xml_tools.py Fri Jan 18 17:55:35 2013 +0100 @@ -22,7 +22,6 @@ from logging import debug, info, error from xml.dom import minidom from wokkel import data_form -import pdb from twisted.words.xish import domish """This library help manage XML used in SàT (parameters, registration, etc) """ @@ -36,9 +35,9 @@ if form.instructions: form_ui.addText('\n'.join(form.instructions), 'instructions') - labels = filter(lambda field:field.label,form.fieldList) + labels = [field for field in form.fieldList if field.label] if labels: - #if there is no label, we don't need to use pairs + # if there is no label, we don't need to use pairs form_ui.changeLayout("pairs") for field in form.fieldList: @@ -51,7 +50,7 @@ elif field.fieldType == 'list-single': __field_type = "list" else: - error (u"FIXME FIXME FIXME: Type [%s] is not managed yet by SàT" % field.fieldType) + error(u"FIXME FIXME FIXME: Type [%s] is not managed yet by SàT" % field.fieldType) __field_type = "string" if labels: @@ -60,9 +59,10 @@ else: form_ui.addEmpty() - elem = form_ui.addElement(__field_type, field.var, field.value, [option.value for option in field.options]) + form_ui.addElement(__field_type, field.var, field.value, [option.value for option in field.options]) return form_ui.toXml() + def tupleList2dataForm(values): """convert a list of tuples (name,value) to a wokkel submit data form""" form = data_form.Form('submit') @@ -72,6 +72,7 @@ return form + def paramsXml2xmlUI(xml): """Convert the xml for parameter to a SàT XML User Interface""" params_doc = minidom.parseString(xml.encode('utf-8')) @@ -93,20 +94,18 @@ if not name: error(_('INTERNAL ERROR: params must have a name')) assert(False) - type = param.getAttribute('type') + type_ = param.getAttribute('type') value = param.getAttribute('value') or None callback_id = param.getAttribute('callback_id') or None - if type == "button": + if type_ == "button": param_ui.addEmpty() else: param_ui.addLabel(label or name) - param_ui.addElement(name=name, type=type, value=value, callback_id=callback_id) + param_ui.addElement(name=name, type_=type_, value=value, callback_id=callback_id) return param_ui.toXml() - - class XMLUI(object): """This class is used to create a user interface (form/window/parameters/etc) using SàT XML""" @@ -127,7 +126,7 @@ if not panel_type in ['window', 'form', 'param']: error(_("Unknown panel type [%s]") % panel_type) assert(False) - self.type = panel_type + self.type_ = panel_type impl = minidom.getDOMImplementation() self.doc = impl.createDocument(None, "sat_xmlui", None) @@ -135,8 +134,9 @@ top_element.setAttribute("type", panel_type) if title: top_element.setAttribute("title", title) - self.parentTabsLayout = None #used only we have 'tabs' layout - self.currentCategory = None #used only we have 'tabs' layout + self.parentTabsLayout = None # used only we have 'tabs' layout + self.currentCategory = None # used only we have 'tabs' layout + self.currentLayout = None self.changeLayout(layout) def __del__(self): @@ -148,17 +148,17 @@ @parent: parent element or None """ if not layout in ['vertical', 'horizontal', 'pairs', 'tabs']: - error (_("Unknown layout type [%s]") % layout) - assert (False) + error(_("Unknown layout type [%s]") % layout) + assert False layout_elt = self.doc.createElement('layout') - layout_elt.setAttribute('type',layout) - if parent != None: + layout_elt.setAttribute('type', layout) + if parent is not None: parent.appendChild(layout_elt) return layout_elt - def __createElem(self, type, name=None, parent = None): + def __createElem(self, type_, name=None, parent=None): """Create an element - @param type: one of + @param type_: one of - empty: empty element (usefull to skip something in a layout, e.g. skip first element in a PAIRS layout) - text: text to be displayed in an multi-line area, e.g. instructions @param name: name of the element or None @@ -167,8 +167,8 @@ elem = self.doc.createElement('elem') if name: elem.setAttribute('name', name) - elem.setAttribute('type', type) - if parent != None: + elem.setAttribute('type', type_) + if parent is not None: parent.appendChild(elem) return elem @@ -178,10 +178,9 @@ if layout == "tabs": self.parentTabsLayout = self.currentLayout - def addEmpty(self, name=None): """Add a multi-lines text""" - elem = self.__createElem('empty', name, self.currentLayout) + self.__createElem('empty', name, self.currentLayout) def addText(self, text, name=None): """Add a multi-lines text""" @@ -214,15 +213,15 @@ def addBool(self, name=None, value="true"): """Add a string box""" - assert value in ["true","false"] + assert value in ["true", "false"] elem = self.__createElem('bool', name, self.currentLayout) elem.setAttribute('value', value) def addList(self, options, name=None, value=None, style=set()): """Add a list of choices""" styles = set(style) - assert (options) - assert (styles.issubset(['multi'])) + assert options + assert styles.issubset(['multi']) elem = self.__createElem('list', name, self.currentLayout) self.addOptions(options, elem) if value: @@ -244,31 +243,29 @@ fback_el.setAttribute('name', field) elem.appendChild(fback_el) - - - def addElement(self, type, name = None, value = None, options = None, callback_id = None): + def addElement(self, type_, name=None, value=None, options=None, callback_id=None): """Convenience method to add element, the params correspond to the ones in addSomething methods""" - if type == 'empty': + if type_ == 'empty': self.addEmpty(name) - elif type == 'text': - assert(value!=None) + elif type_ == 'text': + assert value is not None self.addText(value, name) - elif type == 'label': + elif type_ == 'label': assert(value) self.addLabel(value) - elif type == 'string': + elif type_ == 'string': self.addString(name, value) - elif type == 'password': + elif type_ == 'password': self.addPassword(name, value) - elif type == 'textbox': + elif type_ == 'textbox': self.addTextBox(name, value) - elif type == 'bool': + elif type_ == 'bool': if not value: value = "true" self.addBool(name, value) - elif type == 'list': + elif type_ == 'list': self.addList(options, name, value) - elif type == 'button': + elif type_ == 'button': assert(callback_id and value) self.addButton(callback_id, name, value) @@ -303,26 +300,28 @@ return self.doc.toxml() - class ElementParser(object): """callable class to parse XML string into Element Found at http://stackoverflow.com/questions/2093400/how-to-create-twisted-words-xish-domish-element-entirely-from-raw-xml/2095942#2095942 (c) Karl Anderson""" - def __call__(self, s): + def __call__(self, string): self.result = None - def onStart(el): - self.result = el + + def onStart(elem): + self.result = elem + def onEnd(): pass - def onElement(el): - self.result.addChild(el) + + def onElement(elem): + self.result.addChild(elem) parser = domish.elementStream() parser.DocumentStartEvent = onStart parser.ElementEvent = onElement parser.DocumentEndEvent = onEnd tmp = domish.Element((None, "s")) - tmp.addRawXml(s.replace('\n','').replace('\t','')) + tmp.addRawXml(string.replace('\n', '').replace('\t', '')) parser.parse(tmp.toXml().encode('utf-8')) return self.result.firstChildElement()