fgen.wrapper_building#

fgen’s wrapper building module

fgen.wrapper_building.formatting

Functions to use to format generated code

fgen.wrapper_building.fortran_manager_module

Generation of the Fortran manager module

fgen.wrapper_building.fortran_wrapper_module

Generation of the Fortran wrapper module

fgen.wrapper_building.python_enums_module

Generation of the Python enums module

fgen.wrapper_building.python_init_module

Generation of the Python init module

fgen.wrapper_building.python_wrapper_module

Generation of the Python wrapper module

ModuleWrapperGeneratorCallableLike#

class ModuleWrapperGeneratorCallableLike(*args, **kwargs)[source]#

Bases: Protocol[T_contra]

Callable that can generate a wrapper for a module

PackageWrapperGeneratorCallableLike#

class PackageWrapperGeneratorCallableLike(*args, **kwargs)[source]#

Bases: Protocol

Callable that can generate a wrapper for a package

file_should_be_written#

try_to_write_file#

try_to_write_file(file, contents)[source]#

Try to write a file to disk, logging an exception if this fails.

Parameters:
  • file (pathlib.Path) – File to write to.

  • contents (str) – Contents to write in the file.

Return type:

None

write_module_wrappers#

write_module_wrappers(package, shared, module, manager_directory, wrapper_directory, python_directory, extension, force)[source]#

Write the wrappers for a module

Parameters:
Return type:

None

write_module_wrapper#

write_module_wrapper(desc, generator_function, builder, file_suffix, outdir, force)[source]#

Write a wrapper for a module

Parameters:
Return type:

None

write_package_wrapper#

write_package_wrapper(package, desc, generator_function, outdir, filename, keep_existing=False, force=False)[source]#

Write a wrapper for the package

Parameters:
  • package (fgen.data_models.package.Package) – Package for which to write the wrapper.

  • desc (str) – Description of the wrapper being written (used for logging).

  • generator_function (fgen.wrapper_building.PackageWrapperGeneratorCallableLike) – Function to call to generate the wrapper’s contents.

  • outdir (pathlib.Path) – Directory in which to write the wrapper.

  • filename (str) – Name of the file in which to write the wrapper.

  • keep_existing (bool) – Whether to keep existing files, irrespective of the value of force.

  • force (bool) – Whether to force overwrite existing files.

Return type:

None

process_package#

process_package(package, shared, extension, manager_directory, wrapper_directory, python_directory, force=True)[source]#

Auto-generate python and fortran code for a package

This function writes 3 files for each module that is being wrapped:

  • manager: Lifecycle handling for derived types (in Fortran)

  • wrapper: Provides a fortran wrapper to the generated functions that can be

    made available to Python

  • python module: Python interface for creating and manipulating derived types.

This function may also write other files necessary for wrapping the package as a whole. This includes generating a __init__.py file if it doesn’t already exist in the python directory.

Parameters:
  • package (fgen.data_models.package.Package) – Package to generate wrappers for

  • shared (fgen.data_models.package_shared_elements.PackageSharedElements) – Shared elements which should be consistent across the package’s generated wrappers

  • extension (str) –

    Package name for the compiled extension module

    Typically, this is of the form my_module._lib

  • manager_directory (pathlib.Path) – Directory in which to write the derived type lifecycle manager(s)

  • wrapper_directory (pathlib.Path) – Directory to write the Python-Fortran wrapper(s).

  • python_directory (pathlib.Path) – Directory in which to write the python module(s).

  • force (bool) – If True, overwrite any existing files

Raises:

FileExistsError – If not force and a targeted file exists

Return type:

None