py2txt.py 4.5 KB

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