From 2c03dde87ee0ae84a3051b707b62581d75475f2b Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Mon, 25 Mar 2024 13:13:29 +0000 Subject: [PATCH] #4 use constants in templates --- cppwg/templates/pybind11_default.py | 22 ++++++++++++---------- cppwg/utils/constants.py | 2 ++ cppwg/writers/class_writer.py | 15 +++++++++------ pyproject.toml | 8 +++++++- 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/cppwg/templates/pybind11_default.py b/cppwg/templates/pybind11_default.py index 2c2904b..b4dfd26 100644 --- a/cppwg/templates/pybind11_default.py +++ b/cppwg/templates/pybind11_default.py @@ -1,43 +1,45 @@ +from cppwg.utils.constants import CPPWG_CLASS_OVERRIDE_SUFFIX, CPPWG_EXT + class_cpp_header = """\ #include #include {includes} -#include "{class_short_name}.cppwg.hpp" +#include "{class_short_name}.%s.hpp" namespace py = pybind11; typedef {class_full_name} {class_short_name}; {smart_ptr_handle}; -""" +""" % CPPWG_EXT class_cpp_header_chaste = """\ #include #include {includes} //#include "PythonObjectConverters.hpp" -#include "{class_short_name}.cppwg.hpp" +#include "{class_short_name}.%s.hpp" namespace py = pybind11; //PYBIND11_CVECTOR_TYPECASTER2(); //PYBIND11_CVECTOR_TYPECASTER3(); typedef {class_full_name} {class_short_name}; {smart_ptr_handle}; -""" +""" % CPPWG_EXT class_hpp_header = """\ -#ifndef {class_short_name}_hpp__cppwg_wrapper -#define {class_short_name}_hpp__cppwg_wrapper +#ifndef {class_short_name}_hpp__%s_wrapper +#define {class_short_name}_hpp__%s_wrapper #include void register_{class_short_name}_class(pybind11::module &m); -#endif // {class_short_name}_hpp__cppwg_wrapper -""" +#endif // {class_short_name}_hpp__%s_wrapper +""" % tuple([CPPWG_EXT]*3) class_virtual_override_header = """\ -class {class_short_name}_Overloads : public {class_short_name}{{ +class {class_short_name}%s : public {class_short_name}{{ public: using {class_short_name}::{class_base_name}; -""" +""" % CPPWG_CLASS_OVERRIDE_SUFFIX class_virtual_override_footer = "}\n" diff --git a/cppwg/utils/constants.py b/cppwg/utils/constants.py index 686eb40..32799c5 100644 --- a/cppwg/utils/constants.py +++ b/cppwg/utils/constants.py @@ -10,3 +10,5 @@ CPPWG_FALSE_STRINGS = ["OFF", "NO", "N", "FALSE", "F"] CPPWG_DEFAULT_WRAPPER_DIR = "cppwg_wrappers" + +CPPWG_CLASS_OVERRIDE_SUFFIX = "_Overrides" diff --git a/cppwg/writers/class_writer.py b/cppwg/writers/class_writer.py index e0eaa9f..cc5f9ea 100644 --- a/cppwg/writers/class_writer.py +++ b/cppwg/writers/class_writer.py @@ -9,7 +9,11 @@ from pygccxml.declarations.class_declaration import class_t from cppwg.input.class_info import CppClassInfo -from cppwg.utils.constants import CPPWG_EXT, CPPWG_HEADER_COLLECTION_FILENAME +from cppwg.utils.constants import ( + CPPWG_CLASS_OVERRIDE_SUFFIX, + CPPWG_EXT, + CPPWG_HEADER_COLLECTION_FILENAME, +) from cppwg.writers.base_writer import CppBaseWrapperWriter from cppwg.writers.constructor_writer import CppConstructorWrapperWriter from cppwg.writers.method_writer import CppMethodWrapperWriter @@ -204,7 +208,7 @@ def add_virtual_overrides( # Override virtual methods if methods_needing_override: # Add virtual override class, e.g.: - # class Foo_Overloads : public Foo{{ + # class Foo_Overrides : public Foo{{ # public: # using Foo::Foo; override_header_dict = { @@ -293,13 +297,12 @@ def write(self, work_dir: str) -> None: self.add_virtual_overrides(class_decl, short_name) ) - # Add the virtual "trampoline" overrides from "Foo_Overloads" to + # Add the virtual "trampoline" overrides from "Foo_Overrides" to # the "Foo" wrapper class definition if needed - # e.g. py::class_(m, "Foo") + # e.g. py::class_(m, "Foo") overrides_string = "" if methods_needing_override: - # TODO: Assign the "_Overloads" literal to a constant - overrides_string = f", {short_name}_Overloads" + overrides_string = f", {short_name}{CPPWG_CLASS_OVERRIDE_SUFFIX}" # Add smart pointer support to the wrapper class definition if needed # e.g. py::class_ >(m, "Foo") diff --git a/pyproject.toml b/pyproject.toml index 4ca84ea..6856659 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,6 +25,7 @@ classifiers = [ "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", "Programming Language :: Python :: Implementation :: CPython", "Topic :: Software Development", ] @@ -45,7 +46,12 @@ Repository = "https://github.com/Chaste/cppwg/" [tool.black] target-version = ["py38", "py39", "py310", "py311", "py312"] -extend-exclude = "^/examples/shapes/wrapper/pybind11/" +extend-exclude = """ +( + ^/examples/shapes/wrapper/pybind11/ + | ^/cppwg/templates/ +) +""" [tool.isort] profile = "black"