Skip to content

Commit

Permalink
Updated json2class for namespace issues, overloaded python property f…
Browse files Browse the repository at this point in the history
…unctions
  • Loading branch information
whaeck committed Jun 23, 2021
1 parent 8b7bc79 commit 597282b
Show file tree
Hide file tree
Showing 17 changed files with 144 additions and 143 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ pybind11_add_module( GNDStk.python
python/src/v1.9/containers/Values.python.cpp
python/src/v1.9/containers/Link.python.cpp
python/src/v1.9/containers/Grid.python.cpp
python/src/v1.9/containers/Axes.python.cpp
python/src/v1.9/containers/XYs1d.python.cpp
python/src/v1.9/containers/Regions1d.python.cpp
python/src/v1.9/transport/CrossSection.python.cpp
python/src/v1.9/transport/Reaction.python.cpp
python/src/v1.9/transport/Reactions.python.cpp
python/src/v1.9/transport/ReactionSuite.python.cpp
python/src/v1.9/transport.python.cpp
python/src/v1.9/GNDS.python.cpp
)
Expand Down
39 changes: 21 additions & 18 deletions autogen/json2class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1654,15 +1654,15 @@ void file_python_namespace(

const std::string underver = replace(Version,'.','_');
cpp << "// " << Version << " interface\n";
cpp << "namespace " << underver << " {\n\n";
cpp << "namespace python_" << underver << " {\n\n";

cpp << "// " << nsname << " declarations\n";
cpp << "namespace " << nsname << " {\n";
cpp << "namespace python_" << nsname << " {\n";
for (auto &cl : sortedClassDependencies) {
if (cl.name.first == nsname)
cpp << " void wrap" << cl.name.second << "(python::module &);\n";
}
cpp << "} // namespace " << nsname << "\n";
cpp << "} // namespace python_" << nsname << "\n";
cpp << "\n";

cpp << "// " << nsname << " wrapper\n";
Expand All @@ -1671,18 +1671,18 @@ void file_python_namespace(
cpp << " // create the " << nsname << " submodule\n";
cpp << " python::module submodule = module.def_submodule(\n";
cpp << " \"" << nsname << "\",\n";
cpp << " \"GNDS v1.9 " << nsname << "\"\n";
cpp << " \"GNDS " << Version << " " << nsname << "\"\n";
cpp << " );\n";

cpp << "\n // wrap " << nsname << " components\n";
for (auto &cl : sortedClassDependencies) {
if (cl.name.first == nsname)
cpp << " " << nsname << "::wrap" << cl.name.second
cpp << " python_" << nsname << "::wrap" << cl.name.second
<< "(submodule);\n";
}
cpp << "};\n";

cpp << "\n} // namespace " << underver << "\n";
cpp << "\n} // namespace python_" << underver << "\n";
}


Expand Down Expand Up @@ -1735,17 +1735,18 @@ void file_python_class(const NameDeps &obj, const std::string &filePythonCPP)
cpp << "namespace python = pybind11;\n";
cpp << "\n";

cpp << "namespace " << VersionNamespace << " {\n";
cpp << "namespace " << nsname << " {\n";
cpp << "namespace python_" << VersionNamespace << " {\n";
cpp << "namespace python_" << nsname << " {\n";
cpp << "\n";

cpp << "// " << clname << " wrapper\n";
cpp << "void wrap" << clname << "(python::module &module)\n";
cpp << "{\n";
cpp << " using namespace njoy::GNDStk;\n";
cpp << " using namespace njoy::GNDStk::" << VersionNamespace << ";\n";
cpp << "\n";
cpp << " // type aliases\n";
cpp << " using Component = " << "njoy::GNDStk::" << VersionNamespace << "::" << nsname << "::" << clname << ";\n";
cpp << " using Component = " << nsname << "::" << clname << ";\n";

// using VARIANT = ..., if necessary
for ( const auto& child : cinfo ) {
Expand All @@ -1755,7 +1756,7 @@ void file_python_class(const NameDeps &obj, const std::string &filePythonCPP)
for (const auto &c : cinfo) {
if ( c.isChoice ) {
cpp << ( count++ ? "," : "" ) << "\n";
cpp << " njoy::GNDStk::" << VersionNamespace << "::" << c.varType;
cpp << " " << c.varType;
}
}
cpp << "\n";
Expand Down Expand Up @@ -1858,12 +1859,14 @@ void file_python_class(const NameDeps &obj, const std::string &filePythonCPP)
cpp << " )\n";
}
for (auto &c : cinfo) {
const auto pythonname = toPythonName(c.varName);
cpp << " .def_property_readonly(\n";
cpp << " \"" << pythonname << "\",\n";
cpp << " &Component::" << c.varName << ",\n";
cpp << " Component::documentation(\"" << pythonname << "\").c_str()\n";
cpp << " )\n";
if (!(c.isChoice && c.isVector)) {
const auto pythonname = toPythonName(c.varName);
cpp << " .def_property_readonly(\n";
cpp << " \"" << pythonname << "\",\n";
cpp << " python::overload_cast<>(&Component::" << c.varName << "),\n";
cpp << " Component::documentation(\"" << pythonname << "\").c_str()\n";
cpp << " )\n";
}
}

if (hasBodyText) {
Expand All @@ -1881,8 +1884,8 @@ void file_python_class(const NameDeps &obj, const std::string &filePythonCPP)
cpp << " addStandardComponentDefinitions< Component >( component );\n";
cpp << "}\n";
cpp << "\n";
cpp << "} // namespace " << nsname << "\n";
cpp << "} // namespace " << VersionNamespace << "\n";
cpp << "} // namespace python_" << nsname << "\n";
cpp << "} // namespace python_" << VersionNamespace << "\n";
}


Expand Down
4 changes: 2 additions & 2 deletions python/src/GNDStk.python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace core {
}

// v1.9 interface declarations
namespace v1_9 {
namespace python_v1_9 {

void wrapGNDS( python::module& );
}
Expand Down Expand Up @@ -45,5 +45,5 @@ PYBIND11_MODULE( GNDStk, module ) {
core::wrapInterpolation( module );

// v1.9 components (in the v1_9 module, created in this function)
v1_9::wrapGNDS( module );
python_v1_9::wrapGNDS( module );
}
8 changes: 4 additions & 4 deletions python/src/v1.9/GNDS.python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
namespace python = pybind11;

// v1.9 interface declarations
namespace v1_9 {
namespace python_v1_9 {

void wrapContainers( python::module& );
void wrapTransport( python::module& );
}

namespace v1_9 {
namespace python_v1_9 {

void wrapGNDS( python::module& module ) {

Expand All @@ -25,7 +25,7 @@ namespace v1_9 {
"GNDS 1.9 standard components"
);

v1_9::wrapContainers( submodule );
v1_9::wrapTransport( submodule );
python_v1_9::wrapContainers( submodule );
python_v1_9::wrapTransport( submodule );
}
} // v1_9 namespace
28 changes: 14 additions & 14 deletions python/src/v1.9/containers.python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
namespace python = pybind11;

// v1.9 interface
namespace v1_9 {
namespace python_v1_9 {

// containers declarations
namespace containers {
namespace python_containers {
void wrapAxis(python::module &);
void wrapLink(python::module &);
void wrapValues(python::module &);
void wrapGrid(python::module &);
// void wrapAxes(python::module &);
// void wrapXYs1d(python::module &);
// void wrapRegions1d(python::module &);
} // namespace containers
void wrapAxes(python::module &);
void wrapXYs1d(python::module &);
void wrapRegions1d(python::module &);
} // namespace python_containers

// containers wrapper
void wrapContainers(python::module &module)
Expand All @@ -31,13 +31,13 @@ void wrapContainers(python::module &module)
);

// wrap containers components
containers::wrapAxis(submodule);
containers::wrapLink(submodule);
containers::wrapValues(submodule);
containers::wrapGrid(submodule);
// containers::wrapAxes(submodule);
// containers::wrapXYs1d(submodule);
// containers::wrapRegions1d(submodule);
python_containers::wrapAxis(submodule);
python_containers::wrapLink(submodule);
python_containers::wrapValues(submodule);
python_containers::wrapGrid(submodule);
python_containers::wrapAxes(submodule);
python_containers::wrapXYs1d(submodule);
python_containers::wrapRegions1d(submodule);
};

} // namespace v1_9
} // namespace python_v1_9
27 changes: 9 additions & 18 deletions python/src/v1.9/containers/Axes.python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@
// namespace aliases
namespace python = pybind11;

namespace v1_9 {
namespace containers {
namespace python_v1_9 {
namespace python_containers {

// Axes wrapper
void wrapAxes(python::module &module)
{
using namespace njoy::GNDStk;
using namespace njoy::GNDStk::v1_9;

// type aliases
using Component = njoy::GNDStk::v1_9::containers::Axes;
using Component = containers::Axes;
using VARIANT = std::variant<
njoy::GNDStk::v1_9::containers::Axis,
njoy::GNDStk::v1_9::containers::Grid
containers::Axis,
containers::Grid
>;

// create the component
Expand All @@ -51,19 +52,9 @@ void wrapAxes(python::module &module)
&Component::href,
Component::documentation("href").c_str()
)
.def_property_readonly(
"axis",
&Component::axis,
Component::documentation("axis").c_str()
)
.def_property_readonly(
"grid",
&Component::grid,
Component::documentation("grid").c_str()
)
.def_property_readonly(
"choice",
&Component::choice,
python::overload_cast<>(&Component::choice),
Component::documentation("choice").c_str()
)
;
Expand All @@ -72,5 +63,5 @@ void wrapAxes(python::module &module)
addStandardComponentDefinitions< Component >( component );
}

} // namespace containers
} // namespace v1_9
} // namespace python_containers
} // namespace python_v1_9
11 changes: 6 additions & 5 deletions python/src/v1.9/containers/Axis.python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@
// namespace aliases
namespace python = pybind11;

namespace v1_9 {
namespace containers {
namespace python_v1_9 {
namespace python_containers {

// Axis wrapper
void wrapAxis(python::module &module)
{
using namespace njoy::GNDStk;
using namespace njoy::GNDStk::v1_9;

// type aliases
using Component = njoy::GNDStk::v1_9::containers::Axis;
using Component = containers::Axis;

// create the component
python::class_<Component> component(
Expand Down Expand Up @@ -65,5 +66,5 @@ void wrapAxis(python::module &module)
addStandardComponentDefinitions< Component >( component );
}

} // namespace containers
} // namespace v1_9
} // namespace python_containers
} // namespace python_v1_9
21 changes: 11 additions & 10 deletions python/src/v1.9/containers/Grid.python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@
// namespace aliases
namespace python = pybind11;

namespace v1_9 {
namespace containers {
namespace python_v1_9 {
namespace python_containers {

// Grid wrapper
void wrapGrid(python::module &module)
{
using namespace njoy::GNDStk;
using namespace njoy::GNDStk::v1_9;

// type aliases
using Component = njoy::GNDStk::v1_9::containers::Grid;
using Component = containers::Grid;
using VARIANT = std::variant<
njoy::GNDStk::v1_9::containers::Link,
njoy::GNDStk::v1_9::containers::Values
containers::Link,
containers::Values
>;

// create the component
Expand Down Expand Up @@ -81,17 +82,17 @@ void wrapGrid(python::module &module)
)
.def_property_readonly(
"link",
&Component::link,
python::overload_cast<>(&Component::link),
Component::documentation("link").c_str()
)
.def_property_readonly(
"values",
&Component::values,
python::overload_cast<>(&Component::values),
Component::documentation("values").c_str()
)
.def_property_readonly(
"choice",
&Component::choice,
python::overload_cast<>(&Component::choice),
Component::documentation("choice").c_str()
)
;
Expand All @@ -100,5 +101,5 @@ void wrapGrid(python::module &module)
addStandardComponentDefinitions< Component >( component );
}

} // namespace containers
} // namespace v1_9
} // namespace python_containers
} // namespace python_v1_9
11 changes: 6 additions & 5 deletions python/src/v1.9/containers/Link.python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@
// namespace aliases
namespace python = pybind11;

namespace v1_9 {
namespace containers {
namespace python_v1_9 {
namespace python_containers {

// Link wrapper
void wrapLink(python::module &module)
{
using namespace njoy::GNDStk;
using namespace njoy::GNDStk::v1_9;

// type aliases
using Component = njoy::GNDStk::v1_9::containers::Link;
using Component = containers::Link;

// create the component
python::class_<Component> component(
Expand Down Expand Up @@ -51,5 +52,5 @@ void wrapLink(python::module &module)
addStandardComponentDefinitions< Component >( component );
}

} // namespace containers
} // namespace v1_9
} // namespace python_containers
} // namespace python_v1_9
Loading

0 comments on commit 597282b

Please sign in to comment.