<commit_before>/*--------------------------------------------------------------------------*/
/*  Copyright 2019 <NAME>                                          */
/*                                                                          */
/*  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 <curl/curl.h>

#ifndef NLOHMANN_JSON_VERSION_MAJOR
#include "json.hpp"
#endif

#include <cassert>
#include <functional>
#include <string>
#include <list>
#include <chrono>
#include <thread>
#include <iostream>

#include <stdio.h>
#include <sys/stat.h>

using json = nlohmann::json;
using namespace std;

const string EVEREST_URL = "https://everest.distcomp.org";
const std::chrono::seconds DELAY(1);
const bool VERBOSE = false;

namespace TEMPLET {

	class task;
	
	size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) {
		((string *)userp)->append((char *)contents, size * nmemb);
		return size * nmemb;
	}

	struct event{
		string _state;
		string _id;
		task*  _task;
	};

	class taskengine {
	public:
		taskengine(const char*login, const char*pass, const char* label=0) {
			_login = login; _pass = pass; _curl = NULL; _recount = 0;
			if (label)_label = label; else _label = "templet-session";
			_is_ready = init();
		}
		taskengine(const char* access_token) {
			_curl = NULL; _recount = 0; _access_token = access_token;
			_is_ready = init();
		}

		~taskengine() {	cleanup(); }
		explicit operator bool() { return _is_ready; }

		bool wait_all();
		bool wait_for(task& t);
		bool wait_some();

		bool submit(task&t);
		bool is_idle(task&t);
		bool is_done(task&t);

		bool get_app_description(const char* app_name);
		bool get_access_token(string&t) {if(_is_ready){t=_access_token; return true;} else return false;}

		bool upload(const string& file, string& uri);
		bool download(const string& file, const string& uri);
		bool remove(const string& uri);

	private:
		inline bool _wait_loop_body(event&ev);
		inline bool init();
		inline void cleanup();

	private:
		string _login;
		string _pass;
		string _label;
		string _access_token;
		bool   _is_ready;
		int    _recount;

		curl_slist* _common_headers;
		curl_slist* _upload_headers;
		
		CURL*  _curl;
		long   _code;
		string _response;

		list<event> _submitted;
	};

	class task {
		friend	class taskengine;
	public:
		task(taskengine&e,const char* app_id=0) :
			_eng(&e), _is_idle(true), _is_done(false), _on_ready([]() {}), _app_ID(app_id), _deletable(false) {}
		task() : _eng(0), _is_idle(true), _is_done(false), _on_ready([]() {}), _app_ID(0), _deletable(false) {} 

		bool set_app_id(const char*id) { if (_is_idle) { _app_ID = id; return true; } return false; }
		bool set_engine(taskengine&e) { if (_is_idle) { _eng = &e; return true; } return false; }
		void set_deletable(bool del) { _deletable = del; }
		
		json& result() { return _result; }
		void  input(json&in) { _input = in; }

		bool submit() { return _eng->submit(*this); }<commit_msg>some fixes<commit_after>/*--------------------------------------------------------------------------*/
/*  Copyright 2019 <NAME>                                          */
/*                                                                          */
/*  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 <curl/curl.h>

#ifndef NLOHMANN_JSON_VERSION_MAJOR
#include "json.hpp"
#endif

#include <cassert>
#include <functional>
#include <string>
#include <list>
#include <chrono>
#include <thread>
#include <iostream>

#include <stdio.h>
#include <sys/stat.h>

using json = nlohmann::json;
using namespace std;

const string EVEREST_URL = "https://everest.distcomp.org";
const std::chrono::seconds DELAY(1);
const bool VERBOSE = false;

namespace TEMPLET {

	class task;
	
	size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) {
		((string *)userp)->append((char *)contents, size * nmemb);
		return size * nmemb;
	}

	struct event{
		string _state;
		string _id;
		task*  _task;
	};

	class taskengine {
	public:
		taskengine(const char*login, const char*pass, const char* label=0) {
			_login = login; _pass = pass; _curl = NULL; _recount = 0;
			if (label)_label = label; else _label = "templet-session";
			_is_ready = init();
		}
		taskengine(const char* access_token) {
			_curl = NULL; _recount = 0; _access_token = access_token;
			_is_ready = init();
		}

		~taskengine() {	cleanup(); }
		void free_access_token(){_pass.clear();}
		explicit operator bool() { return _is_ready; }

		bool wait_all();
		bool wait_for(task& t);
		bool wait_some();

		bool submit(task&t);
		bool is_idle(task&t);
		bool is_done(task&t);

		bool get_app_description(const char* app_name);
		bool get_access_token(string&t) {if(_is_ready){t=_access_token; return true;} else return false;}

		bool upload(const string& file, string& uri);
		bool download(const string& file, const string& uri);
		bool remove(const string& uri);

	private:
		inline bool _wait_loop_body(event&ev);
		inline bool init();
		inline void cleanup();

	private:
		string _login;
		string _pass;
		string _label;
		string _access_token;
		bool   _is_ready;
		int    _recount;

		curl_slist* _common_headers;
		curl_slist* _upload_headers;
		
		CURL*  _curl;
		long   _code;
		string _response;

		list<event> _submitted;
	};

	class task {
		friend	class taskengine;
	public:
		task(taskengine&e,const char* app_id=0) :
			_eng(&e), _is_idle(true), _is_done(false), _on_ready([]() {}), _app_ID(app_id), _deletable(false) {}
		task() : _eng(0), _is_idle(true), _is_done(false), _on_ready([]() {}), _app_ID(""), _deletable(false) {} 

		bool set_app_id(const char*id) { if (_is_idle) { _app_ID = id; return true; } return false; }
		bool set_engine(taskengine&e) { if (_is_idle) { _eng = &e; return true; } return false; }
		void set_deletable(bool del) { _deletable = del; }
		
		json& result() { return _result; }
		void  input(json&in) { _input = in; }

		bool submit() { return _eng->submit(*this); }
{'fraction_non_alphanumeric': 0.1568576947842885, 'fraction_numerical': 0.0037347070186735352, 'mean_word_length': 2.2671434581405134, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 31, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3658714', 'n_tokens_mistral': 2538, 'n_tokens_neox': 2361, 'n_words': 675}