Finder.cpp
Go to the documentation of this file.
1 #include "Finder.h"
2 
3 #include <SimoxUtility/algorithm/string.h>
4 #include <SimoxUtility/filesystem/list_directory.h>
5 
9 
11 #include <VisionX/libraries/armem_human/aron/Profile.aron.generated.h>
12 
14 {
15  namespace fs = std::filesystem;
16 
17  Finder::Finder(const std::string& packageName, const Finder::path& relativeDirectory) :
18  packageName(packageName), relativeDirectory(relativeDirectory)
19  {
20  Logging::setTag("HumanProfileFinder");
21  }
22 
23  void
24  Finder::setPath(const std::string& path)
25  {
26  packageName = path;
27  absPackageDataDir.clear();
28  }
29 
30  std::string
32  {
33  return packageName;
34  }
35 
36  void
37  Finder::init() const
38  {
39  if (absPackageDataDir.empty())
40  {
41  CMakePackageFinder packageFinder(packageName);
42  if (not packageFinder.packageFound())
43  {
44  ARMARX_INFO << "Could not find package '" << packageName << "'. "
45  << "Not loading human profiles.";
46  }
47  absPackageDataDir = packageFinder.getDataDir();
48  if (!_ready())
49  {
50  ARMARX_INFO << "No data directory " << relativeDirectory
51  << " in the package '" << packageName << "'. "
52  << "Not loading human profiles.";
53  }
54  else
55  {
56  ARMARX_VERBOSE << "Profile root directory: " << _rootDirAbs();
57 
58  // Make sure this data path is available.
59  armarx::ArmarXDataPath::addDataPaths(std::vector<std::string>{absPackageDataDir});
60  }
61  }
62  }
63 
64  bool
65  Finder::isDatasetDirValid(const path& path) const
66  {
67  return std::filesystem::is_directory(path);
68  }
69 
70  std::optional<armarx::armem::human::Info>
71  Finder::find(const std::string& name) const
72  {
73  init();
74  if (!_ready())
75  {
76  return std::nullopt;
77  }
78  path profileDir = absPackageDataDir / relativeDirectory / name;
79  if (not fs::is_directory(profileDir))
80  {
81  return std::nullopt;
82  }
83  return armarx::armem::human::Info(packageName, absPackageDataDir, relativeDirectory, name);
84  }
85 
86  std::vector<armarx::armem::human::Info>
87  Finder::findAll(bool checkPaths) const
88  {
89  init();
90  if (!_ready())
91  {
92  return {};
93  }
94 
95  std::vector<armarx::armem::human::Info> infos;
96  ARMARX_INFO << "Scanning " << _rootDirAbs() << " for human profiles ...";
97 
98  const bool local = false;
99  for (const path& profileDir : simox::fs::list_directory(_rootDirAbs(), local))
100  {
101  if (fs::is_directory(profileDir))
102  {
103  infos.emplace_back(
104  packageName, absPackageDataDir, relativeDirectory, profileDir.filename());
105  }
106  }
107 
108  ARMARX_INFO << "Found " << infos.size() << " human profiles.";
109 
110  return infos;
111  }
112 
114  Finder::_rootDirAbs() const
115  {
116  return absPackageDataDir / packageName / relativeDirectory;
117  }
118 
120  Finder::_rootDirRel() const
121  {
122  return packageName;
123  }
124 
125  bool
126  Finder::_ready() const
127  {
128  return !absPackageDataDir.empty() &&
129  std::filesystem::exists(absPackageDataDir / packageName / relativeDirectory);
130  }
131 
132 } // namespace armarx::armem::server::human::profile
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:180
Finder.h
armarx::armem::server::human::profile::Finder::getPackageName
std::string getPackageName() const
Definition: Finder.cpp:31
armarx::armem::server::human::profile::Finder::find
std::optional< Info > find(const std::string &name) const
Definition: Finder.cpp:71
Info.h
armarx::CMakePackageFinder
The CMakePackageFinder class provides an interface to the CMake Package finder capabilities.
Definition: CMakePackageFinder.h:53
armarx::armem::server::human::profile::Finder::path
std::filesystem::path path
Definition: Finder.h:22
armarx::armem::server::human::profile
Definition: Finder.cpp:13
armarx::armem::server::human::profile::Finder::Finder
Finder(const std::string &packageName=DefaultPackageName, const path &relativeDirectory=DefaultRelativeDirectory)
Definition: Finder.cpp:17
armarx::armem::server::human::profile::Finder::findAll
std::vector< Info > findAll(bool checkPaths=true) const
Definition: Finder.cpp:87
ExpressionException.h
CMakePackageFinder.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::ArmarXDataPath::addDataPaths
static void addDataPaths(const std::string &dataPathList)
Definition: ArmarXDataPath.cpp:559
armarx::armem::server::human::profile::Finder::setPath
void setPath(const std::string &path)
Definition: Finder.cpp:24
armarx::Logging::setTag
void setTag(const LogTag &tag)
Definition: Logging.cpp:55
ArmarXDataPath.h
armarx::armem::human::Info
Accessor for human profile files.
Definition: Info.h:20