fgen.wrapper_building#
fgen
’s wrapper building module
Functions to use to format generated code |
|
Generation of the Fortran manager module |
|
Generation of the Fortran wrapper module |
|
Generation of the Python enums module |
|
Generation of the Python init module |
|
Generation of the Python wrapper module |
ModuleWrapperGeneratorCallableLike#
PackageWrapperGeneratorCallableLike#
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:
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:
package (
fgen.data_models.package.Package
) – Package this module belongs toshared (
fgen.data_models.package_shared_elements.PackageSharedElements
) – Shared elements acrosspackage
module (
fgen.data_models.module.Module
) – Module to write wrappers formanager_directory (
pathlib.Path
) – Directory in which to write the manager wrapperswrapper_directory (
pathlib.Path
) – Directory in which to write the Fortran wrapperspython_directory (
pathlib.Path
) – Directory in which to write the Python wrappersextension (
str
) – Name of the extension module that will be called from Pythonforce (
bool
) – Whether to force overwrite existing files or not
- Return type:
write_module_wrapper#
- write_module_wrapper(desc, generator_function, builder, file_suffix, outdir, force)[source]#
Write a wrapper for a module
- Parameters:
desc (
str
) – Description of the wrapper being written (used for logging)generator_function (
fgen.wrapper_building.ModuleWrapperGeneratorCallableLike
[typing.TypeVar
(T_builder
, bound=typing.Union
[fgen.wrapper_building.fortran_manager_module.FortranManagerModuleBuilder
,fgen.wrapper_building.fortran_wrapper_module.FortranWrapperModuleBuilder
,fgen.wrapper_building.python_wrapper_module.PythonWrapperModuleBuilder
])]) – Function that generates the wrapper’s contentsbuilder (
typing.TypeVar
(T_builder
, bound=typing.Union
[fgen.wrapper_building.fortran_manager_module.FortranManagerModuleBuilder
,fgen.wrapper_building.fortran_wrapper_module.FortranWrapperModuleBuilder
,fgen.wrapper_building.python_wrapper_module.PythonWrapperModuleBuilder
])) – The builder to use in combination withgenerator_function
to generate the wrapper’s contentsfile_suffix (
str
) – The suffix to apply to the builder’s truncated name. This combination defines the filename.outdir (
pathlib.Path
) – Directory in which to write the wrapper.force (
bool
) – Whether to force overwrite existing files or not.
- Return type:
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 offorce
.force (
bool
) – Whether to force overwrite existing files.
- Return type:
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 forshared (
fgen.data_models.package_shared_elements.PackageSharedElements
) – Shared elements which should be consistent across the package’s generated wrappersextension (
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
) – IfTrue
, overwrite any existing files
- Raises:
FileExistsError – If
not force
and a targeted file exists- Return type: