Skip to content

Python util.vtkAlgorithm: XML output keeps ordering of the definition of the decorators

Lutz Hofmann requested to merge lhofmann/paraview:fix_decorator_order into master

Currently, in the Python module util.vtkAlgorithm, the method smproxy._generate_xml sorts the XML properties defined by the decorators alphabetically. While this is fine for algorithms with few properties, for more complex algorithms keeping the order of their definition would be preferred.

This patch solves this by sorting the properties by the line numbers of their definition (using __code__.co_firstlineno). This should work fine as long as the entire class is defined within the same file.

To reproduce:

from paraview.util.vtkAlgorithm import *

@smproxy.source()
class Test(VTKPythonAlgorithmBase):
    @smproperty.doublevector()
    def These(self, x): pass

    @smproperty.doublevector()
    def Should(self, x): pass

    @smproperty.doublevector()
    def Appear(self, x): pass

    @smproperty.doublevector()
    def In(self, x): pass

    @smproperty.doublevector()
    def Order(self, x): pass

if __name__ == "__main__":
    from paraview.detail.pythonalgorithm import get_plugin_xmls
    from xml.dom.minidom import parseString
    for xml in get_plugin_xmls(globals()):
        dom = parseString(xml)
    print(dom.toprettyxml(' ', '\n'))

Currently:

   <DoubleVectorProperty command="Appear" default_values="None" name="Appear" number_of_elements="1">  </DoubleVectorProperty>
   <DoubleVectorProperty command="In" default_values="None" name="In" number_of_elements="1">  </DoubleVectorProperty>
   <DoubleVectorProperty command="Order" default_values="None" name="Order" number_of_elements="1">  </DoubleVectorProperty>
   <DoubleVectorProperty command="Should" default_values="None" name="Should" number_of_elements="1">  </DoubleVectorProperty>
   <DoubleVectorProperty command="These" default_values="None" name="These" number_of_elements="1">  </DoubleVectorProperty>

With this patch:

   <DoubleVectorProperty command="These" default_values="None" name="These" number_of_elements="1">  </DoubleVectorProperty>
   <DoubleVectorProperty command="Should" default_values="None" name="Should" number_of_elements="1">  </DoubleVectorProperty>
   <DoubleVectorProperty command="Appear" default_values="None" name="Appear" number_of_elements="1">  </DoubleVectorProperty>
   <DoubleVectorProperty command="In" default_values="None" name="In" number_of_elements="1">  </DoubleVectorProperty>
   <DoubleVectorProperty command="Order" default_values="None" name="Order" number_of_elements="1">  </DoubleVectorProperty>

Merge request reports