Exemplo n.º 1
0
def modpyx(mod, classes=None):
    """Generates a pyx Cython implementation file for exposing C/C++ data to 
    other Cython wrappers based off of a dictionary description.

    Parameters
    ----------
    mod : dict
        Module description dictonary.
    classes : dict, optional
        Dictionary which maps all class names that are required to 
        their own descriptions.  This is required for resolving class heirarchy
        dependencies.

    Returns
    -------
    pyx : str
        Cython pyx header file as in-memory string.

    """
    m = {'extra': mod.get('extra', ''), 
         'docstring': mod.get('docstring', "no docstring, please file a bug report!"), 
         "pyx_filename": mod.get("pyx_filename", "")}
    attrs = []
    import_tups = set()
    cimport_tups = set()
    for name, desc in mod.iteritems():
        if isclassdesc(desc):
            i_tup, ci_tup, attr_str = classpyx(desc, classes=classes)
        elif isfuncdesc(desc):
            i_tup, ci_tup, attr_str = funcpyx(desc)
        else:
            continue
        import_tups |= i_tup
        cimport_tups |= ci_tup
        attrs.append(attr_str)
    m['imports'] = "\n".join(sorted(cython_imports(import_tups)))
    m['cimports'] = "\n".join(sorted(cython_cimports(cimport_tups)))
    if 'numpy' in m['cimports']:
        m['imports'] += "\n\nnp.import_array()"
    m['attrs_block'] = "\n".join(attrs)
    t = '\n\n'.join([AUTOGEN_WARNING, '{cimports}', '{attrs_block}', '{extra}'])
    pyx = _pyx_mod_template.format(**m)
    return pyx
Exemplo n.º 2
0
def modcpppxd(mod, exception_type='+'):
    """Generates a cpp_*.pxd Cython header file for exposing a C/C++ module to
    other Cython wrappers based off of a dictionary description of the module.

    Parameters
    ----------
    mod : dict
        Module description dictonary.
    exception_type : str, optional
        Cython exception annotation.  Set to None when exceptions should not 
        be included.

    Returns
    -------
    cpppxd : str
        Cython cpp_*.pxd header file as in-memory string.

    """
    m = {'extra': mod.get('extra', ''), 
         "cpppxd_filename": mod.get("cpppxd_filename", "")}
    attrs = []
    cimport_tups = set()
    for name, desc in mod.iteritems():
        if isclassdesc(desc):
            ci_tup, attr_str = classcpppxd(desc, exception_type)
        elif isfuncdesc(desc):
            ci_tup, attr_str = funccpppxd(desc, exception_type)
        else:
            continue
        cimport_tups |= ci_tup
        attrs.append(attr_str)
    m['cimports'] = "\n".join(sorted(cython_cimports(cimport_tups)))
    m['attrs_block'] = "\n".join(attrs)
    t = '\n\n'.join([AUTOGEN_WARNING, '{cimports}', '{attrs_block}', '{extra}'])
    cpppxd = t.format(**m)
    return cpppxd