From 4fda18ce8e432267491e8c49822847cf38903274 Mon Sep 17 00:00:00 2001 From: XnpioChV Date: Thu, 13 Mar 2025 14:02:56 -0500 Subject: [PATCH 1/6] refactor: Convert LibraryCollectionKey to LibraryElementKey --- opaque_keys/__init__.py | 8 ++++++++ opaque_keys/edx/keys.py | 12 ++++++------ opaque_keys/edx/locator.py | 14 +++----------- setup.py | 3 ++- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/opaque_keys/__init__.py b/opaque_keys/__init__.py index ce00e01..015f70b 100644 --- a/opaque_keys/__init__.py +++ b/opaque_keys/__init__.py @@ -188,6 +188,9 @@ def from_string(cls, serialized: str) -> Self: try: namespace, rest = cls._separate_namespace(serialized) key_class = cls.get_namespace_plugin(namespace) + print("OOOOOOOOOOOO") + print(key_class) + print(cls) if not issubclass(key_class, cls): # CourseKey.from_string() should never return a non-course LearningContextKey, # but they share the same namespace. @@ -234,6 +237,11 @@ def get_namespace_plugin(cls, namespace: str): # they must be loaded before processing any keys. drivers = cls._drivers() + print("IIIIIIIIIIIIIII") + print(namespace) + print(drivers) + print(drivers.__dict__) + try: return drivers[namespace].plugin except KeyError as error: diff --git a/opaque_keys/edx/keys.py b/opaque_keys/edx/keys.py index 7be4794..42733c8 100644 --- a/opaque_keys/edx/keys.py +++ b/opaque_keys/edx/keys.py @@ -93,21 +93,21 @@ def make_asset_key(self, asset_type: str, path: str) -> AssetKey: # pragma: no raise NotImplementedError() -class LibraryCollectionKey(OpaqueKey): +class LibraryElementKey(OpaqueKey): """ - An :class:`opaque_keys.OpaqueKey` identifying a particular Library Collection object. + An :class:`opaque_keys.OpaqueKey` identifying a particular element in a library + that is not an Xblock. """ - KEY_TYPE = 'collection_key' + KEY_TYPE = 'library_element_key' library_key: LibraryLocatorV2 - collection_id: str __slots__ = () @property def org(self) -> str | None: # pragma: no cover """ - The organization that this collection belongs to. + The organization that this object belongs to. """ - raise NotImplementedError() + return self.library_key.org class DefinitionKey(OpaqueKey): diff --git a/opaque_keys/edx/locator.py b/opaque_keys/edx/locator.py index 323106c..181bece 100644 --- a/opaque_keys/edx/locator.py +++ b/opaque_keys/edx/locator.py @@ -16,7 +16,7 @@ from opaque_keys import OpaqueKey, InvalidKeyError from opaque_keys.edx.keys import AssetKey, CourseKey, DefinitionKey, \ - LearningContextKey, UsageKey, UsageKeyV2, LibraryCollectionKey + LearningContextKey, UsageKey, UsageKeyV2, LibraryElementKey log = logging.getLogger(__name__) @@ -1623,14 +1623,13 @@ def html_id(self) -> str: return str(self) -class LibraryCollectionLocator(CheckFieldMixin, LibraryCollectionKey): +class LibraryCollectionLocator(CheckFieldMixin, LibraryElementKey): """ When serialized, these keys look like: lib-collection:org:lib:collection-id """ CANONICAL_NAMESPACE = 'lib-collection' KEY_FIELDS = ('library_key', 'collection_id') - library_key: LibraryLocatorV2 collection_id: str __slots__ = KEY_FIELDS @@ -1646,19 +1645,12 @@ def __init__(self, library_key: LibraryLocatorV2, collection_id: str): if not isinstance(library_key, LibraryLocatorV2): raise TypeError("library_key must be a LibraryLocatorV2") - self._check_key_string_field("collection_id", collection_id, regexp=self.COLLECTION_ID_REGEXP) + self._check_key_string_field("object_id", collection_id, regexp=self.COLLECTION_ID_REGEXP) super().__init__( library_key=library_key, collection_id=collection_id, ) - @property - def org(self) -> str | None: # pragma: no cover - """ - The organization that this collection belongs to. - """ - return self.library_key.org - def _to_string(self) -> str: """ Serialize this key as a string diff --git a/setup.py b/setup.py index 401337e..ed28055 100644 --- a/setup.py +++ b/setup.py @@ -183,8 +183,9 @@ def get_version(*file_paths): 'block_type': [ 'block-type-v1 = opaque_keys.edx.block_types:BlockTypeKeyV1', ], - 'collection_key': [ + 'library_element_key': [ 'lib-collection = opaque_keys.edx.locator:LibraryCollectionLocator', + 'lct = opaque_keys.edx.locator:LibraryContainerLocator', ], } ) From 2f0dae34ad04faae20906e65c454bc7e624b4019 Mon Sep 17 00:00:00 2001 From: XnpioChV Date: Thu, 13 Mar 2025 15:41:14 -0500 Subject: [PATCH 2/6] feat: Add LibraryContainerLocator --- opaque_keys/__init__.py | 8 -- opaque_keys/edx/locator.py | 57 +++++++++++++- .../edx/tests/test_container_locators.py | 75 +++++++++++++++++++ 3 files changed, 131 insertions(+), 9 deletions(-) create mode 100644 opaque_keys/edx/tests/test_container_locators.py diff --git a/opaque_keys/__init__.py b/opaque_keys/__init__.py index 015f70b..ce00e01 100644 --- a/opaque_keys/__init__.py +++ b/opaque_keys/__init__.py @@ -188,9 +188,6 @@ def from_string(cls, serialized: str) -> Self: try: namespace, rest = cls._separate_namespace(serialized) key_class = cls.get_namespace_plugin(namespace) - print("OOOOOOOOOOOO") - print(key_class) - print(cls) if not issubclass(key_class, cls): # CourseKey.from_string() should never return a non-course LearningContextKey, # but they share the same namespace. @@ -237,11 +234,6 @@ def get_namespace_plugin(cls, namespace: str): # they must be loaded before processing any keys. drivers = cls._drivers() - print("IIIIIIIIIIIIIII") - print(namespace) - print(drivers) - print(drivers.__dict__) - try: return drivers[namespace].plugin except KeyError as error: diff --git a/opaque_keys/edx/locator.py b/opaque_keys/edx/locator.py index 181bece..0cc43f8 100644 --- a/opaque_keys/edx/locator.py +++ b/opaque_keys/edx/locator.py @@ -1645,7 +1645,7 @@ def __init__(self, library_key: LibraryLocatorV2, collection_id: str): if not isinstance(library_key, LibraryLocatorV2): raise TypeError("library_key must be a LibraryLocatorV2") - self._check_key_string_field("object_id", collection_id, regexp=self.COLLECTION_ID_REGEXP) + self._check_key_string_field("collection_id", collection_id, regexp=self.COLLECTION_ID_REGEXP) super().__init__( library_key=library_key, collection_id=collection_id, @@ -1668,3 +1668,58 @@ def _from_string(cls, serialized: str) -> Self: return cls(library_key, collection_id) except (ValueError, TypeError) as error: raise InvalidKeyError(cls, serialized) from error + + +class LibraryContainerLocator(CheckFieldMixin, LibraryElementKey): + """ + When serialized, these keys look like: + lct:org:lib:ct-type:ct-id + """ + CANONICAL_NAMESPACE = 'lct' # "Library Container" + KEY_FIELDS = ('library_key', 'container_type', 'container_id') + container_type: str + container_id: str + + __slots__ = KEY_FIELDS + CHECKED_INIT = False + + # Allow container IDs to contian unicode characters + CONTAINER_ID_REGEXP = re.compile(r'^[\w\-.]+$', flags=re.UNICODE) + + def __init__(self, library_key: LibraryLocatorV2, container_type: str, container_id: str): + """ + Construct a CollectionLocator + """ + if not isinstance(library_key, LibraryLocatorV2): + raise TypeError("library_key must be a LibraryLocatorV2") + + self._check_key_string_field("container_type", container_type) + self._check_key_string_field("container_id", container_id, regexp=self.CONTAINER_ID_REGEXP) + super().__init__( + library_key=library_key, + container_type=container_type, + container_id=container_id, + ) + + def _to_string(self) -> str: + """ + Serialize this key as a string + """ + return ":".join(( + self.library_key.org, + self.library_key.slug, + self.container_type, + self.container_id + )) + + @classmethod + def _from_string(cls, serialized: str) -> Self: + """ + Instantiate this key from a serialized string + """ + try: + (org, lib_slug, container_type, container_id) = serialized.split(':') + library_key = LibraryLocatorV2(org, lib_slug) + return cls(library_key, container_type, container_id) + except (ValueError, TypeError) as error: + raise InvalidKeyError(cls, serialized) from error diff --git a/opaque_keys/edx/tests/test_container_locators.py b/opaque_keys/edx/tests/test_container_locators.py new file mode 100644 index 0000000..15ed20c --- /dev/null +++ b/opaque_keys/edx/tests/test_container_locators.py @@ -0,0 +1,75 @@ +""" +Tests of LibraryContainerLocator +""" +import ddt +from opaque_keys import InvalidKeyError +from opaque_keys.edx.tests import LocatorBaseTest +from opaque_keys.edx.locator import LibraryContainerLocator, LibraryLocatorV2 + + +@ddt.ddt +class TestLibraryContainerLocator(LocatorBaseTest): + """ + Tests of :class:`.LibraryContainerLocator` + """ + @ddt.data( + "org/lib/id/foo", + "org/lib/id", + "org+lib+id", + "org+lib+", + "org+lib++id@library", + "org+ne@t", + "per%ent+sign", + ) + def test_coll_key_from_invalid_string(self, coll_id_str): + with self.assertRaises(InvalidKeyError): + LibraryContainerLocator.from_string(coll_id_str) + + def test_key_constructor(self): + org = 'TestX' + lib = 'LibraryX' + container_type = 'unit' + container_id = 'test-container' + library_key = LibraryLocatorV2(org=org, slug=lib) + container_key = LibraryContainerLocator( + library_key=library_key, + container_type=container_type, + container_id=container_id, + ) + library_key = container_key.library_key + self.assertEqual(str(container_key), "lct:TestX:LibraryX:unit:test-container") + self.assertEqual(container_key.org, org) + self.assertEqual(container_key.container_type, container_type) + self.assertEqual(container_key.container_id, container_id) + self.assertEqual(library_key.org, org) + self.assertEqual(library_key.slug, lib) + + def test_key_constructor_bad_ids(self): + library_key = LibraryLocatorV2(org="TestX", slug="lib1") + + with self.assertRaises(TypeError): + LibraryContainerLocator(library_key=None, container_type='unit', container_id='usage') + + with self.assertRaises(ValueError): + LibraryContainerLocator(library_key=library_key, container_type='unit', container_id='usage-!@#{$%^&*}') + + def test_key_constructor_bad_type(self): + library_key = LibraryLocatorV2(org="TestX", slug="lib1") + + with self.assertRaises(ValueError): + LibraryContainerLocator(library_key=library_key, container_type='unit-!@#{$%^&*}', container_id='usage') + + def test_key_from_string(self): + org = 'TestX' + lib = 'LibraryX' + container_type = 'unit' + container_id = 'test-container' + str_key = f"lct:{org}:{lib}:{container_type}:{container_id}" + container_key = LibraryContainerLocator.from_string(str_key) + library_key = container_key.library_key + self.assertEqual(str(container_key), str_key) + self.assertEqual(container_key.org, org) + self.assertEqual(container_key.container_type, container_type) + self.assertEqual(container_key.container_id, container_id) + self.assertEqual(library_key.org, org) + self.assertEqual(library_key.slug, lib) From e1d5a183d2e7dfd4064a966e8bb0043fe205eff0 Mon Sep 17 00:00:00 2001 From: XnpioChV Date: Thu, 13 Mar 2025 16:00:29 -0500 Subject: [PATCH 3/6] refactor: Convert LibraryElementKey as abstract class --- opaque_keys/edx/keys.py | 3 ++- opaque_keys/edx/locator.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/opaque_keys/edx/keys.py b/opaque_keys/edx/keys.py index 42733c8..dd151da 100644 --- a/opaque_keys/edx/keys.py +++ b/opaque_keys/edx/keys.py @@ -103,11 +103,12 @@ class LibraryElementKey(OpaqueKey): __slots__ = () @property + @abstractmethod def org(self) -> str | None: # pragma: no cover """ The organization that this object belongs to. """ - return self.library_key.org + raise NotImplementedError() class DefinitionKey(OpaqueKey): diff --git a/opaque_keys/edx/locator.py b/opaque_keys/edx/locator.py index 0cc43f8..86bd2ba 100644 --- a/opaque_keys/edx/locator.py +++ b/opaque_keys/edx/locator.py @@ -1651,6 +1651,13 @@ def __init__(self, library_key: LibraryLocatorV2, collection_id: str): collection_id=collection_id, ) + @property + def org(self) -> str | None: # pragma: no cover + """ + The organization that this object belongs to. + """ + return self.library_key.org + def _to_string(self) -> str: """ Serialize this key as a string @@ -1701,6 +1708,13 @@ def __init__(self, library_key: LibraryLocatorV2, container_type: str, container container_id=container_id, ) + @property + def org(self) -> str | None: # pragma: no cover + """ + The organization that this object belongs to. + """ + return self.library_key.org + def _to_string(self) -> str: """ Serialize this key as a string From 2419bd7779aca114bf450236a276e1043776144d Mon Sep 17 00:00:00 2001 From: XnpioChV Date: Thu, 13 Mar 2025 16:19:13 -0500 Subject: [PATCH 4/6] chore: Bump version of the package --- CHANGELOG.rst | 5 +++++ opaque_keys/__init__.py | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index f95eeba..070b4f8 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,3 +1,8 @@ +# 2.12.0 + +* Refactor: Rename LibraryCollectionKey to LibraryElementKey. +* Added LibraryContainerLocator. + # 2.11.0 * Added LibraryCollectionKey and LibraryCollectionLocator diff --git a/opaque_keys/__init__.py b/opaque_keys/__init__.py index ce00e01..ec8eb33 100644 --- a/opaque_keys/__init__.py +++ b/opaque_keys/__init__.py @@ -14,7 +14,7 @@ from stevedore.enabled import EnabledExtensionManager from typing_extensions import Self # For python 3.11 plus, can just use "from typing import Self" -__version__ = '2.11.0' +__version__ = '2.12.0' class InvalidKeyError(Exception): From fc0cff1bc932993f47f6fc0b58bb3cd882a1ca31 Mon Sep 17 00:00:00 2001 From: XnpioChV Date: Fri, 14 Mar 2025 17:32:10 -0500 Subject: [PATCH 5/6] refactor: Rename LibraryElementKey to LibraryItemKey --- CHANGELOG.rst | 2 +- opaque_keys/edx/keys.py | 5 ++--- opaque_keys/edx/locator.py | 10 +++++----- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 070b4f8..0956f84 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,6 +1,6 @@ # 2.12.0 -* Refactor: Rename LibraryCollectionKey to LibraryElementKey. +* Refactor: Rename LibraryCollectionKey to LibraryItemKey. * Added LibraryContainerLocator. # 2.11.0 diff --git a/opaque_keys/edx/keys.py b/opaque_keys/edx/keys.py index dd151da..7032cc5 100644 --- a/opaque_keys/edx/keys.py +++ b/opaque_keys/edx/keys.py @@ -93,10 +93,9 @@ def make_asset_key(self, asset_type: str, path: str) -> AssetKey: # pragma: no raise NotImplementedError() -class LibraryElementKey(OpaqueKey): +class LibraryItemKey(OpaqueKey): """ - An :class:`opaque_keys.OpaqueKey` identifying a particular element in a library - that is not an Xblock. + An :class:`opaque_keys.OpaqueKey` identifying a particular item in a library. """ KEY_TYPE = 'library_element_key' library_key: LibraryLocatorV2 diff --git a/opaque_keys/edx/locator.py b/opaque_keys/edx/locator.py index 86bd2ba..8c80a07 100644 --- a/opaque_keys/edx/locator.py +++ b/opaque_keys/edx/locator.py @@ -16,7 +16,7 @@ from opaque_keys import OpaqueKey, InvalidKeyError from opaque_keys.edx.keys import AssetKey, CourseKey, DefinitionKey, \ - LearningContextKey, UsageKey, UsageKeyV2, LibraryElementKey + LearningContextKey, UsageKey, UsageKeyV2, LibraryItemKey log = logging.getLogger(__name__) @@ -1623,7 +1623,7 @@ def html_id(self) -> str: return str(self) -class LibraryCollectionLocator(CheckFieldMixin, LibraryElementKey): +class LibraryCollectionLocator(CheckFieldMixin, LibraryItemKey): """ When serialized, these keys look like: lib-collection:org:lib:collection-id @@ -1654,7 +1654,7 @@ def __init__(self, library_key: LibraryLocatorV2, collection_id: str): @property def org(self) -> str | None: # pragma: no cover """ - The organization that this object belongs to. + The organization that this Collection belongs to. """ return self.library_key.org @@ -1677,7 +1677,7 @@ def _from_string(cls, serialized: str) -> Self: raise InvalidKeyError(cls, serialized) from error -class LibraryContainerLocator(CheckFieldMixin, LibraryElementKey): +class LibraryContainerLocator(CheckFieldMixin, LibraryItemKey): """ When serialized, these keys look like: lct:org:lib:ct-type:ct-id @@ -1711,7 +1711,7 @@ def __init__(self, library_key: LibraryLocatorV2, container_type: str, container @property def org(self) -> str | None: # pragma: no cover """ - The organization that this object belongs to. + The organization that this Container belongs to. """ return self.library_key.org From ec22e77e2232bd600ddac2069e7355823a6279a7 Mon Sep 17 00:00:00 2001 From: XnpioChV Date: Mon, 17 Mar 2025 10:59:04 -0500 Subject: [PATCH 6/6] refactor: Rename library_element_key to library_item_key --- opaque_keys/edx/keys.py | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/opaque_keys/edx/keys.py b/opaque_keys/edx/keys.py index 7032cc5..98a1a81 100644 --- a/opaque_keys/edx/keys.py +++ b/opaque_keys/edx/keys.py @@ -97,7 +97,7 @@ class LibraryItemKey(OpaqueKey): """ An :class:`opaque_keys.OpaqueKey` identifying a particular item in a library. """ - KEY_TYPE = 'library_element_key' + KEY_TYPE = 'library_item_key' library_key: LibraryLocatorV2 __slots__ = () diff --git a/setup.py b/setup.py index ed28055..ee96343 100644 --- a/setup.py +++ b/setup.py @@ -183,7 +183,7 @@ def get_version(*file_paths): 'block_type': [ 'block-type-v1 = opaque_keys.edx.block_types:BlockTypeKeyV1', ], - 'library_element_key': [ + 'library_item_key': [ 'lib-collection = opaque_keys.edx.locator:LibraryCollectionLocator', 'lct = opaque_keys.edx.locator:LibraryContainerLocator', ],