File: PyRunner.hpp

package info (click to toggle)
yade 2026.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 34,448 kB
  • sloc: cpp: 97,645; python: 52,173; sh: 677; makefile: 162
file content (41 lines) | stat: -rw-r--r-- 1,748 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// 2008 © Václav Šmilauer <eudoxos@arcig.cz>
#pragma once
#include <lib/pyutil/gil.hpp>
#include <core/GlobalEngine.hpp>
#include <core/Scene.hpp>
#include <pkg/common/PeriodicEngines.hpp>

namespace yade { // Cannot have #include directive inside.

class PyRunner : public PeriodicEngine {
public:
	/* virtual bool isActivated: not overridden, PeriodicEngine handles that */
	void action() override
	{
		if (command.size() > 0) pyRunString(command, ignoreErrors, updateGlobals);
	}
	// clang-format off
	YADE_CLASS_BASE_DOC_ATTRS(PyRunner,PeriodicEngine,
		"Execute a python command periodically, with defined (and adjustable) periodicity. See :yref:`PeriodicEngine` documentation for details.",
		((string,command,"",,"Command to be run by python interpreter. Not run if empty."))
		((bool,ignoreErrors,false,,"Debug only: set this value to true to tell PyRunner to ignore any errors encountered during command execution."))
		((bool,updateGlobals,true,,R"""(
Whether to workaround `ipython not recognizing local variables <https://github.com/ipython/ipython/issues/62>`__
by calling ``globals().update(locals())``. If ``true`` then PyRunner is able to call functions declared later locally in a running **live** yade session.
The ``PyRunner`` call is a bit slower because it updates ``globals()`` with recently declared python functions.

.. warning::
	When ``updateGlobals==False`` and a function was declared inside a *live* yade session (`ipython <http://ipython.org>`_)
	then an error ``NameError: name 'command' is not defined`` will occur unless python ``globals()`` are updated with command

	.. code-block:: python

		globals().update(locals())

)"""))
	);
	// clang-format on
};
REGISTER_SERIALIZABLE(PyRunner);

} // namespace yade