3
0
mirror of https://github.com/triqs/dft_tools synced 2024-12-25 05:43:40 +01:00

wrapper: add use_module

- a module can use the converters used by another
  with the use_module('A') command.
  In which case :
    - the generate converter header for A will be included.
    - the header, at the top, now contains a simple list of all
      wrapped types, which is then included in the wrapped_types
      of the module for proper code generation.
- simplify the code generation : just generate_code.
- all arguments are analyzed from sys.argv at the import of the
  wrap_generator module. In any case, the xx_desc.py will be called from the corresponding
  cmake command, hence with the right arguments.
- Added a dependencies in my_module_B of wrap_test to show how to make
  the dependencies in the cmake file, if needed.
This commit is contained in:
Olivier Parcollet 2014-05-30 19:21:38 +02:00
parent 31927c9132
commit 243d4a798b
13 changed files with 64 additions and 27 deletions

View File

@ -22,7 +22,9 @@ macro (triqs_python_extension ModuleName)
${CMAKE_SOURCE_DIR}/pytriqs/wrap_generator/wrapper.mako.cpp
${wrap_name}
${CMAKE_SOURCE_DIR}/pytriqs/wrap_generator/py_converter_wrapper.mako.hpp
${converter_name} )
${converter_name}
${CMAKE_BINARY_DIR}/include/pytriqs/converters/
)
set_property (GLOBAL APPEND PROPERTY TRIQS_PY_CONVERTERS_TARGETS "python_wrap_${ModuleName}")

View File

@ -95,7 +95,11 @@ macro (triqs_python_extension ModuleName)
@CMAKE_INSTALL_PREFIX@/share/triqs/wrap_generator/wrapper.mako.cpp
${wrap_name}
@CMAKE_INSTALL_PREFIX@/share/triqs/wrap_generator/py_converter_wrapper.mako.hpp
${converter_name} )
${converter_name}
# after this, list of paths for the header generated by the python wrapper generator
@CMAKE_INSTALL_PREFIX@/include/pytriqs/converters/
${CMAKE_BINARY_DIR}/include/pytriqs/converters/
)
#set_property (GLOBAL APPEND PROPERTY TRIQS_PY_CONVERTERS_TARGETS "python_wrap_${ModuleName}")

View File

