csdms / babelizer

Transform BMI-wrapped models into Python packages
https://babelizer.readthedocs.io
MIT License
4 stars 3 forks source link

Add test for wrapping C++ library #26

Closed mcflugen closed 3 years ago

mcflugen commented 3 years ago

This pull request does a couple things: add tests for wrapping C++ libraries, changes babel.toml (while maintaining backward compatibility).

The main change to babel.toml is to the library section: it is now a dictionary of dictionaries (or table of tables) and removes the entry_point key. The new format (in toml) looks like the following:

[library.HeatBMI]  # HeatBMI is the name of the class in Python
language = "c++"
library = "heat_bmi"  # the library being wrapped, e.g. libheat_bmi.so
header = "heat_bmi.hxx"  # header file that declares the BMI class
class = "Heat"  # the name of the class that implements a BMI

In yaml land this would be,

library: {
    HeatBMI: {
        language: c++,
        library: heat_bmi,
        header: heat_bmi.hxx,
        class: Heat,
    }
}

Using the old-style entry_points, the above is equivalent to HeatBMI=heat_bmi:Heat (notice the old-style doesn't specify a header file, which is a problem for some languages). Multiple [library.*] sections can be provided but, as of now, they all have to be of the same language.

Another change is that the plugin section is now called package as it describes the newly-created Python package. The keys are the same, i.e.

[package]
name = "heat"
requirements = []

The way things are right now, the above would create a new package called pymt_heat but I think we should change it so that a user proves the full name of the package (i.e. name = pymt_heat).

mdpiper commented 3 years ago

@mcflugen This is awesome! I'll update the docs with these changes.

mcflugen commented 3 years ago

@mdpiper I ended up changing the package section so that name refers to the full name of the new package. That is, if you want the new package to be called pymt_heat, you would do the following

[package]
name = "pymt_heat"

rather than name = "heat".

For babelize generate I added new options to specify values in the library section and removed the --entry-points option. The example in the README now becomes,

$ babelize generate babel.toml \
    --no-input \
    --package=pymt_hydrotrend \
    --summary="PyMT plugin for hydrotrend" \
    --langauge=c \
    --library=bmi_hydrotrend \
    --header=bmi_hydrotrend.h \
    --class=register_bmi_hydrotrend \
    --name=Hydrotrend \
    --requirement=hydrotrend

Only a single library can be specified with babelize generate. I realize it's not great and isn't concise but it works to get someone started. I'm happy to consider alternatives, if you have ideas.

mdpiper commented 3 years ago

@mcflugen Thanks for doing this. I'll update the README and the docs to use the new syntax.