Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# If you would like to license the source code under different terms,
# please contact James Kermode, james.kermode@gmail.com
import logging
import os
import warnings
import numpy as np
from f90wrap import codegen as cg
from f90wrap import fortran as ft
from f90wrap.six import string_types # Python 2/3 compatibility library
from f90wrap.transform import ArrayDimensionConverter
class F90WrapperGenerator(ft.FortranVisitor, cg.CodeGenerator):
"""
Creates the Fortran90 code necessary to wrap a given Fortran parse tree
suitable for input to `f2py`.
Each node of the tree (Module, Subroutine etc.) is wrapped according to the
rules in this class when visited (using `F90WrapperGenerator.visit()`).
Each module's wrapper is written to a separate file, with top-level
procedures written to another separate file. Derived-types and arrays (both
of normal types and derive-types) are specially treated. For each, a number
of subroutines allowing the getting/setting of items, and retrieval of array
length are written. Furthermore, derived-types are treated as opaque
references to enable wrapping with `f2py`.
Parameters
----------
doc.append("\t%s" % d)
doc.append("")
elif isinstance(node, ft.Interface):
# for interfaces, list the components
doc.append('')
doc.append('Overloaded interface containing the following procedures:')
for proc in node.procedures:
doc.append(' %s' % (hasattr(proc, 'method_name')
and proc.method_name or proc.name))
doc += [''] + node.doc[:] # incoming docstring from Fortran source
return '\n'.join(['"""'] + doc + ['"""'])
class PythonWrapperGenerator(ft.FortranVisitor, cg.CodeGenerator):
def __init__(self, prefix, mod_name, types, f90_mod_name=None,
make_package=False, kind_map=None, init_file=None,
py_mod_names=None, class_names=None, max_length=None):
if max_length is None:
max_length = 80
cg.CodeGenerator.__init__(self, indent=' ' * 4,
max_length=max_length,
continuation='\\',
comment='#')
ft.FortranVisitor.__init__(self)
self.prefix = prefix
self.py_mod_name = mod_name
self.py_mod_names = py_mod_names
self.class_names = class_names
if f90_mod_name is None:
f90_mod_name = '_' + mod_name
def __init__(self, prefix, sizeof_fortran_t, string_lengths, abort_func,
kind_map, types, default_to_inout, max_length=None):
if max_length is None:
max_length = 120
cg.CodeGenerator.__init__(self, indent=' ' * 4,
max_length=max_length,
continuation='&',
comment='!')
ft.FortranVisitor.__init__(self)
self.prefix = prefix
self.sizeof_fortran_t = sizeof_fortran_t
self.string_lengths = string_lengths
self.abort_func = abort_func
self.kind_map = kind_map
self.types = types
self.default_to_inout = default_to_inout
def __init__(self, prefix, mod_name, types, f90_mod_name=None,
make_package=False, kind_map=None, init_file=None,
py_mod_names=None, class_names=None, max_length=None):
if max_length is None:
max_length = 80
cg.CodeGenerator.__init__(self, indent=' ' * 4,
max_length=max_length,
continuation='\\',
comment='#')
ft.FortranVisitor.__init__(self)
self.prefix = prefix
self.py_mod_name = mod_name
self.py_mod_names = py_mod_names
self.class_names = class_names
if f90_mod_name is None:
f90_mod_name = '_' + mod_name
self.f90_mod_name = f90_mod_name
self.types = types
self.imports = set()
self.make_package = make_package
if kind_map is None:
kind_map = {}