##// END OF EJS Templates
Add OSX relative lib path in python provider...
jeandet -
r1452:223b9254fb15
parent child
Show More
@@ -1,168 +1,168
1 #include "python_providers.h"
1 #include "python_providers.h"
2 #include <Data/DataProviderParameters.h>
2 #include <Data/DataProviderParameters.h>
3 #include <Data/DateTimeRange.h>
3 #include <Data/DateTimeRange.h>
4 #include <Data/IDataProvider.h>
4 #include <Data/IDataProvider.h>
5 #include <Data/ScalarTimeSerie.h>
5 #include <Data/ScalarTimeSerie.h>
6 #include <Data/SpectrogramTimeSerie.h>
6 #include <Data/SpectrogramTimeSerie.h>
7 #include <Data/TimeSeriesUtils.h>
7 #include <Data/TimeSeriesUtils.h>
8 #include <Data/VectorTimeSerie.h>
8 #include <Data/VectorTimeSerie.h>
9 #include <DataSource/DataSourceController.h>
9 #include <DataSource/DataSourceController.h>
10 #include <DataSource/DataSourceItem.h>
10 #include <DataSource/DataSourceItem.h>
11 #include <DataSource/DataSourceItemAction.h>
11 #include <DataSource/DataSourceItemAction.h>
12 #include <QDir>
12 #include <QDir>
13 #include <QStandardPaths>
13 #include <QStandardPaths>
14 #include <QStringList>
14 #include <QStringList>
15 #include <SqpApplication.h>
15 #include <SqpApplication.h>
16 #include <TimeSeries.h>
16 #include <TimeSeries.h>
17 #include <functional>
17 #include <functional>
18 #include <iostream>
18 #include <iostream>
19
19
20
20
21 const auto DATA_SOURCE_NAME = QStringLiteral("PythonProviders");
21 const auto DATA_SOURCE_NAME = QStringLiteral("PythonProviders");
22
22
23 class PythonProvider : public IDataProvider
23 class PythonProvider : public IDataProvider
24 {
24 {
25 public:
25 public:
26 explicit PythonProvider(PythonInterpreter::provider_funct_t f) : _pythonFunction { f } {}
26 explicit PythonProvider(PythonInterpreter::provider_funct_t f) : _pythonFunction { f } {}
27
27
28 PythonProvider(const PythonProvider& other) : _pythonFunction { other._pythonFunction } {}
28 PythonProvider(const PythonProvider& other) : _pythonFunction { other._pythonFunction } {}
29
29
30 std::shared_ptr<IDataProvider> clone() const override
30 std::shared_ptr<IDataProvider> clone() const override
31 {
31 {
32 return std::make_shared<PythonProvider>(*this);
32 return std::make_shared<PythonProvider>(*this);
33 }
33 }
34 virtual TimeSeries::ITimeSerie* getData(const DataProviderParameters& parameters) override
34 virtual TimeSeries::ITimeSerie* getData(const DataProviderParameters& parameters) override
35 {
35 {
36 auto product = parameters.m_Data.value("PRODUCT", "").toString().toStdString();
36 auto product = parameters.m_Data.value("PRODUCT", "").toString().toStdString();
37 auto range = parameters.m_Range;
37 auto range = parameters.m_Range;
38 std::vector<std::tuple<std::string, std::string>> metadata;
38 std::vector<std::tuple<std::string, std::string>> metadata;
39 std::transform(parameters.m_Data.constKeyValueBegin(), parameters.m_Data.constKeyValueEnd(),
39 std::transform(parameters.m_Data.constKeyValueBegin(), parameters.m_Data.constKeyValueEnd(),
40 std::back_inserter(metadata), [](const auto& item) {
40 std::back_inserter(metadata), [](const auto& item) {
41 return std::tuple<std::string, std::string> { item.first.toStdString(),
41 return std::tuple<std::string, std::string> { item.first.toStdString(),
42 item.second.toString().toStdString() };
42 item.second.toString().toStdString() };
43 });
43 });
44 auto result = _pythonFunction(metadata, range.m_TStart, range.m_TEnd);
44 auto result = _pythonFunction(metadata, range.m_TStart, range.m_TEnd);
45 return TimeSeriesUtils::copy(result);
45 return TimeSeriesUtils::copy(result);
46 }
46 }
47
47
48 private:
48 private:
49 PythonInterpreter::provider_funct_t _pythonFunction;
49 PythonInterpreter::provider_funct_t _pythonFunction;
50 };
50 };
51
51
52
52
53 void PythonProviders::initialize()
53 void PythonProviders::initialize()
54 {
54 {
55 auto app_path = sqpApp->applicationDirPath();
55 auto app_path = sqpApp->applicationDirPath();
56 _interpreter.eval_str("import sys");
56 _interpreter.eval_str("import sys");
57 for(const auto& path:{"/../lib","/../lib64","/../core"})
57 for(const auto& path:{"/../lib","/../lib64","/../core","/../../lib"})
58 {
58 {
59 QDir d{app_path+path};
59 QDir d{app_path+path};
60 if(d.exists())
60 if(d.exists())
61 {
61 {
62 _interpreter.eval_str("sys.path.append(\""+d.path().toStdString()+"\")");
62 _interpreter.eval_str("sys.path.append(\""+d.path().toStdString()+"\")");
63 }
63 }
64 }
64 }
65
65
66 _interpreter.add_register_callback(
66 _interpreter.add_register_callback(
67 [this](const std::vector<PythonInterpreter::product_t>& product_list,
67 [this](const std::vector<PythonInterpreter::product_t>& product_list,
68 PythonInterpreter::provider_funct_t f) { this->register_product(product_list, f); });
68 PythonInterpreter::provider_funct_t f) { this->register_product(product_list, f); });
69
69
70 for (const auto& path : QStandardPaths::standardLocations(QStandardPaths::AppLocalDataLocation))
70 for (const auto& path : QStandardPaths::standardLocations(QStandardPaths::AppLocalDataLocation))
71 {
71 {
72 auto dir = QDir(path + "/python");
72 auto dir = QDir(path + "/python");
73 if (dir.exists())
73 if (dir.exists())
74 {
74 {
75 for (const auto& entry :
75 for (const auto& entry :
76 dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot, QDir::Name))
76 dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot, QDir::Name))
77 {
77 {
78 if (entry.isFile() && entry.suffix() == "py")
78 if (entry.isFile() && entry.suffix() == "py")
79 {
79 {
80 _interpreter.eval(entry.absoluteFilePath().toStdString());
80 _interpreter.eval(entry.absoluteFilePath().toStdString());
81 }
81 }
82 }
82 }
83 }
83 }
84 }
84 }
85 for (const auto& embed_file : { ":/test.py", ":/amda.py", ":/cdaweb.py"})
85 for (const auto& embed_file : { ":/test.py", ":/amda.py", ":/cdaweb.py"})
86 {
86 {
87 QFile file(embed_file);
87 QFile file(embed_file);
88 file.open(QFile::ReadOnly);
88 file.open(QFile::ReadOnly);
89 if(file.isOpen())
89 if(file.isOpen())
90 _interpreter.eval_str(file.readAll().toStdString());
90 _interpreter.eval_str(file.readAll().toStdString());
91 }
91 }
92 _interpreter.release();
92 _interpreter.release();
93 }
93 }
94
94
95 PythonProviders::~PythonProviders() {}
95 PythonProviders::~PythonProviders() {}
96
96
97 std::unique_ptr<DataSourceItem> make_folder_item(const QString& name)
97 std::unique_ptr<DataSourceItem> make_folder_item(const QString& name)
98 {
98 {
99 return std::make_unique<DataSourceItem>(DataSourceItemType::NODE, name);
99 return std::make_unique<DataSourceItem>(DataSourceItemType::NODE, name);
100 }
100 }
101
101
102 template <typename T>
102 template <typename T>
103 DataSourceItem* make_path_items(
103 DataSourceItem* make_path_items(
104 const T& path_list_begin, const T& path_list_end, DataSourceItem* root)
104 const T& path_list_begin, const T& path_list_end, DataSourceItem* root)
105 {
105 {
106 std::for_each(path_list_begin, path_list_end, [&root](const auto& folder_name) mutable {
106 std::for_each(path_list_begin, path_list_end, [&root](const auto& folder_name) mutable {
107 auto folder_ptr = root->findItem(folder_name);
107 auto folder_ptr = root->findItem(folder_name);
108 if (folder_ptr == nullptr)
108 if (folder_ptr == nullptr)
109 {
109 {
110 auto folder = make_folder_item(folder_name);
110 auto folder = make_folder_item(folder_name);
111 folder_ptr = folder.get();
111 folder_ptr = folder.get();
112 root->appendChild(std::move(folder));
112 root->appendChild(std::move(folder));
113 }
113 }
114 root = folder_ptr;
114 root = folder_ptr;
115 });
115 });
116 return root;
116 return root;
117 }
117 }
118
118
119 std::unique_ptr<DataSourceItem> make_product_item(
119 std::unique_ptr<DataSourceItem> make_product_item(
120 const QVariantHash& metaData, const QUuid& dataSourceUid)
120 const QVariantHash& metaData, const QUuid& dataSourceUid)
121 {
121 {
122 auto result = std::make_unique<DataSourceItem>(DataSourceItemType::PRODUCT, metaData);
122 auto result = std::make_unique<DataSourceItem>(DataSourceItemType::PRODUCT, metaData);
123
123
124 // Adds plugin name to product metadata
124 // Adds plugin name to product metadata
125 result->setData(DataSourceItem::PLUGIN_DATA_KEY, DATA_SOURCE_NAME);
125 result->setData(DataSourceItem::PLUGIN_DATA_KEY, DATA_SOURCE_NAME);
126 result->setData(DataSourceItem::ID_DATA_KEY, metaData.value(DataSourceItem::NAME_DATA_KEY));
126 result->setData(DataSourceItem::ID_DATA_KEY, metaData.value(DataSourceItem::NAME_DATA_KEY));
127
127
128 auto productName = metaData.value(DataSourceItem::NAME_DATA_KEY).toString();
128 auto productName = metaData.value(DataSourceItem::NAME_DATA_KEY).toString();
129
129
130 // Add action to load product from DataSourceController
130 // Add action to load product from DataSourceController
131 result->addAction(
131 result->addAction(
132 std::make_unique<DataSourceItemAction>(QObject::tr("Load %1 product").arg(productName),
132 std::make_unique<DataSourceItemAction>(QObject::tr("Load %1 product").arg(productName),
133 [productName, dataSourceUid](DataSourceItem& item) {
133 [productName, dataSourceUid](DataSourceItem& item) {
134 if (auto app = sqpApp)
134 if (auto app = sqpApp)
135 {
135 {
136 app->dataSourceController().loadProductItem(dataSourceUid, item);
136 app->dataSourceController().loadProductItem(dataSourceUid, item);
137 }
137 }
138 }));
138 }));
139
139
140 return result;
140 return result;
141 }
141 }
142
142
143 void PythonProviders::register_product(
143 void PythonProviders::register_product(
144 const std::vector<PythonInterpreter::product_t>& product_list,
144 const std::vector<PythonInterpreter::product_t>& product_list,
145 PythonInterpreter::provider_funct_t f)
145 PythonInterpreter::provider_funct_t f)
146 {
146 {
147 auto& dataSourceController = sqpApp->dataSourceController();
147 auto& dataSourceController = sqpApp->dataSourceController();
148 QString test = DATA_SOURCE_NAME + QUuid::createUuid().toString();
148 QString test = DATA_SOURCE_NAME + QUuid::createUuid().toString();
149 auto id = dataSourceController.registerDataSource(test);
149 auto id = dataSourceController.registerDataSource(test);
150 auto root = make_folder_item(test);
150 auto root = make_folder_item(test);
151 std::for_each(std::cbegin(product_list), std::cend(product_list),
151 std::for_each(std::cbegin(product_list), std::cend(product_list),
152 [id, f, root = root.get()](const auto& product) {
152 [id, f, root = root.get()](const auto& product) {
153 const auto& path = std::get<0>(product);
153 const auto& path = std::get<0>(product);
154 auto path_list = QString::fromStdString(path).split('/', QString::SkipEmptyParts);
154 auto path_list = QString::fromStdString(path).split('/', QString::SkipEmptyParts);
155 auto name = *(std::cend(path_list) - 1);
155 auto name = *(std::cend(path_list) - 1);
156 auto path_item
156 auto path_item
157 = make_path_items(std::cbegin(path_list), std::cend(path_list) - 1, root);
157 = make_path_items(std::cbegin(path_list), std::cend(path_list) - 1, root);
158 QVariantHash metaData { { DataSourceItem::NAME_DATA_KEY, name } };
158 QVariantHash metaData { { DataSourceItem::NAME_DATA_KEY, name } };
159 std::for_each(std::cbegin(std::get<2>(product)), std::cend(std::get<2>(product)),
159 std::for_each(std::cbegin(std::get<2>(product)), std::cend(std::get<2>(product)),
160 [&metaData](const auto& mdata) {
160 [&metaData](const auto& mdata) {
161 metaData[QString::fromStdString(mdata.first)]
161 metaData[QString::fromStdString(mdata.first)]
162 = QString::fromStdString(mdata.second);
162 = QString::fromStdString(mdata.second);
163 });
163 });
164 path_item->appendChild(make_product_item(metaData, id));
164 path_item->appendChild(make_product_item(metaData, id));
165 });
165 });
166 dataSourceController.setDataSourceItem(id, std::move(root));
166 dataSourceController.setDataSourceItem(id, std::move(root));
167 dataSourceController.setDataProvider(id, std::make_unique<PythonProvider>(f));
167 dataSourceController.setDataProvider(id, std::make_unique<PythonProvider>(f));
168 }
168 }
General Comments 0
You need to be logged in to leave comments. Login now