File indexing completed on 2024-05-19 05:42:15

0001 import sys
0002 from hooks import HOOKS
0003 
0004 
0005 if __name__ == "__main__":
0006     # Note: Please don't use JINJA for code generation, since some users don't have it installed.
0007     output_path = sys.argv[1]
0008     filename = 'hookbindings.inc.cpp'
0009 
0010     contents = []
0011     contents.append("// This file is automatically generated. Do not modify it directly - Use the generator file instead.")
0012     contents.append('')
0013     contents.append('#include <iostream>')
0014 
0015     for hook in HOOKS:
0016         contents.append(f'static void {hook.name}Wrapper({hook.handler} *handler)')
0017         contents.append(f'{{')
0018         contents.append(f'    auto& module = *PythonLibraryDispatcher::PyResolveContext::activeModule;')
0019         contents.append(f'    auto func = module.attr("hook{hook.name}");')
0020         contents.append(f'    auto pyLksPlugin = py::module_::import("pyLksPlugin");')
0021         contents.append(f'    (void) pyLksPlugin;')
0022         contents.append(f'    try {{')
0023         contents.append(f'        func(handler);')
0024         contents.append(f'    }} catch (py::error_already_set const& e) {{')
0025         contents.append(f'        std::cout << e.what() << "\\n";')
0026         contents.append(f'        py::module::import("traceback").attr("print_exception")(e.type(), e.value(), e.trace());')
0027         contents.append(f'    }}')
0028         contents.append(f'}}')
0029         contents.append(f'')
0030 
0031     contents.append('py::module_ *PythonLibraryDispatcher::PyResolveContext::activeModule = nullptr;')
0032     contents.append('PythonLibraryDispatcher::PyResolveContext::PyResolveContext(py::module_& module, const std::string& hookName)')
0033     contents.append(' : AbstractLibraryDispatcher::ResolveContext(nullptr)')
0034     contents.append('{')
0035     contents.append('    PythonLibraryDispatcher::PyResolveContext::activeModule = &module;')
0036     contents.append('    if (py::hasattr(module, hookName.c_str())) {')
0037     for hook in HOOKS:
0038         contents.append(f'        if (hookName == "hook{hook.name}") {{')
0039         contents.append(f'            this->hook = (functionPointer) (&{hook.name}Wrapper);')
0040         contents.append(f'        }}')
0041     contents.append('    }')
0042     contents.append('}')
0043 
0044     with open(output_path + "/ct_lvtplg_" + filename, 'w') as f:
0045         f.write('\n'.join(contents))