add_signatures.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. """
  2. This code adds Python/Java signatures to the docs.
  3. TODO: Do the same thing for Java
  4. * using javadoc/ get all the methods/classes/constants to a json file
  5. TODO:
  6. * clarify when there are several C++ signatures corresponding to a single Python function.
  7. i.e: calcHist():
  8. http://docs.opencv.org/3.2.0/d6/dc7/group__imgproc__hist.html#ga4b2b5fd75503ff9e6844cc4dcdaed35d
  9. * clarify special case:
  10. http://docs.opencv.org/3.2.0/db/de0/group__core__utils.html#ga4910d7f86336cd4eff9dd05575667e41
  11. """
  12. from __future__ import print_function
  13. import sys
  14. sys.dont_write_bytecode = True # Don't generate .pyc files / __pycache__ directories
  15. import os
  16. from pprint import pprint
  17. import re
  18. import logging
  19. import json
  20. import html_functions
  21. import doxygen_scan
  22. loglevel=os.environ.get("LOGLEVEL", None)
  23. if loglevel:
  24. logging.basicConfig(level=loglevel)
  25. ROOT_DIR = sys.argv[1]
  26. PYTHON_SIGNATURES_FILE = sys.argv[2]
  27. JAVA_OR_PYTHON = sys.argv[3]
  28. ADD_JAVA = False
  29. ADD_PYTHON = False
  30. if JAVA_OR_PYTHON == "python":
  31. ADD_PYTHON = True
  32. python_signatures = dict()
  33. with open(PYTHON_SIGNATURES_FILE, "rt") as f:
  34. python_signatures = json.load(f)
  35. print("Loaded Python signatures: %d" % len(python_signatures))
  36. import xml.etree.ElementTree as ET
  37. root = ET.parse(ROOT_DIR + 'opencv.tag')
  38. files_dict = {}
  39. # constants and function from opencv.tag
  40. namespaces = root.findall("./compound[@kind='namespace']")
  41. #print("Found {} namespaces".format(len(namespaces)))
  42. for ns in namespaces:
  43. ns_name = ns.find("./name").text
  44. #print('NS: {}'.format(ns_name))
  45. doxygen_scan.scan_namespace_constants(ns, ns_name, files_dict)
  46. doxygen_scan.scan_namespace_functions(ns, ns_name, files_dict)
  47. # class methods from opencv.tag
  48. classes = root.findall("./compound[@kind='class']")
  49. #print("Found {} classes".format(len(classes)))
  50. for c in classes:
  51. c_name = c.find("./name").text
  52. file = c.find("./filename").text
  53. #print('Class: {} => {}'.format(c_name, file))
  54. doxygen_scan.scan_class_methods(c, c_name, files_dict)
  55. print('Doxygen files to scan: %s' % len(files_dict))
  56. files_processed = 0
  57. files_skipped = 0
  58. symbols_processed = 0
  59. for file in files_dict:
  60. #if file != "dd/d9e/classcv_1_1VideoWriter.html":
  61. #if file != "d4/d86/group__imgproc__filter.html":
  62. #if file != "df/dfb/group__imgproc__object.html":
  63. # continue
  64. #print('File: ' + file)
  65. anchor_list = files_dict[file]
  66. active_anchors = [a for a in anchor_list if a.cppname in python_signatures]
  67. if len(active_anchors) == 0: # no linked Python symbols
  68. #print('Skip: ' + file)
  69. files_skipped = files_skipped + 1
  70. continue
  71. active_anchors_dict = {a.anchor: a for a in active_anchors}
  72. if len(active_anchors_dict) != len(active_anchors):
  73. logging.info('Duplicate entries detected: %s -> %s (%s)' % (len(active_anchors), len(active_anchors_dict), file))
  74. files_processed = files_processed + 1
  75. #pprint(active_anchors)
  76. symbols_processed = symbols_processed + len(active_anchors_dict)
  77. logging.info('File: %r' % file)
  78. html_functions.insert_python_signatures(python_signatures, active_anchors_dict, ROOT_DIR + file)
  79. print('Done (processed files %d, symbols %d, skipped %d files)' % (files_processed, symbols_processed, files_skipped))