rm ingored
This commit is contained in:
@@ -1,93 +0,0 @@
|
||||
#include "module.h"
|
||||
#include <fstream>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <regex>
|
||||
#include <string>
|
||||
#include "../utils/utils.h"
|
||||
|
||||
StoreModule::StoreModule() {}
|
||||
|
||||
StoreModule::StoreModule(std::string path) {
|
||||
std::ifstream settings_file(path);
|
||||
nlohmann::json settings = nlohmann::json::parse(settings_file);
|
||||
|
||||
this->name = from_utf8(settings["name"]);
|
||||
this->goods_name_regex = from_utf8(settings["goods_name_regex"]);
|
||||
this->goods_price_regex = from_utf8(settings["goods_price_regex"]);
|
||||
this->goods_quantity_regex = from_utf8(settings["goods_quantity_regex"]);
|
||||
this->check_start_regex = from_utf8(settings["check_start_regex"]);
|
||||
this->check_end_regex = from_utf8(settings["check_end_regex"]);
|
||||
|
||||
#ifdef DEBUG
|
||||
std::wcout << "Name: " << this->name << std::endl;
|
||||
std::wcout << "Goods name regex: " << this->goods_name_regex << std::endl;
|
||||
std::wcout << "Goods price regex: " << this->goods_price_regex << std::endl;
|
||||
std::wcout << "Goods quantity regex: " << this->goods_quantity_regex
|
||||
<< std::endl;
|
||||
std::wcout << "Check start regex: " << this->check_start_regex << std::endl;
|
||||
std::wcout << "Check end regex: " << this->check_end_regex << std::endl;
|
||||
#endif
|
||||
}
|
||||
|
||||
std::vector<std::string> StoreModule::parse_name(std::wstring str) {
|
||||
std::vector<std::string> result;
|
||||
std::wregex r(this->goods_name_regex, std::regex::collate);
|
||||
|
||||
for (std::wsregex_iterator it{str.begin(), str.end(), r}, end{}; it != end;
|
||||
it++) {
|
||||
result.push_back(to_utf8(it->str()));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<std::string> StoreModule::parse_price(std::wstring str) {
|
||||
std::vector<std::string> result;
|
||||
std::wregex r(this->goods_price_regex, std::regex::collate);
|
||||
|
||||
for (std::wsregex_iterator it{str.begin(), str.end(), r}, end{}; it != end;
|
||||
it++) {
|
||||
result.push_back(to_utf8(it->str()));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<std::string> StoreModule::parse_quantity(std::wstring str) {
|
||||
std::vector<std::string> result;
|
||||
std::wregex r(this->goods_quantity_regex, std::regex::collate);
|
||||
|
||||
for (std::wsregex_iterator it{str.begin(), str.end(), r}, end{}; it != end;
|
||||
it++) {
|
||||
result.push_back(to_utf8(it->str()));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::wstring StoreModule::trim_check(std::wstring& check) {
|
||||
unsigned int start_pos;
|
||||
unsigned int end_pos;
|
||||
|
||||
std::wregex start_regex(this->check_start_regex, std::regex::collate);
|
||||
std::wregex end_regex(this->check_end_regex, std::regex::collate);
|
||||
|
||||
for (std::wsregex_iterator it{check.begin(), check.end(), start_regex}, end{};
|
||||
it != end; it++) {
|
||||
start_pos = it->position() + it->str().size();
|
||||
break;
|
||||
}
|
||||
|
||||
check = check.substr(start_pos, check.size());
|
||||
|
||||
for (std::wsregex_iterator it{check.begin(), check.end(), end_regex}, end{};
|
||||
it != end; it++) {
|
||||
end_pos = it->position() - 1;
|
||||
break;
|
||||
}
|
||||
|
||||
check = check.substr(0, end_pos);
|
||||
|
||||
return check;
|
||||
}
|
||||
|
||||
std::wstring StoreModule::get_name() { return this->name; }
|
||||
@@ -1,28 +0,0 @@
|
||||
#ifndef STORE_MODULE_H
|
||||
#define STORE_MODULE_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class StoreModule {
|
||||
std::string path;
|
||||
std::wstring name;
|
||||
std::wstring goods_name_regex;
|
||||
std::wstring goods_price_regex;
|
||||
std::wstring goods_quantity_regex;
|
||||
std::wstring check_start_regex;
|
||||
std::wstring check_end_regex;
|
||||
public:
|
||||
StoreModule(std::string);
|
||||
StoreModule();
|
||||
|
||||
std::vector<std::string> parse_name(std::wstring);
|
||||
std::vector<std::string> parse_price(std::wstring);
|
||||
std::vector<std::string> parse_quantity(std::wstring);
|
||||
std::wstring trim_check(std::wstring&);
|
||||
|
||||
|
||||
std::wstring get_name();
|
||||
};
|
||||
|
||||
#endif // STORE_MODULE_H
|
||||
@@ -1,95 +0,0 @@
|
||||
#include "parser.h"
|
||||
#include "../goods/goods.h"
|
||||
#include "../net/net.h"
|
||||
#include "../settings/settings.h"
|
||||
#include "../utils/utils.h"
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
|
||||
Parser::Parser() {}
|
||||
|
||||
std::vector<std::string> Parser::search_modules() {
|
||||
Settings s(get_path_relative_to_home(".local/share/checks_parser/settings.json"));
|
||||
std::string path = get_path_relative_to_home(s.get_setting("stores_modules_dir"));//std::string(std::getenv("HOME")) + "/" + STORES_MODULES_DIR;
|
||||
std::filesystem::directory_entry modules_dir(path);
|
||||
|
||||
if (!modules_dir.exists()) {
|
||||
std::filesystem::create_directories(path);
|
||||
std::cout << "No modules directory found. Created one at " << path
|
||||
<< std::endl;
|
||||
std::cout << "Please, download modules to that directory from my git."
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
std::vector<std::string> modules_files;
|
||||
|
||||
for (auto file : std::filesystem::directory_iterator(path)) {
|
||||
modules_files.push_back(file.path());
|
||||
}
|
||||
|
||||
return modules_files;
|
||||
}
|
||||
|
||||
void Parser::set_module(std::string path) { module = StoreModule(path); }
|
||||
|
||||
std::vector<Goods> Parser::parse(std::wstring check_plaintext) {
|
||||
std::vector<Goods> result;
|
||||
|
||||
module.trim_check(check_plaintext);
|
||||
|
||||
std::vector<std::string> goods_names = module.parse_name(check_plaintext);
|
||||
std::vector<std::string> goods_prices = module.parse_price(check_plaintext);
|
||||
std::vector<std::string> goods_quantities =
|
||||
module.parse_quantity(check_plaintext);
|
||||
|
||||
if (goods_names.size() != goods_prices.size() ||
|
||||
goods_names.size() != goods_quantities.size() ||
|
||||
goods_prices.size() != goods_quantities.size()) {
|
||||
//Error. Amount of names, prices or quantities are not equal. That means, that some regex(es) has mismatched.
|
||||
return {};
|
||||
}
|
||||
short goods_amount = goods_names.size();
|
||||
|
||||
for (short i = 0; i < goods_amount; i++) {
|
||||
Goods goods(goods_names[i], std::stof(goods_prices[i]), std::stof(goods_quantities[i]));
|
||||
|
||||
result.push_back(goods);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<std::string> Parser::check_updates() {
|
||||
std::cout << "Checking updates for stores modules" << std::endl;
|
||||
Settings s(get_path_relative_to_home(".local/share/checks_parser/settings.json"));
|
||||
std::string path = get_path_relative_to_home(s.get_setting("stores_modules_dir"));
|
||||
|
||||
std::vector<std::string> to_download;
|
||||
std::vector<std::string> stored_modules;
|
||||
|
||||
std::filesystem::directory_entry modules_dir(path);
|
||||
if (!modules_dir.exists()) {
|
||||
std::filesystem::create_directories(path);
|
||||
}
|
||||
for (const auto& file : std::filesystem::directory_iterator(path)) {
|
||||
if (!file.is_regular_file()) continue;
|
||||
stored_modules.push_back(file.path().filename());
|
||||
std::cout << file.path().filename() << " detected store module" << std::endl;
|
||||
}
|
||||
Net n;
|
||||
std::cerr << "Downloading modules list from: " << s.get_setting("stores_modules_url");
|
||||
std::vector<std::string> remote_modules = n.get_all_modules(s.get_setting("stores_modules_url"));
|
||||
if (stored_modules.empty()) {
|
||||
std::cout << "I need to download everything" << std::endl;
|
||||
to_download = remote_modules;
|
||||
} else {
|
||||
for (const std::string& module : remote_modules) {
|
||||
if (!vector_contains_element(stored_modules, module)) {
|
||||
std::cout << "I need to download store module " << module << std::endl;
|
||||
to_download.push_back(module);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return to_download;
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
#ifndef PARSER_H
|
||||
#define PARSER_H
|
||||
|
||||
#include "../goods/goods.h"
|
||||
#include "module.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class Parser {
|
||||
|
||||
StoreModule module;
|
||||
|
||||
public:
|
||||
Parser();
|
||||
|
||||
std::vector<std::string> search_modules();
|
||||
|
||||
std::vector<std::string> check_updates();
|
||||
|
||||
void set_module(std::string);
|
||||
|
||||
std::vector<Goods> parse(std::wstring);
|
||||
};
|
||||
|
||||
#endif // PARSER_H
|
||||
Reference in New Issue
Block a user