@@ -1,168 +1,168 | |||
|
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 | auto app_path = sqpApp->applicationDirPath(); |
|
56 | 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 | 59 | QDir d{app_path+path}; |
|
60 | 60 | if(d.exists()) |
|
61 | 61 | { |
|
62 | 62 | _interpreter.eval_str("sys.path.append(\""+d.path().toStdString()+"\")"); |
|
63 | 63 | } |
|
64 | 64 | } |
|
65 | 65 | |
|
66 | 66 | _interpreter.add_register_callback( |
|
67 | 67 | [this](const std::vector<PythonInterpreter::product_t>& product_list, |
|
68 | 68 | PythonInterpreter::provider_funct_t f) { this->register_product(product_list, f); }); |
|
69 | 69 | |
|
70 | 70 | for (const auto& path : QStandardPaths::standardLocations(QStandardPaths::AppLocalDataLocation)) |
|
71 | 71 | { |
|
72 | 72 | auto dir = QDir(path + "/python"); |
|
73 | 73 | if (dir.exists()) |
|
74 | 74 | { |
|
75 | 75 | for (const auto& entry : |
|
76 | 76 | dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot, QDir::Name)) |
|
77 | 77 | { |
|
78 | 78 | if (entry.isFile() && entry.suffix() == "py") |
|
79 | 79 | { |
|
80 | 80 | _interpreter.eval(entry.absoluteFilePath().toStdString()); |
|
81 | 81 | } |
|
82 | 82 | } |
|
83 | 83 | } |
|
84 | 84 | } |
|
85 | 85 | for (const auto& embed_file : { ":/test.py", ":/amda.py", ":/cdaweb.py"}) |
|
86 | 86 | { |
|
87 | 87 | QFile file(embed_file); |
|
88 | 88 | file.open(QFile::ReadOnly); |
|
89 | 89 | if(file.isOpen()) |
|
90 | 90 | _interpreter.eval_str(file.readAll().toStdString()); |
|
91 | 91 | } |
|
92 | 92 | _interpreter.release(); |
|
93 | 93 | } |
|
94 | 94 | |
|
95 | 95 | PythonProviders::~PythonProviders() {} |
|
96 | 96 | |
|
97 | 97 | std::unique_ptr<DataSourceItem> make_folder_item(const QString& name) |
|
98 | 98 | { |
|
99 | 99 | return std::make_unique<DataSourceItem>(DataSourceItemType::NODE, name); |
|
100 | 100 | } |
|
101 | 101 | |
|
102 | 102 | template <typename T> |
|
103 | 103 | DataSourceItem* make_path_items( |
|
104 | 104 | const T& path_list_begin, const T& path_list_end, DataSourceItem* root) |
|
105 | 105 | { |
|
106 | 106 | std::for_each(path_list_begin, path_list_end, [&root](const auto& folder_name) mutable { |
|
107 | 107 | auto folder_ptr = root->findItem(folder_name); |
|
108 | 108 | if (folder_ptr == nullptr) |
|
109 | 109 | { |
|
110 | 110 | auto folder = make_folder_item(folder_name); |
|
111 | 111 | folder_ptr = folder.get(); |
|
112 | 112 | root->appendChild(std::move(folder)); |
|
113 | 113 | } |
|
114 | 114 | root = folder_ptr; |
|
115 | 115 | }); |
|
116 | 116 | return root; |
|
117 | 117 | } |
|
118 | 118 | |
|
119 | 119 | std::unique_ptr<DataSourceItem> make_product_item( |
|
120 | 120 | const QVariantHash& metaData, const QUuid& dataSourceUid) |
|
121 | 121 | { |
|
122 | 122 | auto result = std::make_unique<DataSourceItem>(DataSourceItemType::PRODUCT, metaData); |
|
123 | 123 | |
|
124 | 124 | // Adds plugin name to product metadata |
|
125 | 125 | result->setData(DataSourceItem::PLUGIN_DATA_KEY, DATA_SOURCE_NAME); |
|
126 | 126 | result->setData(DataSourceItem::ID_DATA_KEY, metaData.value(DataSourceItem::NAME_DATA_KEY)); |
|
127 | 127 | |
|
128 | 128 | auto productName = metaData.value(DataSourceItem::NAME_DATA_KEY).toString(); |
|
129 | 129 | |
|
130 | 130 | // Add action to load product from DataSourceController |
|
131 | 131 | result->addAction( |
|
132 | 132 | std::make_unique<DataSourceItemAction>(QObject::tr("Load %1 product").arg(productName), |
|
133 | 133 | [productName, dataSourceUid](DataSourceItem& item) { |
|
134 | 134 | if (auto app = sqpApp) |
|
135 | 135 | { |
|
136 | 136 | app->dataSourceController().loadProductItem(dataSourceUid, item); |
|
137 | 137 | } |
|
138 | 138 | })); |
|
139 | 139 | |
|
140 | 140 | return result; |
|
141 | 141 | } |
|
142 | 142 | |
|
143 | 143 | void PythonProviders::register_product( |
|
144 | 144 | const std::vector<PythonInterpreter::product_t>& product_list, |
|
145 | 145 | PythonInterpreter::provider_funct_t f) |
|
146 | 146 | { |
|
147 | 147 | auto& dataSourceController = sqpApp->dataSourceController(); |
|
148 | 148 | QString test = DATA_SOURCE_NAME + QUuid::createUuid().toString(); |
|
149 | 149 | auto id = dataSourceController.registerDataSource(test); |
|
150 | 150 | auto root = make_folder_item(test); |
|
151 | 151 | std::for_each(std::cbegin(product_list), std::cend(product_list), |
|
152 | 152 | [id, f, root = root.get()](const auto& product) { |
|
153 | 153 | const auto& path = std::get<0>(product); |
|
154 | 154 | auto path_list = QString::fromStdString(path).split('/', QString::SkipEmptyParts); |
|
155 | 155 | auto name = *(std::cend(path_list) - 1); |
|
156 | 156 | auto path_item |
|
157 | 157 | = make_path_items(std::cbegin(path_list), std::cend(path_list) - 1, root); |
|
158 | 158 | QVariantHash metaData { { DataSourceItem::NAME_DATA_KEY, name } }; |
|
159 | 159 | std::for_each(std::cbegin(std::get<2>(product)), std::cend(std::get<2>(product)), |
|
160 | 160 | [&metaData](const auto& mdata) { |
|
161 | 161 | metaData[QString::fromStdString(mdata.first)] |
|
162 | 162 | = QString::fromStdString(mdata.second); |
|
163 | 163 | }); |
|
164 | 164 | path_item->appendChild(make_product_item(metaData, id)); |
|
165 | 165 | }); |
|
166 | 166 | dataSourceController.setDataSourceItem(id, std::move(root)); |
|
167 | 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