py2xml.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #
  2. # Metrix++, Copyright 2009-2019, Metrix++ Project
  3. # Link: https://github.com/metrixplusplus/metrixplusplus
  4. #
  5. # This file is a part of Metrix++ Tool.
  6. #
  7. # Copied from http://code.activestate.com/recipes/577268-python-data-structure-to-xml-serialization/
  8. # - indent feature and better formatting added
  9. # - fixed handling of lists in lists
  10. # - fixed root object name for dictionaries
  11. INDENT_SPACE_SYMBOL = " "
  12. '''
  13. Py2XML - Python to XML serialization
  14. This code transforms a Python data structures into an XML document
  15. Usage:
  16. serializer = Py2XML()
  17. xml_string = serializer.parse( python_object )
  18. print python_object
  19. print xml_string
  20. '''
  21. class Py2XML():
  22. def __init__( self ):
  23. self.data = "" # where we store the processed XML string
  24. def parse( self, pythonObj, objName=None, indent = 0 ):
  25. '''
  26. processes Python data structure into XML string
  27. needs objName if pythonObj is a List
  28. '''
  29. if pythonObj == None:
  30. return "\n" + (INDENT_SPACE_SYMBOL * indent) + ""
  31. if isinstance( pythonObj, dict ):
  32. self.data = self._PyDict2XML( pythonObj, objName, indent=indent+1 )
  33. elif isinstance( pythonObj, list ):
  34. # we need name for List object
  35. self.data = self._PyList2XML( pythonObj, objName, indent=indent+1 )
  36. else:
  37. self.data = "\n" + (INDENT_SPACE_SYMBOL * indent) + "<%(n)s>%(o)s</%(n)s>" % { 'n':objName, 'o':str( pythonObj ) }
  38. return self.data
  39. def _PyDict2XML( self, pyDictObj, objName=None, indent = 0 ):
  40. '''
  41. process Python Dict objects
  42. They can store XML attributes and/or children
  43. '''
  44. tagStr = "" # XML string for this level
  45. attributes = {} # attribute key/value pairs
  46. attrStr = "" # attribute string of this level
  47. childStr = "" # XML string of this level's children
  48. for k, v in list(pyDictObj.items()):
  49. if isinstance( v, dict ):
  50. # child tags, with attributes
  51. childStr += self._PyDict2XML( v, k, indent=indent+1 )
  52. elif isinstance( v, list ):
  53. # child tags, list of children
  54. childStr += self._PyList2XML( v, k, indent=indent+1 )
  55. else:
  56. # tag could have many attributes, let's save until later
  57. attributes.update( { k:v } )
  58. if objName == None:
  59. return childStr
  60. # create XML string for attributes
  61. for k, v in sorted(attributes.items()):
  62. if isinstance(v, float):
  63. attrStr += " %s=\"%s\"" % ( k, round(v, 10) )
  64. else:
  65. attrStr += " %s=\"%s\"" % ( k, v )
  66. # let's assemble our tag string
  67. if childStr == "":
  68. tagStr += "\n" + (INDENT_SPACE_SYMBOL * indent) + "<%(n)s%(a)s />" % { 'n':objName, 'a':attrStr }
  69. else:
  70. tagStr += ("\n" + (INDENT_SPACE_SYMBOL * indent) + "<%(n)s%(a)s>%(c)s" + "\n" + (INDENT_SPACE_SYMBOL * indent) + "</%(n)s>") % { 'n':objName, 'a':attrStr, 'c':childStr }
  71. return tagStr
  72. def _PyList2XML( self, pyListObj, objName=None, indent = 0 ):
  73. '''
  74. process Python List objects
  75. They have no attributes, just children
  76. Lists only hold Dicts or Strings
  77. '''
  78. tagStr = "" # XML string for this level
  79. childStr = "" # XML string of children
  80. for childObj in pyListObj:
  81. if isinstance( childObj, dict ):
  82. # here's some Magic
  83. # we're assuming that List parent has a plural name of child:
  84. # eg, persons > person, so cut off last char
  85. # name-wise, only really works for one level, however
  86. # in practice, this is probably ok
  87. childStr += self._PyDict2XML( childObj, objName[:-1], indent=indent+1 )
  88. elif isinstance( childObj, list ):
  89. # here's some Magic
  90. # we're assuming that List parent has a plural name of child:
  91. # eg, persons > person, so cut off last char
  92. # name-wise, only really works for one level, however
  93. # in practice, this is probably ok
  94. childStr += self._PyList2XML( childObj, objName[:-1], indent=indent+1 )
  95. pass
  96. else:
  97. childStr += "\n" + (INDENT_SPACE_SYMBOL * (indent + 1)) + "<" + objName[:-1] + ">"
  98. for string in childObj:
  99. childStr += str(string);
  100. childStr += "</" + objName[:-1] + ">"
  101. if objName == None:
  102. return childStr
  103. tagStr += ("\n" + (INDENT_SPACE_SYMBOL * indent) + "<%(n)s>%(c)s" + "\n" + (INDENT_SPACE_SYMBOL * indent) + "</%(n)s>") % { 'n':objName, 'c':childStr }
  104. return tagStr