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