Feature/enhance profile validator (#4278)

* check more profile issues

* 1

* update ci/cd

* update name
This commit is contained in:
SoftFever 2024-03-01 21:27:12 +08:00 committed by GitHub
parent 01b0c87471
commit 2b6937acbe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 223 additions and 49 deletions

View file

@ -831,17 +831,21 @@ bool PresetBundle::import_json_presets(PresetsConfigSubstitutions & s
// Report configuration fields, which are misplaced into a wrong group.
const Preset &default_preset = collection->default_preset_for(new_config);
std::string incorrect_keys = Preset::remove_invalid_keys(preset.config, default_preset.config);
if (!incorrect_keys.empty())
BOOST_LOG_TRIVIAL(error) << "Error in a preset file: The preset \"" << preset.file << "\" contains the following incorrect keys: " << incorrect_keys
<< ", which were removed";
if (!incorrect_keys.empty()) {
++m_errors;
BOOST_LOG_TRIVIAL(error) << "Error in a preset file: The preset \"" << preset.file
<< "\" contains the following incorrect keys: " << incorrect_keys << ", which were removed";
}
if (!config_substitutions.empty())
substitutions.push_back({name, collection->type(), PresetConfigSubstitutions::Source::UserFile, file, std::move(config_substitutions)});
preset.save(inherit_preset ? &inherit_preset->config : nullptr);
result.push_back(file);
} catch (const std::ifstream::failure &err) {
++m_errors;
BOOST_LOG_TRIVIAL(error) << boost::format("The config cannot be loaded: %1%. Reason: %2%") % file % err.what();
} catch (const std::runtime_error &err) {
++m_errors;
BOOST_LOG_TRIVIAL(error) << boost::format("Failed importing config file: %1%. Reason: %2%") % file % err.what();
}
return true;
@ -945,6 +949,7 @@ void PresetBundle::update_system_preset_setting_ids(std::map<std::string, std::m
Preset* preset = preset_collection->find_preset(name, false, true);
if (preset) {
if (!preset->setting_id.empty() && (preset->setting_id.compare(setting_id) != 0)) {
++m_errors;
BOOST_LOG_TRIVIAL(error) << boost::format("name %1%, local setting_id %2% is different with remote id %3%")
%preset->name %preset->setting_id %setting_id;
}
@ -3156,7 +3161,7 @@ std::pair<PresetsConfigSubstitutions, size_t> PresetBundle::load_vendor_configs_
std::vector<std::pair<std::string, std::string>> process_subfiles;
std::vector<std::pair<std::string, std::string>> filament_subfiles;
std::vector<std::pair<std::string, std::string>> machine_subfiles;
auto get_name_and_subpath = [](json::iterator& it, std::vector<std::pair<std::string, std::string>>& subfile_map) {
auto get_name_and_subpath = [this](json::iterator& it, std::vector<std::pair<std::string, std::string>>& subfile_map) {
if (it.value().is_array()) {
for (auto iter1 = it.value().begin(); iter1 != it.value().end(); iter1++) {
if (iter1.value().is_object()) {
@ -3170,18 +3175,21 @@ std::pair<PresetsConfigSubstitutions, size_t> PresetBundle::load_vendor_configs_
}
}
else {
++m_errors;
BOOST_LOG_TRIVIAL(error) << __FUNCTION__<< ": invalid value type for " << iter2.key();
}
}
if (!name.empty() && !subpath.empty())
subfile_map.push_back(std::make_pair(name, subpath));
} else {
++m_errors;
BOOST_LOG_TRIVIAL(error) << __FUNCTION__ << ": invalid type for " << iter1.key();
}
else
BOOST_LOG_TRIVIAL(error) << __FUNCTION__<< ": invalid type for " << iter1.key();
}
} else {
++m_errors;
BOOST_LOG_TRIVIAL(error) << __FUNCTION__ << ": invalid type for " << it.key();
}
else
BOOST_LOG_TRIVIAL(error) << __FUNCTION__<< ": invalid type for " << it.key();
};
try {
boost::nowide::ifstream ifs(root_file);
@ -3279,6 +3287,7 @@ std::pair<PresetsConfigSubstitutions, size_t> PresetBundle::load_vendor_configs_
model.variants.emplace_back(VendorProfile::PrinterVariant(variant_name));
}
} else {
++m_errors;
BOOST_LOG_TRIVIAL(error)<< __FUNCTION__ << boost::format(": invalid nozzle_diameters %1% for Vendor %1%") % nozzle_diameters % vendor_name;
}
}
@ -3313,6 +3322,7 @@ std::pair<PresetsConfigSubstitutions, size_t> PresetBundle::load_vendor_configs_
// An empty material was inserted into the list of default materials. Remove it.
model.default_materials.erase(model.default_materials.begin());
} else {
++m_errors;
BOOST_LOG_TRIVIAL(error) << __FUNCTION__ << boost::format(": invalid default_materials %1% for Vendor %1%") % default_materials_field % vendor_name;
}
}
@ -3340,9 +3350,8 @@ std::pair<PresetsConfigSubstitutions, size_t> PresetBundle::load_vendor_configs_
// 3) paste the process/filament/print configs
PresetCollection *presets = nullptr;
size_t presets_loaded = 0;
bool _validation_mode = validation_mode;
auto parse_subfile = [path, vendor_name, presets_loaded, current_vendor_profile, _validation_mode](
auto parse_subfile = [this, path, vendor_name, presets_loaded, current_vendor_profile](
ConfigSubstitutionContext& substitution_context,
PresetsConfigSubstitutions& substitutions,
LoadConfigBundleAttributes& flags,
@ -3368,6 +3377,7 @@ std::pair<PresetsConfigSubstitutions, size_t> PresetBundle::load_vendor_configs_
DynamicPrintConfig config_src;
config_src.load_from_json(subfile, substitution_context, false, key_values, reason);
if (!reason.empty()) {
++m_errors;
BOOST_LOG_TRIVIAL(error) << __FUNCTION__<< ": load config file "<<subfile<<" Failed!";
return reason;
}
@ -3394,6 +3404,7 @@ std::pair<PresetsConfigSubstitutions, size_t> PresetBundle::load_vendor_configs_
}
}
else {
++m_errors;
BOOST_LOG_TRIVIAL(error) << __FUNCTION__<< ": can not find inherits "<<inherits<<" for " << preset_name;
//throw ConfigurationError(format("can not find inherits %1% for %2%", inherits, preset_name));
reason = "Can not find inherits: " + inherits;
@ -3423,6 +3434,7 @@ std::pair<PresetsConfigSubstitutions, size_t> PresetBundle::load_vendor_configs_
Preset::normalize(config);
}
catch(nlohmann::detail::parse_error &err) {
++m_errors;
BOOST_LOG_TRIVIAL(error) << __FUNCTION__<< ": parse "<< subfile <<" got a nlohmann::detail::parse_error, reason = " << err.what();
reason = std::string("json parse error") + err.what();
return reason;
@ -3430,15 +3442,18 @@ std::pair<PresetsConfigSubstitutions, size_t> PresetBundle::load_vendor_configs_
// Report configuration fields, which are misplaced into a wrong group.
std::string incorrect_keys = Preset::remove_invalid_keys(config, *default_config);
if (! incorrect_keys.empty())
BOOST_LOG_TRIVIAL(error)<< __FUNCTION__ << ": The config " <<
subfile << " contains incorrect keys: " << incorrect_keys << ", which were removed";
if (!incorrect_keys.empty()) {
++m_errors;
BOOST_LOG_TRIVIAL(error) << __FUNCTION__ << ": The config " << subfile << " contains incorrect keys: " << incorrect_keys
<< ", which were removed";
}
if (presets_collection->type() == Preset::TYPE_PRINTER) {
// Filter out printer presets, which are not mentioned in the vendor profile.
// These presets are considered not installed.
auto printer_model = config.opt_string("printer_model");
if (printer_model.empty()) {
++m_errors;
BOOST_LOG_TRIVIAL(error) << "Error in a Vendor Config Bundle \"" << path << "\": The printer preset \"" <<
preset_name << "\" defines no printer model, it will be ignored.";
reason = std::string("can not find printer_model");
@ -3446,6 +3461,7 @@ std::pair<PresetsConfigSubstitutions, size_t> PresetBundle::load_vendor_configs_
}
auto printer_variant = config.opt_string("printer_variant");
if (printer_variant.empty()) {
++m_errors;
BOOST_LOG_TRIVIAL(error) << "Error in a Vendor Config Bundle \"" << path << "\": The printer preset \"" <<
preset_name << "\" defines no printer variant, it will be ignored.";
reason = std::string("can not find printer_variant");
@ -3455,6 +3471,7 @@ std::pair<PresetsConfigSubstitutions, size_t> PresetBundle::load_vendor_configs_
[&](const VendorProfile::PrinterModel &m) { return m.id == printer_model; }
);
if (it_model == current_vendor_profile->models.end()) {
++m_errors;
BOOST_LOG_TRIVIAL(error) << "Error in a Vendor Config Bundle \"" << path << "\": The printer preset \"" <<
preset_name << "\" defines invalid printer model \"" << printer_model << "\", it will be ignored.";
reason = std::string("can not find printer model in vendor profile");
@ -3462,6 +3479,7 @@ std::pair<PresetsConfigSubstitutions, size_t> PresetBundle::load_vendor_configs_
}
auto it_variant = it_model->variant(printer_variant);
if (it_variant == nullptr) {
++m_errors;
BOOST_LOG_TRIVIAL(error) << "Error in a Vendor Config Bundle \"" << path << "\": The printer preset \"" <<
preset_name << "\" defines invalid printer variant \"" << printer_variant << "\", it will be ignored.";
reason = std::string("can not find printer_variant in vendor profile");
@ -3470,6 +3488,7 @@ std::pair<PresetsConfigSubstitutions, size_t> PresetBundle::load_vendor_configs_
}
const Preset *preset_existing = presets_collection->find_preset(preset_name, false);
if (preset_existing != nullptr) {
++m_errors;
BOOST_LOG_TRIVIAL(error) << "Error in a Vendor Config Bundle \"" << path << "\": The printer preset \"" <<
preset_name << "\" has already been loaded from another Config Bundle.";
reason = std::string("duplicated defines");
@ -3477,7 +3496,7 @@ std::pair<PresetsConfigSubstitutions, size_t> PresetBundle::load_vendor_configs_
}
auto file_path = (boost::filesystem::path(data_dir()) /PRESET_SYSTEM_DIR/ vendor_name / subfile_iter.second).make_preferred();
if(_validation_mode)
if(validation_mode)
file_path = (boost::filesystem::path(data_dir()) / vendor_name / subfile_iter.second).make_preferred();
// Load the preset into the list of presets, save it to disk.
@ -3491,6 +3510,7 @@ std::pair<PresetsConfigSubstitutions, size_t> PresetBundle::load_vendor_configs_
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << " " << __LINE__ << loaded.name << " load filament_id: " << filament_id;
if (presets_collection->type() == Preset::TYPE_FILAMENT) {
if (filament_id.empty() && "Template" != vendor_name) {
++m_errors;
BOOST_LOG_TRIVIAL(error) << __FUNCTION__<< ": can not find filament_id for " << preset_name;
//throw ConfigurationError(format("can not find inherits %1% for %2%", inherits, preset_name));
reason = "Can not find filament_id for " + preset_name;
@ -3539,6 +3559,7 @@ std::pair<PresetsConfigSubstitutions, size_t> PresetBundle::load_vendor_configs_
{
std::string reason = parse_subfile(substitution_context, substitutions, flags, subfile, configs, filament_id_maps, presets, presets_loaded);
if (!reason.empty()) {
++m_errors;
//parse error
std::string subfile_path = path + "/" + vendor_name + "/" + subfile.second;
BOOST_LOG_TRIVIAL(error) << __FUNCTION__ << boost::format(", got error when parse process setting from %1%") % subfile_path;
@ -3554,6 +3575,7 @@ std::pair<PresetsConfigSubstitutions, size_t> PresetBundle::load_vendor_configs_
{
std::string reason = parse_subfile(substitution_context, substitutions, flags, subfile, configs, filament_id_maps, presets, presets_loaded);
if (!reason.empty()) {
++m_errors;
//parse error
std::string subfile_path = path + "/" + vendor_name + "/" + subfile.second;
BOOST_LOG_TRIVIAL(error) << __FUNCTION__ << boost::format(", got error when parse filament setting from %1%") % subfile_path;
@ -3569,6 +3591,7 @@ std::pair<PresetsConfigSubstitutions, size_t> PresetBundle::load_vendor_configs_
{
std::string reason = parse_subfile(substitution_context, substitutions, flags, subfile, configs, filament_id_maps, presets, presets_loaded);
if (!reason.empty()) {
++m_errors;
//parse error
std::string subfile_path = path + "/" + vendor_name + "/" + subfile.second;
BOOST_LOG_TRIVIAL(error) << __FUNCTION__ << boost::format(", got error when parse printer setting from %1%") % subfile_path;