config_file.cpp
Go to the documentation of this file.
1 
11 
14 
15 #include <boost/foreach.hpp>
16 #include <boost/property_tree/ptree.hpp>
17 #include <boost/property_tree/xml_parser.hpp>
18 #include <boost/filesystem.hpp>
19 
20 #include <algorithm>
21 #include <iostream>
22 
23 using boost::property_tree::ptree;
24 
25 namespace
26 {
27 
28 Common::ParametersGroup GetGroup(const std::string & name, const ptree & groupTree)
29 {
30  Common::ParametersGroup group(name);
31 
32  if (groupTree.empty())
33  {
34  return group;
35  }
36 
37  BOOST_FOREACH(const ptree::value_type & child, groupTree)
38  {
39  if (child.second.empty())
40  {
41  group.Parameters.push_back(Common::Parameter(child.first, child.second.data()));
42  continue;
43  }
44 
45  group.Groups.push_back(GetGroup(child.first, child.second));
46  }
47 
48  return group;
49 }
50 
51 void AddParameter(Common::AddonParameters & params, const std::string & name, const ptree & tree)
52 {
53  if (tree.empty())
54  {
55  params.Parameters.push_back(Common::Parameter(name, tree.data()));
56  return;
57  }
58 
59  params.Groups.push_back(GetGroup(name, tree));
60 }
61 
62 } // namespace
63 
64 
66 {
67  ptree pt;
68  read_xml(configPath, pt);
69  Configuration configuration;
70  BOOST_FOREACH(const ptree::value_type & config, pt)
71  {
72  if (config.first != "config")
73  {
74  std::cerr << "Unknown root tag " << config.first << " in the config file '" << configPath << "'" << std::endl;
75  continue;
76  }
77 
78  BOOST_FOREACH(const ptree::value_type & param, config.second)
79  {
80  if (param.first != "modules")
81  {
82  AddParameter(configuration.Parameters, param.first, param.second);
83  continue;
84  }
85 
86  BOOST_FOREACH(const ptree::value_type & module, param.second)
87  {
88  if (module.first != "module")
89  {
90  std::cerr << "Unknown tag " << module.first << " inside 'modules' in the config file '" << configPath << "'" << std::endl;
91  continue;
92  }
93 
94  Common::ModuleConfiguration moduleConfig;
95  moduleConfig.Id = module.second.get<std::string>("id");
96  moduleConfig.Path = module.second.get<std::string>("path");
97 
98  if (boost::optional<const ptree &> dependsOn = module.second.get_child_optional("depends_on"))
99  {
100  BOOST_FOREACH(const ptree::value_type & depend, dependsOn.get())
101  {
102  if (depend.first != "id")
103  {
104  continue;
105  }
106 
107  moduleConfig.Dependencies.push_back(depend.second.data());
108  }
109  }
110 
111  if (boost::optional<const ptree &> parameters = module.second.get_child_optional("parameters"))
112  {
113  BOOST_FOREACH(const ptree::value_type & parameter, parameters.get())
114  {
115  AddParameter(moduleConfig.Parameters, parameter.first, parameter.second);
116  }
117  }
118 
119  configuration.Modules.push_back(moduleConfig);
120  }
121  }
122  }
123  return configuration;
124 }
125 
127 {
128  using namespace boost::filesystem;
129  Common::Configuration configuration;
130  path p(directory);
131  directory_iterator dirIt(p);
132  directory_iterator end;
133 
134  std::for_each(dirIt, end, [&configuration](const directory_entry & entry)
135  {
136  if (entry.path().filename().extension() == ".conf")
137  {
138  std::cout << "Parsing config file: " << entry.path().native() << std::endl;
139  Common::Configuration tmp = Common::ParseConfiguration(entry.path().string());
140  configuration.Modules.insert(configuration.Modules.end(), tmp.Modules.begin(), tmp.Modules.end());
141  configuration.Parameters.Groups.insert(configuration.Parameters.Groups.end(), tmp.Parameters.Groups.begin(), tmp.Parameters.Groups.end());
142  configuration.Parameters.Parameters.insert(configuration.Parameters.Parameters.end(), tmp.Parameters.Parameters.begin(), tmp.Parameters.Parameters.end());
143  }
144  });
145  return configuration;
146 }
147 
148 
149 namespace
150 {
151 void AddDependencies(ptree & moduleTree, const std::vector<Common::AddonId> & ids)
152 {
153  if (ids.empty())
154  {
155  return;
156  }
157 
158  ptree & deps = moduleTree.add("depends_on", "");
159 
160  for (auto idIt = ids.begin(); idIt != ids.end(); ++idIt)
161  {
162  deps.add("id", *idIt);
163  }
164 }
165 
166 void AddGroup(ptree & moduleTree, const Common::ParametersGroup & group)
167 {
168  ptree & groupTree = moduleTree.add(group.Name, "");
169 
170  for (auto paramIt = group.Parameters.begin(); paramIt != group.Parameters.end(); ++paramIt)
171  {
172  groupTree.add(paramIt->Name, paramIt->Value);
173  }
174 
175  for (auto groupIt = group.Groups.begin(); groupIt != group.Groups.end(); ++groupIt)
176  {
177  AddGroup(groupTree, *groupIt);
178  }
179 }
180 
181 void AddParameters(ptree & moduleTree, const Common::AddonParameters & params, const char * groupName)
182 {
183  if (params.Parameters.empty() && params.Groups.empty())
184  {
185  return;
186  }
187 
188  ptree & paramsTree = moduleTree.add(groupName, "");
189 
190  for (auto paramIt = params.Parameters.begin(); paramIt != params.Parameters.end(); ++paramIt)
191  {
192  paramsTree.add(paramIt->Name, paramIt->Value);
193  }
194 
195  for (auto groupIt = params.Groups.begin(); groupIt != params.Groups.end(); ++groupIt)
196  {
197  AddGroup(paramsTree, *groupIt);
198  }
199 }
200 }
201 
203 {
204  ptree pt;
205  ptree & modulesPt = pt.put("config.modules", "");
206 
207  for (auto configIt = modules.begin(); configIt != modules.end(); ++configIt)
208  {
209  ptree & moduleTree = modulesPt.add("module", "");
210  const Common::ModuleConfiguration & config = *configIt;
211  moduleTree.add("id", config.Id);
212  moduleTree.add("path", config.Path);
213  AddDependencies(moduleTree, config.Dependencies);
214  AddParameters(moduleTree, config.Parameters, "parameters");
215  }
216 
217  write_xml(configPath, pt);
218 }
219 
221 {
223  info.Id = config.Id;
224  info.Dependencies = config.Dependencies;
225  info.Parameters = config.Parameters;
227  return info;
228 }
Configuration ParseConfigurationFiles(const std::string &directory)
Configuration ParseConfiguration(const std::string &configPath)
Definition: config_file.cpp:65
void SaveConfiguration(const ModulesConfiguration &configuration, const std::string &configPath)
bool param(const std::string &param_name, T &param_val, const T &default_val)
std::vector< AddonId > Dependencies
Definition: config_file.h:22
Common::AddonInformation GetAddonInfomation(const ModuleConfiguration &config)
std::vector< Parameter > Parameters
std::vector< ParametersGroup > Groups
name
Definition: setup.py:38
list modules
Definition: tests/setup.py:49
Common::AddonParameters Parameters
Definition: config_file.h:31
std::shared_ptr< AddonFactory > Factory
Definition: addon_manager.h:32
std::vector< AddonId > Dependencies
Definition: addon_manager.h:33
AddonParameters Parameters
Definition: addon_manager.h:34
AddonFactory::UniquePtr CreateDynamicAddonFactory(const char *modulePath)
std::vector< ParametersGroup > Groups
AddonParameters Parameters
Definition: config_file.h:24
std::vector< Common::ModuleConfiguration > ModulesConfiguration
Definition: config_file.h:27
std::vector< ModuleConfiguration > Modules
Definition: config_file.h:32
std::vector< Parameter > Parameters


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:06:04