Port documentation_parser.py to Python3

My attempt with python2 on Ubuntu 20.04 failed. Let's move to Python3,
at least for this script.
This commit is contained in:
Laurent Rineau 2021-08-10 13:34:40 +02:00
parent c9780d93e4
commit 97fb041ad5
2 changed files with 21 additions and 19 deletions

View File

@ -28,7 +28,7 @@ FAILURES=()
for dir in $PATH_TO_DOC/* for dir in $PATH_TO_DOC/*
do do
OUTPUT=$(basename $dir) OUTPUT=$(basename $dir)
python2 ../documentation_parser.py $dir/xml > ./"$OUTPUT.txt" python3 ../documentation_parser.py $dir/xml > ./"$OUTPUT.txt"
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
echo "$dir OK" echo "$dir OK"
else else

View File

@ -1,3 +1,5 @@
#/usr/bin/env python3
from pyquery import PyQuery as pq from pyquery import PyQuery as pq
from collections import defaultdict from collections import defaultdict
from sys import argv from sys import argv
@ -12,17 +14,17 @@ def check_type(_in, args):
root_path=argv[1] root_path=argv[1]
d = pq(filename=op.join(op.sep, root_path,'index.xml'), parser="xml") d = pq(filename=op.join(op.sep, root_path,'index.xml'), parser="xml")
compounds=[p.text() for p in d('compound').items()] compounds=[p.text() for p in list(d('compound').items())]
types=[p.attr('kind') for p in d('compound').items()] types=[p.attr('kind') for p in list(d('compound').items())]
type_map = defaultdict(list) #map <type, name> type_map = defaultdict(list) #map <type, name>
dict_map = defaultdict(dict)#map <name, map<member type, member name>> dict_map = defaultdict(dict)#map <name, map<member type, member name>>
#FOREACH compounds : fill maps #FOREACH compounds : fill maps
for i in xrange(0,len(compounds)): for i in range(0,len(compounds)):
if check_type(types[i], "typedef"): if check_type(types[i], "typedef"):
types[i]="type" types[i]="type"
name=d('compound').children("name").eq(i).text() name=d('compound').children("name").eq(i).text()
members=[p.text() for p in d('compound').eq(i).children("member").items()] members=[p.text() for p in list(d('compound').eq(i).children("member").items())]
m_types=[p.attr('kind') for p in d('compound').eq(i).children("member").items()] m_types=[p.attr('kind') for p in list(d('compound').eq(i).children("member").items())]
if (not check_type(types[i], ['example', 'file', 'dir', 'page', 'group']) and if (not check_type(types[i], ['example', 'file', 'dir', 'page', 'group']) and
not (types[i] == "namespace" and len(members) == 0) and not (types[i] == "namespace" and len(members) == 0) and
not (types[i] == "enum" and len(members) == 0) ): not (types[i] == "enum" and len(members) == 0) ):
@ -32,7 +34,7 @@ for i in xrange(0,len(compounds)):
total_path=op.join(op.sep, root_path,filepath) total_path=op.join(op.sep, root_path,filepath)
if(op.isfile(total_path)): if(op.isfile(total_path)):
e = pq(filename=total_path, parser="xml") e = pq(filename=total_path, parser="xml")
compoundnames=[p.text() for p in e('includes').items()] compoundnames=[p.text() for p in list(e('includes').items())]
if(len(compoundnames) > 1 and compoundnames[0].find("Concept") != -1): if(len(compoundnames) > 1 and compoundnames[0].find("Concept") != -1):
types[i] = 'Concept '+types[i].lower() types[i] = 'Concept '+types[i].lower()
@ -41,7 +43,7 @@ for i in xrange(0,len(compounds)):
mtype_map = defaultdict(list)# map<member type, member name> mtype_map = defaultdict(list)# map<member type, member name>
#FOREACH member : #FOREACH member :
for j in xrange(0,len(members)): for j in range(0,len(members)):
if(check_type(types[i], ['class', 'Concept class']) if(check_type(types[i], ['class', 'Concept class'])
and m_types[j] == "function"): and m_types[j] == "function"):
m_types[j]="method" m_types[j]="method"
@ -62,7 +64,7 @@ for btype in type_map:
out=btype out=btype
if btype.endswith('s'): if btype.endswith('s'):
out+='e' out+='e'
print out.title()+'s' print(out.title()+'s')
indent+=" " indent+=" "
#FOREACH name #FOREACH name
for name in type_map[btype]: for name in type_map[btype]:
@ -74,7 +76,7 @@ for btype in type_map:
templates=[] templates=[]
if op.isfile(op.join(op.sep, root_path,filepath)): if op.isfile(op.join(op.sep, root_path,filepath)):
f=pq(filename=op.join(op.sep, root_path,filepath), parser="xml") f=pq(filename=op.join(op.sep, root_path,filepath), parser="xml")
templateparams=f("compounddef").children("templateparamlist").eq(0).children("param").items() templateparams=list(f("compounddef").children("templateparamlist").eq(0).children("param").items())
for param in templateparams: for param in templateparams:
template_type="" template_type=""
template_name="" template_name=""
@ -91,7 +93,7 @@ for btype in type_map:
complete_template+=' = '+template_defval complete_template+=' = '+template_defval
templates.append(complete_template) templates.append(complete_template)
if templates==[]:#if no child was found, just take param.text() if templates==[]:#if no child was found, just take param.text()
templates=[t.text() for t in param.items()] templates=[t.text() for t in list(param.items())]
suffix="<" suffix="<"
#as template got type, defname and declname, name is twice in template. keep only one of them. #as template got type, defname and declname, name is twice in template. keep only one of them.
to_remove=[""] to_remove=[""]
@ -101,7 +103,7 @@ for btype in type_map:
suffix="" suffix=""
if suffix.endswith(', '): if suffix.endswith(', '):
suffix = suffix[:-2]+'>' suffix = suffix[:-2]+'>'
print indent+name+suffix print(indent+name+suffix)
indent+=" " indent+=" "
#FOREACH mtype #FOREACH mtype
@ -109,7 +111,7 @@ for btype in type_map:
out=mtype out=mtype
if mtype.endswith('s'): if mtype.endswith('s'):
out+='e' out+='e'
print indent+out.title()+'s' print(indent+out.title()+'s')
indent+=" " indent+=" "
#FOREACH member #FOREACH member
overload_map = defaultdict(int) #contains the number of times a member has appeared (to manage the overloads) overload_map = defaultdict(int) #contains the number of times a member has appeared (to manage the overloads)
@ -123,16 +125,16 @@ for btype in type_map:
if op.isfile(op.join(op.sep, root_path,filepath)): if op.isfile(op.join(op.sep, root_path,filepath)):
f=pq(filename=op.join(op.sep, root_path,filepath), parser="xml") f=pq(filename=op.join(op.sep, root_path,filepath), parser="xml")
index=0 index=0
memberdefs=[m.text() for m in f("memberdef").items()] memberdefs=[m.text() for m in list(f("memberdef").items())]
for i in xrange(0,len(memberdefs)): for i in range(0,len(memberdefs)):
member_names=[member_name.text() for member_name in f('memberdef').eq(i).children("name").items()] member_names=[member_name.text() for member_name in list(f('memberdef').eq(i).children("name").items())]
if f('memberdef').eq(i).children("name").text() == member: if f('memberdef').eq(i).children("name").text() == member:
if (index < overload_map[member]): if (index < overload_map[member]):
index+=1 index+=1
elif (index == overload_map[member]): elif (index == overload_map[member]):
if check_type(mtype, ['function', 'method']): if check_type(mtype, ['function', 'method']):
args=[f('memberdef').eq(i).children("argsstring").text()] args=[f('memberdef').eq(i).children("argsstring").text()]
templateparams=f('memberdef').eq(i).children("templateparamlist").children("param").items() templateparams=list(f('memberdef').eq(i).children("templateparamlist").children("param").items())
if check_type(mtype, ['function', 'method', 'type', 'variable']): if check_type(mtype, ['function', 'method', 'type', 'variable']):
return_type=[f('memberdef').eq(i).children("type").text()] return_type=[f('memberdef').eq(i).children("type").text()]
break; break;
@ -158,7 +160,7 @@ for btype in type_map:
complete_template+=' = '+template_defval complete_template+=' = '+template_defval
templates.append(complete_template) templates.append(complete_template)
if templates==[]:#if no child was found, just take param.text() if templates==[]:#if no child was found, just take param.text()
templates=[t.text() for t in param.items()] templates=[t.text() for t in list(param.items())]
prefix="template <" prefix="template <"
for template in templates: for template in templates:
@ -171,7 +173,7 @@ for btype in type_map:
prefix+=definition prefix+=definition
if(prefix != ""): if(prefix != ""):
prefix+=" " prefix+=" "
print indent+prefix+member+arguments print(indent+prefix+member+arguments)
overload_map[member]+=1 overload_map[member]+=1
#END foreach member #END foreach member
indent=indent[:-2] indent=indent[:-2]