Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 2.12.0

* Refactor: Rename LibraryCollectionKey to LibraryItemKey.
* Added LibraryContainerLocator.

# 2.11.0

* Added LibraryCollectionKey and LibraryCollectionLocator
Expand Down
2 changes: 1 addition & 1 deletion opaque_keys/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
10 changes: 5 additions & 5 deletions opaque_keys/edx/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,19 @@ def make_asset_key(self, asset_type: str, path: str) -> AssetKey: # pragma: no
raise NotImplementedError()


class LibraryCollectionKey(OpaqueKey):
class LibraryItemKey(OpaqueKey):
"""
An :class:`opaque_keys.OpaqueKey` identifying a particular Library Collection object.
An :class:`opaque_keys.OpaqueKey` identifying a particular item in a library.
"""
KEY_TYPE = 'collection_key'
KEY_TYPE = 'library_item_key'
library_key: LibraryLocatorV2
collection_id: str
__slots__ = ()

@property
@abstractmethod
def org(self) -> str | None: # pragma: no cover
"""
The organization that this collection belongs to.
The organization that this object belongs to.
"""
raise NotImplementedError()

Expand Down
69 changes: 65 additions & 4 deletions opaque_keys/edx/locator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, LibraryItemKey

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -1623,14 +1623,13 @@
return str(self)


class LibraryCollectionLocator(CheckFieldMixin, LibraryCollectionKey):
class LibraryCollectionLocator(CheckFieldMixin, LibraryItemKey):
"""
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
Expand All @@ -1655,7 +1654,7 @@
@property
def org(self) -> str | None: # pragma: no cover
"""
The organization that this collection belongs to.
The organization that this Collection belongs to.
"""
return self.library_key.org

Expand All @@ -1676,3 +1675,65 @@
return cls(library_key, collection_id)
except (ValueError, TypeError) as error:
raise InvalidKeyError(cls, serialized) from error


class LibraryContainerLocator(CheckFieldMixin, LibraryItemKey):
"""
When serialized, these keys look like:
lct:org:lib:ct-type:ct-id
"""
CANONICAL_NAMESPACE = 'lct' # "Library Container"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Do we need this to be short? If size doesn't matter, maybe change this to something readable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@navinkarkera Yes, other larger possibilities were ruled out; you can see that conversation here: openedx/openedx-learning#282 (comment)

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,
)

@property
def org(self) -> str | None: # pragma: no cover
"""
The organization that this Container belongs to.
"""
return self.library_key.org

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

Check warning on line 1739 in opaque_keys/edx/locator.py

View check run for this annotation

Codecov / codecov/patch

opaque_keys/edx/locator.py#L1738-L1739

Added lines #L1738 - L1739 were not covered by tests
75 changes: 75 additions & 0 deletions opaque_keys/edx/tests/test_container_locators.py
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,9 @@ def get_version(*file_paths):
'block_type': [
'block-type-v1 = opaque_keys.edx.block_types:BlockTypeKeyV1',
],
'collection_key': [
'library_item_key': [
'lib-collection = opaque_keys.edx.locator:LibraryCollectionLocator',
'lct = opaque_keys.edx.locator:LibraryContainerLocator',
],
}
)