[9ce41c6] | 1 | import sys |
---|
| 2 | import os |
---|
| 3 | import os.path |
---|
| 4 | from WrapperGenerator import WrapperGenerator |
---|
| 5 | |
---|
[642a025] | 6 | def generate_wrappers(header_dir, output_dir='.', c_wrapper_dir='.'): |
---|
[2d1b700] | 7 | nModels=0 |
---|
[0a9686d] | 8 | model_list = list() |
---|
| 9 | |
---|
[2d1b700] | 10 | for item in os.listdir(header_dir): |
---|
| 11 | toks = os.path.splitext(os.path.basename(item)) |
---|
| 12 | if toks[1]=='.h': |
---|
| 13 | nModels += 1 |
---|
| 14 | name = toks[0] |
---|
[642a025] | 15 | app = WrapperGenerator(os.path.join(header_dir, name+".h"), |
---|
| 16 | output_dir=output_dir, |
---|
| 17 | c_wrapper_dir=c_wrapper_dir) |
---|
[2d1b700] | 18 | app.read() |
---|
| 19 | app.write_c_wrapper() |
---|
| 20 | app.write_python_wrapper() |
---|
[0a9686d] | 21 | model_list.append(app.getModelName()) |
---|
| 22 | write_c_models(model_list) |
---|
[2d1b700] | 23 | print "Total number of model wrappers created is %s" % nModels |
---|
| 24 | |
---|
[0a9686d] | 25 | def write_c_models(model_list): |
---|
| 26 | # simultaneously generates 'sansmodels/installed_models.txt' |
---|
| 27 | # and 'sansmodels/src/c_models/c_models.cpp' |
---|
| 28 | model_list_file = open(os.path.join("sansmodels","installed_models.txt"),"w") |
---|
| 29 | for model in model_list: |
---|
| 30 | model_list_file.write(model + "\n") |
---|
| 31 | |
---|
| 32 | model_list_file.close() |
---|
| 33 | |
---|
| 34 | |
---|
| 35 | template_file = open(os.path.join("sansmodels","src","c_models","c_models.cpp.template"),"r") |
---|
| 36 | write_file = open(os.path.join("sansmodels","src","c_models","c_models.cpp"),"w") |
---|
| 37 | buf = template_file.read() |
---|
| 38 | lines = buf.split('\n') |
---|
| 39 | |
---|
| 40 | tag1 = "[TAG_1]" |
---|
| 41 | tag2 = "[TAG_2]" |
---|
| 42 | |
---|
| 43 | for line in lines: |
---|
| 44 | |
---|
| 45 | if line.count(tag1) > 0: |
---|
| 46 | write_file.write("\n // adding generated code \n") |
---|
| 47 | for pyclass in model_list: |
---|
| 48 | write_file.write("void addC" + pyclass |
---|
| 49 | + "(PyObject *module); \n") |
---|
| 50 | write_file.write(" // end generated code \n") |
---|
| 51 | |
---|
| 52 | |
---|
| 53 | elif line.count(tag2) > 0: |
---|
| 54 | write_file.write("\n // adding generated code \n") |
---|
| 55 | |
---|
| 56 | for pyclass in model_list: |
---|
| 57 | write_file.write("addC" + pyclass + "(m); \n") |
---|
| 58 | |
---|
| 59 | write_file.write("addDisperser(m); \n") |
---|
| 60 | write_file.write("\n // end generated code \n") |
---|
| 61 | |
---|
| 62 | |
---|
| 63 | else: |
---|
| 64 | write_file.write(line + "\n") |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | |
---|
[2d1b700] | 68 | if __name__ == '__main__': |
---|
[101065a] | 69 | header_dir = os.path.join('..', 'include') |
---|
[642a025] | 70 | generate_wrappers(header_dir, output_dir="../sans/models/", c_wrapper_dir='.') |
---|
[9ce41c6] | 71 | |
---|
| 72 | |
---|
[0a9686d] | 73 | |
---|
| 74 | |
---|