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