37 #include <boost/program_options.hpp>
38 #include <boost/python/raw_function.hpp>
47 template <
class MODTYPE>
48 struct ModuleWrap :
public MODTYPE,
public py::wrapper<MODTYPE>
64 boost::ignore_unused(vm);
65 this->get_override(
"Process")();
70 py::override f = this->get_override(
"GetModuleName");
71 return py::call<std::string>(f.ptr());
76 py::override f = this->get_override(
"GetModulePriority");
77 return py::call<ModulePriority>(f.ptr());
92 this->m_config[key] = conf;
104 if (m->m_f->m_nParts > 1)
106 if (m->GetModulePriority() ==
eOutput)
108 m->m_f->m_comm = m->m_f->m_partComm;
109 if (m->GetModuleName() !=
"OutputInfo")
111 m->RegisterConfig(
"writemultiplefiles");
116 m->m_f->m_comm = m->m_f->m_partComm;
120 m->m_f->m_comm = m->m_f->m_defComm;
124 m->Process(m->m_f->m_vm);
127 template <
typename T>
130 return mod->GetConfigOption(key).as<T>();
164 template <
typename MODTYPE>
173 throw NekError(
"ProcessModule.Create() requires two arguments: "
174 "module name and a Field object.");
179 "Module.Create() requires "
180 "two arguments: module name and a Field object; "
181 "optionally a filename.");
184 std::string modName = py::extract<std::string>(args[0]);
185 ModuleKey modKey = std::make_pair(modType, modName);
187 if (!py::extract<FieldSharedPtr>(args[1]).check())
189 throw NekError(
"Second argument to Create() should be a Field object.");
200 for (
int i = 2; i < py::len(args); ++i)
202 std::string in_fname = py::extract<std::string>(args[i]);
203 mod->RegisterConfig(
"infile", in_fname);
204 mod->AddFile(modName, in_fname);
211 mod->RegisterConfig(
"outfile", py::extract<std::string>(args[2]));
215 py::list items = kwargs.items();
217 for (
int i = 0; i < py::len(items); ++i)
219 std::string arg = py::extract<std::string>(items[i][0]);
223 py::extract<py::dict> dict_check(items[i][1]);
225 if (!dict_check.check())
227 throw NekError(
"infile should be a dictionary.");
230 py::dict ftype_fname_dict = py::extract<py::dict>(items[i][1]);
231 py::list ft_fn_items = ftype_fname_dict.items();
232 for (
int i = 0; i < py::len(ft_fn_items); ++i)
235 py::extract<std::string>(ft_fn_items[i][0]);
236 std::string f_name = py::extract<std::string>(
237 ft_fn_items[i][1].attr(
"__str__")());
238 mod->RegisterConfig(arg, f_name);
239 mod->AddFile(f_type, f_name);
245 py::extract<std::string>(items[i][1].attr(
"__str__")());
246 mod->RegisterConfig(arg, val);
263 std::string
const &value)
265 mod->RegisterConfig(key, value);
268 template <
typename MODTYPE>
270 std::string
const &key,
271 std::string
const &defValue,
272 std::string
const &desc,
bool isBool)
274 mod->AddConfigOption(key, defValue, desc, isBool);
282 py::incref(obj.ptr());
287 py::decref(m_obj.ptr());
292 py::object inst = m_obj(field);
293 return py::extract<ModuleSharedPtr>(inst);
300 #if PY_MAJOR_VERSION == 2
348 helper, std::placeholders::_1));
356 #if PY_MAJOR_VERSION == 2
365 py::import(
"__main__").attr(modkey.c_str()) = capsule;
376 py::objects::class_value_wrapper<
377 std::shared_ptr<MODTYPE>,
378 py::objects::make_ptr_instance<
379 MODTYPE, py::objects::pointer_holder<std::shared_ptr<MODTYPE>,
388 py::class_<ModuleWrap<MODTYPE>, std::shared_ptr<ModuleWrap<MODTYPE>>,
389 py::bases<Module>, boost::noncopyable>(
390 modName.c_str(), py::init<FieldSharedPtr>())
392 .def(
"AddConfigOption", ModuleWrap_AddConfigOption<MODTYPE>,
393 (py::arg(
"key"), py::arg(
"defValue"), py::arg(
"desc"),
394 py::arg(
"isBool") =
false))
402 .def(
"Create", py::raw_function(Module_Create<MODTYPE>))
403 .staticmethod(
"Create");
416 py::implicitly_convertible<std::shared_ptr<ModuleWrap<Module>>,
417 std::shared_ptr<Module>>();
418 py::implicitly_convertible<std::shared_ptr<ModuleWrap<InputModule>>,
419 std::shared_ptr<Module>>();
420 py::implicitly_convertible<std::shared_ptr<ModuleWrap<OutputModule>>,
421 std::shared_ptr<Module>>();
422 py::implicitly_convertible<std::shared_ptr<ModuleWrap<ProcessModule>>,
423 std::shared_ptr<Module>>();
430 py::class_<ModuleWrap<Module>, std::shared_ptr<ModuleWrap<Module>>,
431 boost::noncopyable>(
"Module", py::init<FieldSharedPtr>())
439 (py::arg(
"key"), py::arg(
"value") =
""))
442 .def(
"GetStringConfig", Module_GetConfig<std::string>)
443 .def(
"GetFloatConfig", Module_GetConfig<double>)
444 .def(
"GetIntConfig", Module_GetConfig<int>)
445 .def(
"GetBoolConfig", Module_GetConfig<bool>)
446 .def(
"AddConfigOption", ModuleWrap_AddConfigOption<Module>,
447 (py::arg(
"key"), py::arg(
"defValue"), py::arg(
"desc"),
448 py::arg(
"isBool") =
false))
455 .staticmethod(
"Register");
Nektar::ErrorUtil::NekError NekError
#define NEKPY_WRAP_ENUM_STRING(ENUMNAME, MAPNAME)
void ModuleCapsuleDestructor(PyObject *ptr)
T Module_GetConfig(std::shared_ptr< Module > mod, const std::string &key)
ModuleSharedPtr Module_Create(py::tuple args, py::dict kwargs)
Lightweight wrapper for Module factory creation function.
void Module_RegisterConfig(std::shared_ptr< Module > mod, std::string const &key, std::string const &value)
Lightweight wrapper for FieldUtils::Module::RegisterConfig.
void ModuleWrap_AddConfigOption(std::shared_ptr< ModuleWrap< MODTYPE >> mod, std::string const &key, std::string const &defValue, std::string const &desc, bool isBool)
void Module_Process(ModuleSharedPtr m)
void Module_Register(ModuleType const &modType, std::string const &modName, py::object &obj)
Lightweight wrapper for the Module factory RegisterCreatorFunction, to support the ability for Python...
ModuleRegisterHelper(py::object obj)
ModuleSharedPtr create(FieldSharedPtr field)
FIELD_UTILS_EXPORT void PrintConfig()
Print out all configuration options for a module.
FIELD_UTILS_EXPORT void SetDefaults()
Sets default configuration options for those which have not been set.
Abstract base class for output modules.
Abstract base class for processing modules.
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
tBaseSharedPtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
std::shared_ptr< Field > FieldSharedPtr
std::pair< ModuleType, std::string > ModuleKey
const std::string ModuleTypeMap[]
std::shared_ptr< Module > ModuleSharedPtr
ModuleFactory & GetModuleFactory()
The above copyright notice and this permission notice shall be included.
Module wrapper to handle virtual function calls in Module and its subclasses as defined by the templa...
void AddConfigOption(std::string key, std::string def, std::string desc, bool isBool)
Defines a configuration option for this module.
std::string v_GetModuleName() override
ModulePriority v_GetModulePriority() override
void v_Process(po::variables_map &vm) override
Concrete implementation of the Module::Process function.
ModuleWrap(FieldSharedPtr field)
Constructor, which is identical to FieldUtils::Module.
Represents a command-line configuration option.
PythonModuleClass(std::string modName)