ros_util.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2021, Rein Appeldoorn
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 #include <boost/algorithm/string/predicate.hpp>
19 #include <boost/algorithm/string/regex.hpp>
20 #include <boost/filesystem.hpp>
21 
22 #include "./ros_util.h"
23 #include "./util.h"
24 
25 namespace picovoice_driver
26 {
27 std::string defaultResourceUrl()
28 {
29  return "package://picovoice_driver/resources";
30 }
31 
32 std::string defaultRecordDirectory(const std::string& name)
33 {
34  return std::string("/tmp/picovoice_driver/" + name);
35 }
36 
37 void validatePathExistence(const std::string& path, const std::string& extension = "")
38 {
39  if (!boost::filesystem::exists(path))
40  {
41  throw std::runtime_error("Path '" + path + "' does not exist");
42  }
43  if (!extension.empty() && !boost::algorithm::ends_with(path, extension))
44  {
45  throw std::runtime_error("Path '" + path + "' does not match extension '" + extension + "'");
46  }
47 }
48 
49 std::string pathFromUrl(const std::string& url, const std::string& extension, const std::string& directory)
50 {
51  try
52  {
53  if (url.find("://") == std::string::npos && !directory.empty())
54  {
55  validatePathExistence(directory);
56 
57  if (boost::filesystem::exists(directory + "/" + url) && boost::algorithm::ends_with(url, extension))
58  {
59  return directory + "/" + url;
60  }
61  if (boost::filesystem::exists(directory + "/" + url + extension))
62  {
63  return directory + "/" + url + extension;
64  }
65 
66  std::vector<std::string> candidates;
67  for (const auto& entry : boost::make_iterator_range(boost::filesystem::directory_iterator(directory)))
68  {
69  if (entry.path().extension().string() == extension)
70  {
71  candidates.push_back(entry.path().stem().string());
72  }
73  }
74  throw std::runtime_error("Invalid relative file '" + url + "'. Available: " + toString(candidates));
75  }
76 
77  std::vector<std::string> parts;
78  boost::algorithm::split_regex(parts, url, boost::regex("://"));
79  if (parts.size() != 2)
80  {
81  throw std::runtime_error("Invalid package or file url");
82  }
83  const std::string& url_type = parts.front();
84  const std::string& url_arg = parts.back();
85 
86  if (url_type == "package")
87  {
88  size_t first_slash_pos = url_arg.find("/");
89  std::string package = url_arg.substr(0, first_slash_pos);
90  std::string path_relative_to_package = first_slash_pos < url_arg.size() ? url_arg.substr(first_slash_pos) : "";
91 
92  auto pkg_path = ros::package::getPath(package);
93  if (pkg_path.empty())
94  {
95  throw std::runtime_error("Could not find package + '" + package + "'");
96  }
97  auto path = pkg_path + path_relative_to_package;
98  validatePathExistence(path, extension);
99  return path;
100  }
101  else if (url_type == "file")
102  {
103  validatePathExistence(url_arg, extension);
104  return url_arg;
105  }
106  throw std::runtime_error("Invalid url type '" + url_type + "'");
107  }
108  catch (const std::exception& e)
109  {
110  std::string err = "Failed to get path from url '" + url + "': " + std::string(e.what()) +
111  " - The url should be of form {package://path_relative_to_package" + extension +
112  ", file:///absolute/path" + extension + "}";
113  if (!directory.empty() && !extension.empty())
114  {
115  err += " or a filename[" + extension + "] relative to directory '" + directory + "'";
116  }
117  throw std::runtime_error(err);
118  }
119 }
120 } // namespace picovoice_driver
ros::package::getPath
ROSLIB_DECL std::string getPath(const std::string &package_name)
picovoice_driver::pathFromUrl
std::string pathFromUrl(const std::string &url, const std::string &extension, const std::string &directory)
pathFromUrl Get a file path from an url
Definition: ros_util.cpp:49
python.setup.url
url
Definition: porcupine/binding/python/setup.py:76
picovoice_driver::validatePathExistence
void validatePathExistence(const std::string &path, const std::string &extension="")
Definition: ros_util.cpp:37
package
string package
python.setup.name
name
Definition: porcupine/binding/python/setup.py:69
picovoice_driver::defaultRecordDirectory
std::string defaultRecordDirectory(const std::string &name)
defaultRecordDirectory Get the default record dir
Definition: ros_util.cpp:32
picovoice_driver
Definition: porcupine_node.cpp:24
picovoice_driver::defaultResourceUrl
std::string defaultResourceUrl()
defaultResourcePath Get the default resource path
Definition: ros_util.cpp:27
picovoice_driver::toString
std::string toString(const T &v)
Definition: util.h:28
util.h
ros_util.h


picovoice_driver
Author(s):
autogenerated on Fri Apr 1 2022 02:14:50