Skip to content

Commit

Permalink
[Core] Full file:// syntax support
Browse files Browse the repository at this point in the history
Closes #14. Fully support `file` URLs by making use the OpenAssetIO
`FileUrlPathConverter` utility.

The utility will throw on non-`file` URLs being provided, maintaining
existing behaviour (with a slightly different error message). Confirmed
with manual testing.

The `FileUrlPathConverter` utility is only available as of OpenAssetIO
1.0.0-beta.2.0. So update README.

Signed-off-by: David Feltell <[email protected]>
  • Loading branch information
feltech committed Jun 13, 2024
1 parent 85be346 commit c29f67e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 47 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,15 @@ limitations.

## Dependencies

### [OpenAssetIO](https://github.com/OpenAssetIO/OpenAssetIO/) (1.0.0-alpha.14)
### [OpenAssetIO](https://github.com/OpenAssetIO/OpenAssetIO/) (1.0.0-beta.2.0)

Currently, OpenAssetIO must be built from source to build
`usdOpenAssetIOResolver`.
The OpenAssetIO libary is available pre-built for some common platforms
are available
[here](https://github.com/OpenAssetIO/OpenAssetIO/releases).

The steps to do this can be found
[here.](https://github.com/OpenAssetIO/OpenAssetIO/blob/main/doc/BUILDING.md)
Alternatively, OpenAssetIO can be built from source. The steps to do
this can be found
[here](https://github.com/OpenAssetIO/OpenAssetIO/blob/main/doc/BUILDING.md).

### [USD](https://github.com/PixarAnimationStudios/USD) (22.11)

Expand Down
61 changes: 20 additions & 41 deletions src/resolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "resolver.h"

#include <optional>
#include <stdexcept>
#include <thread>

Expand All @@ -20,7 +21,7 @@
#include <openassetio/log/LoggerInterface.hpp>
#include <openassetio/log/SeverityFilter.hpp>
#include <openassetio/python/hostApi.hpp>
#include <openassetio/trait/TraitsData.hpp>
#include <openassetio/utils/path.hpp>

#include <openassetio_mediacreation/traits/content/LocatableContentTrait.hpp>

Expand All @@ -35,21 +36,6 @@ TF_DEBUG_CODES(OPENASSETIO_RESOLVER)
PXR_NAMESPACE_CLOSE_SCOPE

namespace {
/*
* Replaces all occurrences of the search string in the subject with
* the supplied replacement.
*/
void replaceAllInString(std::string &subject, const std::string &search,
const std::string &replace) {
const size_t searchLength = search.length();
const size_t replaceLength = replace.length();
size_t pos = 0;
while ((pos = subject.find(search, pos)) != std::string::npos) {
subject.replace(pos, searchLength, replace);
pos += replaceLength;
}
}

/*
* OpenAssetIO LoggerInterface implementation
*
Expand Down Expand Up @@ -110,7 +96,7 @@ class UsdOpenAssetIOResolver::Impl {
Impl() {
logger_ =
openassetio::log::SeverityFilter::make(std::make_shared<UsdOpenAssetIOResolverLogger>());
auto managerImplementationFactory =
const auto managerImplementationFactory =
openassetio::python::hostApi::createPythonPluginSystemManagerImplementationFactory(
logger_);

Expand All @@ -133,7 +119,7 @@ class UsdOpenAssetIOResolver::Impl {
context_ = openassetio::Context::make();
}

[[nodiscard]] std::optional<std::string> _CreateIdentifier(
[[nodiscard]] std::optional<std::string> createIdentifier(
const std::string &assetPath, [[maybe_unused]] const ArResolvedPath &anchorAssetPath) const {
return catchAndLogExceptions(
[&]() -> std::optional<std::string> {
Expand All @@ -145,18 +131,18 @@ class UsdOpenAssetIOResolver::Impl {
TF_FUNC_NAME());
}

[[nodiscard]] std::optional<ArResolvedPath> _Resolve(const std::string &assetPath) const {
[[nodiscard]] std::optional<ArResolvedPath> resolve(const std::string &assetPath) const {
return catchAndLogExceptions(
[&]() -> std::optional<ArResolvedPath> {
if (auto entityReference = manager_->createEntityReferenceIfValid(assetPath)) {
if (const auto entityReference = manager_->createEntityReferenceIfValid(assetPath)) {
return ArResolvedPath{resolveToPath(*entityReference)};
}
return std::nullopt;
},
TF_FUNC_NAME());
}

[[nodiscard]] std::optional<std::string> _CreateIdentifierForNewAsset(
[[nodiscard]] std::optional<std::string> createIdentifierForNewAsset(
const std::string &assetPath, [[maybe_unused]] const ArResolvedPath &anchorAssetPath) const {
if (manager_->isEntityReferenceString(assetPath)) {
std::string message = "Writes to OpenAssetIO entity references are not currently supported ";
Expand All @@ -167,7 +153,7 @@ class UsdOpenAssetIOResolver::Impl {
return std::nullopt;
}

[[nodiscard]] std::optional<ArResolvedPath> _ResolveForNewAsset(
[[nodiscard]] std::optional<ArResolvedPath> resolveForNewAsset(
const std::string &assetPath) const {
if (manager_->isEntityReferenceString(assetPath)) {
std::string message = "Writes to OpenAssetIO entity references are not currently supported ";
Expand Down Expand Up @@ -239,27 +225,20 @@ class UsdOpenAssetIOResolver::Impl {
const openassetio::trait::TraitsDataPtr traitsData = manager_->resolve(
entityReference, {LocatableContentTrait::kId}, ResolveAccess::kRead, context_);

// OpenAssetIO is URL based, but we need a path, so check the
// scheme and decode into a path

static constexpr std::string_view kFileURLScheme{"file://"};
static constexpr std::size_t kProtocolSize = kFileURLScheme.size();

openassetio::Str url = LocatableContentTrait(traitsData).getLocation();
if (url.rfind(kFileURLScheme, 0) == openassetio::Str::npos) {
std::string msg = "Only file URLs are supported: ";
msg += url;
throw std::runtime_error(msg);
const std::optional<openassetio::Str> url = LocatableContentTrait(traitsData).getLocation();
if (!url) {
throw std::invalid_argument{"Entity reference does not have a location: " +
entityReference.toString()};
}

// TODO(tc): Decode % escape sequences properly
replaceAllInString(url, "%20", " ");
return url.substr(kProtocolSize);
// OpenAssetIO is URL based, but we need a path. Note: will throw if
// the URL is not valid.
return fileUrlPathConverter_.pathFromUrl(*url);
}

openassetio::log::LoggerInterfacePtr logger_;
openassetio::hostApi::ManagerPtr manager_;
openassetio::ContextConstPtr context_;
openassetio::utils::FileUrlPathConverter fileUrlPathConverter_;
};

UsdOpenAssetIOResolver::UsdOpenAssetIOResolver() : impl_{std::make_unique<Impl>()} {}
Expand All @@ -268,24 +247,24 @@ UsdOpenAssetIOResolver::~UsdOpenAssetIOResolver() = default;

std::string UsdOpenAssetIOResolver::_CreateIdentifier(
const std::string &assetPath, const ArResolvedPath &anchorAssetPath) const {
auto result = impl_->_CreateIdentifier(assetPath, anchorAssetPath);
auto result = impl_->createIdentifier(assetPath, anchorAssetPath);
return result ? *result : ArDefaultResolver::_CreateIdentifier(assetPath, anchorAssetPath);
}

ArResolvedPath UsdOpenAssetIOResolver::_Resolve(const std::string &assetPath) const {
auto result = impl_->_Resolve(assetPath);
auto result = impl_->resolve(assetPath);
return result ? *result : ArDefaultResolver::_Resolve(assetPath);
}

std::string UsdOpenAssetIOResolver::_CreateIdentifierForNewAsset(
const std::string &assetPath, const ArResolvedPath &anchorAssetPath) const {
auto result = impl_->_CreateIdentifierForNewAsset(assetPath, anchorAssetPath);
auto result = impl_->createIdentifierForNewAsset(assetPath, anchorAssetPath);
return result ? *result
: ArDefaultResolver::_CreateIdentifierForNewAsset(assetPath, anchorAssetPath);
}

ArResolvedPath UsdOpenAssetIOResolver::_ResolveForNewAsset(const std::string &assetPath) const {
auto result = impl_->_ResolveForNewAsset(assetPath);
auto result = impl_->resolveForNewAsset(assetPath);
return result ? *result : ArDefaultResolver::_ResolveForNewAsset(assetPath);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/resources/integration_test_data/bal_library.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"traits": {
"openassetio-mediacreation:content.LocatableContent": {
"location":
"file://${bal_library_dir}/recursive_assetized_resolve/floors/floor%201.usd"
"file://${bal_library_dir}/recursive_assetized_resolve/floors/floor%20%40%201.usd"
}
}
}
Expand Down

0 comments on commit c29f67e

Please sign in to comment.