converted necessary files for python3 compatibility using 2to3

pull/2403/head
Hilton Bristow 12 years ago
parent b29835a8f8
commit 2f45942adc
  1. 96
      modules/java/generator/rst_parser.py
  2. 3
      modules/matlab/generator/filters.py
  3. 21
      modules/matlab/generator/parse_tree.py

@ -1,6 +1,8 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import os, sys, re, string, fnmatch import os, sys, re, string, fnmatch
allmodules = ["core", "flann", "imgproc", "ml", "highgui", "video", "features2d", "calib3d", "objdetect", "legacy", "contrib", "cuda", "androidcamera", "java", "python", "stitching", "ts", "photo", "nonfree", "videostab", "ocl", "softcascade", "superres"] allmodules = ["core", "flann", "imgproc", "ml", "highgui", "video", "features2d", "calib3d", "objdetect", "legacy", "contrib", "cuda", "androidcamera", "java", "python", "stitching", "ts", "photo", "nonfree", "videostab", "ocl", "softcascade", "superres"]
verbose = False verbose = False
show_warnings = True show_warnings = True
@ -141,10 +143,10 @@ class RstParser(object):
def parse_section_safe(self, module_name, section_name, file_name, lineno, lines): def parse_section_safe(self, module_name, section_name, file_name, lineno, lines):
try: try:
self.parse_section(module_name, section_name, file_name, lineno, lines) self.parse_section(module_name, section_name, file_name, lineno, lines)
except AssertionError, args: except AssertionError as args:
if show_errors: if show_errors:
print >> sys.stderr, "RST parser error E%03d: assertion in \"%s\" at %s:%s" % (ERROR_001_SECTIONFAILURE, section_name, file_name, lineno) print("RST parser error E%03d: assertion in \"%s\" at %s:%s" % (ERROR_001_SECTIONFAILURE, section_name, file_name, lineno), file=sys.stderr)
print >> sys.stderr, " Details: %s" % args print(" Details: %s" % args, file=sys.stderr)
def parse_section(self, module_name, section_name, file_name, lineno, lines): def parse_section(self, module_name, section_name, file_name, lineno, lines):
self.sections_total += 1 self.sections_total += 1
@ -152,7 +154,7 @@ class RstParser(object):
#if section_name.find(" ") >= 0 and section_name.find("::operator") < 0: #if section_name.find(" ") >= 0 and section_name.find("::operator") < 0:
if (section_name.find(" ") >= 0 and not bool(re.match(r"(\w+::)*operator\s*(\w+|>>|<<|\(\)|->|\+\+|--|=|==|\+=|-=)", section_name)) ) or section_name.endswith(":"): if (section_name.find(" ") >= 0 and not bool(re.match(r"(\w+::)*operator\s*(\w+|>>|<<|\(\)|->|\+\+|--|=|==|\+=|-=)", section_name)) ) or section_name.endswith(":"):
if show_errors: if show_errors:
print >> sys.stderr, "RST parser warning W%03d: SKIPPED: \"%s\" File: %s:%s" % (WARNING_002_HDRWHITESPACE, section_name, file_name, lineno) print("RST parser warning W%03d: SKIPPED: \"%s\" File: %s:%s" % (WARNING_002_HDRWHITESPACE, section_name, file_name, lineno), file=sys.stderr)
self.sections_skipped += 1 self.sections_skipped += 1
return return
@ -311,7 +313,7 @@ class RstParser(object):
if fdecl.balance != 0: if fdecl.balance != 0:
if show_critical_errors: if show_critical_errors:
print >> sys.stderr, "RST parser error E%03d: invalid parentheses balance in \"%s\" at %s:%s" % (ERROR_003_PARENTHESES, section_name, file_name, lineno) print("RST parser error E%03d: invalid parentheses balance in \"%s\" at %s:%s" % (ERROR_003_PARENTHESES, section_name, file_name, lineno), file=sys.stderr)
return return
# save last parameter if needed # save last parameter if needed
@ -328,7 +330,7 @@ class RstParser(object):
elif func: elif func:
if func["name"] in known_text_sections_names: if func["name"] in known_text_sections_names:
if show_errors: if show_errors:
print >> sys.stderr, "RST parser warning W%03d: SKIPPED: \"%s\" File: %s:%s" % (WARNING_002_HDRWHITESPACE, section_name, file_name, lineno) print("RST parser warning W%03d: SKIPPED: \"%s\" File: %s:%s" % (WARNING_002_HDRWHITESPACE, section_name, file_name, lineno), file=sys.stderr)
self.sections_skipped += 1 self.sections_skipped += 1
elif show_errors: elif show_errors:
self.print_info(func, True, sys.stderr) self.print_info(func, True, sys.stderr)
@ -351,7 +353,7 @@ class RstParser(object):
if l.find("\t") >= 0: if l.find("\t") >= 0:
whitespace_warnings += 1 whitespace_warnings += 1
if whitespace_warnings <= max_whitespace_warnings and show_warnings: if whitespace_warnings <= max_whitespace_warnings and show_warnings:
print >> sys.stderr, "RST parser warning W%03d: tab symbol instead of space is used at %s:%s" % (WARNING_004_TABS, doc, lineno) print("RST parser warning W%03d: tab symbol instead of space is used at %s:%s" % (WARNING_004_TABS, doc, lineno), file=sys.stderr)
l = l.replace("\t", " ") l = l.replace("\t", " ")
# handle first line # handle first line
@ -388,8 +390,8 @@ class RstParser(object):
def add_new_fdecl(self, func, decl): def add_new_fdecl(self, func, decl):
if decl.fdecl.endswith(";"): if decl.fdecl.endswith(";"):
print >> sys.stderr, "RST parser error E%03d: unexpected semicolon at the end of declaration in \"%s\" at %s:%s" \ print("RST parser error E%03d: unexpected semicolon at the end of declaration in \"%s\" at %s:%s" \
% (ERROR_011_EOLEXPECTED, func["name"], func["file"], func["line"]) % (ERROR_011_EOLEXPECTED, func["name"], func["file"], func["line"]), file=sys.stderr)
decls = func.get("decls", []) decls = func.get("decls", [])
if (decl.lang == "C++" or decl.lang == "C"): if (decl.lang == "C++" or decl.lang == "C"):
rst_decl = self.cpp_parser.parse_func_decl_no_wrap(decl.fdecl) rst_decl = self.cpp_parser.parse_func_decl_no_wrap(decl.fdecl)
@ -405,37 +407,37 @@ class RstParser(object):
if show_errors: if show_errors:
#check black_list #check black_list
if decl.name not in params_blacklist.get(func["name"], []): if decl.name not in params_blacklist.get(func["name"], []):
print >> sys.stderr, "RST parser error E%03d: redefinition of parameter \"%s\" in \"%s\" at %s:%s" \ print("RST parser error E%03d: redefinition of parameter \"%s\" in \"%s\" at %s:%s" \
% (ERROR_005_REDEFENITIONPARAM, decl.name, func["name"], func["file"], func["line"]) % (ERROR_005_REDEFENITIONPARAM, decl.name, func["name"], func["file"], func["line"]), file=sys.stderr)
else: else:
params[decl.name] = decl.comment params[decl.name] = decl.comment
func["params"] = params func["params"] = params
def print_info(self, func, skipped=False, out = sys.stdout): def print_info(self, func, skipped=False, out = sys.stdout):
print >> out print(file=out)
if skipped: if skipped:
print >> out, "SKIPPED DEFINITION:" print("SKIPPED DEFINITION:", file=out)
print >> out, "name: %s" % (func.get("name","~empty~")) print("name: %s" % (func.get("name","~empty~")), file=out)
print >> out, "file: %s:%s" % (func.get("file","~empty~"), func.get("line","~empty~")) print("file: %s:%s" % (func.get("file","~empty~"), func.get("line","~empty~")), file=out)
print >> out, "is class: %s" % func.get("isclass", False) print("is class: %s" % func.get("isclass", False), file=out)
print >> out, "is struct: %s" % func.get("isstruct", False) print("is struct: %s" % func.get("isstruct", False), file=out)
print >> out, "module: %s" % func.get("module","~unknown~") print("module: %s" % func.get("module","~unknown~"), file=out)
print >> out, "namespace: %s" % func.get("namespace", "~empty~") print("namespace: %s" % func.get("namespace", "~empty~"), file=out)
print >> out, "class: %s" % (func.get("class","~empty~")) print("class: %s" % (func.get("class","~empty~")), file=out)
print >> out, "method: %s" % (func.get("method","~empty~")) print("method: %s" % (func.get("method","~empty~")), file=out)
print >> out, "brief: %s" % (func.get("brief","~empty~")) print("brief: %s" % (func.get("brief","~empty~")), file=out)
if "decls" in func: if "decls" in func:
print >> out, "declarations:" print("declarations:", file=out)
for d in func["decls"]: for d in func["decls"]:
print >> out, " %7s: %s" % (d[0], re.sub(r"[ ]+", " ", d[1])) print(" %7s: %s" % (d[0], re.sub(r"[ ]+", " ", d[1])), file=out)
if "seealso" in func: if "seealso" in func:
print >> out, "seealso: ", func["seealso"] print("seealso: ", func["seealso"], file=out)
if "params" in func: if "params" in func:
print >> out, "parameters:" print("parameters:", file=out)
for name, comment in func["params"].items(): for name, comment in func["params"].items():
print >> out, "%23s: %s" % (name, comment) print("%23s: %s" % (name, comment), file=out)
print >> out, "long: %s" % (func.get("long","~empty~")) print("long: %s" % (func.get("long","~empty~")), file=out)
print >> out print(file=out)
def validate(self, func): def validate(self, func):
if func.get("decls", None) is None: if func.get("decls", None) is None:
@ -443,13 +445,13 @@ class RstParser(object):
return False return False
if func["name"] in self.definitions: if func["name"] in self.definitions:
if show_errors: if show_errors:
print >> sys.stderr, "RST parser error E%03d: \"%s\" from: %s:%s is already documented at %s:%s" \ print("RST parser error E%03d: \"%s\" from: %s:%s is already documented at %s:%s" \
% (ERROR_006_REDEFENITIONFUNC, func["name"], func["file"], func["line"], self.definitions[func["name"]]["file"], self.definitions[func["name"]]["line"]) % (ERROR_006_REDEFENITIONFUNC, func["name"], func["file"], func["line"], self.definitions[func["name"]]["file"], self.definitions[func["name"]]["line"]), file=sys.stderr)
return False return False
return self.validateParams(func) return self.validateParams(func)
def validateParams(self, func): def validateParams(self, func):
documentedParams = func.get("params", {}).keys() documentedParams = list(func.get("params", {}).keys())
params = [] params = []
for decl in func.get("decls", []): for decl in func.get("decls", []):
@ -464,13 +466,13 @@ class RstParser(object):
# 1. all params are documented # 1. all params are documented
for p in params: for p in params:
if p not in documentedParams and show_warnings: if p not in documentedParams and show_warnings:
print >> sys.stderr, "RST parser warning W%03d: parameter \"%s\" of \"%s\" is undocumented. %s:%s" % (WARNING_007_UNDOCUMENTEDPARAM, p, func["name"], func["file"], func["line"]) print("RST parser warning W%03d: parameter \"%s\" of \"%s\" is undocumented. %s:%s" % (WARNING_007_UNDOCUMENTEDPARAM, p, func["name"], func["file"], func["line"]), file=sys.stderr)
# 2. only real params are documented # 2. only real params are documented
for p in documentedParams: for p in documentedParams:
if p not in params and show_warnings: if p not in params and show_warnings:
if p not in params_blacklist.get(func["name"], []): if p not in params_blacklist.get(func["name"], []):
print >> sys.stderr, "RST parser warning W%03d: unexisting parameter \"%s\" of \"%s\" is documented at %s:%s" % (WARNING_008_MISSINGPARAM, p, func["name"], func["file"], func["line"]) print("RST parser warning W%03d: unexisting parameter \"%s\" of \"%s\" is documented at %s:%s" % (WARNING_008_MISSINGPARAM, p, func["name"], func["file"], func["line"]), file=sys.stderr)
return True return True
def normalize(self, func): def normalize(self, func):
@ -541,7 +543,7 @@ class RstParser(object):
func["name"] = fname[4:] func["name"] = fname[4:]
func["method"] = fname[4:] func["method"] = fname[4:]
elif show_warnings: elif show_warnings:
print >> sys.stderr, "RST parser warning W%03d: \"%s\" - section name is \"%s\" instead of \"%s\" at %s:%s" % (WARNING_009_HDRMISMATCH, fname, func["name"], fname[6:], func["file"], func["line"]) print("RST parser warning W%03d: \"%s\" - section name is \"%s\" instead of \"%s\" at %s:%s" % (WARNING_009_HDRMISMATCH, fname, func["name"], fname[6:], func["file"], func["line"]), file=sys.stderr)
#self.print_info(func) #self.print_info(func)
def normalizeText(self, s): def normalizeText(self, s):
@ -632,11 +634,11 @@ class RstParser(object):
return s return s
def printSummary(self): def printSummary(self):
print "RST Parser Summary:" print("RST Parser Summary:")
print " Total sections: %s" % self.sections_total print(" Total sections: %s" % self.sections_total)
print " Skipped sections: %s" % self.sections_skipped print(" Skipped sections: %s" % self.sections_skipped)
print " Parsed sections: %s" % self.sections_parsed print(" Parsed sections: %s" % self.sections_parsed)
print " Invalid sections: %s" % (self.sections_total - self.sections_parsed - self.sections_skipped) print(" Invalid sections: %s" % (self.sections_total - self.sections_parsed - self.sections_skipped))
# statistic by language # statistic by language
stat = {} stat = {}
@ -651,12 +653,12 @@ class RstParser(object):
for decl in d.get("decls", []): for decl in d.get("decls", []):
stat[decl[0]] = stat.get(decl[0], 0) + 1 stat[decl[0]] = stat.get(decl[0], 0) + 1
print print()
print " classes documented: %s" % classes print(" classes documented: %s" % classes)
print " structs documented: %s" % structs print(" structs documented: %s" % structs)
for lang in sorted(stat.items()): for lang in sorted(stat.items()):
print " %7s functions documented: %s" % lang print(" %7s functions documented: %s" % lang)
print print()
def mathReplace2(match): def mathReplace2(match):
m = mathReplace(match) m = mathReplace(match)
@ -743,7 +745,7 @@ def mathReplace(match):
if __name__ == "__main__": if __name__ == "__main__":
if len(sys.argv) < 2: if len(sys.argv) < 2:
print "Usage:\n", os.path.basename(sys.argv[0]), " <module path>" print("Usage:\n", os.path.basename(sys.argv[0]), " <module path>")
exit(0) exit(0)
if len(sys.argv) >= 3: if len(sys.argv) >= 3:
@ -759,7 +761,7 @@ if __name__ == "__main__":
module = sys.argv[1] module = sys.argv[1]
if module != "all" and not os.path.isdir(os.path.join(rst_parser_dir, "../../" + module)): if module != "all" and not os.path.isdir(os.path.join(rst_parser_dir, "../../" + module)):
print "RST parser error E%03d: module \"%s\" could not be found." % (ERROR_010_NOMODULE, module) print("RST parser error E%03d: module \"%s\" could not be found." % (ERROR_010_NOMODULE, module))
exit(1) exit(1)
parser = RstParser(hdr_parser.CppHeaderParser()) parser = RstParser(hdr_parser.CppHeaderParser())

