OpenTwin 0.1
OpenTwin
 
Loading...
Searching...
No Matches
PythonExtension.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "PythonExtension.h"
4#include "PythonException.h"
5#include "PythonModuleAPI.h"
8#include "EntityBuffer.h"
9#include "PortDataBuffer.h"
10
11#include "EntityFile.h"
12
13PyObject* PythonExtensions::OT_GetPropertyValue(PyObject* _self, PyObject* _args) {
14 auto numberOfArguments = PyTuple_Size(_args);
15 const int expectedNumberOfArguments = 2;
16 if (numberOfArguments != expectedNumberOfArguments) {
17 throw std::exception("OT_GetPropertyValue expects two arguments");
18 }
19 PythonObjectBuilder pyObBuilder;
20 std::string absoluteEntityName = pyObBuilder.getStringValueFromTuple(_args, 0, "Parameter 0");
21 std::string propertyName = pyObBuilder.getStringValueFromTuple(_args, 1, "Parameter 1");
22
23 PyObject* returnValue = EntityBuffer::instance().getEntityPropertyValue(absoluteEntityName, propertyName);
24 return returnValue;
25}
26
27PyObject* PythonExtensions::OT_GetScript(PyObject* _self, PyObject* _args) {
28 auto numberOfArguments = PyTuple_Size(_args);
29 const int expectedNumberOfArguments = 1;
30 if (numberOfArguments != expectedNumberOfArguments) {
31 throw std::exception("OT_GetScript expects one argument");
32 }
33 PythonObjectBuilder pyObBuilder;
34 std::string absoluteScriptName = pyObBuilder.getStringValueFromTuple(_args, 0, "Parameter 0");
35
36
37 auto baseEntity = EntityBuffer::instance().getEntity(absoluteScriptName);
38 ot::EntityInformation entityInfo(baseEntity.get());
39 std::optional<std::string> moduleName = PythonLoadedModules::instance().getModuleName(entityInfo);
40 CPythonObjectNew moduleImported(nullptr);
41
42 if (!moduleName.has_value()) {
43 EntityFile* script = dynamic_cast<EntityFile*>(baseEntity.get());
44 if (script == nullptr) {
45 throw std::exception("Requested script execution cannot be done, since the entity is not a python script.");
46 }
47
49 moduleName = PythonLoadedModules::instance().getModuleName(entityInfo);
50
51 auto plainData = script->getData()->getData();
52 std::string execution(plainData.begin(), plainData.end());
53 CPythonObjectBorrowed newModule(PyImport_AddModule(moduleName.value().c_str()));
54 moduleImported = PyImport_ImportModule(moduleName.value().c_str());
55
56 CPythonObjectBorrowed globalDictionary(PyModule_GetDict(moduleImported));
57 CPythonObjectNew result(PyRun_String(execution.c_str(), Py_file_input, globalDictionary, globalDictionary));
58 if (result == nullptr) {
59 throw PythonException();
60 }
61
62 }
63 else {
64 moduleImported = PyImport_ImportModule(moduleName.value().c_str());
65 }
66
67 std::string entryPoint = PythonModuleAPI::instance().getModuleEntryPoint(moduleImported);
68 return PyObject_GetAttrString(moduleImported, entryPoint.c_str());
69}
70
71PyObject* PythonExtensions::OT_SetPropertyValue(PyObject* _self, PyObject* _args) {
72 auto numberOfArguments = PyTuple_Size(_args);
73 const int expectedNumberOfArguments = 3;
74 if (numberOfArguments != expectedNumberOfArguments) {
75 throw std::exception("OT_SetPropertyValue expects three arguments");
76 }
77
78 PythonObjectBuilder pyObBuilder;
79 std::string absoluteEntityName = pyObBuilder.getStringValueFromTuple(_args, 0, "Parameter 0");
80 std::string propertyName = pyObBuilder.getStringValueFromTuple(_args, 1, "Parameter 1");
81 CPythonObjectBorrowed pvalue = pyObBuilder.getTupleItem(_args, 2, "Parameter 2");
82
83 EntityBuffer::instance().updateEntityPropertyValue(absoluteEntityName, propertyName, pvalue);
84 return PyBool_FromLong(true);
85}
86
87PyObject* PythonExtensions::OT_Flush(PyObject* _self, PyObject* _args) {
88 if (_args != nullptr) {
89 throw std::exception("OT_Flush expects zero arguments");
90 }
91
93 PythonObjectBuilder pyObBuilder;
94 return PyBool_FromLong(true);
95}
96
97PyObject* PythonExtensions::OT_FlushEntity(PyObject* _self, PyObject* _args) {
98 auto numberOfArguments = PyTuple_Size(_args);
99 const int expectedNumberOfArguments = 1;
100 if (numberOfArguments != expectedNumberOfArguments) {
101 throw std::exception("OT_FlushEntity expects one argument");
102 }
104 PythonObjectBuilder pyObBuilder;
105 return pyObBuilder.setBool(true);
106}
107
108PyObject* PythonExtensions::OT_GetTableCell(PyObject* _self, PyObject* _args) {
109 auto numberOfArguments = PyTuple_Size(_args);
110 const int expectedNumberOfArguments = 3;
111 if (numberOfArguments != expectedNumberOfArguments) {
112 throw std::exception("OT_GetTableCell expects three arguments");
113 }
114 PythonObjectBuilder pyObBuilder;
115 std::string absoluteEntityName = pyObBuilder.getStringValueFromTuple(_args, 0, "Parameter 0");
116 int32_t row = pyObBuilder.getInt32ValueFromTuple(_args, 1, "Parameter 1");
117 int32_t column = pyObBuilder.getInt32ValueFromTuple(_args, 2, "Parameter 2");
118
119 if (row < 0 || column < 0)
120 {
121 throw std::exception("OT_GetTableCell requires positive indices");
122 }
123
124 PyObject* returnValue = EntityBuffer::instance().getTableCellValue(absoluteEntityName, static_cast<uint32_t>(row), static_cast<uint32_t>(column));
125 return returnValue;
126}
127
128PyObject* PythonExtensions::OT_GetPortData(PyObject* _self, PyObject* _args) {
129 auto numberOfArguments = PyTuple_Size(_args);
130 const int expectedNumberOfArguments = 1;
131 if (numberOfArguments != expectedNumberOfArguments) {
132 throw std::exception("OT_GetPortData expects one arguments");
133 }
134 PythonObjectBuilder pyObBuilder;
135 std::string portName = pyObBuilder.getStringValueFromTuple(_args, 0, "Parameter 0");
136 PyObject* returnValue = PortDataBuffer::instance().getPortData(portName);
137 return returnValue;
138}
139
140PyObject* PythonExtensions::OT_SetPortData(PyObject* _self, PyObject* _args) {
141 auto numberOfArguments = PyTuple_Size(_args);
142 const int expectedNumberOfArguments = 2;
143 if (numberOfArguments != expectedNumberOfArguments) {
144 throw std::exception("OT_GetPortData expects two argument");
145 }
146 PythonObjectBuilder pyObBuilder;
147 std::string portName = pyObBuilder.getStringValueFromTuple(_args, 0, "Parameter 0");
148 CPythonObjectBorrowed pvalue = pyObBuilder.getTupleItem(_args, 1, "Parameter 1");
149 auto values = pyObBuilder.getGenericDataStructList(pvalue);
150 PortDataBuffer::instance().overridePortData(portName, std::move(values));
151 return PyBool_FromLong(true);
152}
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:11
std::shared_ptr< EntityBase > getEntity(const std::string &_absoluteEntityName)
Definition EntityBuffer.cpp:97
PyObject * getEntityPropertyValue(const std::string &_absoluteEntityName, const std::string &_propertyName)
Definition EntityBuffer.cpp:16
void saveChangedEntities()
Definition EntityBuffer.cpp:55
PyObject * getTableCellValue(const std::string &_absoluteEntityName, uint32_t _row, uint32_t _column)
Definition EntityBuffer.cpp:23
void updateEntityPropertyValue(const std::string &_absoluteEntityName, const std::string &_propertyName, const CPythonObject &_values)
Definition EntityBuffer.cpp:48
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:220
std::string getStringValueFromTuple(const CPythonObject &pValue, int position, const std::string &varName)
Definition PythonObjectBuilder.cpp:240
CPythonObjectBorrowed getTupleItem(const CPythonObject &pValue, int position, const std::string &varName)
Definition PythonObjectBuilder.cpp:210
CPythonObjectNew setBool(const bool value)
Definition PythonObjectBuilder.cpp:527
ot::GenericDataStructList getGenericDataStructList(CPythonObject &pValue)
Definition PythonObjectBuilder.cpp:346
static PyObject * OT_GetTableCell(PyObject *_self, PyObject *_args)
Definition PythonExtension.hpp:108
static PyObject * OT_GetPropertyValue(PyObject *_self, PyObject *_args)
Definition PythonExtension.hpp:13
static PyObject * OT_Flush(PyObject *_self, PyObject *_args)
Definition PythonExtension.hpp:87
static PyObject * OT_GetPortData(PyObject *_self, PyObject *_args)
Definition PythonExtension.hpp:128
static PyObject * OT_SetPropertyValue(PyObject *_self, PyObject *_args)
Definition PythonExtension.hpp:71
static PyObject * OT_GetScript(PyObject *_self, PyObject *_args)
Definition PythonExtension.hpp:27
static PyObject * OT_FlushEntity(PyObject *_self, PyObject *_args)
Definition PythonExtension.hpp:97
static PyObject * OT_SetPortData(PyObject *_self, PyObject *_args)
Definition PythonExtension.hpp:140