-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add python binding for UsdValidatorMetadata
- Loading branch information
1 parent
102a3e2
commit fc17b6e
Showing
4 changed files
with
274 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
#!/pxrpythonsubst | ||
# | ||
# Copyright 2024 Pixar | ||
# | ||
# Licensed under the terms set forth in the LICENSE.txt file available at | ||
# https://openusd.org/license. | ||
|
||
import unittest | ||
|
||
from pxr import Plug, Sdf, Usd | ||
|
||
|
||
class TestUsdValidatorMetadata(unittest.TestCase): | ||
def _verify_metadata( | ||
self, | ||
metadata: Usd.ValidatorMetadata, | ||
name="", | ||
doc="", | ||
keywords=[], | ||
schemaTypes=[], | ||
plugin=None, | ||
isSuite=False | ||
): | ||
self.assertEqual(metadata.name, name) | ||
self.assertEqual(metadata.doc, doc) | ||
self.assertEqual(metadata.keywords, keywords) | ||
self.assertEqual(metadata.schemaTypes, schemaTypes) | ||
self.assertEqual(metadata.plugin, plugin) | ||
self.assertEqual(metadata.isSuite, isSuite) | ||
|
||
def test_create_default_metadata(self): | ||
metadata = Usd.ValidatorMetadata() | ||
self._verify_metadata(metadata) | ||
|
||
def test_create_metadata_with_valid_keyword_args(self): | ||
all_plugins = Plug.Registry().GetAllPlugins() | ||
expected_plugin = all_plugins[0] if all_plugins else None | ||
valid_metadatas = [ | ||
{ | ||
"name": "empty_validator" | ||
}, | ||
{ | ||
"name": "validator1", | ||
"doc": "This is a test validator.", | ||
"keywords": ["validator1", "test"], | ||
"schemaTypes": ["SomePrimType"], | ||
"plugin": None, | ||
"isSuite": False | ||
}, | ||
{ | ||
"name": "validator2", | ||
"doc": "This is another test validator.", | ||
"keywords": ["validator2", "test"], | ||
"schemaTypes": ["NewPrimType"], | ||
"plugin": expected_plugin, | ||
"isSuite": False | ||
} | ||
] | ||
|
||
for args in valid_metadatas: | ||
with self.subTest(name=args["name"]): | ||
metadata = Usd.ValidatorMetadata(**args) | ||
self._verify_metadata(metadata, **args) | ||
|
||
def test_create_metadata_with_invalid_keyword_args(self): | ||
invalid_metadatas = { | ||
"Wrong Name Type": { | ||
"name": 123 | ||
}, | ||
"Wrong Doc Type": { | ||
"doc": 123 | ||
}, | ||
"Wrong Keywords Type": { | ||
"keywords": 123 | ||
}, | ||
"Wrong Schema Types": { | ||
"schemaTypes": 123 | ||
}, | ||
"Wrong Plugin Type": { | ||
"plugin": 123 | ||
}, | ||
"Wrong IsSuite Type": { | ||
"isSuite": "wrong type" | ||
} | ||
} | ||
|
||
for error_category, args in invalid_metadatas.items(): | ||
with self.subTest(error_type=error_category): | ||
with self.assertRaises(Exception): | ||
Usd.ValidatorMetadata(**args) | ||
|
||
def test_metadata_name_getter_and_setter(self): | ||
metadata = Usd.ValidatorMetadata() | ||
for name in ["validator1", "validator2"]: | ||
metadata.name = name | ||
self.assertEqual(metadata.name, name) | ||
|
||
# Invalid type | ||
with self.assertRaises(Exception): | ||
metadata.name = 123 | ||
|
||
def test_metadata_doc_getter_and_setter(self): | ||
metadata = Usd.ValidatorMetadata() | ||
for doc in ["doc1", "doc2"]: | ||
metadata.doc = doc | ||
self.assertEqual(metadata.doc, doc) | ||
|
||
# Invalid type | ||
with self.assertRaises(Exception): | ||
metadata.doc = 123 | ||
|
||
def test_metadata_keywords_getter_and_setter(self): | ||
metadata = Usd.ValidatorMetadata() | ||
for keywords in [["keyword1"], ["keyword2"]]: | ||
metadata.keywords = keywords | ||
self.assertEqual(metadata.keywords, keywords) | ||
|
||
# Invalid type | ||
with self.assertRaises(Exception): | ||
metadata.keywords = 123 | ||
with self.assertRaises(Exception): | ||
metadata.keywords = "123" | ||
|
||
def test_metadata_schemaTypes_getter_and_setter(self): | ||
metadata = Usd.ValidatorMetadata() | ||
for schema_types in [["PrimType1"], ["PrimType2"]]: | ||
metadata.schemaTypes = schema_types | ||
self.assertEqual(metadata.schemaTypes, schema_types) | ||
|
||
# Invalid type | ||
with self.assertRaises(Exception): | ||
metadata.keywords = 123 | ||
with self.assertRaises(Exception): | ||
metadata.keywords = "123" | ||
|
||
def test_metadata_plugin_getter_and_setter(self): | ||
all_plugins = Plug.Registry().GetAllPlugins() | ||
expected_plugin = all_plugins[0] if all_plugins else None | ||
metadata = Usd.ValidatorMetadata() | ||
metadata.plugin = expected_plugin | ||
self.assertEqual(metadata.plugin, expected_plugin) | ||
|
||
# Invalid type | ||
with self.assertRaises(Exception): | ||
metadata.keywords = 123 | ||
|
||
def test_metadata_is_suite_getter_and_setter(self): | ||
metadata = Usd.ValidatorMetadata() | ||
for suite in [True, False]: | ||
metadata.isSuite = suite | ||
self.assertEqual(metadata.isSuite, suite) | ||
|
||
# Invalid type | ||
with self.assertRaises(Exception): | ||
metadata.keywords = "123" | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// | ||
// Copyright 2024 Pixar | ||
// | ||
// Licensed under the terms set forth in the LICENSE.txt file available at | ||
// https://openusd.org/license. | ||
// | ||
#include "pxr/pxr.h" | ||
#include "pxr/usd/usd/validator.h" | ||
|
||
#include "pxr/base/tf/pyContainerConversions.h" | ||
#include "pxr/base/tf/pyPtrHelpers.h" | ||
#include "pxr/base/tf/pyResultConversions.h" | ||
|
||
#include <boost/python/class.hpp> | ||
#include <boost/python/make_constructor.hpp> | ||
#include <boost/python/operators.hpp> | ||
#include <boost/python/object.hpp> | ||
|
||
using namespace boost::python; | ||
|
||
PXR_NAMESPACE_USING_DIRECTIVE | ||
|
||
namespace | ||
{ | ||
|
||
UsdValidatorMetadata * | ||
_NewMetadata( | ||
const TfToken &name, | ||
const PlugPluginPtr &plugin, | ||
const TfTokenVector &keywords, | ||
const TfToken &doc, | ||
const TfTokenVector &schemaTypes, | ||
bool isSuite) | ||
{ | ||
return new UsdValidatorMetadata{name, plugin, keywords, doc, schemaTypes, isSuite}; | ||
} | ||
|
||
TfToken | ||
_GetMetadataName(const UsdValidatorMetadata &metadata) | ||
{ | ||
return metadata.name; | ||
} | ||
|
||
void | ||
_SetMetadataName(UsdValidatorMetadata &metadata, const TfToken &name) | ||
{ | ||
metadata.name = name; | ||
} | ||
|
||
TfTokenVector | ||
_GetMetadataKeywords(const UsdValidatorMetadata &metadata) | ||
{ | ||
return metadata.keywords; | ||
} | ||
|
||
void | ||
_SetMetadataKeywords(UsdValidatorMetadata &metadata, const TfTokenVector &keywords) | ||
{ | ||
metadata.keywords = keywords; | ||
} | ||
|
||
TfTokenVector | ||
_GetMetadataSchemaTypes(const UsdValidatorMetadata &metadata) | ||
{ | ||
return metadata.schemaTypes; | ||
} | ||
|
||
void | ||
_SetMetadataSchemaTypes(UsdValidatorMetadata &metadata, const TfTokenVector &schemaTypes) | ||
{ | ||
metadata.schemaTypes = schemaTypes; | ||
} | ||
|
||
PlugPluginPtr | ||
_GetMetadataPlugin(const UsdValidatorMetadata &metadata) | ||
{ | ||
return metadata.pluginPtr; | ||
} | ||
|
||
void | ||
_SetMetadataPlugin(UsdValidatorMetadata &metadata, const PlugPluginPtr &plugin) | ||
{ | ||
metadata.pluginPtr = plugin; | ||
} | ||
|
||
} // anonymous namespace | ||
|
||
void wrapUsdValidator() | ||
{ | ||
class_<UsdValidatorMetadata>("ValidatorMetadata", no_init) | ||
.def("__init__", make_constructor(&_NewMetadata, default_call_policies(), | ||
(arg("name") = TfToken(), | ||
arg("plugin") = PlugPluginPtr(), | ||
arg("keywords") = TfTokenVector(), | ||
arg("doc") = TfToken(), | ||
arg("schemaTypes") = TfTokenVector(), | ||
arg("isSuite") = false))) | ||
.add_property("name", &_GetMetadataName, &_SetMetadataName) | ||
.add_property("plugin", &_GetMetadataPlugin, &_SetMetadataPlugin) | ||
.add_property("keywords", make_function( | ||
&_GetMetadataKeywords, return_value_policy<TfPySequenceToList>()), _SetMetadataKeywords) | ||
.def_readwrite("doc", &UsdValidatorMetadata::doc) | ||
.add_property("schemaTypes", make_function( | ||
&_GetMetadataSchemaTypes, return_value_policy<TfPySequenceToList>()), _SetMetadataSchemaTypes) | ||
.def_readwrite("isSuite", &UsdValidatorMetadata::isSuite); | ||
} |