AYON Cpp Dev Tools  0.1.0
Loading...
Searching...
No Matches
rootHelpers.hpp
Go to the documentation of this file.
1#ifndef TOOL_AYON_ROT_HELPERS_DEF
2#define TOOL_AYON_ROT_HELPERS_DEF
3
4#include "../../../../NameSpaceDef/namespaces.hpp"
5#include "../../lib/logging/AyonLogger.hpp"
6
7#include <algorithm>
8#include <regex>
9#include <string>
10#include <unordered_map>
11
20
21std::string
22rootReplace(const std::string &rootLessPath, const std::unordered_map<std::string, std::string> &siteRoots) {
23 auto & logger = AyonLogger::getInstance();
24 static const std::string kLogKeyName = "rootReplace";
25 static const bool kRegistered = logger.registerLoggingKey(kLogKeyName);
26 (void)kRegistered;
27
28 auto logKey = logger.key(kLogKeyName);
29
30 std::string rootedPath;
31
32 std::smatch matchea;
33 std::regex rootFindPattern("\\{root\\[.*?\\]\\}");
34 if (std::regex_search(rootLessPath, matchea, rootFindPattern)) {
35 std::string siteRootOverwriteName = matchea.str(0);
36
37 std::smatch matcheb;
38 std::regex rootBracketPattern("\\[(.*?)\\]");
39 if (std::regex_search(siteRootOverwriteName, matcheb, rootBracketPattern)) {
40 std::string key = matcheb.str(1);
41 try {
42 std::string replacement = siteRoots.at(key);
43 rootedPath = std::regex_replace(rootLessPath, rootFindPattern, replacement);
44 std::replace(rootedPath.begin(), rootedPath.end(), '\\', '/');
45 return rootedPath;
46 }
47 catch (std::out_of_range &e) {
48 logger.warn(logKey, "Could not find site root '{}' for path '{}'", key, rootLessPath);
49 return rootLessPath;
50 }
51 }
52 }
53
54 return rootLessPath;
55};
56
58#endif // !TOOL_AYON_ROT_HELPERS_DEF
static AyonLogger & getInstance()
Definition: AyonLogger.hpp:33
#define YNPUT_TOOL_AYON_NAMESPACE_CLOSE
Definition: namespaces.hpp:106
#define YNPUT_TOOL_AYON_NAMESPACE_OPEN
Definition: namespaces.hpp:105
YNPUT_TOOL_AYON_NAMESPACE_OPEN std::string rootReplace(const std::string &rootLessPath, const std::unordered_map< std::string, std::string > &siteRoots)
allows to replace the {root[xxx]} key from an Ayon resolver endpoint with the local root overwrites
Definition: rootHelpers.hpp:22