37#include <boost/program_options.hpp>
38#include <boost/python/raw_function.hpp>
47template <
class MODTYPE>
48struct ModuleWrap :
public MODTYPE,
public py::wrapper<MODTYPE>
62 void v_Process([[maybe_unused]] po::variables_map &vm)
override
64 this->get_override(
"Process")();
69 py::override f = this->get_override(
"GetModuleName");
70 return py::call<std::string>(f.ptr());
75 py::override f = this->get_override(
"GetModulePriority");
76 return py::call<ModulePriority>(f.ptr());
91 this->m_config[key] = conf;
103 if (m->m_f->m_nParts > 1)
105 if (m->GetModulePriority() ==
eOutput)
107 m->m_f->m_comm = m->m_f->m_partComm;
108 if (m->GetModuleName() !=
"OutputInfo")
110 m->RegisterConfig(
"writemultiplefiles");
115 m->m_f->m_comm = m->m_f->m_partComm;
119 m->m_f->m_comm = m->m_f->m_defComm;
123 m->Process(m->m_f->m_vm);
129 return mod->GetConfigOption(key).as<T>();
163template <
typename MODTYPE>
172 throw NekError(
"ProcessModule.Create() requires two arguments: "
173 "module name and a Field object.");
178 "Module.Create() requires "
179 "two arguments: module name and a Field object; "
180 "optionally a filename.");
183 std::string modName = py::extract<std::string>(args[0]);
184 ModuleKey modKey = std::make_pair(modType, modName);
186 if (!py::extract<FieldSharedPtr>(args[1]).check())
188 throw NekError(
"Second argument to Create() should be a Field object.");
199 for (
int i = 2; i < py::len(args); ++i)
201 std::string in_fname = py::extract<std::string>(args[i]);
202 mod->RegisterConfig(
"infile", in_fname);
203 mod->AddFile(modName, in_fname);
210 mod->RegisterConfig(
"outfile", py::extract<std::string>(args[2]));
214 py::list items = kwargs.items();
216 for (
int i = 0; i < py::len(items); ++i)
218 std::string arg = py::extract<std::string>(items[i][0]);
222 py::extract<py::dict> dict_check(items[i][1]);
224 if (!dict_check.check())
226 throw NekError(
"infile should be a dictionary.");
229 py::dict ftype_fname_dict = py::extract<py::dict>(items[i][1]);
230 py::list ft_fn_items = ftype_fname_dict.items();
231 for (
int i = 0; i < py::len(ft_fn_items); ++i)
234 py::extract<std::string>(ft_fn_items[i][0]);
235 std::string f_name = py::extract<std::string>(
236 ft_fn_items[i][1].attr(
"__str__")());
237 mod->RegisterConfig(arg, f_name);
238 mod->AddFile(f_type, f_name);
244 py::extract<std::string>(items[i][1].attr(
"__str__")());
245 mod->RegisterConfig(arg, val);
262 std::string
const &value)
264 mod->RegisterConfig(key, value);
267template <
typename MODTYPE>
269 std::string
const &key,
270 std::string
const &defValue,
271 std::string
const &desc,
bool isBool)
273 mod->AddConfigOption(key, defValue, desc, isBool);
281 py::incref(obj.ptr());
286 py::decref(
m_obj.ptr());
291 py::object inst =
m_obj(field);
292 return py::extract<ModuleSharedPtr>(inst);
299#if PY_MAJOR_VERSION == 2
347 helper, std::placeholders::_1));
355#if PY_MAJOR_VERSION == 2
364 py::import(
"__main__").attr(modkey.c_str()) = capsule;
375 py::objects::class_value_wrapper<
376 std::shared_ptr<MODTYPE>,
377 py::objects::make_ptr_instance<
378 MODTYPE, py::objects::pointer_holder<std::shared_ptr<MODTYPE>,
387 py::class_<ModuleWrap<MODTYPE>, std::shared_ptr<ModuleWrap<MODTYPE>>,
388 py::bases<Module>, boost::noncopyable>(
389 modName.c_str(), py::init<FieldSharedPtr>())
391 .def(
"AddConfigOption", ModuleWrap_AddConfigOption<MODTYPE>,
392 (py::arg(
"key"), py::arg(
"defValue"), py::arg(
"desc"),
393 py::arg(
"isBool") =
false))
401 .def(
"Create", py::raw_function(Module_Create<MODTYPE>))
402 .staticmethod(
"Create");
415 py::implicitly_convertible<std::shared_ptr<ModuleWrap<Module>>,
416 std::shared_ptr<Module>>();
417 py::implicitly_convertible<std::shared_ptr<ModuleWrap<InputModule>>,
418 std::shared_ptr<Module>>();
419 py::implicitly_convertible<std::shared_ptr<ModuleWrap<OutputModule>>,
420 std::shared_ptr<Module>>();
421 py::implicitly_convertible<std::shared_ptr<ModuleWrap<ProcessModule>>,
422 std::shared_ptr<Module>>();
429 py::class_<ModuleWrap<Module>, std::shared_ptr<ModuleWrap<Module>>,
430 boost::noncopyable>(
"Module", py::init<FieldSharedPtr>())
438 (py::arg(
"key"), py::arg(
"value") =
""))
439 .def(
"PrintConfig", &Module::PrintConfig)
440 .def(
"SetDefaults", &Module::SetDefaults)
441 .def(
"GetStringConfig", Module_GetConfig<std::string>)
442 .def(
"GetFloatConfig", Module_GetConfig<double>)
443 .def(
"GetIntConfig", Module_GetConfig<int>)
444 .def(
"GetBoolConfig", Module_GetConfig<bool>)
445 .def(
"AddConfigOption", ModuleWrap_AddConfigOption<Module>,
446 (py::arg(
"key"), py::arg(
"defValue"), py::arg(
"desc"),
447 py::arg(
"isBool") =
false))
454 .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 Module_Process(ModuleSharedPtr m)
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_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)
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()
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)