def genpyx(env, classes=None): """Generates all pyx Cython implementation files for an environment of modules. Parameters ---------- env : dict Environment dictonary mapping target module names to module description dictionaries. classes : dict, optional Dictionary which maps all class names that are required to their own descriptions. This is required for resolving class heirarchy dependencies. If None, this will be computed here. Returns ------- pyxs : str Maps environment target names to Cython pxd header files strings. """ if classes is None: # get flat namespace of class descriptions classes = {} for envname, mod in env.iteritems(): for modname, desc in mod.iteritems(): if isclassdesc(desc): classes[desc['name']] = desc # gen files pyxs = {} for name, mod in env.iteritems(): if mod['pyx_filename'] is None: continue pyxs[name] = modpyx(mod, classes=classes) return pyxs
def modpxd(mod): """Generates a pxd Cython header file for exposing C/C++ data to other Cython wrappers based off of a dictionary description. Parameters ---------- mod : dict Module description dictonary. Returns ------- pxd : str Cython .pxd header file as in-memory string. """ m = {'extra': mod.get('extra', ''), "pxd_filename": mod.get("pxd_filename", "")} attrs = [] cimport_tups = set() for name, desc in mod.iteritems(): if isclassdesc(desc): ci_tup, attr_str = classpxd(desc) # no need to wrap functions again 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}']) pxd = t.format(**m) return pxd
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
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