2013-03-11 08:19:17 +08:00
|
|
|
#/usr/bin/env python
|
|
|
|
|
2013-03-14 15:26:18 +08:00
|
|
|
class MatlabWrapperGenerator(object):
|
|
|
|
|
2013-03-18 10:37:42 +08:00
|
|
|
def gen(self, input_files, output_dir):
|
2013-03-14 15:26:18 +08:00
|
|
|
# parse each of the files and store in a dictionary
|
|
|
|
# as a separate "namespace"
|
|
|
|
parser = CppHeaderParser()
|
|
|
|
ns = {}
|
|
|
|
for file in input_files:
|
|
|
|
# get the file name
|
|
|
|
name = os.path.splitext(os.path.basename(file))[0]
|
|
|
|
ns[name] = parser.parse(file)
|
|
|
|
|
|
|
|
# cleanify the parser output
|
|
|
|
parse_tree = ParseTree()
|
|
|
|
parse_tree.build(ns)
|
2013-03-18 09:47:48 +08:00
|
|
|
|
|
|
|
# setup the template engine
|
2013-06-18 15:29:04 +08:00
|
|
|
jtemplate = Environment(loader=PackageLoader('templates', ''), trim_blocks=True, lstrip_blocks=True)
|
2013-03-18 09:47:48 +08:00
|
|
|
|
|
|
|
# add the custom filters
|
|
|
|
jtemplate.filters['toUpperCamelCase'] = toUpperCamelCase
|
|
|
|
jtemplate.filters['toLowerCamelCase'] = toLowerCamelCase
|
|
|
|
jtemplate.filters['toUnderCase'] = toUnderCase
|
2013-06-22 08:33:24 +08:00
|
|
|
jtemplate.filters['comment'] = comment
|
|
|
|
jtemplate.filters['inputs'] = inputs
|
2013-06-22 13:55:48 +08:00
|
|
|
jtemplate.filters['ninputs'] = ninputs
|
2013-06-22 08:33:24 +08:00
|
|
|
jtemplate.filters['outputs'] = outputs
|
|
|
|
jtemplate.filters['noutputs'] = noutputs
|
2013-06-22 13:55:48 +08:00
|
|
|
jtemplate.filters['only'] = only
|
|
|
|
jtemplate.filters['void'] = void
|
|
|
|
jtemplate.filters['not'] = flip
|
|
|
|
|
2013-03-18 09:47:48 +08:00
|
|
|
|
|
|
|
# load the templates
|
2013-03-18 10:37:42 +08:00
|
|
|
tfunction = jtemplate.get_template('template_function_base.cpp')
|
|
|
|
tclassm = jtemplate.get_template('template_class_base.m')
|
|
|
|
tclassc = jtemplate.get_template('template_class_base.cpp')
|
|
|
|
tdoc = jtemplate.get_template('template_doc_base.m')
|
|
|
|
|
|
|
|
# create the build directory
|
2013-03-18 12:48:10 +08:00
|
|
|
output_source_dir = output_dir+'/src'
|
|
|
|
output_private_dir = output_source_dir+'/private'
|
|
|
|
output_class_dir = output_dir+'/+cv'
|
|
|
|
if not os.path.isdir(output_source_dir):
|
|
|
|
os.mkdir(output_source_dir)
|
|
|
|
if not os.path.isdir(output_private_dir):
|
|
|
|
os.mkdir(output_private_dir)
|
|
|
|
if not os.path.isdir(output_class_dir):
|
|
|
|
os.mkdir(output_class_dir)
|
|
|
|
|
|
|
|
# populate templates
|
|
|
|
for namespace in parse_tree.namespaces:
|
|
|
|
# functions
|
|
|
|
for function in namespace.functions:
|
2013-06-22 08:33:24 +08:00
|
|
|
populated = tfunction.render(fun=function, time=time, includes=namespace.name)
|
2013-03-18 12:48:10 +08:00
|
|
|
with open(output_source_dir+'/'+function.name+'.cpp', 'wb') as f:
|
|
|
|
f.write(populated)
|
|
|
|
# classes
|
|
|
|
for clss in namespace.classes:
|
|
|
|
# cpp converter
|
|
|
|
populated = tclassc.render(clss=clss, time=time)
|
|
|
|
with open(output_private_dir+'/'+clss.name+'Bridge.cpp', 'wb') as f:
|
|
|
|
f.write(populated)
|
|
|
|
# matlab classdef
|
|
|
|
populated = tclassm.render(clss=clss, time=time)
|
|
|
|
with open(output_class_dir+'/'+clss.name+'.m', 'wb') as f:
|
|
|
|
f.write(populated)
|
2013-06-24 04:34:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
|
|
# add the hdr_parser to the path
|
|
|
|
import sys, re, os, time
|
|
|
|
sys.path.append(sys.argv[1])
|
|
|
|
from string import Template
|
|
|
|
from hdr_parser import CppHeaderParser
|
|
|
|
from parse_tree import ParseTree, todict
|
|
|
|
from filters import *
|
|
|
|
from jinja2 import Environment, PackageLoader
|
|
|
|
|
|
|
|
# get the IO from the command line arguments
|
|
|
|
input_files = sys.argv[2:-1]
|
|
|
|
output_dir = sys.argv[-1]
|
|
|
|
|
|
|
|
# create the generator
|
|
|
|
mwg = MatlabWrapperGenerator()
|
|
|
|
mwg.gen(input_files, output_dir)
|