Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 47 #141

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ add_subdirectory(gwhisper)
add_subdirectory(version)
anna-riesch marked this conversation as resolved.
Show resolved Hide resolved
add_subdirectory(utils)
add_subdirectory(libLocalDescriptorCache)
add_subdirectory(gWhisperSetting)

get_filename_component(BIN_DIR_ABS ${CMAKE_BINARY_DIR}/. DIRECTORY)
get_filename_component(SRC_DIR_ABS ${CMAKE_SOURCE_DIR}/. DIRECTORY)
Expand Down
37 changes: 37 additions & 0 deletions src/gWhisperSetting/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright 2022 IBM Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

include(FetchContent)
FetchContent_Declare(json URL https://github.com/nlohmann/json/releases/download/v3.10.5/json.tar.xz)
FetchContent_MakeAvailable(json)

set(TARGET_NAME "gWhisperSetting")
set(TARGET_SRC
GWhisperSetting.cpp
anna-riesch marked this conversation as resolved.
Show resolved Hide resolved
)
add_library(${TARGET_NAME} ${TARGET_SRC})

target_link_libraries ( ${TARGET_NAME}
PRIVATE
reflection
protoDoc
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think none of those linked libs are needed

)
target_link_libraries ( ${TARGET_NAME}
PUBLIC
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only last two deps needed?

gwhisperUtils
DescDbProxy
ArgParse
nlohmann_json::nlohmann_json
)
target_include_directories(${TARGET_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
146 changes: 146 additions & 0 deletions src/gWhisperSetting/GWhisperSetting.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
// Copyright 2022 IBM Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <fstream>
#include <iostream>
#include <nlohmann/json.hpp>

#include "GWhisperSetting.hpp"
#include "libArgParse/ArgParse.hpp"

using json = nlohmann::json;

void gWhisperSetting::parseConfigFile(const std::string &f_inputFile){
// Parse JSON File into member SON object
std::ifstream ifs(f_inputFile.c_str());

if (!ifs.is_open())
{
std::cout << "Error while opening config file at: " << f_inputFile << std::endl;
exit(EXIT_FAILURE);
}
// TODO: check for empty value with nlohmann::isempty()
anna-riesch marked this conversation as resolved.
Show resolved Hide resolved
ifs >> m_config;
ifs.close();

}

void gWhisperSetting::retrieveConfigParameters(json &f_startElement){
//recursively go over config file and retrieve all parameter keys
if(f_startElement.is_structured())
{
for (const auto &item : f_startElement.items())
{
m_configParameters.push_back(item.key());

if (item.value().is_object())
{
retrieveConfigParameters(item.value());
}
}
}
}

json gWhisperSetting::findParameterSettingInConfig(const std::string &f_parameter,const json &f_startElement)
{
// TODO: If enough time, optimmize for
bool containsParameter = false;
json setting;

for (const auto& item : f_startElement.items())
{
if (item.value().contains(f_parameter))
{
//Abbruchkrit

setting[f_parameter] = item.value().at(f_parameter);
break;
}

for(auto &innerElement : item.value().items())
{
if (innerElement.key() == "")
{
break;
}

// Only Call recursion, if current element contains further elements
if(innerElement.value().is_structured())
{
json tempElement;
tempElement[innerElement.key()] = innerElement.value();
return findParameterSettingInConfig(f_parameter, tempElement);
}
else
{
continue;
}
}
}
return setting;
}

// f_paramaetr entrpricht label des Grammatik Elements in ParseTree
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

english

std::string gWhisperSetting::lookUpSetting(const std::string &f_parameter, ArgParse::ParsedElement &f_parseTree)
{
bool containsParameter;
std::string setting;
json someJson;

if (f_parseTree.findFirstChild(f_parameter) != "")
{
setting = f_parseTree.findFirstChild(f_parameter);
anna-riesch marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
someJson = findParameterSettingInConfig(f_parameter, m_config); //copy
anna-riesch marked this conversation as resolved.
Show resolved Hide resolved
if (!someJson.is_null())
{
if (!someJson.at(f_parameter).is_null())
{
containsParameter = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where used?

setting = someJson.at(f_parameter);
anna-riesch marked this conversation as resolved.
Show resolved Hide resolved
}

if(someJson.at(f_parameter).is_null())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make this a else path instead?

{
setting = ""; // TODO: Is this the right semantic for Timeout?
anna-riesch marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
//std::cout << "Found setting for " << f_parameter << " : " << setting << std::endl;
return setting;
}

// Ersetze ParseTree beim Suchen -> f_parseTree.findFirstChild
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

german comment

// Für Parameter, die nicht im config file stehen / keine optionen sind z.B. services
gWhisperSetting::gWhisperSetting(ArgParse::ParsedElement &f_parseTree){ //ParameterKey
if(m_config.is_null())
{
bool useCustomPath = f_parseTree.findFirstChild("ConfigFile")!= "";
const char* home = std::getenv("HOME");
std::string defaultPath = "/.config/gWhisperConfig.json";
std::string inputFile = home + defaultPath;

if(useCustomPath)
{
inputFile = f_parseTree.findFirstChild("ConfigFilePath");
}

parseConfigFile(inputFile);
}
retrieveConfigParameters(m_config);
}

gWhisperSetting::~gWhisperSetting(){}
40 changes: 40 additions & 0 deletions src/gWhisperSetting/GWhisperSetting.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2022 IBM Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once
#include <nlohmann/json.hpp>
#include "libArgParse/ArgParse.hpp"

using json = nlohmann::json;

class gWhisperSetting{
public:
gWhisperSetting(ArgParse::ParsedElement &f_parseTree);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

document that f_parseTree is here only used to retrieve config location

~gWhisperSetting();

std::string lookUpSetting(const std::string &f_parameter, ArgParse::ParsedElement &f_parseTree);
anna-riesch marked this conversation as resolved.
Show resolved Hide resolved


private:
void parseConfigFile(const std::string &f_inputFile);
void retrieveConfigParameters(json &f_startElement);

/// Searches in all layers of config file, if config contains parameter
/// in
/// out: returns setting for parameter, if setting is found else returns null Json object (at the moment)
json findParameterSettingInConfig(const std::string &f_parameter, const json &f_startLayer);

json m_config;
std::vector<std::string> m_configParameters;
};
1 change: 1 addition & 0 deletions src/gwhisper/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ target_link_libraries ( ${TARGET_NAME}
reflection
version
generateHelpString
gWhisperSetting
)

set_target_properties(
Expand Down
10 changes: 7 additions & 3 deletions src/gwhisper/gwhisper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <libCli/Call.hpp>
#include <libCli/Completion.hpp>
#include <versionDefine.h> // generated during build
#include "GWhisperSetting.hpp"

using namespace ArgParse;

Expand Down Expand Up @@ -51,19 +52,22 @@ int main(int argc, char **argv)
// First we construct the initial Grammar for the CLI tool:
Grammar grammarPool;
GrammarElement *grammarRoot = cli::constructGrammar(grammarPool);

//Here: Build everything gWhisper provides. --> Config replacement here?

// Now we parse the given arguments using the grammar:
std::string args = getArgsAsString(argc, argv);
ParsedElement parseTree;
ParseRc rc = grammarRoot->parse(args.c_str(), parseTree);
ParseRc rc = grammarRoot->parse(args.c_str(), parseTree); //braucht evtl. schon die config
gWhisperSetting paramProxy(parseTree); // look up on this element

if (parseTree.findFirstChild("DotExport") != "")
{
std::cout << grammarPool.getDotGraph();
return 0;
}

if (parseTree.findFirstChild("Complete") != "")
if (paramProxy.lookUpSetting("Complete", parseTree) != "")
{
bool completeDebug = (parseTree.findFirstChild("CompleteDebug") != "");
if (parseTree.findFirstChild("fish") != "")
Expand Down Expand Up @@ -113,7 +117,7 @@ int main(int argc, char **argv)
{
//printf("\nchoice:\n%s", candidate->getDebugString().c_str());
printf("\n '%s'", candidate->getMatchedString().c_str());
}
}
std::cout << std::endl;
}

Expand Down
5 changes: 5 additions & 0 deletions src/libCli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

include(FetchContent)
FetchContent_Declare(json URL https://github.com/nlohmann/json/releases/download/v3.10.5/json.tar.xz)
FetchContent_MakeAvailable(json)

set(TARGET_NAME "cli")
set(TARGET_SRC
Expand All @@ -34,12 +37,14 @@ target_link_libraries ( ${TARGET_NAME}
PRIVATE
reflection
protoDoc
nlohmann_json::nlohmann_json
)
target_link_libraries ( ${TARGET_NAME}
PUBLIC
gwhisperUtils
DescDbProxy
ArgParse
gWhisperSetting
)
target_include_directories(${TARGET_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

Expand Down
Loading