1
+ from __future__ import print_function
1
2
import logging
2
3
import os
3
4
import codecs
4
- import cv2
5
-
5
+ from pprint import pprint
6
6
7
7
try :
8
+ import bs4
8
9
from bs4 import BeautifulSoup
9
10
except ImportError :
10
11
raise ImportError ('Error: '
@@ -63,14 +64,12 @@ def add_signature_to_table(tmp_soup, new_row, signature, function_name, language
63
64
else :
64
65
new_item = tmp_soup .new_tag ('td' )
65
66
66
- if "-> None" in signature :
67
- pass
68
- elif "->" in signature :
69
- new_item .append (signature .split ("->" , 1 )[1 ] + ' =' )
67
+ if str (signature .get ('ret' , None )) != "None" :
68
+ new_item .append (signature .get ('ret' ) + ' =' )
70
69
new_row .append (new_item )
71
70
72
71
if "Python" in language :
73
- function_name = "cv2." + function_name
72
+ pass # function_name = "cv2." + function_name
74
73
elif "Java" in language :
75
74
# get word before function_name (= output)
76
75
str_before_bracket = signature .split ('(' , 1 )[0 ]
@@ -79,8 +78,8 @@ def add_signature_to_table(tmp_soup, new_row, signature, function_name, language
79
78
new_item .append (output + " " )
80
79
new_row .append (new_item )
81
80
82
- new_row = add_item (tmp_soup , new_row , False , function_name + '(' )
83
- new_row = add_item (tmp_soup , new_row , True , get_text_between_substrings ( signature , "(" , ")" ) )
81
+ new_row = add_item (tmp_soup , new_row , False , signature . get ( 'name' , function_name ) + '(' )
82
+ new_row = add_item (tmp_soup , new_row , True , signature [ 'arg' ] )
84
83
new_row = add_item (tmp_soup , new_row , False , ')' )
85
84
return new_row
86
85
@@ -100,98 +99,81 @@ def add_bolded(tmp_soup, new_row, text):
100
99
return new_row
101
100
102
101
103
- def append_table_to ( cpp_table , tmp_soup , language , signature , function_name ):
102
+ def create_description ( tmp_soup , language , signatures , function_name ):
104
103
""" Insert the new Python / Java table after the current html c++ table """
105
- if signature != "" :
106
- tmp_table = tmp_soup .new_tag ('table' )
107
- new_row = tmp_soup .new_tag ('tr' )
108
- new_row = add_bolded (tmp_soup , new_row , language )
109
- ident = False
110
-
111
- if len (signature ) > 120 :
112
- new_row = new_line (tmp_soup , tmp_table , new_row )
113
- ident = True
114
-
115
- if " or " in signature :
116
- ident = True
117
- for tmp_sig in signature .split (" or " ):
118
- new_row = new_line (tmp_soup , tmp_table , new_row )
119
- new_row = add_signature_to_table (tmp_soup , new_row , tmp_sig , function_name , language , ident )
120
- new_row = new_line (tmp_soup , tmp_table , new_row )
121
- else :
122
- new_row = add_signature_to_table (tmp_soup , new_row , signature , function_name , language , ident )
123
- tmp_table .append (new_row )
104
+ assert signatures
105
+ tmp_table = tmp_soup .new_tag ('table' )
106
+ new_row = tmp_soup .new_tag ('tr' )
107
+ new_row = add_bolded (tmp_soup , new_row , language )
108
+ ident = False
124
109
125
- cpp_table . insert_after ( tmp_table )
126
- return cpp_table
110
+ new_row = new_line ( tmp_soup , tmp_table , new_row )
111
+ ident = True
127
112
113
+ for s in signatures :
114
+ new_row = new_line (tmp_soup , tmp_table , new_row )
115
+ new_row = add_signature_to_table (tmp_soup , new_row , s , function_name , language , ident )
116
+ new_row = new_line (tmp_soup , tmp_table , new_row )
128
117
129
- def add_signatures (tmp_soup , tmp_dir , ADD_JAVA , ADD_PYTHON , module_name ):
118
+ return tmp_table
119
+
120
+
121
+ def add_signatures (tmp_soup , tmp_dir , module_name , config ):
130
122
""" Add signatures to the current soup and rewrite the html file"""
123
+
131
124
logging .debug (tmp_dir )
132
125
sign_counter = 0
133
126
python_sign_counter = 0
134
127
java_sign_counter = 0
135
128
136
- if ADD_JAVA :
129
+ if config . ADD_JAVA :
137
130
functions_file = "java_doc_txts/" + module_name + "/functions.txt"
138
131
if os .path .exists (functions_file ):
139
132
with open (functions_file , 'r' ) as f :
140
133
java_signatures = f .read ().split ("\n " )
141
134
else :
142
- ADD_JAVA = False # This C++ module (module_name) may not exist in Java
135
+ config . ADD_JAVA = False # This C++ module (module_name) may not exist in Java
143
136
144
137
# the HTML tag & class being used to find functions
145
138
for function in tmp_soup .findAll ("h2" , {"class" : "memtitle" }):
146
- function_name = function .getText ()
147
- if os .name == 'nt' : # if Windows
148
- function_name = function_name .encode ("ascii" ,"ignore" ).decode ()
149
-
150
- # all functions have () in it's name
151
- if "()" not in function_name :
139
+ function_name = None
140
+ for c in function .contents :
141
+ if isinstance (c , bs4 .element .NavigableString ):
142
+ fn = str (c ).encode ("ascii" ,"ignore" ).decode ().strip ()
143
+ if not fn .endswith ('()' ): # all functions have () in it's name
144
+ # enums, structures, etc
145
+ continue
146
+ function_name = fn [:- 2 ]
147
+
148
+ if not function_name :
152
149
continue
153
150
154
- if "[" in function_name :
155
- if "[1/" in function_name :
156
- function_name = function_name .replace (' ' , '' )[:- 7 ]
157
- else :
158
- continue
159
- else :
160
- function_name = function_name .replace (' ' , '' )[:- 2 ]
161
151
sign_counter += 1
162
152
163
- # if not Windows computer
164
- if os .name != 'nt' :
165
- function_name = function_name .replace (' ' , '' )[2 :]
166
-
167
153
cpp_table = function .findNext ('table' )
168
154
169
- if ADD_PYTHON :
170
- try :
155
+ if config .ADD_PYTHON :
156
+ signatures = config .python_signatures .get ("cv::" + str (function_name ), None )
157
+ if signatures :
171
158
print (function_name )
172
- method = getattr (cv2 , str (function_name ))
173
- description = str (method .__doc__ ).split ("\n " )
174
- signature = ""
175
- is_first_sig = True
176
- for line in description :
177
- if line .startswith ("." ) or line == "" :
178
- continue
179
- else :
180
- if is_first_sig :
181
- signature += line
182
- is_first_sig = False
183
- else :
184
- signature += " or " + line
185
-
186
- cpp_table = append_table_to (cpp_table , tmp_soup , "Python:" , signature , function_name )
159
+
160
+ description = create_description (tmp_soup , "Python:" , signatures , function_name )
161
+ description ['class' ] = 'python_language'
162
+ old = cpp_table .next_sibling
163
+ if old .name != 'table' :
164
+ old = None
165
+ elif not 'python_language' in old .get ('class' , []):
166
+ old = None
167
+ if old is None :
168
+ cpp_table .insert_after (description )
169
+ else :
170
+ old .replace_with (description )
187
171
python_sign_counter += 1
188
- except AttributeError :
189
- continue
190
172
191
- if ADD_JAVA :
173
+ if config . ADD_JAVA :
192
174
for signature in java_signatures :
193
175
if function_name in signature :
194
- append_table_to (cpp_table , tmp_soup , "Java:" , signature , function_name )
176
+ create_description (cpp_table , tmp_soup , "Java:" , signature , function_name )
195
177
java_sign_counter += 1
196
178
break
197
179
0 commit comments