doc_merge.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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(
  105. f,
  106. '<argument index="'
  107. + a.attrib["index"]
  108. + '" name="'
  109. + escape(a.attrib["name"])
  110. + '" type="'
  111. + a.attrib["type"]
  112. + '"'
  113. + default
  114. + ">",
  115. )
  116. write_string(f, "</argument>")
  117. write_string(f, "<description>")
  118. if old_class != None:
  119. old_method_descr = find_method_descr(old_class, m.attrib["name"])
  120. if old_method_descr:
  121. write_string(f, escape(escape(old_method_descr.strip())))
  122. write_string(f, "</description>")
  123. dec_tab()
  124. write_string(f, "</method>")
  125. dec_tab()
  126. write_string(f, "</methods>")
  127. signals = c.find("signals")
  128. if signals != None and len(list(signals)) > 0:
  129. write_string(f, "<signals>")
  130. inc_tab()
  131. for m in list(signals):
  132. write_string(f, '<signal name="' + escape(m.attrib["name"]) + '">')
  133. inc_tab()
  134. for a in list(m):
  135. if a.tag == "argument":
  136. write_string(
  137. f,
  138. '<argument index="'
  139. + a.attrib["index"]
  140. + '" name="'
  141. + escape(a.attrib["name"])
  142. + '" type="'
  143. + a.attrib["type"]
  144. + '">',
  145. )
  146. write_string(f, "</argument>")
  147. write_string(f, "<description>")
  148. if old_class != None:
  149. old_signal_descr = find_signal_descr(old_class, m.attrib["name"])
  150. if old_signal_descr:
  151. write_string(f, escape(old_signal_descr.strip()))
  152. write_string(f, "</description>")
  153. dec_tab()
  154. write_string(f, "</signal>")
  155. dec_tab()
  156. write_string(f, "</signals>")
  157. constants = c.find("constants")
  158. if constants != None and len(list(constants)) > 0:
  159. write_string(f, "<constants>")
  160. inc_tab()
  161. for m in list(constants):
  162. write_string(f, '<constant name="' + escape(m.attrib["name"]) + '" value="' + m.attrib["value"] + '">')
  163. old_constant_descr = find_constant_descr(old_class, m.attrib["name"])
  164. if old_constant_descr:
  165. write_string(f, escape(old_constant_descr.strip()))
  166. write_string(f, "</constant>")
  167. dec_tab()
  168. write_string(f, "</constants>")
  169. dec_tab()
  170. write_string(f, "</class>")
  171. for c in list(old_doc):
  172. old_classes[c.attrib["name"]] = c
  173. for c in list(new_doc):
  174. write_class(c)
  175. write_string(f, "</doc>\n")