py2txt.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #
  2. # Metrix++, Copyright 2009-2013, Metrix++ Project
  3. # Link: http://metrixplusplus.sourceforge.net
  4. #
  5. # This file is a part of Metrix++ Tool.
  6. #
  7. # Metrix++ is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, version 3 of the License.
  10. #
  11. # Metrix++ is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Metrix++. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. # Copied from http://code.activestate.com/recipes/577268-python-data-structure-to-TXT-serialization/ and modified
  20. '''
  21. Py2TXT - Python to TXT serialization
  22. This code transforms a Python data structures into an TXT document
  23. Usage:
  24. serializer = Py2TXT()
  25. txt_string = serializer.parse( python_object )
  26. print python_object
  27. print txt_string
  28. '''
  29. INDENT_SPACE_SYMBOL = ". "
  30. class Py2TXT():
  31. def __init__( self ):
  32. self.data = "" # where we store the processed TXT string
  33. def parse( self, pythonObj, objName=None, indent = 0 ):
  34. '''
  35. processes Python data structure into TXT string
  36. needs objName if pythonObj is a List
  37. '''
  38. if pythonObj == None:
  39. return "\n" + (INDENT_SPACE_SYMBOL * indent) + ""
  40. if isinstance( pythonObj, dict ):
  41. self.data = self._PyDict2TXT( pythonObj, objName, indent = indent + 1 )
  42. elif isinstance( pythonObj, list ):
  43. # we need name for List object
  44. self.data = self._PyList2TXT( pythonObj, objName, indent = indent + 1 )
  45. else:
  46. self.data = "\n" + (INDENT_SPACE_SYMBOL * indent) + "%(n)s: %(o)s" % { 'n':objName, 'o':str( pythonObj ) }
  47. self.data = (INDENT_SPACE_SYMBOL * (indent + 1)) + "-" * 80 + self.data + "\n" + (INDENT_SPACE_SYMBOL * (indent + 1)) + "=" * 80
  48. return self.data
  49. def _PyDict2TXT( self, pyDictObj, objName=None, indent = 0 ):
  50. '''
  51. process Python Dict objects
  52. They can store TXT attributes and/or children
  53. '''
  54. tagStr = "" # TXT string for this level
  55. attributes = {} # attribute key/value pairs
  56. attrStr = "" # attribute string of this level
  57. childStr = "" # TXT string of this level's children
  58. for k, v in pyDictObj.items():
  59. if isinstance( v, dict ):
  60. # child tags, with attributes
  61. childStr += self._PyDict2TXT( v, k, indent = indent + 1 )
  62. elif isinstance( v, list ):
  63. # child tags, list of children
  64. childStr += self._PyList2TXT( v, k, indent = indent + 1 )
  65. else:
  66. # tag could have many attributes, let's save until later
  67. attributes.update( { k:v } )
  68. if objName == None:
  69. return childStr
  70. # create TXT string for attributes
  71. attrStr += ""
  72. for k, v in attributes.items():
  73. attrStr += "\n" + (INDENT_SPACE_SYMBOL * (indent + 1)) + "%s=\"%s\"" % ( k, v )
  74. # let's assemble our tag string
  75. if childStr == "":
  76. tagStr += "\n" + (INDENT_SPACE_SYMBOL * indent) + "%(n)s: %(a)s" % { 'n':objName, 'a':attrStr }
  77. else:
  78. tagStr += "\n" + (INDENT_SPACE_SYMBOL * indent) + "%(n)s: %(a)s %(c)s" % { 'n':objName, 'a':attrStr, 'c':childStr }
  79. return tagStr
  80. def _PyList2TXT( self, pyListObj, objName=None, indent = 0 ):
  81. '''
  82. process Python List objects
  83. They have no attributes, just children
  84. Lists only hold Dicts or Strings
  85. '''
  86. tagStr = "" # TXT string for this level
  87. childStr = "" # TXT string of children
  88. for childObj in pyListObj:
  89. if isinstance( childObj, dict ):
  90. # here's some Magic
  91. # we're assuming that List parent has a plural name of child:
  92. # eg, persons > person, so cut off last char
  93. # name-wise, only really works for one level, however
  94. # in practice, this is probably ok
  95. childStr += "\n" + (INDENT_SPACE_SYMBOL * indent) + self._PyDict2TXT( childObj, objName[:-1], indent = indent + 1 )
  96. elif isinstance( childObj, list ):
  97. # here's some Magic
  98. # we're assuming that List parent has a plural name of child:
  99. # eg, persons > person, so cut off last char
  100. # name-wise, only really works for one level, however
  101. # in practice, this is probably ok
  102. childStr += self._PyList2TXT( childObj, objName[:-1], indent = indent + 1 )
  103. else:
  104. childStr += "\n" + (INDENT_SPACE_SYMBOL * (indent + 1))
  105. for string in childObj:
  106. childStr += string;
  107. if objName == None:
  108. return childStr
  109. tagStr += "\n" + (INDENT_SPACE_SYMBOL * indent) + "%(n)s:%(c)s" % { 'n':objName, 'c':childStr }
  110. return tagStr