added store autodetect in email scene
This commit is contained in:
		@@ -16,7 +16,7 @@ std::vector<std::string> StoreModule::parse(std::wstring str, std::wstring regex
 | 
			
		||||
    std::cout << "Handling: " << to_utf8(str) << std::endl;
 | 
			
		||||
    for (boost::wsregex_iterator it{str.begin(), str.end(), r}, end{}; it != end;
 | 
			
		||||
         it++) {
 | 
			
		||||
        std::wcout << "Parsed: " << it->str() << std::endl;
 | 
			
		||||
        std::cout << "Parsed: " << to_utf8(it->str()) << std::endl;
 | 
			
		||||
        result.push_back(to_utf8(it->str()));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -28,6 +28,7 @@ StoreModule::StoreModule(std::string path) {
 | 
			
		||||
    nlohmann::json settings = nlohmann::json::parse(settings_file);
 | 
			
		||||
 | 
			
		||||
    this->name = from_utf8(settings["name"]);
 | 
			
		||||
    this->autodetect_regex = from_utf8(settings["autodetect_regex"]);
 | 
			
		||||
    this->goods_name_regex = from_utf8(settings["goods_name_regex"]);
 | 
			
		||||
    this->goods_price_regex = from_utf8(settings["goods_price_regex"]);
 | 
			
		||||
    this->goods_net_weight_regex = from_utf8(settings["goods_net_weight_regex"]);
 | 
			
		||||
@@ -89,3 +90,8 @@ std::wstring StoreModule::trim_check(std::wstring& check) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::wstring StoreModule::get_name() { return this->name; }
 | 
			
		||||
 | 
			
		||||
bool StoreModule::search_autodetect_regex(std::wstring str) {
 | 
			
		||||
    std::vector<std::string> parsed = parse(str, this->autodetect_regex, boost::regex_constants::collate);
 | 
			
		||||
    return parsed.size() > 0;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -8,6 +8,7 @@
 | 
			
		||||
class StoreModule {
 | 
			
		||||
    std::string path;
 | 
			
		||||
    std::wstring name;
 | 
			
		||||
    std::wstring autodetect_regex;
 | 
			
		||||
    std::wstring goods_name_regex;
 | 
			
		||||
    std::wstring goods_price_regex;
 | 
			
		||||
    std::wstring goods_net_weight_regex;
 | 
			
		||||
@@ -15,9 +16,9 @@ class StoreModule {
 | 
			
		||||
    std::wstring check_start_regex;
 | 
			
		||||
    std::wstring check_end_regex;
 | 
			
		||||
 | 
			
		||||
    std::vector<std::string> parse(std::wstring, std::wstring, boost::regex_constants::flag_type_);
 | 
			
		||||
    std::vector<std::string> parse(std::wstring str, std::wstring regexp, boost::regex_constants::flag_type_ flag);
 | 
			
		||||
public:
 | 
			
		||||
    StoreModule(std::string);
 | 
			
		||||
    StoreModule(std::string path);
 | 
			
		||||
    StoreModule();
 | 
			
		||||
 | 
			
		||||
    std::vector<std::string> parse_name(std::wstring);
 | 
			
		||||
@@ -27,8 +28,9 @@ public:
 | 
			
		||||
 | 
			
		||||
    std::wstring trim_check(std::wstring&);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    std::wstring get_name();
 | 
			
		||||
 | 
			
		||||
    bool search_autodetect_regex(std::wstring str);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif // STORE_MODULE_H
 | 
			
		||||
 
 | 
			
		||||
@@ -12,6 +12,7 @@
 | 
			
		||||
    using namespace std::experimental;
 | 
			
		||||
    using namespace std::experimental::filesystem;
 | 
			
		||||
#else
 | 
			
		||||
#include <QObject>
 | 
			
		||||
#       include <filesystem>
 | 
			
		||||
    using namespace std::filesystem;
 | 
			
		||||
#endif
 | 
			
		||||
@@ -25,9 +26,7 @@ std::vector<std::string> Parser::search_modules() {
 | 
			
		||||
 | 
			
		||||
    if (!exists(modules_dir)) {
 | 
			
		||||
        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::cout << QObject::tr("No modules directory found. Created one at ").toStdString() << path
 | 
			
		||||
                  << std::endl;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -86,33 +85,33 @@ std::vector<Goods> Parser::parse(std::wstring check_plaintext) {
 | 
			
		||||
    return result;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::string Parser::try_autodetect_store(std::string str) {
 | 
			
		||||
    std::vector<std::string> stored_modules_paths = search_modules();
 | 
			
		||||
 | 
			
		||||
    for (auto &module_path : stored_modules_paths) {
 | 
			
		||||
        StoreModule module(module_path);
 | 
			
		||||
        if (module.search_autodetect_regex(from_utf8(str))) return to_utf8(module.get_name());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return "";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::vector<std::string> Parser::check_updates() {
 | 
			
		||||
    std::cout << "Checking updates for stores modules" << std::endl;
 | 
			
		||||
    std::cout << QObject::tr("Checking updates for stores modules").toStdString() << 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;
 | 
			
		||||
 | 
			
		||||
    directory_entry modules_dir(path);
 | 
			
		||||
    if (!exists(modules_dir)) {
 | 
			
		||||
        create_directories(path);
 | 
			
		||||
    }
 | 
			
		||||
    for (const auto& file : directory_iterator(path)) {
 | 
			
		||||
        if (!is_regular_file(file)) continue;
 | 
			
		||||
        stored_modules.push_back(file.path().filename());
 | 
			
		||||
        std::cout << file.path().filename() << " detected local store module" << std::endl;
 | 
			
		||||
    }
 | 
			
		||||
    std::vector<std::string> stored_modules = search_modules();
 | 
			
		||||
    Net n;
 | 
			
		||||
    std::cerr << "Downloading modules list from: " << s.get_setting("stores_modules_url") << std::endl;
 | 
			
		||||
    std::cerr << QObject::tr("Downloading modules list from: ").toStdString() << s.get_setting("stores_modules_url") << std::endl;
 | 
			
		||||
    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;
 | 
			
		||||
                std::cout << QObject::tr("Queueing download of ").toStdString() << module << std::endl;
 | 
			
		||||
                to_download.push_back(module);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
@@ -22,6 +22,9 @@ public:
 | 
			
		||||
    void set_module(std::string);
 | 
			
		||||
 | 
			
		||||
    std::vector<Goods> parse(std::wstring);
 | 
			
		||||
 | 
			
		||||
    std::string try_autodetect_store(std::string);
 | 
			
		||||
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif // PARSER_H
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user