@ -1,5 +1,4 @@
from textwrap import TextWrapper from textwrap import TextWrapper
from string import split, join
import re, os import re, os
# precompile a URL matching regular expression # precompile a URL matching regular expression
urlexpr = re.compile(r"((https?):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)", re.MULTILINE|re.UNICODE) urlexpr = re.compile(r"((https?):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)", re.MULTILINE|re.UNICODE)
@ -177,4 +176,4 @@ def comment(text, wrap=80, escape='% ', escape_first='', escape_last=''):
escapn = '\n'+escape escapn = '\n'+escape
lines = text.split('\n') lines = text.split('\n')
wlines = (tw.wrap(line) for line in lines) wlines = (tw.wrap(line) for line in lines)
return escape_first+escape+join((join(line, escapn) for line in wlines), escapn)+escape_last return escape_first+escape+escapn.join(escapn.join(line) for line in wlines)+escape_last

@ -1,4 +1,3 @@
from string import join
from textwrap import fill from textwrap import fill
from filters import * from filters import *
@ -74,7 +73,7 @@ class ParseTree(object):
self.namespaces = namespaces if namespaces else [] self.namespaces = namespaces if namespaces else []
def __str__(self): def __str__(self):
return join((ns.__str__() for ns in self.namespaces), '\n\n\n') return '\n\n\n'.join(ns.__str__() for ns in self.namespaces)
def build(self, namespaces): def build(self, namespaces):
babel = Translator() babel = Translator()
@ -94,7 +93,7 @@ class ParseTree(object):
constants.append(obj) constants.append(obj)
else: else:
raise TypeError('Unexpected object type: '+str(type(obj))) raise TypeError('Unexpected object type: '+str(type(obj)))
self.namespaces.append(Namespace(name, constants, class_tree.values(), methods)) self.namespaces.append(Namespace(name, constants, list(class_tree.values()), methods))
def insertIntoClassTree(self, obj, class_tree): def insertIntoClassTree(self, obj, class_tree):
cname = obj.name if type(obj) is Class else obj.clss cname = obj.name if type(obj) is Class else obj.clss
@ -208,9 +207,9 @@ class Namespace(object):
def __str__(self): def __str__(self):
return 'namespace '+self.name+' {\n\n'+\ return 'namespace '+self.name+' {\n\n'+\
(join((c.__str__() for c in self.constants), '\n')+'\n\n' if self.constants else '')+\ ('\n'.join(c.__str__() for c in self.constants)+'\n\n' if self.constants else '')+\
(join((f.__str__() for f in self.methods), '\n')+'\n\n' if self.methods else '')+\ ('\n'.join(f.__str__() for f in self.methods)+'\n\n' if self.methods else '')+\
(join((o.__str__() for o in self.classes), '\n\n') if self.classes else '')+'\n};' ('\n\n'.join(o.__str__() for o in self.classes) if self.classes else '')+'\n};'
class Class(object): class Class(object):
""" """
@ -228,8 +227,8 @@ class Class(object):
def __str__(self): def __str__(self):
return 'class '+self.name+' {\n\t'+\ return 'class '+self.name+' {\n\t'+\
(join((c.__str__() for c in self.constants), '\n\t')+'\n\n\t' if self.constants else '')+\ ('\n\t'.join(c.__str__() for c in self.constants)+'\n\n\t' if self.constants else '')+\
(join((f.__str__() for f in self.methods), '\n\t') if self.methods else '')+'\n};' ('\n\t'.join(f.__str__() for f in self.methods) if self.methods else '')+'\n};'
class Method(object): class Method(object):
""" """
@ -260,7 +259,7 @@ class Method(object):
def __str__(self): def __str__(self):
return (self.rtp+' ' if self.rtp else '')+self.name+'('+\ return (self.rtp+' ' if self.rtp else '')+self.name+'('+\
join((arg.__str__() for arg in self.req+self.opt), ', ')+\ ', '.join(arg.__str__() for arg in self.req+self.opt)+\
')'+(' const' if self.const else '')+';' ')'+(' const' if self.const else '')+';'
class Argument(object): class Argument(object):
@ -343,11 +342,11 @@ def todict(obj, classkey=None):
for k in obj.keys(): for k in obj.keys():
obj[k] = todict(obj[k], classkey) obj[k] = todict(obj[k], classkey)
return obj return obj
elif hasattr(obj, "__iter__"): elif isinstance(obj, list):
return [todict(v, classkey) for v in obj] return [todict(v, classkey) for v in obj]
elif hasattr(obj, "__dict__"): elif hasattr(obj, "__dict__"):
data = dict([(key, todict(value, classkey)) data = dict([(key, todict(value, classkey))
for key, value in obj.__dict__.iteritems() for key, value in obj.__dict__.items()
if not callable(value) and not key.startswith('_')]) if not callable(value) and not key.startswith('_')])
if classkey is not None and hasattr(obj, "__class__"): if classkey is not None and hasattr(obj, "__class__"):
data[classkey] = obj.__class__.__name__ data[classkey] = obj.__class__.__name__

Loading…
Cancel
Save