def buildmodule()

in source/predictive_maintenance/numpy/f2py/rules.py [0:0]


def buildmodule(m, um):
    """
    Return
    """
    global f2py_version, options
    outmess('\tBuilding module "%s"...\n' % (m['name']))
    ret = {}
    mod_rules = defmod_rules[:]
    vrd = capi_maps.modsign2map(m)
    rd = dictappend({'f2py_version': f2py_version}, vrd)
    funcwrappers = []
    funcwrappers2 = []  # F90 codes
    for n in m['interfaced']:
        nb = None
        for bi in m['body']:
            if not bi['block'] == 'interface':
                errmess('buildmodule: Expected interface block. Skipping.\n')
                continue
            for b in bi['body']:
                if b['name'] == n:
                    nb = b
                    break

        if not nb:
            errmess(
                'buildmodule: Could not found the body of interfaced routine "%s". Skipping.\n' % (n))
            continue
        nb_list = [nb]
        if 'entry' in nb:
            for k, a in nb['entry'].items():
                nb1 = copy.deepcopy(nb)
                del nb1['entry']
                nb1['name'] = k
                nb1['args'] = a
                nb_list.append(nb1)
        for nb in nb_list:
            api, wrap = buildapi(nb)
            if wrap:
                if ismoduleroutine(nb):
                    funcwrappers2.append(wrap)
                else:
                    funcwrappers.append(wrap)
            ar = applyrules(api, vrd)
            rd = dictappend(rd, ar)

    # Construct COMMON block support
    cr, wrap = common_rules.buildhooks(m)
    if wrap:
        funcwrappers.append(wrap)
    ar = applyrules(cr, vrd)
    rd = dictappend(rd, ar)

    # Construct F90 module support
    mr, wrap = f90mod_rules.buildhooks(m)
    if wrap:
        funcwrappers2.append(wrap)
    ar = applyrules(mr, vrd)
    rd = dictappend(rd, ar)

    for u in um:
        ar = use_rules.buildusevars(u, m['use'][u['name']])
        rd = dictappend(rd, ar)

    needs = cfuncs.get_needs()
    code = {}
    for n in needs.keys():
        code[n] = []
        for k in needs[n]:
            c = ''
            if k in cfuncs.includes0:
                c = cfuncs.includes0[k]
            elif k in cfuncs.includes:
                c = cfuncs.includes[k]
            elif k in cfuncs.userincludes:
                c = cfuncs.userincludes[k]
            elif k in cfuncs.typedefs:
                c = cfuncs.typedefs[k]
            elif k in cfuncs.typedefs_generated:
                c = cfuncs.typedefs_generated[k]
            elif k in cfuncs.cppmacros:
                c = cfuncs.cppmacros[k]
            elif k in cfuncs.cfuncs:
                c = cfuncs.cfuncs[k]
            elif k in cfuncs.callbacks:
                c = cfuncs.callbacks[k]
            elif k in cfuncs.f90modhooks:
                c = cfuncs.f90modhooks[k]
            elif k in cfuncs.commonhooks:
                c = cfuncs.commonhooks[k]
            else:
                errmess('buildmodule: unknown need %s.\n' % (repr(k)))
                continue
            code[n].append(c)
    mod_rules.append(code)
    for r in mod_rules:
        if ('_check' in r and r['_check'](m)) or ('_check' not in r):
            ar = applyrules(r, vrd, m)
            rd = dictappend(rd, ar)
    ar = applyrules(module_rules, rd)

    fn = os.path.join(options['buildpath'], vrd['coutput'])
    ret['csrc'] = fn
    f = open(fn, 'w')
    f.write(ar['modulebody'].replace('\t', 2 * ' '))
    f.close()
    outmess('\tWrote C/API module "%s" to file "%s"\n' % (m['name'], fn))

    if options['dorestdoc']:
        fn = os.path.join(
            options['buildpath'], vrd['modulename'] + 'module.rest')
        f = open(fn, 'w')
        f.write('.. -*- rest -*-\n')
        f.write('\n'.join(ar['restdoc']))
        f.close()
        outmess('\tReST Documentation is saved to file "%s/%smodule.rest"\n' %
                (options['buildpath'], vrd['modulename']))
    if options['dolatexdoc']:
        fn = os.path.join(
            options['buildpath'], vrd['modulename'] + 'module.tex')
        ret['ltx'] = fn
        f = open(fn, 'w')
        f.write(
            '%% This file is auto-generated with f2py (version:%s)\n' % (f2py_version))
        if 'shortlatex' not in options:
            f.write(
                '\\documentclass{article}\n\\usepackage{a4wide}\n\\begin{document}\n\\tableofcontents\n\n')
        f.write('\n'.join(ar['latexdoc']))
        if 'shortlatex' not in options:
            f.write('\\end{document}')
        f.close()
        outmess('\tDocumentation is saved to file "%s/%smodule.tex"\n' %
                (options['buildpath'], vrd['modulename']))
    if funcwrappers:
        wn = os.path.join(options['buildpath'], vrd['f2py_wrapper_output'])
        ret['fsrc'] = wn
        f = open(wn, 'w')
        f.write('C     -*- fortran -*-\n')
        f.write(
            'C     This file is autogenerated with f2py (version:%s)\n' % (f2py_version))
        f.write(
            'C     It contains Fortran 77 wrappers to fortran functions.\n')
        lines = []
        for l in ('\n\n'.join(funcwrappers) + '\n').split('\n'):
            if l and l[0] == ' ':
                while len(l) >= 66:
                    lines.append(l[:66] + '\n     &')
                    l = l[66:]
                lines.append(l + '\n')
            else:
                lines.append(l + '\n')
        lines = ''.join(lines).replace('\n     &\n', '\n')
        f.write(lines)
        f.close()
        outmess('\tFortran 77 wrappers are saved to "%s"\n' % (wn))
    if funcwrappers2:
        wn = os.path.join(
            options['buildpath'], '%s-f2pywrappers2.f90' % (vrd['modulename']))
        ret['fsrc'] = wn
        f = open(wn, 'w')
        f.write('!     -*- f90 -*-\n')
        f.write(
            '!     This file is autogenerated with f2py (version:%s)\n' % (f2py_version))
        f.write(
            '!     It contains Fortran 90 wrappers to fortran functions.\n')
        lines = []
        for l in ('\n\n'.join(funcwrappers2) + '\n').split('\n'):
            if len(l) > 72 and l[0] == ' ':
                lines.append(l[:72] + '&\n     &')
                l = l[72:]
                while len(l) > 66:
                    lines.append(l[:66] + '&\n     &')
                    l = l[66:]
                lines.append(l + '\n')
            else:
                lines.append(l + '\n')
        lines = ''.join(lines).replace('\n     &\n', '\n')
        f.write(lines)
        f.close()
        outmess('\tFortran 90 wrappers are saved to "%s"\n' % (wn))
    return ret