pybind_wrap.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 """
4 Helper script to wrap C++ to Python with Pybind.
5 This script is installed via CMake to the user's binary directory
6 and invoked during the wrapping by CMake.
7 """
8 
9 # pylint: disable=import-error
10 
11 import argparse
12 
13 import gtwrap.interface_parser as parser
14 import gtwrap.template_instantiator as instantiator
15 from gtwrap.pybind_wrapper import PybindWrapper
16 
17 
18 def main():
19  """Main runner."""
20  arg_parser = argparse.ArgumentParser(
21  formatter_class=argparse.ArgumentDefaultsHelpFormatter)
22  arg_parser.add_argument(
23  "--src",
24  type=str,
25  required=True,
26  help="Input interface .i/.h file")
27  arg_parser.add_argument(
28  "--module_name",
29  type=str,
30  required=True,
31  help="Name of the Python module to be generated and "
32  "used in the Python `import` statement.",
33  )
34  arg_parser.add_argument(
35  "--out",
36  type=str,
37  required=True,
38  help="Name of the output pybind .cc file",
39  )
40  arg_parser.add_argument(
41  "--use-boost",
42  action="store_true",
43  help="using boost's shared_ptr instead of std's",
44  )
45  arg_parser.add_argument(
46  "--top_module_namespaces",
47  type=str,
48  default="",
49  help="C++ namespace for the top module, e.g. `ns1::ns2::ns3`. "
50  "Only the content within this namespace and its sub-namespaces "
51  "will be wrapped. The content of this namespace will be available at "
52  "the top module level, and its sub-namespaces' in the submodules.\n"
53  "For example, `import <module_name>` gives you access to a Python "
54  "`<module_name>.Class` of the corresponding C++ `ns1::ns2::ns3::Class`"
55  "and `from <module_name> import ns4` gives you access to a Python "
56  "`ns4.Class` of the C++ `ns1::ns2::ns3::ns4::Class`. ",
57  )
58  arg_parser.add_argument(
59  "--ignore",
60  nargs='*',
61  type=str,
62  help="A space-separated list of classes to ignore. "
63  "Class names must include their full namespaces.",
64  )
65  arg_parser.add_argument("--template", type=str,
66  help="The module template file")
67  args = arg_parser.parse_args()
68 
69  top_module_namespaces = args.top_module_namespaces.split("::")
70  if top_module_namespaces[0]:
71  top_module_namespaces = [''] + top_module_namespaces
72 
73  # Read in the complete interface (.i) file
74  with open(args.src, "r") as f:
75  content = f.read()
76 
77  module = parser.Module.parseString(content)
78  instantiator.instantiate_namespace_inplace(module)
79 
80  with open(args.template, "r") as f:
81  template_content = f.read()
82 
83  wrapper = PybindWrapper(
84  module=module,
85  module_name=args.module_name,
86  use_boost=args.use_boost,
87  top_module_namespaces=top_module_namespaces,
88  ignore_classes=args.ignore,
89  module_template=template_content,
90  )
91 
92  # Wrap the code and get back the cpp/cc code.
93  cc_content = wrapper.wrap()
94 
95  # Generate the C++ code which Pybind11 will use.
96  with open(args.out, "w") as f:
97  f.write(cc_content)
98 
99 
100 if __name__ == "__main__":
101  main()
def main()
Definition: pybind_wrap.py:18


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:43:44