post_process_documentation.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #! /usr/bin/env python
  2. # vin: sw=3 et:
  3. '''
  4. Copyright (C) 2012, Digium, Inc.
  5. Matt Jordan <mjordan@digium.com>
  6. This program is free software, distributed under the terms of
  7. the GNU General Public License Version 2.
  8. '''
  9. import sys
  10. import os
  11. import optparse
  12. import xml.dom.minidom
  13. from xml.dom.minidom import Element, parse
  14. def merge_parameter_information(managerEvent):
  15. ''' Merge the parameter information across all managerEventInstances
  16. within a managerEvent node '''
  17. def __swap_parameter_documentation(one, two):
  18. # See who has the better documentation and use it
  19. if (one.hasChildNodes() and not two.hasChildNodes()):
  20. two.parentNode.replaceChild(one.cloneNode(True), two)
  21. elif (two.hasChildNodes() and not one.hasChildNodes()):
  22. one.parentNode.replaceChild(two.cloneNode(True), one)
  23. def __merge_parameter(param, other_instances):
  24. # Compare the parameter to every other instance's set of parameters
  25. for other in other_instances:
  26. other_parameters = other.getElementsByTagName("parameter")
  27. match = [p for p in other_parameters
  28. if p.getAttribute('name') == param.getAttribute('name')]
  29. if (match):
  30. # See who has the better documentation and use it
  31. __swap_parameter_documentation(param, match[0])
  32. instances = managerEvent.getElementsByTagName("managerEventInstance")
  33. merged = []
  34. for instance in instances:
  35. others = [i for i in instances if i != instance]
  36. parameters = instance.getElementsByTagName("parameter")
  37. for parameter in parameters:
  38. if parameter not in merged:
  39. merged.append(parameter)
  40. __merge_parameter(parameter, others)
  41. def collapse_event_pair(managerEventOne, managerEventTwo):
  42. # Move all children of managerEventTwo to managerEventOne
  43. for node in managerEventTwo.childNodes:
  44. managerEventOne.appendChild(node.cloneNode(True))
  45. return managerEventOne
  46. def collapse_manager_events(rootNode, managerEvents):
  47. events = {}
  48. for managerEvent in managerEvents:
  49. if (managerEvent.parentNode.nodeName == 'list-elements'
  50. or managerEvent.parentNode.nodeName == 'responses'):
  51. continue
  52. managerEvent.parentNode.removeChild(managerEvent)
  53. attr = managerEvent.getAttribute('name')
  54. if attr in events:
  55. # match, collapse the two managerEvents
  56. events[attr] = collapse_event_pair(events[attr], managerEvent)
  57. else:
  58. events[attr] = managerEvent
  59. # Combine parameter information and re-add the manager Events
  60. for k, event in events.items():
  61. merge_parameter_information(event)
  62. rootNode.appendChild(event)
  63. return
  64. def main(argv=None):
  65. if argv is None:
  66. argv = sys.argv
  67. parser = optparse.OptionParser()
  68. parser.add_option('-i', '--input', dest='input_file',
  69. default='doc/core-full-en_US.xml',
  70. help='The XML file to process')
  71. parser.add_option('-o', '--output', dest='output_file',
  72. default='doc/core-en_US.xml',
  73. help='The XML file to create')
  74. (options, args) = parser.parse_args(argv)
  75. dom = parse(options.input_file)
  76. datasource = open(options.output_file, 'w')
  77. docs = dom.getElementsByTagName("docs")[0]
  78. managerEvents = dom.getElementsByTagName("managerEvent")
  79. if (managerEvents):
  80. collapse_manager_events(docs, managerEvents)
  81. dom.writexml(datasource)
  82. datasource.close()
  83. return 0
  84. if __name__ == "__main__":
  85. sys.exit(main() or 0)