2013-03-14 15:26:18 +08:00
|
|
|
from string import join
|
|
|
|
from textwrap import fill
|
2013-06-27 15:25:48 +08:00
|
|
|
from filters import *
|
2013-03-14 15:26:18 +08:00
|
|
|
|
|
|
|
class ParseTree(object):
|
2013-08-11 22:52:27 +08:00
|
|
|
"""
|
2013-09-02 22:16:46 +08:00
|
|
|
The ParseTree class produces a semantic tree of C++ definitions given
|
2013-08-11 22:52:27 +08:00
|
|
|
the output of the CppHeaderParser (from opencv/modules/python/src2/hdr_parser.py)
|
|
|
|
|
|
|
|
The full hierarchy is as follows:
|
|
|
|
|
|
|
|
Namespaces
|
|
|
|
|
|
|
|
|
|- name
|
|
|
|
|- Classes
|
|
|
|
|
|
|
|
|
|- name
|
|
|
|
|- Methods
|
|
|
|
|- Constants
|
|
|
|
|- Methods
|
|
|
|
|
|
|
|
|
|- name
|
|
|
|
|- static (T/F)
|
|
|
|
|- return type
|
|
|
|
|- required Arguments
|
|
|
|
|
|
|
|
|
|- name
|
|
|
|
|- const (T/F)
|
|
|
|
|- reference ('&'/'*')
|
|
|
|
|- type
|
|
|
|
|- input
|
|
|
|
|- output (pass return by reference)
|
|
|
|
|- default value
|
|
|
|
|- optional Arguments
|
|
|
|
|- Constants
|
|
|
|
|
|
|
|
|
|- name
|
|
|
|
|- const (T/F)
|
|
|
|
|- reference ('&'/'*')
|
|
|
|
|- type
|
|
|
|
|- value
|
|
|
|
|
|
|
|
The semantic tree contains substantial information for easily introspecting
|
|
|
|
information about objects. How many methods does the 'core' namespace have?
|
|
|
|
Does the 'randn' method have any return by reference (output) arguments?
|
|
|
|
How many required and optional arguments does the 'add' method have? Is the
|
|
|
|
variable passed by reference or raw pointer?
|
|
|
|
|
|
|
|
Individual definitions from the parse tree (Classes, Functions, Constants)
|
|
|
|
are passed to the Jinja2 template engine where they are manipulated to
|
|
|
|
produce Matlab mex sources.
|
|
|
|
|
|
|
|
A common call tree for constructing and using a ParseTree object is:
|
|
|
|
|
|
|
|
# parse a set of definitions into a dictionary of namespaces
|
|
|
|
parser = CppHeaderParser()
|
|
|
|
ns['core'] = parser.parse('path/to/opencv/core.hpp')
|
|
|
|
|
|
|
|
# refactor into a semantic tree
|
|
|
|
parse_tree = ParseTree()
|
|
|
|
parse_tree.build(ns)
|
|
|
|
|
|
|
|
# iterate over the tree
|
|
|
|
for namespace in parse_tree.namespaces:
|
|
|
|
for clss in namespace.classes:
|
|
|
|
# do stuff
|
|
|
|
for method in namespace.methods:
|
|
|
|
# do stuff
|
|
|
|
|
|
|
|
Calling 'print' on a ParseTree object will reconstruct the definitions
|
|
|
|
to produce an output resembling the original C++ code.
|
|
|
|
"""
|
2013-03-18 10:37:42 +08:00
|
|
|
def __init__(self, namespaces=None):
|
|
|
|
self.namespaces = namespaces if namespaces else []
|
2013-03-14 15:26:18 +08:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return join((ns.__str__() for ns in self.namespaces), '\n\n\n')
|
|
|
|
|
|
|
|
def build(self, namespaces):
|
|
|
|
babel = Translator()
|
|
|
|
for name, definitions in namespaces.items():
|
|
|
|
class_tree = {}
|
2013-07-03 07:42:37 +08:00
|
|
|
methods = []
|
2013-03-14 15:26:18 +08:00
|
|
|
constants = []
|
|
|
|
for defn in definitions:
|
2013-09-02 22:16:46 +08:00
|
|
|
obj = babel.translate(defn)
|
2013-06-28 09:13:04 +08:00
|
|
|
if obj is None:
|
|
|
|
continue
|
2013-03-14 15:26:18 +08:00
|
|
|
if type(obj) is Class or obj.clss:
|
|
|
|
self.insertIntoClassTree(obj, class_tree)
|
2013-07-03 07:42:37 +08:00
|
|
|
elif type(obj) is Method:
|
|
|
|
methods.append(obj)
|
2013-03-14 15:26:18 +08:00
|
|
|
elif type(obj) is Constant:
|
|
|
|
constants.append(obj)
|
|
|
|
else:
|
|
|
|
raise TypeError('Unexpected object type: '+str(type(obj)))
|
2013-07-03 07:42:37 +08:00
|
|
|
self.namespaces.append(Namespace(name, constants, class_tree.values(), methods))
|
2013-03-14 15:26:18 +08:00
|
|
|
|
|
|
|
def insertIntoClassTree(self, obj, class_tree):
|
|
|
|
cname = obj.name if type(obj) is Class else obj.clss
|
|
|
|
if not cname:
|
|
|
|
return
|
|
|
|
if not cname in class_tree:
|
|
|
|
# add a new class to the tree
|
|
|
|
class_tree[cname] = Class(cname)
|
|
|
|
# insert the definition into the class
|
|
|
|
val = class_tree[cname]
|
2013-07-03 07:42:37 +08:00
|
|
|
if type(obj) is Method:
|
|
|
|
val.methods.append(obj)
|
2013-03-14 15:26:18 +08:00
|
|
|
elif type(obj) is Constant:
|
|
|
|
val.constants.append(obj)
|
|
|
|
else:
|
|
|
|
raise TypeError('Unexpected object type: '+str(type(obj)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Translator(object):
|
2013-08-11 22:52:27 +08:00
|
|
|
"""
|
2013-09-02 22:16:46 +08:00
|
|
|
The Translator class does the heavy lifting of translating the nested
|
2013-08-11 22:52:27 +08:00
|
|
|
list representation of the hdr_parser into individual definitions that
|
|
|
|
are inserted into the ParseTree.
|
|
|
|
Translator consists of a top-level method: translate()
|
|
|
|
along with a number of helper methods: translateClass(), translateMethod(),
|
|
|
|
translateArgument(), translateConstant(), translateName(), and
|
|
|
|
translateClassName()
|
|
|
|
"""
|
2013-03-14 15:26:18 +08:00
|
|
|
def translate(self, defn):
|
|
|
|
# --- class ---
|
2013-09-02 22:16:46 +08:00
|
|
|
# classes have 'class' prefixed on their name
|
2013-06-28 02:45:19 +08:00
|
|
|
if 'class' in defn[0].split(' ') or 'struct' in defn[0].split(' '):
|
2013-03-14 15:26:18 +08:00
|
|
|
return self.translateClass(defn)
|
2013-06-28 07:25:18 +08:00
|
|
|
# --- operators! ---
|
|
|
|
#TODO: implement operators: http://www.mathworks.com.au/help/matlab/matlab_oop/implementing-operators-for-your-class.html
|
|
|
|
if 'operator' in defn[0]:
|
2013-06-28 09:13:04 +08:00
|
|
|
return
|
2013-06-28 07:25:18 +08:00
|
|
|
# --- constant ---
|
|
|
|
elif convertibleToInt(defn[1]):
|
|
|
|
return self.translateConstant(defn)
|
2013-03-14 15:26:18 +08:00
|
|
|
# --- function ---
|
|
|
|
# functions either need to have input arguments, or not uppercase names
|
2013-06-27 15:25:48 +08:00
|
|
|
elif defn[3] or not self.translateName(defn[0]).split('_')[0].isupper():
|
2013-07-03 07:42:37 +08:00
|
|
|
return self.translateMethod(defn)
|
2013-03-14 15:26:18 +08:00
|
|
|
# --- constant ---
|
|
|
|
else:
|
|
|
|
return self.translateConstant(defn)
|
|
|
|
|
|
|
|
def translateClass(self, defn):
|
|
|
|
return Class()
|
|
|
|
|
2013-07-03 07:42:37 +08:00
|
|
|
def translateMethod(self, defn, class_tree=None):
|
2013-03-14 15:26:18 +08:00
|
|
|
name = self.translateName(defn[0])
|
|
|
|
clss = self.translateClassName(defn[0])
|
|
|
|
rtp = defn[1]
|
2013-09-02 22:16:46 +08:00
|
|
|
static = True if 'S' in ''.join(defn[2]) else False
|
2013-03-14 15:26:18 +08:00
|
|
|
args = defn[3]
|
2013-09-02 22:16:46 +08:00
|
|
|
req = []
|
2013-03-14 15:26:18 +08:00
|
|
|
opt = []
|
|
|
|
for arg in args:
|
|
|
|
if arg:
|
|
|
|
a = self.translateArgument(arg)
|
|
|
|
opt.append(a) if a.default else req.append(a)
|
2013-07-03 07:42:37 +08:00
|
|
|
return Method(name, clss, static, '', rtp, False, req, opt)
|
2013-09-02 22:16:46 +08:00
|
|
|
|
2013-03-14 15:26:18 +08:00
|
|
|
def translateConstant(self, defn):
|
|
|
|
const = True if 'const' in defn[0] else False
|
|
|
|
name = self.translateName(defn[0])
|
|
|
|
clss = self.translateClassName(defn[0])
|
|
|
|
tp = 'int'
|
|
|
|
val = defn[1]
|
|
|
|
return Constant(name, clss, tp, const, '', val)
|
|
|
|
|
|
|
|
def translateArgument(self, defn):
|
2013-06-22 13:55:48 +08:00
|
|
|
ref = '*' if '*' in defn[0] else ''
|
|
|
|
ref = '&' if '&' in defn[0] else ref
|
|
|
|
const = ' const ' in ' '+defn[0]+' '
|
|
|
|
tp = " ".join([word for word in defn[0].replace(ref, '').split() if not ' const ' in ' '+word+' '])
|
2013-03-14 15:26:18 +08:00
|
|
|
name = defn[1]
|
2013-06-22 08:33:24 +08:00
|
|
|
default = defn[2] if defn[2] else ''
|
2013-06-22 13:55:48 +08:00
|
|
|
modifiers = ''.join(defn[3])
|
|
|
|
I = True if not modifiers or 'I' in modifiers else False
|
|
|
|
O = True if 'O' in modifiers else False
|
|
|
|
return Argument(name, tp, const, I, O, ref, default)
|
2013-03-14 15:26:18 +08:00
|
|
|
|
|
|
|
def translateName(self, name):
|
|
|
|
return name.split(' ')[-1].split('.')[-1]
|
|
|
|
|
|
|
|
def translateClassName(self, name):
|
2013-06-28 02:45:19 +08:00
|
|
|
name = name.split(' ')[-1]
|
2013-03-14 15:26:18 +08:00
|
|
|
parts = name.split('.')
|
2013-06-28 02:45:19 +08:00
|
|
|
return parts[-2] if len(parts) > 1 and not parts[-2] == 'cv' else ''
|
2013-03-14 15:26:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Namespace(object):
|
2013-08-11 22:52:27 +08:00
|
|
|
"""
|
|
|
|
Namespace
|
|
|
|
|
|
|
|
|
|- name
|
|
|
|
|- Constants
|
|
|
|
|- Methods
|
|
|
|
|- Constants
|
2013-09-02 22:16:46 +08:00
|
|
|
"""
|
2013-07-03 07:42:37 +08:00
|
|
|
def __init__(self, name='', constants=None, classes=None, methods=None):
|
2013-03-14 15:26:18 +08:00
|
|
|
self.name = name
|
|
|
|
self.constants = constants if constants else []
|
|
|
|
self.classes = classes if classes else []
|
2013-07-03 07:42:37 +08:00
|
|
|
self.methods = methods if methods else []
|
2013-03-14 15:26:18 +08:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return 'namespace '+self.name+' {\n\n'+\
|
2013-03-14 15:31:39 +08:00
|
|
|
(join((c.__str__() for c in self.constants), '\n')+'\n\n' if self.constants else '')+\
|
2013-07-03 07:42:37 +08:00
|
|
|
(join((f.__str__() for f in self.methods), '\n')+'\n\n' if self.methods else '')+\
|
2013-03-14 15:31:39 +08:00
|
|
|
(join((o.__str__() for o in self.classes), '\n\n') if self.classes else '')+'\n};'
|
2013-03-14 15:26:18 +08:00
|
|
|
|
|
|
|
class Class(object):
|
2013-08-11 22:52:27 +08:00
|
|
|
"""
|
|
|
|
Class
|
|
|
|
|
|
|
|
|
|- name
|
|
|
|
|- Methods
|
|
|
|
|- Constants
|
|
|
|
"""
|
2013-07-03 07:42:37 +08:00
|
|
|
def __init__(self, name='', namespace='', constants=None, methods=None):
|
2013-03-14 15:26:18 +08:00
|
|
|
self.name = name
|
|
|
|
self.namespace = namespace
|
|
|
|
self.constants = constants if constants else []
|
2013-07-03 07:42:37 +08:00
|
|
|
self.methods = methods if methods else []
|
2013-03-14 15:26:18 +08:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return 'class '+self.name+' {\n\t'+\
|
|
|
|
(join((c.__str__() for c in self.constants), '\n\t')+'\n\n\t' if self.constants else '')+\
|
2013-07-03 07:42:37 +08:00
|
|
|
(join((f.__str__() for f in self.methods), '\n\t') if self.methods else '')+'\n};'
|
2013-03-14 15:26:18 +08:00
|
|
|
|
2013-07-03 07:42:37 +08:00
|
|
|
class Method(object):
|
2013-08-11 22:52:27 +08:00
|
|
|
"""
|
|
|
|
Method
|
|
|
|
int VideoWriter::read( cv::Mat& frame, const cv::Mat& mask=cv::Mat() );
|
|
|
|
--- ----- ---- -------- ----------------
|
|
|
|
rtp class name required optional
|
|
|
|
|
|
|
|
name the method name
|
|
|
|
clss the class the method belongs to ('' if free)
|
|
|
|
static static?
|
|
|
|
namespace the namespace the method belongs to ('' if free)
|
|
|
|
rtp the return type
|
|
|
|
const const?
|
|
|
|
req list of required arguments
|
|
|
|
opt list of optional arguments
|
|
|
|
"""
|
2013-06-22 13:55:48 +08:00
|
|
|
def __init__(self, name='', clss='', static=False, namespace='', rtp='', const=False, req=None, opt=None):
|
2013-03-14 15:26:18 +08:00
|
|
|
self.name = name
|
|
|
|
self.clss = clss
|
2013-07-03 07:42:37 +08:00
|
|
|
self.constructor = True if name == clss else False
|
2013-06-22 13:55:48 +08:00
|
|
|
self.static = static
|
2013-03-14 15:26:18 +08:00
|
|
|
self.const = const
|
|
|
|
self.namespace = namespace
|
2013-09-02 22:16:46 +08:00
|
|
|
self.rtp = rtp
|
2013-03-14 15:26:18 +08:00
|
|
|
self.req = req if req else []
|
|
|
|
self.opt = opt if opt else []
|
|
|
|
|
|
|
|
def __str__(self):
|
2013-03-18 12:48:10 +08:00
|
|
|
return (self.rtp+' ' if self.rtp else '')+self.name+'('+\
|
2013-03-14 15:26:18 +08:00
|
|
|
join((arg.__str__() for arg in self.req+self.opt), ', ')+\
|
2013-03-18 12:48:10 +08:00
|
|
|
')'+(' const' if self.const else '')+';'
|
2013-03-14 15:26:18 +08:00
|
|
|
|
|
|
|
class Argument(object):
|
2013-08-11 22:52:27 +08:00
|
|
|
"""
|
|
|
|
Argument
|
|
|
|
const cv::Mat& mask=cv::Mat()
|
|
|
|
----- ---- --- ---- -------
|
|
|
|
const tp ref name default
|
|
|
|
|
|
|
|
name the argument name
|
|
|
|
tp the argument type
|
|
|
|
const const?
|
|
|
|
I is the argument treated as an input?
|
|
|
|
O is the argument treated as an output (return by reference)
|
|
|
|
ref is the argument passed by reference? ('*'/'&')
|
|
|
|
default the default value of the argument ('' if required)
|
|
|
|
"""
|
2013-06-22 13:55:48 +08:00
|
|
|
def __init__(self, name='', tp='', const=False, I=True, O=False, ref='', default=''):
|
2013-03-14 15:26:18 +08:00
|
|
|
self.name = name
|
|
|
|
self.tp = tp
|
|
|
|
self.ref = ref
|
2013-06-22 13:55:48 +08:00
|
|
|
self.I = I
|
|
|
|
self.O = O
|
2013-03-14 15:26:18 +08:00
|
|
|
self.const = const
|
|
|
|
self.default = default
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return ('const ' if self.const else '')+self.tp+self.ref+\
|
|
|
|
' '+self.name+('='+self.default if self.default else '')
|
|
|
|
|
|
|
|
class Constant(object):
|
2013-08-11 22:52:27 +08:00
|
|
|
"""
|
|
|
|
Constant
|
|
|
|
DFT_COMPLEX_OUTPUT = 12;
|
|
|
|
---- -------
|
|
|
|
name default
|
|
|
|
|
|
|
|
name the name of the constant
|
|
|
|
clss the class that the constant belongs to ('' if free)
|
|
|
|
tp the type of the constant ('' if int)
|
|
|
|
const const?
|
|
|
|
ref is the constant a reference? ('*'/'&')
|
|
|
|
default default value, required for constants
|
|
|
|
"""
|
2013-03-14 15:26:18 +08:00
|
|
|
def __init__(self, name='', clss='', tp='', const=False, ref='', default=''):
|
|
|
|
self.name = name
|
|
|
|
self.clss = clss
|
|
|
|
self.tp = tp
|
|
|
|
self.ref = ref
|
|
|
|
self.const = const
|
|
|
|
self.default = default
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return ('const ' if self.const else '')+self.tp+self.ref+\
|
|
|
|
' '+self.name+('='+self.default if self.default else '')+';'
|
|
|
|
|
2013-06-27 06:45:05 +08:00
|
|
|
def constants(tree):
|
2013-08-11 22:52:27 +08:00
|
|
|
"""
|
|
|
|
recursive generator to strip all Constant objects from the ParseTree
|
|
|
|
and place them into a flat dictionary of { name, value (default) }
|
|
|
|
"""
|
2013-06-27 06:45:05 +08:00
|
|
|
if isinstance(tree, dict) and 'constants' in tree and isinstance(tree['constants'], list):
|
|
|
|
for node in tree['constants']:
|
|
|
|
yield (node['name'], node['default'])
|
|
|
|
if isinstance(tree, dict):
|
|
|
|
for key, val in tree.items():
|
|
|
|
for gen in constants(val):
|
|
|
|
yield gen
|
|
|
|
if isinstance(tree, list):
|
|
|
|
for val in tree:
|
|
|
|
for gen in constants(val):
|
|
|
|
yield gen
|
2013-03-14 15:26:18 +08:00
|
|
|
|
|
|
|
def todict(obj, classkey=None):
|
2013-08-11 22:52:27 +08:00
|
|
|
"""
|
|
|
|
Convert the ParseTree to a dictionary, stripping all objects of their
|
|
|
|
methods and converting class names to strings
|
|
|
|
"""
|
2013-03-14 15:26:18 +08:00
|
|
|
if isinstance(obj, dict):
|
|
|
|
for k in obj.keys():
|
|
|
|
obj[k] = todict(obj[k], classkey)
|
|
|
|
return obj
|
|
|
|
elif hasattr(obj, "__iter__"):
|
|
|
|
return [todict(v, classkey) for v in obj]
|
|
|
|
elif hasattr(obj, "__dict__"):
|
|
|
|
data = dict([(key, todict(value, classkey))
|
|
|
|
for key, value in obj.__dict__.iteritems()
|
|
|
|
if not callable(value) and not key.startswith('_')])
|
|
|
|
if classkey is not None and hasattr(obj, "__class__"):
|
|
|
|
data[classkey] = obj.__class__.__name__
|
|
|
|
return data
|
|
|
|
else:
|
|
|
|
return obj
|