File indexing completed on 2024-09-27 07:03:09
0001 #include "DumpFlags_processor.h"
0002
0003 #include <JANA/JApplication.h>
0004 #include <JANA/JEvent.h>
0005 #include <JANA/JException.h>
0006 #include <JANA/Services/JParameterManager.h>
0007 #include <fmt/core.h>
0008 #include <spdlog/common.h>
0009 #include <spdlog/logger.h>
0010 #include <spdlog/spdlog.h>
0011 #include <string.h>
0012 #include <exception>
0013 #include <map>
0014 #include <ostream>
0015
0016
0017 using namespace fmt;
0018
0019
0020
0021
0022 DumpFlags_processor::DumpFlags_processor(JApplication *app) :
0023 JEventProcessor(app)
0024 {
0025 }
0026
0027
0028
0029
0030 void DumpFlags_processor::Init()
0031 {
0032
0033
0034 auto *app = GetApplication();
0035 app->SetDefaultParameter("dump_flags:python", m_python_file_name, "If not empty, a python file to generate");
0036 app->SetDefaultParameter("dump_flags:markdown", m_markdown_file_name, "If not empty, a markdown file to generate");
0037 app->SetDefaultParameter("dump_flags:json", m_json_file_name, "If not empty, a json file to generate");
0038 app->SetDefaultParameter("dump_flags:screen", m_print_to_screen, "If not empty, print summary to screen at end of job");
0039
0040
0041 InitLogger(app, "dump_flags", level::info);
0042 }
0043
0044
0045
0046
0047
0048 void DumpFlags_processor::Process(const std::shared_ptr<const JEvent>& event)
0049 {
0050
0051 }
0052
0053
0054
0055
0056
0057 void DumpFlags_processor::Finish()
0058 {
0059 auto *pm = GetApplication()->GetJParameterManager();
0060
0061
0062 size_t max_name_len = 0;
0063 size_t max_default_val_len = 0;
0064 for(auto [name,param]: pm->GetAllParameters()) {
0065 if(max_name_len < strlen(name.c_str())) {
0066 max_name_len = strlen(name.c_str());
0067 }
0068
0069 if(max_default_val_len < strlen(param->GetDefault().c_str())) {
0070 max_default_val_len = strlen(param->GetDefault().c_str());
0071 }
0072 }
0073
0074
0075 spdlog::info("max_name_len={} max_default_val_len={}", max_name_len, max_default_val_len);
0076
0077
0078 std::string python_content ="# format [ (flag_name, default_val, description), ...]\neicrecon_flags=[\n";
0079 std::string json_content = "[\n";
0080
0081
0082 size_t line_num = 0;
0083 for(auto [name,param]: pm->GetAllParameters())
0084 {
0085
0086 std::string python_escaped_descr = param->GetDescription();
0087 std::replace(python_escaped_descr.begin(), python_escaped_descr.end(), '\'', '`');
0088 python_content += fmt::format(" ({:{}} {:{}} '{}'),\n",
0089 fmt::format("'{}',", param->GetKey()),
0090 max_name_len + 3,
0091 fmt::format("'{}',", param->GetDefault()),
0092 max_default_val_len + 3,
0093 python_escaped_descr
0094 );
0095
0096
0097 std::string json_escaped_descr = param->GetDescription();
0098 std::replace(json_escaped_descr.begin(), json_escaped_descr.end(), '"', '\'');
0099 json_content += fmt::format(" {}[\"{}\", \"{}\", \"{}\", \"{}\"]\n",
0100 line_num++==0?' ': ',',
0101 param->GetKey(),
0102 param->GetValue(),
0103 param->GetDefault(),
0104 json_escaped_descr);
0105
0106
0107 if( m_print_to_screen ) fmt::print(" {:{}} : {}\n", param->GetKey(), max_name_len + 3, param->GetValue());
0108 }
0109
0110
0111 python_content+="]\n\n";
0112 json_content+="]\n\n";
0113
0114
0115 if(!m_python_file_name.empty()) {
0116 try{
0117 std::ofstream ofs(m_python_file_name);
0118 ofs << python_content;
0119 m_log->info("Created python file with flags: '{}'", m_python_file_name);
0120 }
0121 catch(std::exception ex) {
0122 m_log->error("Can't open file '{}' for write", m_python_file_name);
0123 throw JException(ex.what());
0124 }
0125 }
0126
0127
0128 if(!m_json_file_name.empty()) {
0129 try{
0130 std::ofstream ofs(m_json_file_name);
0131 ofs << json_content;
0132 m_log->info("Created json file with flags: '{}'", m_json_file_name);
0133 m_log->info("Json records format is: [name, value, default-value, comment]", m_json_file_name);
0134 }
0135 catch(std::exception ex) {
0136 m_log->error("Can't open file '{}' for write", m_json_file_name);
0137 throw JException(ex.what());
0138 }
0139 }
0140
0141
0142 if(!m_janaconfig_file_name.empty()) {
0143 try{
0144 pm->WriteConfigFile(m_janaconfig_file_name);
0145 }
0146 catch(std::exception ex) {
0147 m_log->error("Can't open file '{}' for write", m_janaconfig_file_name);
0148 throw JException(ex.what());
0149 }
0150 }
0151 }