BlockDescLoader.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright (c) 2016-2017 Josh Blum
  2. // SPDX-License-Identifier: BSL-1.0
  3. #include <Pothos/Plugin.hpp>
  4. #include <Poco/Path.h>
  5. #include <Poco/File.h>
  6. #include <fstream>
  7. #include <sstream>
  8. #include <cassert>
  9. #include <map>
  10. #include <json.hpp>
  11. using json = nlohmann::json;
  12. /***********************************************************************
  13. * Load a JSON block description from file and register the descriptions.
  14. * return a list of registration paths and a list of paths for blocks.
  15. **********************************************************************/
  16. static std::vector<Pothos::PluginPath> blockDescParser(std::istream &is, std::vector<Pothos::PluginPath> &blockPaths)
  17. {
  18. std::vector<Pothos::PluginPath> entries;
  19. //parse the stream into a JSON array
  20. auto arrayOut = json::parse(is);
  21. if (arrayOut.is_object()) arrayOut = {arrayOut};
  22. for (const auto &obj : arrayOut)
  23. {
  24. const std::string JsonObjStr(obj.dump());
  25. std::vector<std::string> paths;
  26. paths.push_back(obj["path"]);
  27. for (const auto &alias : obj.value("aliases", json::array()))
  28. {
  29. paths.push_back(alias);
  30. }
  31. //register the block description for every path
  32. for (const auto &path : paths)
  33. {
  34. const auto pluginPath = Pothos::PluginPath("/blocks/docs", path);
  35. Pothos::PluginRegistry::add(pluginPath, JsonObjStr);
  36. entries.push_back(pluginPath);
  37. blockPaths.push_back(Pothos::PluginPath("/blocks", path));
  38. }
  39. }
  40. return entries;
  41. }
  42. /***********************************************************************
  43. * Load a JSON block description described by a config file section
  44. **********************************************************************/
  45. static std::vector<Pothos::PluginPath> blockDescLoader(const std::map<std::string, std::string> &config)
  46. {
  47. //config file path set by caller
  48. const auto confFilePathIt = config.find("confFilePath");
  49. if (confFilePathIt == config.end() or confFilePathIt->second.empty())
  50. throw Pothos::Exception("missing confFilePath");
  51. //determine JSON description file path
  52. const auto jsonIt = config.find("json");
  53. if (jsonIt == config.end() or jsonIt->second.empty())
  54. throw Pothos::Exception("JSON file not specified");
  55. Poco::Path jsonPath(jsonIt->second);
  56. jsonPath.makeAbsolute(Poco::Path(confFilePathIt->second).makeParent());
  57. if (not Poco::File(jsonPath).exists())
  58. throw Pothos::Exception(jsonPath.toString() + " does not exist");
  59. //open an input file stream
  60. std::ifstream ifs(Poco::Path::expand(jsonPath.toString()));
  61. std::vector<Pothos::PluginPath> blockPaths;
  62. return blockDescParser(ifs, blockPaths);
  63. }
  64. /***********************************************************************
  65. * loader registration
  66. **********************************************************************/
  67. pothos_static_block(pothosFrameworkRegisterBlockDescLoader)
  68. {
  69. Pothos::PluginRegistry::addCall("/framework/conf_loader/block_desc", &blockDescLoader);
  70. }