File tree 3 files changed +45
-12
lines changed
3 files changed +45
-12
lines changed Original file line number Diff line number Diff line change @@ -328,7 +328,7 @@ int main(int argc, char *argv[])
328
328
329
329
// positional args
330
330
| lyra::arg (opt_crateConfig, " crateConfig" )
331
- (" crate config yaml file" ).required ()
331
+ (" crate config YAML or JSON file" ).required ()
332
332
333
333
| lyra::arg (opt_secondsToRun, " secondsToRun" )
334
334
(" duration the DAQ should run in seconds" ).optional ()
@@ -365,15 +365,6 @@ int main(int argc, char *argv[])
365
365
if (opt_logTrace)
366
366
set_global_log_level (spdlog::level::trace);
367
367
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
-
377
368
try
378
369
{
379
370
auto timeToRun = std::chrono::seconds (opt_secondsToRun);
@@ -382,11 +373,11 @@ int main(int argc, char *argv[])
382
373
383
374
try
384
375
{
385
- crateConfig = crate_config_from_yaml (inConfig );
376
+ crateConfig = crate_config_from_file (opt_crateConfig );
386
377
}
387
378
catch (const std::runtime_error &e)
388
379
{
389
- cerr << " Error parsing CrateConfig: " << e.what () << endl ;
380
+ cerr << fmt::format ( " Error loading CrateConfig from '{}': {} \n " , opt_crateConfig, e.what ()) ;
390
381
return 1 ;
391
382
}
392
383
Original file line number Diff line number Diff line change 2
2
3
3
#include < cassert>
4
4
#include < fstream>
5
+ #include < nlohmann/json.hpp>
5
6
6
7
#include " util/fmt.h"
7
8
#include " util/string_util.h"
@@ -331,6 +332,39 @@ StackCommandBuilder stack_command_builder_from_yaml_file(
331
332
return stack_command_builder_from_yaml (input);
332
333
}
333
334
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
+
334
368
namespace detail
335
369
{
336
370
template <typename T>
Original file line number Diff line number Diff line change @@ -75,6 +75,14 @@ StackCommandBuilder MESYTEC_MVLC_EXPORT stack_command_builder_from_yaml_file(con
75
75
std::string MESYTEC_MVLC_EXPORT to_json (const CrateConfig &crateConfig);
76
76
CrateConfig MESYTEC_MVLC_EXPORT crate_config_from_json (const std::string &json);
77
77
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
78
86
std::string MESYTEC_MVLC_EXPORT to_json (const StackCommandBuilder &sb);
79
87
StackCommandBuilder MESYTEC_MVLC_EXPORT stack_command_builder_from_json (const std::string &json);
80
88
You can’t perform that action at this time.
0 commit comments