Skip to content

Commit 1698f30

Browse files
committed
add crate_config_from_file() and use it in the mini-daq to load both JSON or YAML
1 parent 6d7d455 commit 1698f30

File tree

3 files changed

+45
-12
lines changed

3 files changed

+45
-12
lines changed

extras/mini-daq/src/mini_daq_main.cc

+3-12
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ int main(int argc, char *argv[])
328328

329329
// positional args
330330
| lyra::arg(opt_crateConfig, "crateConfig")
331-
("crate config yaml file").required()
331+
("crate config YAML or JSON file").required()
332332

333333
| lyra::arg(opt_secondsToRun, "secondsToRun")
334334
("duration the DAQ should run in seconds").optional()
@@ -365,15 +365,6 @@ int main(int argc, char *argv[])
365365
if (opt_logTrace)
366366
set_global_log_level(spdlog::level::trace);
367367

368-
369-
std::ifstream inConfig(opt_crateConfig);
370-
371-
if (!inConfig.is_open())
372-
{
373-
cerr << "Error opening crate config " << argv[1] << " for reading." << endl;
374-
return 1;
375-
}
376-
377368
try
378369
{
379370
auto timeToRun = std::chrono::seconds(opt_secondsToRun);
@@ -382,11 +373,11 @@ int main(int argc, char *argv[])
382373

383374
try
384375
{
385-
crateConfig = crate_config_from_yaml(inConfig);
376+
crateConfig = crate_config_from_file(opt_crateConfig);
386377
}
387378
catch (const std::runtime_error &e)
388379
{
389-
cerr << "Error parsing CrateConfig: " << e.what() << endl;
380+
cerr << fmt::format("Error loading CrateConfig from '{}': {}\n", opt_crateConfig, e.what());
390381
return 1;
391382
}
392383

src/mesytec-mvlc/mvlc_readout_config.cc

+34
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <cassert>
44
#include <fstream>
5+
#include <nlohmann/json.hpp>
56

67
#include "util/fmt.h"
78
#include "util/string_util.h"
@@ -331,6 +332,39 @@ StackCommandBuilder stack_command_builder_from_yaml_file(
331332
return stack_command_builder_from_yaml(input);
332333
}
333334

335+
CrateConfig crate_config_from_data(const std::string &data, const std::string &format_)
336+
{
337+
auto format = util::str_tolower(format_);
338+
339+
if (format == "yaml")
340+
return crate_config_from_yaml(data);
341+
342+
if (format == "json")
343+
return crate_config_from_json(data);
344+
345+
throw std::runtime_error(fmt::format("unexpected format '{}', want 'json' or 'yaml'.", format));
346+
}
347+
348+
CrateConfig crate_config_from_file(const std::string &filename)
349+
{
350+
std::filesystem::path fp(filename);
351+
352+
if (fp.extension() == ".json")
353+
{
354+
std::ifstream input(filename);
355+
input.exceptions(std::ios::failbit | std::ios::badbit);
356+
auto json = nlohmann::json::parse(input);
357+
return crate_config_from_json(json.dump());
358+
}
359+
else if (fp.extension() == ".yaml")
360+
{
361+
return crate_config_from_yaml_file(filename);
362+
}
363+
364+
throw std::runtime_error(fmt::format("unexpected file extension '{}', want '.json' or '.yaml'.",
365+
fp.extension().string()));
366+
}
367+
334368
namespace detail
335369
{
336370
template<typename T>

src/mesytec-mvlc/mvlc_readout_config.h

+8
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ StackCommandBuilder MESYTEC_MVLC_EXPORT stack_command_builder_from_yaml_file(con
7575
std::string MESYTEC_MVLC_EXPORT to_json(const CrateConfig &crateConfig);
7676
CrateConfig MESYTEC_MVLC_EXPORT crate_config_from_json(const std::string &json);
7777

78+
// Wrapper around the from_yaml/from_json functions.
79+
// - data contains the raw YAML or JSON data.
80+
// - format is either "yaml" or "json".
81+
// Throws on error
82+
CrateConfig MESYTEC_MVLC_EXPORT crate_config_from_data(const std::string &data, const std::string &format);
83+
CrateConfig MESYTEC_MVLC_EXPORT crate_config_from_file(const std::string &filename);
84+
85+
// StackCommandBuilder serialization
7886
std::string MESYTEC_MVLC_EXPORT to_json(const StackCommandBuilder &sb);
7987
StackCommandBuilder MESYTEC_MVLC_EXPORT stack_command_builder_from_json(const std::string &json);
8088

0 commit comments

Comments
 (0)