37 #include <boost/python/raw_function.hpp>
38 #include <boost/program_options.hpp>
47 template <
class MODTYPE>
48 struct ModuleWrap :
public MODTYPE,
public py::wrapper<MODTYPE>
62 void Process(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);
126 template <
typename T>
129 return mod->GetConfigOption(key).as<T>();
163 template <
typename MODTYPE>
172 throw NekError(
"ProcessModule.Create() requires two arguments: "
173 "module name and a Field object.");
178 "two arguments: module name and a Field object; "
179 "optionally a filename.");
182 std::string modName = py::extract<std::string>(args[0]);
183 ModuleKey modKey = std::make_pair(modType, modName);
185 if (!py::extract<FieldSharedPtr>(args[1]).check())
187 throw NekError(
"Second argument to Create() should be a Field object.");
198 for (
int i = 2; i < py::len(args); ++i)
200 std::string in_fname = py::extract<std::string>(args[i]);
201 mod->RegisterConfig(
"infile", in_fname);
202 mod->AddFile(modName, in_fname);
209 mod->RegisterConfig(
"outfile", py::extract<std::string>(args[2]));
213 py::list items = kwargs.items();
215 for (
int i = 0; i < py::len(items); ++i)
217 std::string arg = py::extract<std::string>(items[i][0]);
221 py::extract<py::dict> dict_check(items[i][1]);
223 if (!dict_check.check())
225 throw NekError(
"infile should be a dictionary.");
228 py::dict ftype_fname_dict = py::extract<py::dict>(items[i][1]);
229 py::list ft_fn_items = ftype_fname_dict.items();
230 for (
int i = 0; i < py::len(ft_fn_items); ++i)
233 py::extract<std::string>(ft_fn_items[i][0]);
234 std::string f_name = py::extract<std::string>(
235 ft_fn_items[i][1].attr(
"__str__")());
236 mod->RegisterConfig(arg, f_name);
237 mod->AddFile(f_type, f_name);
243 py::extract<std::string>(items[i][1].attr(
"__str__")());
244 mod->RegisterConfig(arg, val);
261 std::string
const &value)
263 mod->RegisterConfig(key, value);
266 template <
typename MODTYPE>
268 std::string
const &key,
269 std::string
const &defValue,
270 std::string
const &desc,
bool isBool)
272 mod->AddConfigOption(key, defValue, desc, isBool);
280 py::incref(obj.ptr());
285 py::decref(m_obj.ptr());
290 py::object inst = m_obj(field);
291 return py::extract<ModuleSharedPtr>(inst);
298 #if PY_MAJOR_VERSION == 2
346 helper, std::placeholders::_1));
354 #if PY_MAJOR_VERSION == 2
363 py::import(
"__main__").attr(modkey.c_str()) = capsule;
374 py::objects::class_value_wrapper<
375 std::shared_ptr<MODTYPE>,
376 py::objects::make_ptr_instance<
377 MODTYPE, py::objects::pointer_holder<std::shared_ptr<MODTYPE>,
386 py::class_<ModuleWrap<MODTYPE>, std::shared_ptr<ModuleWrap<MODTYPE>>,
387 py::bases<Module>, boost::noncopyable>(
388 modName.c_str(), py::init<FieldSharedPtr>())
390 .def(
"AddConfigOption", ModuleWrap_AddConfigOption<MODTYPE>,
391 (py::arg(
"key"), py::arg(
"defValue"), py::arg(
"desc"),
392 py::arg(
"isBool") =
false))
400 .def(
"Create", py::raw_function(Module_Create<MODTYPE>))
401 .staticmethod(
"Create");
414 py::implicitly_convertible<std::shared_ptr<ModuleWrap<Module>>,
415 std::shared_ptr<Module>>();
416 py::implicitly_convertible<std::shared_ptr<ModuleWrap<InputModule>>,
417 std::shared_ptr<Module>>();
418 py::implicitly_convertible<std::shared_ptr<ModuleWrap<OutputModule>>,
419 std::shared_ptr<Module>>();
420 py::implicitly_convertible<std::shared_ptr<ModuleWrap<ProcessModule>>,
421 std::shared_ptr<Module>>();
428 py::class_<ModuleWrap<Module>, std::shared_ptr<ModuleWrap<Module>>,
429 boost::noncopyable>(
"Module", py::init<FieldSharedPtr>())
437 (py::arg(
"key"), py::arg(
"value") =
""))
440 .def(
"GetStringConfig", Module_GetConfig<std::string>)
441 .def(
"GetFloatConfig", Module_GetConfig<double>)
442 .def(
"GetIntConfig", Module_GetConfig<int>)
443 .def(
"GetBoolConfig", Module_GetConfig<bool>)
444 .def(
"AddConfigOption", ModuleWrap_AddConfigOption<Module>,
445 (py::arg(
"key"), py::arg(
"defValue"), py::arg(
"desc"),
446 py::arg(
"isBool") =
false))
453 .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 Process(po::variables_map &vm) override
Concrete implementation of the Module::Process function.
std::string GetModuleName() override
void AddConfigOption(std::string key, std::string def, std::string desc, bool isBool)
Defines a configuration option for this module.
ModuleWrap(FieldSharedPtr field)
Constructor, which is identical to FieldUtils::Module.
ModulePriority GetModulePriority() override
Represents a command-line configuration option.
PythonModuleClass(std::string modName)