--- a/src/ScriptAttributeFinder.h Mon Nov 11 14:42:13 2019 +0100
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,123 +0,0 @@
-/**
- * Relational pipes
- * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, version 3 of the License.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-#pragma once
-
-#include <vector>
-#include <filesystem>
-
-#include <relpipe/writer/typedefs.h>
-#include <relpipe/writer/AttributeMetadata.h>
-#include <relpipe/writer/RelationalWriter.h>
-#include <regex>
-
-#include "RequestedField.h"
-#include "SystemProcess.h"
-#include "AttributeFinder.h"
-
-namespace relpipe {
-namespace in {
-namespace filesystem {
-
-namespace fs = std::filesystem;
-using namespace relpipe::writer;
-
-class ScriptAttributeFinder : public AttributeFinder {
-private:
- std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
-
- std::string getScriptCommand(const RequestedField& field) {
- return SCRIPT_PREFIX + convertor.to_bytes(field.name);
- }
-
- std::vector<std::string> toEnvironmentalVariables(const std::vector<string_t>& vector) {
- std::vector<std::string> result;
- for (int i = 0; i < vector.size();) {
- string_t name = vector[i++];
- string_t value = vector[i++];
- if (name.rfind(L"env:" == 0)) {
- result.push_back(convertor.to_bytes(name.substr(4)));
- result.push_back(convertor.to_bytes(value));
- }
- }
- return result;
- }
-
- TypeId getAttributeType(const RequestedField& field, const string_t& alias) {
- // TODO: put latest supported version in the environmental variable
- // TODO: put alias in the environmental variable
- SystemProcess process({getScriptCommand(field)}, toEnvironmentalVariables(field.options));
- std::string output = process.execute();
- std::regex pattern("(.*)\\n(.*)\\n");
- std::smatch match;
- std::regex_match(output, match, pattern);
- if (match.ready() && match[1] == "1") {
- // TODO: move to a common library
- if (match[2] == "boolean") return TypeId::BOOLEAN;
- if (match[2] == "integer") return TypeId::INTEGER;
- if (match[2] == "string") return TypeId::STRING;
- throw RelpipeWriterException(L"Unsupported script data type – field: „" + field.name + L"“ type: „" + convertor.from_bytes(match[2]) + L"“");
- } else {
- throw RelpipeWriterException(L"Unsupported script version – field: „" + field.name + L"“ output: „" + convertor.from_bytes(output) + L"“");
- }
-
- }
-
- string_t getScriptOutput(const fs::path& file, const RequestedField& field, const string_t& alias) {
- try {
- // TODO: put alias in the environmental variable
- SystemProcess process({getScriptCommand(field), currentFileRaw}, toEnvironmentalVariables(field.options));
- return convertor.from_bytes(process.execute());
- } catch (relpipe::cli::RelpipeCLIException& e) {
- // TODO: print warnings?
- // TODO: do not fork/exec if the file is not readable
- return L"";
- }
- }
-protected:
-
- virtual void writeFieldOfExistingFile(RelationalWriter* writer, const RequestedField& field) override {
- // TODO: paralelization?
- if (field.group == RequestedField::GROUP_SCRIPT) {
- for (string_t alias : field.getAliases()) {
- writer->writeAttribute(getScriptOutput(currentFile, field, alias));
- }
- }
- }
-
-public:
-
- static const std::string SCRIPT_PREFIX;
-
- virtual vector<AttributeMetadata> toMetadata(const RequestedField& field) override {
- if (field.group == RequestedField::GROUP_SCRIPT) {
- vector<AttributeMetadata> metadata;
- for (string_t alias : field.getAliases()) metadata.push_back(AttributeMetadata{alias, getAttributeType(field, alias)});
- return metadata;
- } else {
- return {};
- }
- }
-
- virtual ~ScriptAttributeFinder() override {
- }
-};
-
-const std::string ScriptAttributeFinder::SCRIPT_PREFIX = "__relpipe_in_filesystem_script_";
-
-}
-}
-}