@ -473,6 +473,5 @@ module.add_function(name = "make_gf_from_inverse_fourier", signature="gf_view<re
########################
if __name__ == '__main__' :
module.generate_code(mako_template = sys.argv[1], wrap_file = sys.argv[2])
module.generate_py_converter_header(mako_template = sys.argv[3], wrap_file = sys.argv[4])
module.generate_code()

View File

@ -100,6 +100,5 @@ module.add_function(name = "energies_on_bz_grid",
########################
if __name__ == '__main__' :
module.generate_code(mako_template = sys.argv[1], wrap_file = sys.argv[2])
module.generate_py_converter_header(mako_template = sys.argv[3], wrap_file = sys.argv[4])
module.generate_code()

View File

@ -51,6 +51,5 @@ module.add_function(name = "c_dag", signature="many_body_operator<double>(int i,
# to generate the module code
if __name__ == '__main__' :
module.generate_code(mako_template = sys.argv[1], wrap_file = sys.argv[2])
module.generate_py_converter_header(mako_template = sys.argv[3], wrap_file = sys.argv[4])
module.generate_code()

View File

@ -8,7 +8,8 @@ module.add_using("namespace triqs::params")
# one class
g = class_(
py_type = "Parameters",
c_type = "triqs::params::parameters",
c_type = "parameters",
c_type_absolute = "triqs::params::parameters",
#serializable= "tuple",
is_printable= True,
hdf5 = True,
@ -35,6 +36,5 @@ g.add_setitem(signature = "void(const char * key, PyObject * ob)",
module.add_class(g)
if __name__ == '__main__' :
module.generate_code(mako_template = sys.argv[1], wrap_file = sys.argv[2])
module.generate_py_converter_header(mako_template = sys.argv[3], wrap_file = sys.argv[4])
module.generate_code()

View File

@ -40,7 +40,6 @@ module.add_function(name = "random_generator_names_list",
########################
if __name__ == '__main__' :
module.generate_code(mako_template = sys.argv[1], wrap_file = sys.argv[2])
module.generate_py_converter_header(mako_template = sys.argv[3], wrap_file = sys.argv[4])
module.generate_code()

View File

@ -1,5 +1,9 @@
// Specialization of py_converter to types wrapped by the wrap_generator.
// DO NOT EDIT. Generated automatically by wrap_generator
// We store here the list of the C++ converted types for use in another module
// WrappedTypeList
// ${list(set(module.wrapped_types))}
//
#pragma once
// in case it is included in the module ${module.full_name}.so, we do not want this specialization
#ifndef TRIQS_PYTHON_WRAPPER_MODULE_${module.name}

View File

@ -4,6 +4,11 @@ import os
from mako.template import Template
import importlib
# the xxx_desc.py will always be called by cmake command, with the proper arguments
# analyse sys.argv right now : we need it early for the use_module
wrapper_mako, wrapper_target, header_mako, header_target = sys.argv[1:5]
module_path_list = sys.argv[5:]
# the correspondance c type -> py_type
c_to_py_type = {'void' : 'None', 'int' : 'int', 'long' : 'int', 'double' : "float", "std::string" : "str"}
@ -461,7 +466,7 @@ class module_ :
- functions : dict : string -> function_. Modules functions. Key is the python name.
- include_list : a list of files to include for compilation
"""
wrapped_types = {}
wrapped_types = []
def __init__(self, full_name, doc = '') :
self.full_name = full_name
@ -474,12 +479,12 @@ class module_ :
self.using =[]
self.python_functions = {}
self.hidden_python_functions = {}
self.module_path_list = []
def add_class(self, cls):
if cls.py_type in self.classes : raise IndexError, "The class %s already exists"%cls.py_type
self.classes[cls.py_type] = cls
self.wrapped_types[cls.c_type] = cls
self.wrapped_types[cls.c_type_absolute] = cls # we can call is by its name or its absolute name
self.wrapped_types += [cls.c_type, cls.c_type_absolute] # we can call is by its name or its absolute name
def add_function(self, **kw):
if "name" in kw :
@ -499,6 +504,30 @@ class module_ :
else :
self.hidden_python_functions[name or f.__name__] = python_function(name or f.__name__, f)
def use_module(self,modulename) :
""" From the name of the module :
- add the header file generated for this module to the include list
- read this file, extract the list of wrapped_types, and add it to
the wrapped_type list
"""
f = None
print "argv", sys.argv
for path in module_path_list :
hppfile = path + '/' + modulename + '.hpp'
if os.path.exists(hppfile) :
f = open(hppfile ,'r')
break
if not f : raise RuntimeError, "Can not find the module %s.\n ... module_path_list = %s"%(modulename, self.module_path_list)
while f.readline().strip() != "// WrappedTypeList" :
pass
l = f.readline()[3:] # // strip "// "
self.wrapped_types += eval(l)
self.add_include(hppfile)
print "Loading triqs wrapped module %s"%modulename
print " ... found C++ header file %s"%hppfile
print " ... found wrapped types %s"%l
def add_include(self, *path) :
self.include_list.extend(path)
@ -527,7 +556,7 @@ class module_ :
for n,f in class_.hidden_python_function.items() :
self.add_python_function(f,name = n, hidden=True)
def generate_code(self, mako_template, wrap_file) :
def generate_wrapper_code(self, mako_template, wrap_file) :
self.prepare_for_generation()
tpl = Template(filename=mako_template)
rendered = tpl.render(module=self, regular_type_if_view_else_type= regular_type_if_view_else_type, is_type_a_view = is_type_a_view)
@ -541,7 +570,8 @@ class module_ :
with open(wrap_file,'w') as f:
f.write(rendered)
#def generate_code_and_header(self, arglist) :
# self.generate_code(mako_template = arglist.argv[0], wrap_file = arglist.argv[1])
# self.generate_py_converter_header(mako_template = arglist.argv[2], wrap_file = arglist.argv[3])
def generate_code(self) :
self.generate_wrapper_code(mako_template = wrapper_mako, wrap_file = wrapper_target)
self.generate_py_converter_header(mako_template = header_mako, wrap_file = header_target)

View File

@ -13,6 +13,9 @@ triqs_python_extension(my_moduleB wrap_test)
triqs_python_extension(test_g wrap_test)
# ??triqs_set_rpath_for_target(my_module)
# the module B must be generated AFTER my_module since it uses some wrapped types...
add_dependencies(python_wrap_my_moduleB python_wrap_my_module)
include_directories( ${CMAKE_CURRENT_SOURCE_DIR} )
install (FILES ${CMAKE_SOURCE_DIR}/pytriqs/__init__.py.template DESTINATION "include/pytriqs/gf/local" RENAME __init__.py)

View File

@ -2,11 +2,11 @@ from wrap_generator import *
# The module
mod = module_(full_name = "pytriqs.wrap_test.my_moduleB", doc = " Doc of my_module ")
mod.use_module('my_module')
mod.add_include("<triqs/../pytriqs/wrap_test/b.hpp>")
mod.add_include("<pytriqs/converters/my_module.hpp>")
mod.add_function (name = "print_a2", signature = "void(A a)", doc = "DOC of print_a")
mod.generate_code(mako_template = sys.argv[1], wrap_file = sys.argv[2])
mod.generate_py_converter_header(mako_template = sys.argv[3], wrap_file = sys.argv[4])
mod.generate_code()

View File

@ -103,6 +103,5 @@ def f1(x,y):
module.add_python_function(f1)
if __name__ == '__main__' :
module.generate_code(mako_template = sys.argv[1], wrap_file = sys.argv[2])
module.generate_py_converter_header(mako_template = sys.argv[3], wrap_file = sys.argv[4])
module.generate_code()

View File

@ -13,6 +13,5 @@ module.add_function (name = "pass_sgf", signature = "void (gf_view<imfreq,scalar
if __name__ == '__main__' :
module.generate_code(mako_template = sys.argv[1], wrap_file = sys.argv[2])
module.generate_py_converter_header(mako_template = sys.argv[3], wrap_file = sys.argv[4])
module.generate_code()