python_providers.cpp
168 lines
| 6.2 KiB
| text/x-c
|
CppLexer
r1423 | #include "python_providers.h" | |||
r1428 | #include <Data/DataProviderParameters.h> | |||
#include <Data/DateTimeRange.h> | ||||
#include <Data/IDataProvider.h> | ||||
#include <Data/ScalarTimeSerie.h> | ||||
#include <Data/SpectrogramTimeSerie.h> | ||||
r1429 | #include <Data/TimeSeriesUtils.h> | |||
r1428 | #include <Data/VectorTimeSerie.h> | |||
#include <DataSource/DataSourceController.h> | ||||
#include <DataSource/DataSourceItem.h> | ||||
#include <DataSource/DataSourceItemAction.h> | ||||
#include <QDir> | ||||
#include <QStandardPaths> | ||||
#include <QStringList> | ||||
#include <SqpApplication.h> | ||||
#include <TimeSeries.h> | ||||
#include <functional> | ||||
#include <iostream> | ||||
const auto DATA_SOURCE_NAME = QStringLiteral("PythonProviders"); | ||||
class PythonProvider : public IDataProvider | ||||
{ | ||||
public: | ||||
r1431 | explicit PythonProvider(PythonInterpreter::provider_funct_t f) : _pythonFunction { f } {} | |||
r1428 | ||||
PythonProvider(const PythonProvider& other) : _pythonFunction { other._pythonFunction } {} | ||||
r1477 | // std::shared_ptr<IDataProvider> clone() const override | |||
// { | ||||
// return std::make_shared<PythonProvider>(*this); | ||||
// } | ||||
r1428 | virtual TimeSeries::ITimeSerie* getData(const DataProviderParameters& parameters) override | |||
{ | ||||
auto product = parameters.m_Data.value("PRODUCT", "").toString().toStdString(); | ||||
auto range = parameters.m_Range; | ||||
r1430 | std::vector<std::tuple<std::string, std::string>> metadata; | |||
std::transform(parameters.m_Data.constKeyValueBegin(), parameters.m_Data.constKeyValueEnd(), | ||||
std::back_inserter(metadata), [](const auto& item) { | ||||
return std::tuple<std::string, std::string> { item.first.toStdString(), | ||||
item.second.toString().toStdString() }; | ||||
}); | ||||
auto result = _pythonFunction(metadata, range.m_TStart, range.m_TEnd); | ||||
r1429 | return TimeSeriesUtils::copy(result); | |||
r1428 | } | |||
private: | ||||
r1429 | PythonInterpreter::provider_funct_t _pythonFunction; | |||
r1428 | }; | |||
r1423 | ||||
void PythonProviders::initialize() | ||||
{ | ||||
r1440 | auto app_path = sqpApp->applicationDirPath(); | |||
_interpreter.eval_str("import sys"); | ||||
r1452 | for(const auto& path:{"/../lib","/../lib64","/../core","/../../lib"}) | |||
r1440 | { | |||
QDir d{app_path+path}; | ||||
if(d.exists()) | ||||
{ | ||||
_interpreter.eval_str("sys.path.append(\""+d.path().toStdString()+"\")"); | ||||
} | ||||
} | ||||
r1428 | _interpreter.add_register_callback( | |||
r1430 | [this](const std::vector<PythonInterpreter::product_t>& product_list, | |||
r1429 | PythonInterpreter::provider_funct_t f) { this->register_product(product_list, f); }); | |||
r1428 | ||||
for (const auto& path : QStandardPaths::standardLocations(QStandardPaths::AppLocalDataLocation)) | ||||
{ | ||||
auto dir = QDir(path + "/python"); | ||||
if (dir.exists()) | ||||
{ | ||||
for (const auto& entry : | ||||
dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot, QDir::Name)) | ||||
{ | ||||
if (entry.isFile() && entry.suffix() == "py") | ||||
{ | ||||
_interpreter.eval(entry.absoluteFilePath().toStdString()); | ||||
} | ||||
} | ||||
} | ||||
} | ||||
r1440 | for (const auto& embed_file : { ":/test.py", ":/amda.py", ":/cdaweb.py"}) | |||
{ | ||||
QFile file(embed_file); | ||||
file.open(QFile::ReadOnly); | ||||
if(file.isOpen()) | ||||
_interpreter.eval_str(file.readAll().toStdString()); | ||||
} | ||||
r1429 | _interpreter.release(); | |||
r1428 | } | |||
PythonProviders::~PythonProviders() {} | ||||
std::unique_ptr<DataSourceItem> make_folder_item(const QString& name) | ||||
{ | ||||
return std::make_unique<DataSourceItem>(DataSourceItemType::NODE, name); | ||||
} | ||||
template <typename T> | ||||
DataSourceItem* make_path_items( | ||||
const T& path_list_begin, const T& path_list_end, DataSourceItem* root) | ||||
{ | ||||
std::for_each(path_list_begin, path_list_end, [&root](const auto& folder_name) mutable { | ||||
auto folder_ptr = root->findItem(folder_name); | ||||
if (folder_ptr == nullptr) | ||||
{ | ||||
auto folder = make_folder_item(folder_name); | ||||
folder_ptr = folder.get(); | ||||
root->appendChild(std::move(folder)); | ||||
} | ||||
root = folder_ptr; | ||||
}); | ||||
return root; | ||||
} | ||||
std::unique_ptr<DataSourceItem> make_product_item( | ||||
const QVariantHash& metaData, const QUuid& dataSourceUid) | ||||
{ | ||||
auto result = std::make_unique<DataSourceItem>(DataSourceItemType::PRODUCT, metaData); | ||||
// Adds plugin name to product metadata | ||||
result->setData(DataSourceItem::PLUGIN_DATA_KEY, DATA_SOURCE_NAME); | ||||
result->setData(DataSourceItem::ID_DATA_KEY, metaData.value(DataSourceItem::NAME_DATA_KEY)); | ||||
auto productName = metaData.value(DataSourceItem::NAME_DATA_KEY).toString(); | ||||
// Add action to load product from DataSourceController | ||||
result->addAction( | ||||
std::make_unique<DataSourceItemAction>(QObject::tr("Load %1 product").arg(productName), | ||||
[productName, dataSourceUid](DataSourceItem& item) { | ||||
if (auto app = sqpApp) | ||||
{ | ||||
app->dataSourceController().loadProductItem(dataSourceUid, item); | ||||
} | ||||
})); | ||||
return result; | ||||
r1425 | } | |||
r1429 | void PythonProviders::register_product( | |||
r1430 | const std::vector<PythonInterpreter::product_t>& product_list, | |||
r1429 | PythonInterpreter::provider_funct_t f) | |||
r1425 | { | |||
r1428 | auto& dataSourceController = sqpApp->dataSourceController(); | |||
r1430 | QString test = DATA_SOURCE_NAME + QUuid::createUuid().toString(); | |||
auto id = dataSourceController.registerDataSource(test); | ||||
auto root = make_folder_item(test); | ||||
r1429 | std::for_each(std::cbegin(product_list), std::cend(product_list), | |||
[id, f, root = root.get()](const auto& product) { | ||||
r1430 | const auto& path = std::get<0>(product); | |||
r1438 | auto path_list = QString::fromStdString(path).split('/', QString::SkipEmptyParts); | |||
r1428 | auto name = *(std::cend(path_list) - 1); | |||
auto path_item | ||||
= make_path_items(std::cbegin(path_list), std::cend(path_list) - 1, root); | ||||
r1429 | QVariantHash metaData { { DataSourceItem::NAME_DATA_KEY, name } }; | |||
r1430 | std::for_each(std::cbegin(std::get<2>(product)), std::cend(std::get<2>(product)), | |||
r1429 | [&metaData](const auto& mdata) { | |||
metaData[QString::fromStdString(mdata.first)] | ||||
= QString::fromStdString(mdata.second); | ||||
}); | ||||
path_item->appendChild(make_product_item(metaData, id)); | ||||
r1428 | }); | |||
dataSourceController.setDataSourceItem(id, std::move(root)); | ||||
dataSourceController.setDataProvider(id, std::make_unique<PythonProvider>(f)); | ||||
r1423 | } | |||