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