Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utils: Adding collapsePath #1859

Merged
merged 9 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions library/public/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "exception.h"
#include "export.h"

#include <filesystem>
#include <map>
#include <regex>
#include <sstream>
Expand Down Expand Up @@ -55,6 +56,18 @@ class F3D_EXPORT utils
[[nodiscard]] static std::vector<std::string> tokenize(std::string_view str);
// clang-format on

/**
* Collapse a string filesystem path by:
* - Expanding tilda `~` into home dir in a cross-platform way
* - Transform relative path into an absolute path based on basedDirectory if provided, or the
* current directory if not
* - Remove any `..` if any
* Rely on vtksys::SystemTools::CollapseFullPath but return empty string if the provided
* string is empty.
*/
[[nodiscard]] static std::filesystem::path collapsePath(
const std::filesystem::path& path, const std::filesystem::path& baseDirectory = {});

/**
* An exception that can be thrown by tokenize
*/
Expand Down
22 changes: 22 additions & 0 deletions library/src/utils.cxx
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#include "utils.h"

#include "levenshtein.h"
#include "log.h"

#include <vtksys/SystemTools.hxx>

namespace fs = std::filesystem;

namespace f3d
{
Expand Down Expand Up @@ -97,6 +102,23 @@
return tokens;
}

//----------------------------------------------------------------------------
fs::path utils::collapsePath(const fs::path& path, const fs::path& baseDirectory)
{
try
{
return path.empty() ? path
: baseDirectory.empty()
? fs::path(vtksys::SystemTools::CollapseFullPath(path.string()))
: fs::path(vtksys::SystemTools::CollapseFullPath(path.string(), baseDirectory.string()));
}
catch (const fs::filesystem_error& ex)

Check warning on line 115 in library/src/utils.cxx

View check run for this annotation

Codecov / codecov/patch

library/src/utils.cxx#L115

Added line #L115 was not covered by tests
{
log::error("Could not collapse path: ", ex.what());
return {};
Meakk marked this conversation as resolved.
Show resolved Hide resolved
}

Check warning on line 119 in library/src/utils.cxx

View check run for this annotation

Codecov / codecov/patch

library/src/utils.cxx#L117-L119

Added lines #L117 - L119 were not covered by tests
}

//----------------------------------------------------------------------------
utils::tokenize_exception::tokenize_exception(const std::string& what)
: exception(what)
Expand Down
16 changes: 16 additions & 0 deletions library/testing/TestSDKUtils.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

#include <utils.h>

#include <filesystem>

namespace fs = std::filesystem;

int TestSDKUtils(int argc, char* argv[])
{
PseudoUnitTest test;
Expand Down Expand Up @@ -101,5 +105,17 @@ int TestSDKUtils(int argc, char* argv[])
.str(),
"hello {foo}!");

//

test("collapsePath: empty", f3d::utils::collapsePath("").empty());
test("collapsePath: relative to absolute",
fs::path(f3d::utils::collapsePath("folder/file.ext")).is_absolute());
mwestphal marked this conversation as resolved.
Show resolved Hide resolved
test("collapsePath: relative with folder",
fs::path(f3d::utils::collapsePath("folder/file.ext", "/")) == fs::path("/folder/file.ext"));
mwestphal marked this conversation as resolved.
Show resolved Hide resolved
test(
"collapsePath: remove dotdot", f3d::utils::collapsePath("/folder/../file.ext") == "/file.ext");
test("collapsePath: expand home",
fs::path(f3d::utils::collapsePath("~/folder/file.ext")).is_absolute());
mwestphal marked this conversation as resolved.
Show resolved Hide resolved

return test.result();
}
3 changes: 2 additions & 1 deletion python/F3DPythonBindings.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ PYBIND11_MODULE(pyf3d, module)
py::class_<f3d::utils> utils(module, "Utils");

utils //
.def_static("text_distance", &f3d::utils::textDistance);
.def_static("text_distance", &f3d::utils::textDistance)
.def_static("collapse_path", &f3d::utils::collapsePath);

// f3d::interactor
py::class_<f3d::interaction_bind_t> interaction_bind(module, "InteractionBind");
Expand Down
4 changes: 4 additions & 0 deletions python/testing/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@

def test_text_distance():
assert f3d.Utils.text_distance("modle", "model") == 2


def test_collapse_path():
assert f3d.Utils.collapse_path("/folder/../file.ext", ".") == "/file.ext"
Loading