2013-03-11 10:19:17 +10:00
|
|
|
#/usr/bin/env python
|
|
|
|
|
2013-03-18 12:37:42 +10:00
|
|
|
import sys, re, os, time
|
2013-03-11 10:19:17 +10:00
|
|
|
from string import Template
|
2013-03-14 17:26:18 +10:00
|
|
|
from hdr_parser import CppHeaderParser
|
|
|
|
from parse_tree import ParseTree, todict
|
2013-03-18 11:47:48 +10:00
|
|
|
from filters import *
|
|
|
|
from jinja2 import Environment, PackageLoader
|
2013-03-14 17:26:18 +10:00
|
|
|
|
|
|
|
class MatlabWrapperGenerator(object):
|
|
|
|
|
2013-03-18 12:37:42 +10:00
|
|
|
def gen(self, input_files, output_dir):
|
2013-03-14 17:26:18 +10: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 11:47:48 +10:00
|
|
|
|
|
|
|
# setup the template engine
|
2013-03-18 12:37:42 +10:00
|
|
|
jtemplate = Environment(loader=PackageLoader('templates', ''), trim_blocks=True)
|
2013-03-18 11:47:48 +10:00
|
|
|
|
|
|
|
# add the custom filters
|
|
|
|
jtemplate.filters['toUpperCamelCase'] = toUpperCamelCase
|
|
|
|
jtemplate.filters['toLowerCamelCase'] = toLowerCamelCase
|
|
|
|
jtemplate.filters['toUnderCase'] = toUnderCase
|
|
|
|
jtemplate.filters['comment'] = comment
|
|
|
|
|
|
|
|
# load the templates
|
2013-03-18 12:37:42 +10: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
|
|
|
|
if not os.path.isdir(output_dir):
|
|
|
|
os.mkdir(output_dir)
|
2013-03-18 11:47:48 +10:00
|
|
|
|
|
|
|
# populate!
|
2013-03-18 12:37:42 +10:00
|
|
|
function = parse_tree.namespaces[0].functions[0]
|
|
|
|
print function
|
|
|
|
populated = tfunction.render(fun=function, time=time)
|
|
|
|
with open(output_dir+'/'+function.name+'.cpp', 'wb') as f:
|
|
|
|
f.write(populated)
|
|
|
|
#for name, namespace in ns:
|
|
|
|
# for function in namespace.functions:
|
|
|
|
# print 'populating function tempaltes from '+name
|
|
|
|
# populated = tfunction.render(function)
|
|
|
|
|