<commit_before>/**
*  Copyright (C) 2019 3D Repo Ltd
*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU Affero General Public License as
*  published by the Free Software Foundation, either version 3 of the
*  License, or (at your option) any later version.
*
*  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 Affero General Public License for more details.
*
*  You should have received a copy of the GNU Affero General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
/**
* A (attempt to be) thread safe stack essentially that would push/pop items as required
* Incredibly weird, I know.
*/

#include "repo_config.h"
#include "repo_exception.h"
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/filesystem.hpp>
using namespace repo::lib;


RepoConfig::RepoConfig(
	const std::string &databaseAddr,
	const int &port,
	const std::string &username,
	const std::string &password,
	const bool pwDigested) : defaultStorage(FileStorageEngine::GRIDFS)
{
	dbConf.addr = databaseAddr;
	dbConf.port = port;
	dbConf.username = username;
	dbConf.password = password;
	dbConf.pwDigested = pwDigested;
}

void RepoConfig::configureS3(
	const std::string &bucketName,
	const std::string &bucketRegion,
	const bool useAsDefault)
{
	s3Conf.bucketName = bucketName;
	s3Conf.bucketRegion = bucketRegion;
	s3Conf.configured = true;

	if (useAsDefault) defaultStorage = FileStorageEngine::S3;
}

void RepoConfig::configureFS(
	const std::string &directory,
	const int         &level,
	const bool useAsDefault)
{
	fsConf.dir = directory;
	fsConf.nLevel = level;
	fsConf.configured = true;

	if (useAsDefault) defaultStorage = FileStorageEngine::FS;
}

RepoConfig RepoConfig::fromFile(const std::string &filePath) {
	boost::property_tree::ptree jsonTree;
	
	try {
		boost::property_tree::read_json(filePath, jsonTree);
	}
	catch (const std::exception &e) {
		std::string errMsg = "Failed to read configuration file [" + filePath + "] : " + e.what();
		throw RepoException(errMsg.c_str());
	}
	
	//Read database configurations
	auto dbTree = jsonTree.get_child_optional("db");

	if (!dbTree) {
		throw RepoException("Cannot find entry 'db' within configuration file.");
	}

	auto dbAddr = dbTree->get<std::string>("dbhost", "");
	auto dbPort = dbTree->get<int>("dbport", -1);
	auto username = dbTree->get<std::string>("username", "");
	auto password = dbTree->get<std::string>("password", "");

	if (dbAddr.empty() || dbPort == -1) {
		throw RepoException("Database address and port not specified within configuration file.");
	}

	repo::lib::RepoConfig config = { dbAddr, dbPort, username, password };

	//Read S3 configurations if found
	auto s3Tree = jsonTree.get_child_optional("aws");

	if (s3Tree) {
		auto bucketName = s3Tree->get<std::string>("bucket_name", "");
		auto bucketRegion = s3Tree->get<std::string>("bucket_region", "");
		auto useAsDefault = s3Tree->get<bool>("default", false);
		if(!bucketName.empty() && !bucketRegion.empty())
			config.configureS3(bucketName, bucketRegion, useAsDefault);
	}
<commit_msg>ISSUE #320 no longer need c_str() since it's our own class.<commit_after>/**
*  Copyright (C) 2019 3D Repo Ltd
*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU Affero General Public License as
*  published by the Free Software Foundation, either version 3 of the
*  License, or (at your option) any later version.
*
*  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 Affero General Public License for more details.
*
*  You should have received a copy of the GNU Affero General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
/**
* A (attempt to be) thread safe stack essentially that would push/pop items as required
* Incredibly weird, I know.
*/

#include "repo_config.h"
#include "repo_exception.h"
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/filesystem.hpp>
using namespace repo::lib;


RepoConfig::RepoConfig(
	const std::string &databaseAddr,
	const int &port,
	const std::string &username,
	const std::string &password,
	const bool pwDigested) : defaultStorage(FileStorageEngine::GRIDFS)
{
	dbConf.addr = databaseAddr;
	dbConf.port = port;
	dbConf.username = username;
	dbConf.password = password;
	dbConf.pwDigested = pwDigested;
}

void RepoConfig::configureS3(
	const std::string &bucketName,
	const std::string &bucketRegion,
	const bool useAsDefault)
{
	s3Conf.bucketName = bucketName;
	s3Conf.bucketRegion = bucketRegion;
	s3Conf.configured = true;

	if (useAsDefault) defaultStorage = FileStorageEngine::S3;
}

void RepoConfig::configureFS(
	const std::string &directory,
	const int         &level,
	const bool useAsDefault)
{
	fsConf.dir = directory;
	fsConf.nLevel = level;
	fsConf.configured = true;

	if (useAsDefault) defaultStorage = FileStorageEngine::FS;
}

RepoConfig RepoConfig::fromFile(const std::string &filePath) {
	boost::property_tree::ptree jsonTree;
	
	try {
		boost::property_tree::read_json(filePath, jsonTree);
	}
	catch (const std::exception &e) {
		throw RepoException("Failed to read configuration file [" + filePath + "] : " + e.what());
	}
	
	//Read database configurations
	auto dbTree = jsonTree.get_child_optional("db");

	if (!dbTree) {
		throw RepoException("Cannot find entry 'db' within configuration file.");
	}

	auto dbAddr = dbTree->get<std::string>("dbhost", "");
	auto dbPort = dbTree->get<int>("dbport", -1);
	auto username = dbTree->get<std::string>("username", "");
	auto password = dbTree->get<std::string>("password", "");

	if (dbAddr.empty() || dbPort == -1) {
		throw RepoException("Database address and port not specified within configuration file.");
	}

	repo::lib::RepoConfig config = { dbAddr, dbPort, username, password };

	//Read S3 configurations if found
	auto s3Tree = jsonTree.get_child_optional("aws");

	if (s3Tree) {
		auto bucketName = s3Tree->get<std::string>("bucket_name", "");
		auto bucketRegion = s3Tree->get<std::string>("bucket_region", "");
		auto useAsDefault = s3Tree->get<bool>("default", false);
		if(!bucketName.empty() && !bucketRegion.empty())
			config.configureS3(bucketName, bucketRegion, useAsDefault);
	}

{'fraction_non_alphanumeric': 0.1251696576685266, 'fraction_numerical': 0.006484693108128488, 'mean_word_length': 4.99096657633243, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1064029', 'n_tokens_mistral': 2120, 'n_tokens_neox': 2086, 'n_words': 621}