doc_merge.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import sys
  4. import xml.etree.ElementTree as ET
  5. tree = ET.parse(sys.argv[1])
  6. old_doc = tree.getroot()
  7. tree = ET.parse(sys.argv[2])
  8. new_doc = tree.getroot()
  9. f = file(sys.argv[3], "wb")
  10. tab = 0
  11. old_classes = {}
  12. def write_string(_f, text, newline=True):
  13. for t in range(tab):
  14. _f.write("\t")
  15. _f.write(text)
  16. if (newline):
  17. _f.write("\n")
  18. def escape(ret):
  19. ret = ret.replace("&", "&")
  20. ret = ret.replace("<", "&gt;")
  21. ret = ret.replace(">", "&lt;")
  22. ret = ret.replace("'", "&apos;")
  23. ret = ret.replace("\"", "&quot;")
  24. return ret
  25. def inc_tab():
  26. global tab
  27. tab += 1
  28. def dec_tab():
  29. global tab
  30. tab -= 1
  31. write_string(f, '<?xml version="1.0" encoding="UTF-8" ?>')
  32. write_string(f, '<doc version="' + new_doc.attrib["version"] + '">')
  33. def get_tag(node, name):
  34. tag = ""
  35. if (name in node.attrib):
  36. tag = ' ' + name + '="' + escape(node.attrib[name]) + '" '
  37. return tag
  38. def find_method_descr(old_class, name):
  39. methods = old_class.find("methods")
  40. if(methods != None and len(list(methods)) > 0):
  41. for m in list(methods):
  42. if (m.attrib["name"] == name):
  43. description = m.find("description")
  44. if (description != None and description.text.strip() != ""):
  45. return description.text
  46. return None
  47. def find_signal_descr(old_class, name):
  48. signals = old_class.find("signals")
  49. if(signals != None and len(list(signals)) > 0):
  50. for m in list(signals):
  51. if (m.attrib["name"] == name):
  52. description = m.find("description")
  53. if (description != None and description.text.strip() != ""):
  54. return description.text
  55. return None
  56. def find_constant_descr(old_class, name):
  57. if (old_class is None):
  58. return None
  59. constants = old_class.find("constants")
  60. if(constants != None and len(list(constants)) > 0):
  61. for m in list(constants):
  62. if (m.attrib["name"] == name):
  63. if (m.text.strip() != ""):
  64. return m.text
  65. return None
  66. def write_class(c):
  67. class_name = c.attrib["name"]
  68. print("Parsing Class: " + class_name)
  69. if (class_name in old_classes):
  70. old_class = old_classes[class_name]
  71. else:
  72. old_class = None
  73. category = get_tag(c, "category")
  74. inherits = get_tag(c, "inherits")
  75. write_string(f, '<class name="' + class_name + '" ' + category + inherits + '>')
  76. inc_tab()
  77. write_string(f, "<brief_description>")
  78. if (old_class != None):
  79. old_brief_descr = old_class.find("brief_description")
  80. if (old_brief_descr != None):
  81. write_string(f, escape(old_brief_descr.text.strip()))
  82. write_string(f, "</brief_description>")
  83. write_string(f, "<description>")
  84. if (old_class != None):
  85. old_descr = old_class.find("description")
  86. if (old_descr != None):
  87. write_string(f, escape(old_descr.text.strip()))
  88. write_string(f, "</description>")
  89. methods = c.find("methods")
  90. if(methods != None and len(list(methods)) > 0):
  91. write_string(f, "<methods>")
  92. inc_tab()
  93. for m in list(methods):
  94. qualifiers = get_tag(m, "qualifiers")
  95. write_string(f, '<method name="' + escape(m.attrib["name"]) + '" ' + qualifiers + '>')
  96. inc_tab()
  97. for a in list(m):
  98. if (a.tag == "return"):
  99. typ = get_tag(a, "type")
  100. write_string(f, '<return' + typ + '>')
  101. write_string(f, '</return>')
  102. elif (a.tag == "argument"):
  103. default = get_tag(a, "default")
  104. write_string(f, '<argument index="' + a.attrib["index"] + '" name="' + escape(a.attrib["name"]) + '" type="' + a.attrib["type"] + '"' + default + '>')
  105. write_string(f, '</argument>')
  106. write_string(f, '<description>')
  107. if (old_class != None):
  108. old_method_descr = find_method_descr(old_class, m.attrib["name"])
  109. if (old_method_descr):
  110. write_string(f, escape(escape(old_method_descr.strip())))
  111. write_string(f, '</description>')
  112. dec_tab()
  113. write_string(f, "</method>")
  114. dec_tab()
  115. write_string(f, "</methods>")
  116. signals = c.find("signals")
  117. if(signals != None and len(list(signals)) > 0):
  118. write_string(f, "<signals>")
  119. inc_tab()
  120. for m in list(signals):
  121. write_string(f, '<signal name="' + escape(m.attrib["name"]) + '">')
  122. inc_tab()
  123. for a in list(m):
  124. if (a.tag == "argument"):
  125. write_string(f, '<argument index="' + a.attrib["index"] + '" name="' + escape(a.attrib["name"]) + '" type="' + a.attrib["type"] + '">')
  126. write_string(f, '</argument>')
  127. write_string(f, '<description>')
  128. if (old_class != None):
  129. old_signal_descr = find_signal_descr(old_class, m.attrib["name"])
  130. if (old_signal_descr):
  131. write_string(f, escape(old_signal_descr.strip()))
  132. write_string(f, '</description>')
  133. dec_tab()
  134. write_string(f, "</signal>")
  135. dec_tab()
  136. write_string(f, "</signals>")
  137. constants = c.find("constants")
  138. if(constants != None and len(list(constants)) > 0):
  139. write_string(f, "<constants>")
  140. inc_tab()
  141. for m in list(constants):
  142. write_string(f, '<constant name="' + escape(m.attrib["name"]) + '" value="' + m.attrib["value"] + '">')
  143. old_constant_descr = find_constant_descr(old_class, m.attrib["name"])
  144. if (old_constant_descr):
  145. write_string(f, escape(old_constant_descr.strip()))
  146. write_string(f, "</constant>")
  147. dec_tab()
  148. write_string(f, "</constants>")
  149. dec_tab()
  150. write_string(f, "</class>")
  151. for c in list(old_doc):
  152. old_classes[c.attrib["name"]] = c
  153. for c in list(new_doc):
  154. write_class(c)
  155. write_string(f, '</doc>\n')