1 /** |
|
2 * Relational pipes |
|
3 * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 * |
|
5 * This program is free software: you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation, version 3 of the License. |
|
8 * |
|
9 * This program is distributed in the hope that it will be useful, |
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 * GNU General Public License for more details. |
|
13 * |
|
14 * You should have received a copy of the GNU General Public License |
|
15 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 */ |
|
17 #pragma once |
|
18 |
|
19 #include <vector> |
|
20 #include <filesystem> |
|
21 |
|
22 #include <relpipe/writer/typedefs.h> |
|
23 #include <relpipe/writer/AttributeMetadata.h> |
|
24 #include <relpipe/writer/RelationalWriter.h> |
|
25 #include <regex> |
|
26 |
|
27 #include "RequestedField.h" |
|
28 #include "SystemProcess.h" |
|
29 #include "AttributeFinder.h" |
|
30 |
|
31 namespace relpipe { |
|
32 namespace in { |
|
33 namespace filesystem { |
|
34 |
|
35 namespace fs = std::filesystem; |
|
36 using namespace relpipe::writer; |
|
37 |
|
38 class ScriptAttributeFinder : public AttributeFinder { |
|
39 private: |
|
40 std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings. |
|
41 |
|
42 std::string getScriptCommand(const RequestedField& field) { |
|
43 return SCRIPT_PREFIX + convertor.to_bytes(field.name); |
|
44 } |
|
45 |
|
46 std::vector<std::string> toEnvironmentalVariables(const std::vector<string_t>& vector) { |
|
47 std::vector<std::string> result; |
|
48 for (int i = 0; i < vector.size();) { |
|
49 string_t name = vector[i++]; |
|
50 string_t value = vector[i++]; |
|
51 if (name.rfind(L"env:" == 0)) { |
|
52 result.push_back(convertor.to_bytes(name.substr(4))); |
|
53 result.push_back(convertor.to_bytes(value)); |
|
54 } |
|
55 } |
|
56 return result; |
|
57 } |
|
58 |
|
59 TypeId getAttributeType(const RequestedField& field, const string_t& alias) { |
|
60 // TODO: put latest supported version in the environmental variable |
|
61 // TODO: put alias in the environmental variable |
|
62 SystemProcess process({getScriptCommand(field)}, toEnvironmentalVariables(field.options)); |
|
63 std::string output = process.execute(); |
|
64 std::regex pattern("(.*)\\n(.*)\\n"); |
|
65 std::smatch match; |
|
66 std::regex_match(output, match, pattern); |
|
67 if (match.ready() && match[1] == "1") { |
|
68 // TODO: move to a common library |
|
69 if (match[2] == "boolean") return TypeId::BOOLEAN; |
|
70 if (match[2] == "integer") return TypeId::INTEGER; |
|
71 if (match[2] == "string") return TypeId::STRING; |
|
72 throw RelpipeWriterException(L"Unsupported script data type – field: „" + field.name + L"“ type: „" + convertor.from_bytes(match[2]) + L"“"); |
|
73 } else { |
|
74 throw RelpipeWriterException(L"Unsupported script version – field: „" + field.name + L"“ output: „" + convertor.from_bytes(output) + L"“"); |
|
75 } |
|
76 |
|
77 } |
|
78 |
|
79 string_t getScriptOutput(const fs::path& file, const RequestedField& field, const string_t& alias) { |
|
80 try { |
|
81 // TODO: put alias in the environmental variable |
|
82 SystemProcess process({getScriptCommand(field), currentFileRaw}, toEnvironmentalVariables(field.options)); |
|
83 return convertor.from_bytes(process.execute()); |
|
84 } catch (relpipe::cli::RelpipeCLIException& e) { |
|
85 // TODO: print warnings? |
|
86 // TODO: do not fork/exec if the file is not readable |
|
87 return L""; |
|
88 } |
|
89 } |
|
90 protected: |
|
91 |
|
92 virtual void writeFieldOfExistingFile(RelationalWriter* writer, const RequestedField& field) override { |
|
93 // TODO: paralelization? |
|
94 if (field.group == RequestedField::GROUP_SCRIPT) { |
|
95 for (string_t alias : field.getAliases()) { |
|
96 writer->writeAttribute(getScriptOutput(currentFile, field, alias)); |
|
97 } |
|
98 } |
|
99 } |
|
100 |
|
101 public: |
|
102 |
|
103 static const std::string SCRIPT_PREFIX; |
|
104 |
|
105 virtual vector<AttributeMetadata> toMetadata(const RequestedField& field) override { |
|
106 if (field.group == RequestedField::GROUP_SCRIPT) { |
|
107 vector<AttributeMetadata> metadata; |
|
108 for (string_t alias : field.getAliases()) metadata.push_back(AttributeMetadata{alias, getAttributeType(field, alias)}); |
|
109 return metadata; |
|
110 } else { |
|
111 return {}; |
|
112 } |
|
113 } |
|
114 |
|
115 virtual ~ScriptAttributeFinder() override { |
|
116 } |
|
117 }; |
|
118 |
|
119 const std::string ScriptAttributeFinder::SCRIPT_PREFIX = "__relpipe_in_filesystem_script_"; |
|
120 |
|
121 } |
|
122 } |
|
123 } |
|