Skip to content

Commit b15bc19

Browse files
committed
doc: finalize Python signatures injection
1 parent 164a77e commit b15bc19

File tree

4 files changed

+153
-240
lines changed

4 files changed

+153
-240
lines changed

doc/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,15 +218,15 @@ if(BUILD_DOCS AND DOXYGEN_FOUND)
218218
COMPONENT "docs" OPTIONAL
219219
)
220220

221-
if(BUILD_opencv_python2)
221+
if(PYTHON2_EXECUTABLE)
222222
add_custom_target(doxygen_python
223223
COMMAND ${PYTHON2_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/tools/add_signatures.py" "${CMAKE_CURRENT_BINARY_DIR}/doxygen/html/" "${OPENCV_PYTHON2_SIGNATURES_FILE}" "python"
224224
DEPENDS "${doxygen_result}" gen_opencv_python2
225225
)
226226
add_custom_target(doxygen
227227
DEPENDS doxygen_cpp doxygen_python
228228
)
229-
elseif(BUILD_opencv_python3)
229+
elseif(PYTHON3_EXECUTABLE)
230230
add_custom_target(doxygen_python
231231
COMMAND ${PYTHON3_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/tools/add_signatures.py" "${CMAKE_CURRENT_BINARY_DIR}/doxygen/html/" "${OPENCV_PYTHON3_SIGNATURES_FILE}" "python"
232232
DEPENDS "${doxygen_result}" gen_opencv_python3

doc/tools/add_signatures.py

Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,90 +12,88 @@
1212
http://docs.opencv.org/3.2.0/db/de0/group__core__utils.html#ga4910d7f86336cd4eff9dd05575667e41
1313
"""
1414
from __future__ import print_function
15+
import sys
16+
sys.dont_write_bytecode = True # Don't generate .pyc files / __pycache__ directories
17+
1518
import os
19+
from pprint import pprint
1620
import re
17-
import sys
1821
import logging
22+
import json
23+
1924
import html_functions
2025
import doxygen_scan
2126

2227
loglevel=os.environ.get("LOGLEVEL", None)
2328
if loglevel:
2429
logging.basicConfig(level=loglevel)
2530

26-
2731
ROOT_DIR = sys.argv[1]
2832
PYTHON_SIGNATURES_FILE = sys.argv[2]
29-
JAVA_PYTHON = sys.argv[3]
33+
JAVA_OR_PYTHON = sys.argv[3]
3034

3135
ADD_JAVA = False
3236
ADD_PYTHON = False
33-
if JAVA_PYTHON == "python":
37+
if JAVA_OR_PYTHON == "python":
3438
ADD_PYTHON = True
3539

36-
import json
3740
python_signatures = dict()
3841
with open(PYTHON_SIGNATURES_FILE, "rt") as f:
3942
python_signatures = json.load(f)
4043
print("Loaded Python signatures: %d" % len(python_signatures))
4144

42-
# only name -> class
43-
# name and ret -> constant
44-
# name, ret, arg-> function / class method
45-
46-
class Configuration():
47-
def __init__(self):
48-
self.ADD_PYTHON = ADD_PYTHON
49-
self.python_signatures = python_signatures
50-
self.ADD_JAVA = ADD_JAVA
51-
52-
config = Configuration()
53-
5445
import xml.etree.ElementTree as ET
5546
root = ET.parse(ROOT_DIR + 'opencv.tag')
56-
files_dict = dict()
47+
files_dict = {}
5748

5849
# constants and function from opencv.tag
5950
namespaces = root.findall("./compound[@kind='namespace']")
6051
#print("Found {} namespaces".format(len(namespaces)))
6152
for ns in namespaces:
6253
ns_name = ns.find("./name").text
6354
#print('NS: {}'.format(ns_name))
64-
65-
files_dict = doxygen_scan.scan_namespace_constants(ns, ns_name, files_dict)
66-
files_dict = doxygen_scan.scan_namespace_functions(ns, ns_name, files_dict)
55+
doxygen_scan.scan_namespace_constants(ns, ns_name, files_dict)
56+
doxygen_scan.scan_namespace_functions(ns, ns_name, files_dict)
6757

6858
# class methods from opencv.tag
6959
classes = root.findall("./compound[@kind='class']")
7060
#print("Found {} classes".format(len(classes)))
7161
for c in classes:
7262
c_name = c.find("./name").text
73-
name = ns_name + '::' + c_name
7463
file = c.find("./filename").text
75-
#print('Class: {} => {}'.format(name, file))
76-
files_dict = doxygen_scan.scan_class_methods(c, c_name, files_dict)
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
7772

78-
# test
7973
for file in files_dict:
80-
soup = html_functions.load_html_file(ROOT_DIR + file)
81-
if file == "dd/d9e/classcv_1_1VideoWriter.html":#"d4/d86/group__imgproc__filter.html":#"d4/d86/group__imgproc__filter.html":
82-
anchor_list = files_dict[file]
83-
counter = 0
84-
anchor_tmp_list = []
85-
for anchor in anchor_list:
86-
counter += 1
87-
# if the next anchor shares the same C++ name (= same method/function), join them together
88-
if counter < len(anchor_list) and anchor_list[counter].cppname == anchor.cppname:
89-
anchor_tmp_list.append(anchor)
90-
continue
91-
else:
92-
anchor_tmp_list.append(anchor)
93-
# check if extists a python equivalent signature
94-
for signature in python_signatures: # signature is a key with the C++ name
95-
if signature == anchor.cppname: # if available name in python
96-
# they should also have the same type
97-
soup = html_functions.append_python_signature(python_signatures[signature], anchor_tmp_list, soup)
98-
#print(signature)
99-
# reset anchor temporary list
100-
anchor_tmp_list[:] = []
101-
html_functions.update_html(ROOT_DIR + file, soup)
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))

doc/tools/doxygen_scan.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
class Anchor(object):
2-
anchor = ""
3-
type = ""
4-
cppname = ""
1+
import traceback
52

3+
class Symbol(object):
64
def __init__(self, anchor, type, cppname):
75
self.anchor = anchor
86
self.type = type
97
self.cppname = cppname
8+
#if anchor == 'ga586ebfb0a7fb604b35a23d85391329be':
9+
# print(repr(self))
10+
# traceback.print_stack()
11+
12+
def __repr__(self):
13+
return '%s:%s@%s' % (self.type, self.cppname, self.anchor)
1014

1115
def add_to_file(files_dict, file, anchor):
12-
if file in files_dict:
13-
# if that file already exists as a key in the dictionary
14-
files_dict[file].append(anchor)
15-
else:
16-
files_dict[file] = [anchor]
17-
return files_dict
16+
anchors = files_dict.setdefault(file, [])
17+
anchors.append(anchor)
1818

1919

2020
def scan_namespace_constants(ns, ns_name, files_dict):
@@ -25,8 +25,7 @@ def scan_namespace_constants(ns, ns_name, files_dict):
2525
file = c.find("./anchorfile").text
2626
anchor = c.find("./anchor").text
2727
#print(' CONST: {} => {}#{}'.format(name, file, anchor))
28-
files_dict = add_to_file(files_dict, file, Anchor(anchor, "const", name))
29-
return files_dict
28+
add_to_file(files_dict, file, Symbol(anchor, "const", name))
3029

3130
def scan_namespace_functions(ns, ns_name, files_dict):
3231
functions = ns.findall("./member[@kind='function']")
@@ -36,8 +35,7 @@ def scan_namespace_functions(ns, ns_name, files_dict):
3635
file = f.find("./anchorfile").text
3736
anchor = f.find("./anchor").text
3837
#print(' FN: {} => {}#{}'.format(name, file, anchor))
39-
files_dict = add_to_file(files_dict, file, Anchor(anchor, "fn", name))
40-
return files_dict
38+
add_to_file(files_dict, file, Symbol(anchor, "fn", name))
4139

4240
def scan_class_methods(c, c_name, files_dict):
4341
methods = c.findall("./member[@kind='function']")
@@ -47,5 +45,4 @@ def scan_class_methods(c, c_name, files_dict):
4745
file = m.find("./anchorfile").text
4846
anchor = m.find("./anchor").text
4947
#print(' Method: {} => {}#{}'.format(name, file, anchor))
50-
files_dict = add_to_file(files_dict, file, Anchor(anchor, "method", name))
51-
return files_dict
48+
add_to_file(files_dict, file, Symbol(anchor, "method", name))

0 commit comments

Comments
 (0)