OpenTwin 0.1
OpenTwin
 
Loading...
Searching...
No Matches
PythonExtension.hpp
Go to the documentation of this file.
1#include "PythonExtension.h"
2#include "PythonException.h"
3#include "PythonModuleAPI.h"
6#include "EntityBuffer.h"
7#include "PortDataBuffer.h"
8
9#include "EntityFile.h"
10
11PyObject* PythonExtensions::OT_GetPropertyValue(PyObject* _self, PyObject* _args) {
12 auto numberOfArguments = PyTuple_Size(_args);
13 const int expectedNumberOfArguments = 2;
14 if (numberOfArguments != expectedNumberOfArguments) {
15 throw std::exception("OT_GetPropertyValue expects two arguments");
16 }
17 PythonObjectBuilder pyObBuilder;
18 std::string absoluteEntityName = pyObBuilder.getStringValueFromTuple(_args, 0, "Parameter 0");
19 std::string propertyName = pyObBuilder.getStringValueFromTuple(_args, 1, "Parameter 1");
20
21 PyObject* returnValue = EntityBuffer::instance().getEntityPropertyValue(absoluteEntityName, propertyName);
22 return returnValue;
23}
24
25PyObject* PythonExtensions::OT_GetScript(PyObject* _self, PyObject* _args) {
26 auto numberOfArguments = PyTuple_Size(_args);
27 const int expectedNumberOfArguments = 1;
28 if (numberOfArguments != expectedNumberOfArguments) {
29 throw std::exception("OT_GetScript expects one argument");
30 }
31 PythonObjectBuilder pyObBuilder;
32 std::string absoluteScriptName = pyObBuilder.getStringValueFromTuple(_args, 0, "Parameter 0");
33
34
35 auto baseEntity = EntityBuffer::instance().getEntity(absoluteScriptName);
36 ot::EntityInformation entityInfo(baseEntity.get());
37 std::optional<std::string> moduleName = PythonLoadedModules::instance().getModuleName(entityInfo);
38 CPythonObjectNew moduleImported(nullptr);
39
40 if (!moduleName.has_value()) {
41 EntityFile* script = dynamic_cast<EntityFile*>(baseEntity.get());
42 if (script == nullptr) {
43 throw std::exception("Requested script execution cannot be done, since the entity is not a python script.");
44 }
45
47 moduleName = PythonLoadedModules::instance().getModuleName(entityInfo);
48
49 auto plainData = script->getData()->getData();
50 std::string execution(plainData.begin(), plainData.end());
51 CPythonObjectBorrowed newModule(PyImport_AddModule(moduleName.value().c_str()));
52 moduleImported = PyImport_ImportModule(moduleName.value().c_str());
53
54 CPythonObjectBorrowed globalDictionary(PyModule_GetDict(moduleImported));
55 CPythonObjectNew result(PyRun_String(execution.c_str(), Py_file_input, globalDictionary, globalDictionary));
56 if (result == nullptr) {
57 throw PythonException();
58 }
59
60 }
61 else {
62 moduleImported = PyImport_ImportModule(moduleName.value().c_str());
63 }
64
65 std::string entryPoint = PythonModuleAPI::instance().getModuleEntryPoint(moduleImported);
66 return PyObject_GetAttrString(moduleImported, entryPoint.c_str());
67}
68
69PyObject* PythonExtensions::OT_SetPropertyValue(PyObject* _self, PyObject* _args) {
70 auto numberOfArguments = PyTuple_Size(_args);
71 const int expectedNumberOfArguments = 3;
72 if (numberOfArguments != expectedNumberOfArguments) {
73 throw std::exception("OT_SetPropertyValue expects three arguments");
74 }
75
76 PythonObjectBuilder pyObBuilder;
77 std::string absoluteEntityName = pyObBuilder.getStringValueFromTuple(_args, 0, "Parameter 0");
78 std::string propertyName = pyObBuilder.getStringValueFromTuple(_args, 1, "Parameter 1");
79 CPythonObjectBorrowed pvalue = pyObBuilder.getTupleItem(_args, 2, "Parameter 2");
80
81 EntityBuffer::instance().updateEntityPropertyValue(absoluteEntityName, propertyName, pvalue);
82 return PyBool_FromLong(true);
83}
84
85PyObject* PythonExtensions::OT_Flush(PyObject* _self, PyObject* _args) {
86 if (_args != nullptr) {
87 throw std::exception("OT_Flush expects zero arguments");
88 }
89
91 PythonObjectBuilder pyObBuilder;
92 return PyBool_FromLong(true);
93}
94
95PyObject* PythonExtensions::OT_FlushEntity(PyObject* _self, PyObject* _args) {
96 auto numberOfArguments = PyTuple_Size(_args);
97 const int expectedNumberOfArguments = 1;
98 if (numberOfArguments != expectedNumberOfArguments) {
99 throw std::exception("OT_FlushEntity expects one argument");
100 }
102 PythonObjectBuilder pyObBuilder;
103 return pyObBuilder.setBool(true);
104}
105
106PyObject* PythonExtensions::OT_GetTableCell(PyObject* _self, PyObject* _args) {
107 auto numberOfArguments = PyTuple_Size(_args);
108 const int expectedNumberOfArguments = 3;
109 if (numberOfArguments != expectedNumberOfArguments) {
110 throw std::exception("OT_GetTableCell expects three arguments");
111 }
112 PythonObjectBuilder pyObBuilder;
113 std::string absoluteEntityName = pyObBuilder.getStringValueFromTuple(_args, 0, "Parameter 0");
114 int32_t row = pyObBuilder.getInt32ValueFromTuple(_args, 1, "Parameter 1");
115 int32_t column = pyObBuilder.getInt32ValueFromTuple(_args, 2, "Parameter 2");
116
117 if (row < 0 || column < 0)
118 {
119 throw std::exception("OT_GetTableCell requires positive indices");
120 }
121
122 PyObject* returnValue = EntityBuffer::instance().getTableCellValue(absoluteEntityName, static_cast<uint32_t>(row), static_cast<uint32_t>(column));
123 return returnValue;
124}
125
126PyObject* PythonExtensions::OT_GetPortData(PyObject* _self, PyObject* _args) {
127 auto numberOfArguments = PyTuple_Size(_args);
128 const int expectedNumberOfArguments = 1;
129 if (numberOfArguments != expectedNumberOfArguments) {
130 throw std::exception("OT_GetPortData expects one arguments");
131 }
132 PythonObjectBuilder pyObBuilder;
133 std::string portName = pyObBuilder.getStringValueFromTuple(_args, 0, "Parameter 0");
134 PyObject* returnValue = PortDataBuffer::instance().getPortData(portName);
135 return returnValue;
136}
137
138PyObject* PythonExtensions::OT_SetPortData(PyObject* _self, PyObject* _args) {
139 auto numberOfArguments = PyTuple_Size(_args);
140 const int expectedNumberOfArguments = 2;
141 if (numberOfArguments != expectedNumberOfArguments) {
142 throw std::exception("OT_GetPortData expects two argument");
143 }
144 PythonObjectBuilder pyObBuilder;
145 std::string portName = pyObBuilder.getStringValueFromTuple(_args, 0, "Parameter 0");
146 CPythonObjectBorrowed pvalue = pyObBuilder.getTupleItem(_args, 1, "Parameter 1");
147 auto values = pyObBuilder.getGenericDataStructList(pvalue);
148 PortDataBuffer::instance().overridePortData(portName, std::move(values));
149 return PyBool_FromLong(true);
150}
Meyer' singleton that buffers all entities and their properties that were used so far.
Entity that holds a binary representation of a file. Any type of file should be supported.
Embedded python functions don't throw exceptions. If the return value holds an error code that says t...
C Python Extension that hold functions for OpenTwin that can be called from a python script.
Meyer's singleton that holds a map of all modules that were loaded in the python interpreter,...
Builder to transfer typically used PyObjects to c++ types and vice versa.
int result
Definition dllmain.cpp:82
Definition CPythonObjectBorrowed.h:15
Definition CPythonObjectNew.h:14
static EntityBuffer & instance()
Definition EntityBuffer.cpp:10
std::shared_ptr< EntityBase > getEntity(const std::string &_absoluteEntityName)
Definition EntityBuffer.cpp:101
PyObject * getEntityPropertyValue(const std::string &_absoluteEntityName, const std::string &_propertyName)
Definition EntityBuffer.cpp:20
void saveChangedEntities()
Definition EntityBuffer.cpp:59
PyObject * getTableCellValue(const std::string &_absoluteEntityName, uint32_t _row, uint32_t _column)
Definition EntityBuffer.cpp:27
void updateEntityPropertyValue(const std::string &_absoluteEntityName, const std::string &_propertyName, const CPythonObject &_values)
Definition EntityBuffer.cpp:52
static PortDataBuffer & instance(void)
Definition PortDataBuffer.cpp:5
PyObject * getPortData(const std::string &_portName)
Definition PortDataBuffer.cpp:32
void overridePortData(const std::string &_portName, const ot::GenericDataStructList &_data)
Definition PortDataBuffer.cpp:16
Definition PythonException.h:15
std::optional< std::string > getModuleName(const ot::EntityInformation &_scriptEntityInfos)
Definition PythonLoadedModules.cpp:8
std::string addModuleForEntity(const ot::EntityInformation &_scriptEntityInfo)
Definition PythonLoadedModules.cpp:24
static PythonLoadedModules & instance(void)
Definition PythonLoadedModules.cpp:3
std::string getModuleEntryPoint(const std::string &_moduleName) const
Definition PythonModuleAPI.cpp:10
static const PythonModuleAPI & instance(void)
Definition PythonModuleAPI.cpp:5
PyObjects don't distingiush between float and double and int32 and int64. All floating point values a...
Definition PythonObjectBuilder.h:28
int32_t getInt32ValueFromTuple(const CPythonObject &pValue, int position, const std::string &varName)
Definition PythonObjectBuilder.cpp:219
std::string getStringValueFromTuple(const CPythonObject &pValue, int position, const std::string &varName)
Definition PythonObjectBuilder.cpp:239
CPythonObjectBorrowed getTupleItem(const CPythonObject &pValue, int position, const std::string &varName)
Definition PythonObjectBuilder.cpp:209
CPythonObjectNew setBool(const bool value)
Definition PythonObjectBuilder.cpp:526
ot::GenericDataStructList getGenericDataStructList(CPythonObject &pValue)
Definition PythonObjectBuilder.cpp:345
Definition EntityInformation.h:14
static PyObject * OT_GetTableCell(PyObject *_self, PyObject *_args)
Definition PythonExtension.hpp:106
static PyObject * OT_GetPropertyValue(PyObject *_self, PyObject *_args)
Definition PythonExtension.hpp:11
static PyObject * OT_Flush(PyObject *_self, PyObject *_args)
Definition PythonExtension.hpp:85
static PyObject * OT_GetPortData(PyObject *_self, PyObject *_args)
Definition PythonExtension.hpp:126
static PyObject * OT_SetPropertyValue(PyObject *_self, PyObject *_args)
Definition PythonExtension.hpp:69
static PyObject * OT_GetScript(PyObject *_self, PyObject *_args)
Definition PythonExtension.hpp:25
static PyObject * OT_FlushEntity(PyObject *_self, PyObject *_args)
Definition PythonExtension.hpp:95
static PyObject * OT_SetPortData(PyObject *_self, PyObject *_args)
Definition PythonExtension.hpp:138