opaque_keys.edx package

Submodules

opaque_keys.edx.keys module

; OpaqueKey abstract classes for edx-platform object types (courses, definitions, usages, and assets).

class opaque_keys.edx.keys.AsideDefinitionKey(*args, **kwargs)[source]

Bases: opaque_keys.edx.keys.DefinitionKey

A definition key for an aside.

abstract property aside_type

Return the type of this aside.

abstract property definition_key

Return the DefinitionKey that this aside is decorating.

class opaque_keys.edx.keys.AsideUsageKey(*args, **kwargs)[source]

Bases: opaque_keys.edx.keys.UsageKey

A usage key for an aside.

abstract property aside_type

Return the type of this aside.

abstract property usage_key

Return the UsageKey that this aside is decorating.

class opaque_keys.edx.keys.AssetKey(*args, **kwargs)[source]

Bases: opaque_keys.edx.keys.CourseObjectMixin, opaque_keys.OpaqueKey

An opaque_keys.OpaqueKey identifying a course asset.

KEY_TYPE = 'asset_key'
abstract property asset_type

Return what type of asset this is.

abstract property path

Return the path for this asset.

class opaque_keys.edx.keys.BlockTypeKey(*args, **kwargs)[source]

Bases: opaque_keys.OpaqueKey

A key class that encodes XBlock-family block types, including which family the block was loaded from.

KEY_TYPE = 'block_type'
abstract property block_family

Return the block-family identifier (the entry-point used to load that block family).

abstract property block_type

Return the block_type of this block (the key in the entry-point to load the block with).

class opaque_keys.edx.keys.CourseKey(*args, **kwargs)[source]

Bases: opaque_keys.edx.keys.LearningContextKey

An opaque_keys.OpaqueKey identifying a particular Course object.

abstract property course

The name for this course.

In old-style IDs, it’s the “course” in org/course/run

is_course = True
abstract make_asset_key(asset_type, path)[source]

Return an asset key, given the given the specified path.

This function should not actually create any new ids, but should simply return one that already exists.

abstract make_usage_key(block_type, block_id)[source]

Return a usage key, given the given the specified block_type and block_id.

This function should not actually create any new ids, but should simply return one that already exists.

abstract property org

The organization that this course belongs to.

abstract property run

The run for this course.

In old-style IDs, it’s the “run” in org/course/run

class opaque_keys.edx.keys.CourseObjectMixin[source]

Bases: object

An abstract opaque_keys.OpaqueKey mixin for keys that belong to courses.

abstract property course_key

Return the CourseKey for the course containing this usage.

abstract map_into_course(course_key)[source]

Return a new UsageKey or AssetKey representing this usage inside the course identified by the supplied CourseKey. It returns the same type as self

Args:

course_key (CourseKey): The course to map this object into.

Returns:

A new CourseObjectMixin instance.

class opaque_keys.edx.keys.DefinitionKey(*args, **kwargs)[source]

Bases: opaque_keys.OpaqueKey

An opaque_keys.OpaqueKey identifying an XBlock definition.

KEY_TYPE = 'definition_key'
abstract property block_type

The XBlock type of this definition.

class opaque_keys.edx.keys.LearningContextKey(*args, **kwargs)[source]

Bases: opaque_keys.OpaqueKey

An opaque_keys.OpaqueKey identifying a course, a library, a program, a website, or some other collection of content where learning happens.

This concept is more generic than “course.”

A learning context does not necessarily have an org, course, or, run.

KEY_TYPE = 'context_key'
is_course = False
make_definition_usage(definition_key, usage_id=None)[source]

Return a usage key, given the given the specified definition key and usage_id.

class opaque_keys.edx.keys.UsageKey(*args, **kwargs)[source]

Bases: opaque_keys.edx.keys.CourseObjectMixin, opaque_keys.OpaqueKey

An opaque_keys.OpaqueKey identifying an XBlock usage.

KEY_TYPE = 'usage_key'
abstract property block_id

The name of this usage.

abstract property block_type

The XBlock type of this usage.

property context_key

Get the learning context key (LearningContextKey) for this XBlock usage.

abstract property definition_key

Return the DefinitionKey for the XBlock containing this usage.

class opaque_keys.edx.keys.UsageKeyV2(*args, **kwargs)[source]

Bases: opaque_keys.edx.keys.UsageKey

An opaque_keys.OpaqueKey identifying an XBlock used in a specific learning context (e.g. a course).

Definition + Learning Context = Usage

UsageKeyV2 is just a subclass of UsageKey with slightly different behavior, but not a distinct key type (same KEY_TYPE). UsageKeyV2 should be used for new usage key types; the main differences between it and UsageKey are:

  • the .course_key property is considered deprecated for the new V2 key types, and they should implement .context_key instead.

  • the .definition_key property is explicitly disabled for V2 usage keys

abstract property context_key

Get the learning context key (LearningContextKey) for this XBlock usage. May be a course key, library key, or some other LearningContextKey type.

property course_key

Return the CourseKey for the course containing this usage.

property definition_key

Returns the definition key for this usage. For the newer V2 key types, this cannot be done with the key alone, so it’s necessary to ask the key’s learning context to provide the underlying definition key.

map_into_course(course_key)[source]

Implement map_into_course for API compatibility. Shouldn’t be used in new code.

class opaque_keys.edx.keys.i4xEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]

Bases: json.encoder.JSONEncoder

If provided as the cls to json.dumps, will serialize and Locations as i4x strings and other keys using the unicode strings.

default(key)[source]

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

For example, to support arbitrary iterators, you could implement default like this:

def default(self, o):
    try:
        iterable = iter(o)
    except TypeError:
        pass
    else:
        return list(iterable)
    # Let the base class default method raise the TypeError
    return JSONEncoder.default(self, o)

opaque_keys.edx.locations module

Deprecated OpaqueKey implementations used by XML and Mongo modulestores

class opaque_keys.edx.locations.AssetLocation(org, course, run, category, name, revision=None, **kwargs)[source]

Bases: opaque_keys.edx.locations.LocationBase, opaque_keys.edx.locator.AssetLocator

Deprecated. Use locator.AssetLocator

DEPRECATED_TAG = 'c4x'
block_id
block_type
course_key
replace(**kwargs)[source]
Return: a new AssetLocation with specific kwargs replacing

their corresponding values.

Using AssetLocator’s replace function results in a mismatch of __init__ args and kwargs.

Replace tries to instantiate an AssetLocation object with AssetLocators args and kwargs.

class opaque_keys.edx.locations.DeprecatedLocation(course_key, block_type, block_id)[source]

Bases: opaque_keys.edx.locator.BlockUsageLocator

The short-lived location:org+course+run+block_type+block_id syntax

CANONICAL_NAMESPACE = 'location'
URL_RE = re.compile('^\n        (?P<org>[\\w\\-~.:]+)\\+(?P<course>[\\w\\-~.:]+)\\+(?P<run>[\\w\\-~.:]+)\\+\n        (?P<block_type>[\\w\\-~.:]+)\\+\n        (?P<block_id>[\\w\\-~.:]+)\n        \\Z', re.VERBOSE)
URL_RE_SOURCE = '\n        (?P<org>[\\w\\-~.:]+)\\+(?P<course>[\\w\\-~.:]+)\\+(?P<run>[\\w\\-~.:]+)\\+\n        (?P<block_type>[\\w\\-~.:]+)\\+\n        (?P<block_id>[\\w\\-~.:]+)\n        '
class opaque_keys.edx.locations.Location(org, course, run, category, name, revision=None, **kwargs)[source]

Bases: opaque_keys.edx.locations.LocationBase, opaque_keys.edx.locator.BlockUsageLocator

Deprecated. Use locator.BlockUsageLocator

DEPRECATED_TAG = 'i4x'
replace(**kwargs)[source]
Return: a new Location with specific kwargs replacing

their corresponding values.

Using BlockUsageLocator’s replace function results in a mismatch of __init__ args and kwargs.

Replace tries to instantiate a Location object with BlockUsageLocator’s args and kwargs.

class opaque_keys.edx.locations.LocationBase(org, course, run, category, name, revision=None, **kwargs)[source]

Bases: object

Deprecated. Base class for Location and AssetLocation

DEPRECATED_TAG = None
classmethod clean(value)[source]

Deprecated. See BlockUsageLocator.clean

classmethod clean_for_html(value)[source]

Deprecated. See BlockUsageLocator.clean_for_html

classmethod clean_for_url_name(value)[source]

Deprecated. See BlockUsageLocator.clean_for_url_name

classmethod clean_keeping_underscores(value)[source]

Deprecated. See BlockUsageLocator.clean_keeping_underscores

classmethod from_string(serialized)[source]

Deprecated. Use locator.BlockUsageLocator.from_string().

property tag

Deprecated. Returns the deprecated tag for this Location.

class opaque_keys.edx.locations.SlashSeparatedCourseKey(org, course, run, **kwargs)[source]

Bases: opaque_keys.edx.locator.CourseLocator

Deprecated. Use locator.CourseLocator

branch
course
classmethod from_string(serialized)[source]

Deprecated. Use locator.CourseLocator.from_string().

org
replace(**kwargs)[source]
Return: a new SlashSeparatedCourseKey with specific kwargs replacing

their corresponding values.

Using CourseLocator’s replace function results in a mismatch of __init__ args and kwargs.

Replace tries to instantiate a SlashSeparatedCourseKey object with CourseLocator args and kwargs.

run
version_guid
class opaque_keys.edx.locations.i4xEncoder(*args, **kwargs)[source]

Bases: opaque_keys.edx.keys.i4xEncoder

Deprecated. Use keys.i4xEncoder

opaque_keys.edx.locator module

Identifier for course resources.

class opaque_keys.edx.locator.AssetLocator(course_key, block_type, block_id, **kwargs)[source]

Bases: opaque_keys.edx.locator.BlockUsageLocator, opaque_keys.edx.keys.AssetKey

An AssetKey implementation class.

ALLOWED_ID_RE = re.compile('^[\\w\\-~.:%]+\\Z')
ASSET_URL_RE = re.compile('\n        ^\n        /c4x/\n        (?P<org>[^/]+)/\n        (?P<course>[^/]+)/\n        (?P<category>[^/]+)/\n        (?P<name>[^@]+)\n        (@(?P<revision>[^/]+))?\n        \\Z\n    ', re.VERBOSE)
CANONICAL_NAMESPACE = 'asset-v1'
DEPRECATED_ALLOWED_ID_RE = re.compile('^[\\w\\-~.:%]+\\Z')
DEPRECATED_TAG = 'c4x'
property asset_type

Return what type of asset this is.

block_id
block_type
course_key
property path

Return the path for this asset.

replace(**kwargs)[source]
Return: a new OpaqueKey with KEY_FIELDS specified in kwargs replaced

their corresponding values. Deprecation value is also preserved.

Subclasses should override this if they have required properties that aren’t included in their KEY_FIELDS.

property tag

Returns the deprecated tag for this Location.

to_deprecated_list_repr()[source]

Thumbnail locations are stored as lists [c4x, org, course, thumbnail, path, None] in contentstore.mongo That should be the only use of this method, but the method is general enough to provide the pre-opaque Location fields as an array in the old order with the tag.

class opaque_keys.edx.locator.BlockLocatorBase(*args, **kwargs)[source]

Bases: opaque_keys.edx.locator.Locator

Abstract base clase for XBlock locators.

See subclasses for more detail, particularly CourseLocator and BlockUsageLocator.

ALLOWED_ID_RE = re.compile('^[\\w\\-~.:]+\\Z')
BLOCK_ALLOWED_ID_CHARS = '[\\w\\-~.:%]'
BLOCK_PREFIX = 'block'
BRANCH_PREFIX = 'branch'
DEPRECATED_ALLOWED_ID_RE = re.compile('^[\\w\\-~.:%]+\\Z')
URL_RE = re.compile('^\n        ((?P<org>[\\w\\-~.:]+)\\+(?P<course>[\\w\\-~.:]+)(\\+(?P<run>[\\w\\-~.:]+))?(\\+(?=.)|\\Z))??\n        (branch@(?P<branch>[\\w\\-~.:]+)(\\+(?=.)|\\Z))?\n        (version@(?P<version_guid>[, re.VERBOSE)
URL_RE_SOURCE = '\n        ((?P<org>[\\w\\-~.:]+)\\+(?P<course>[\\w\\-~.:]+)(\\+(?P<run>[\\w\\-~.:]+))?(\\+(?=.)|\\Z))??\n        (branch@(?P<branch>[\\w\\-~.:]+)(\\+(?=.)|\\Z))?\n        (version@(?P<version_guid>[a-f0-9]+)(\\+(?=.)|\\Z))?\n        (type@(?P<block_type>[\\w\\-~.:]+)(\\+(?=.)|\\Z))?\n        (block@(?P<block_id>[\\w\\-~.:%]+))?\n    '
deprecated
classmethod parse_url(string)[source]

If it can be parsed as a version_guid with no preceding org + offering, returns a dict with key ‘version_guid’ and the value,

If it can be parsed as a org + offering, returns a dict with key ‘id’ and optional keys ‘branch’ and ‘version_guid’.

Raises:

InvalidKeyError: if string cannot be parsed -or- string ends with a newline.

class opaque_keys.edx.locator.BlockUsageLocator(course_key, block_type, block_id, **kwargs)[source]

Bases: opaque_keys.edx.locator.BlockLocatorBase, opaque_keys.edx.keys.UsageKey

Encodes a location.

Locations address modules (aka blocks) which are definitions situated in a course instance. Thus, a Location must identify the course and the occurrence of the defined element in the course. Courses can be a version of an offering, the current draft head, or the current production version.

Locators can contain both a version and a org + course + run w/ branch. The split mongo functions may raise errors if these conflict w/ the current db state (i.e., the course’s branch != the version_guid)

Locations can express as urls as well as dictionaries. They consist of

package_identifier: course_guid | version_guid block : guid branch : string

BlockUsageLocators also support deprecated Location-style formatting with the following mapping: Location(org, course, run, category, name, revision) is represented as a BlockUsageLocator with:

  • course_key = a CourseKey comprised of (org, course, run, branch=revision)

  • block_type = category

  • block_id = name

CANONICAL_NAMESPACE = 'block-v1'
CHECKED_INIT = False
DEPRECATED_INVALID_CHARS = re.compile('[^\\w.%-]')
DEPRECATED_INVALID_CHARS_NAME = re.compile('[^\\w.:%-]')
DEPRECATED_INVALID_HTML_CHARS = re.compile('[^\\w-]')
DEPRECATED_TAG = 'i4x'
DEPRECATED_URL_RE = re.compile('\n        i4x://\n        (?P<org>[^/]+)/\n        (?P<course>[^/]+)/\n        (?P<category>[^/]+)/     # category == block_type\n        (?P<name>[^@]+)          # name == block_id\n        (@(?P<re, re.VERBOSE)
KEY_FIELDS = ('course_key', 'block_type', 'block_id')
block_id = None
block_type = None
property branch

Returns the branch for this object’s course_key.

property category

Deprecated. The ambiguously named field from Location which code expects to find. Equivalent to block_type.

classmethod clean(value)[source]

Should only be called on deprecated-style values

Return value, made into a form legal for locations

classmethod clean_for_html(value)[source]

Should only be called on deprecated-style values

Convert a string into a form that’s safe for use in html ids, classes, urls, etc. Replaces all INVALID_HTML_CHARS with ‘_’, collapses multiple ‘_’ chars

classmethod clean_for_url_name(value)[source]

Should only be called on deprecated-style values

Convert value into a format valid for location names (allows colons).

classmethod clean_keeping_underscores(value)[source]

Should only be called on deprecated-style values

Return value, replacing INVALID_CHARS, but not collapsing multiple ‘_’ chars. This for cleaning asset names, as the YouTube ID’s may have underscores in them, and we need the transcript asset name to match. In the future we may want to change the behavior of _clean.

property course

Returns the course for this object’s course_key.

course_agnostic()[source]

We only care about the locator’s version not its course. Returns a copy of itself without any course info.

Raises:

ValueError if the block locator has no version_guid

course_key = None
property definition_key

Returns the definition key for this object. Undefined for Locators.

for_branch(branch)[source]

Return a UsageLocator for the same block in a different branch of the course.

for_version(version_guid)[source]

Return a UsageLocator for the same block in a different branch of the course.

html_id()[source]

Return an id which can be used on an html page as an id attr of an html element. It is currently also persisted by some clients to identify blocks.

To make compatible with old Location object functionality. I don’t believe this behavior fits at this place, but I have no way to override. We should clearly define the purpose and restrictions of this (e.g., I’m assuming periods are fine).

is_fully_specified()[source]

Returns boolean; whether or not this object’s course_key is fully specified.

classmethod make_relative(course_locator, block_type, block_id)[source]

Return a new instance which has the given block_id in the given course :param course_locator: may be a BlockUsageLocator in the same snapshot

map_into_course(course_key)[source]

Return a new instance which has the this block_id in the given course :param course_key: a CourseKey object representing the new course to map into

property name

Deprecated. The ambiguously named field from Location which code expects to find. Equivalent to block_id.

property offering

Deprecated. Use course and run independently.

property org

Returns the org for this object’s course_key.

replace(**kwargs)[source]
Return: a new OpaqueKey with KEY_FIELDS specified in kwargs replaced

their corresponding values. Deprecation value is also preserved.

Subclasses should override this if they have required properties that aren’t included in their KEY_FIELDS.

property revision

Deprecated. The ambiguously named field from Location which code expects to find. Equivalent to branch.

property run

Returns the run for this object’s course_key.

to_deprecated_son(prefix='', tag='i4x')[source]

Returns a SON object that represents this location

property version

Deprecated. The ambiguously named field from CourseLocation which code expects to find. Equivalent to version_guid.

version_agnostic()[source]

We don’t care if the locator’s version is not the current head; so, avoid version conflict by reducing info. Returns a copy of itself without any version info.

Raises:

ValueError: if the block locator has no org, course, and run

property version_guid

Returns the version guid for this object.

class opaque_keys.edx.locator.BundleDefinitionLocator(bundle_uuid, block_type, olx_path, bundle_version=None, draft_name=None, _version_or_draft=None)[source]

Bases: opaque_keys.edx.locator.CheckFieldMixin, opaque_keys.edx.keys.DefinitionKey

Implementation of the DefinitionKey type, for XBlock content stored in Blockstore bundles. This is a low-level identifier used within the Open edX system for identifying and retrieving OLX.

A “Definition” is a specific OLX file in a specific BundleVersion (or sometimes rather than a BundleVersion, it may point to a named draft.) The OLX file, and thus the definition key, defines Scope.content fields as well as defaults for Scope.settings and Scope.children fields. However the definition has no parent and no position in any particular course or other context - both of which require a usage key and not just a definition key. The same block definition (.olx file) can be used in multiple places in a course, each with a different usage key.

Example serialized definition keys follow.

The ‘html’ type OLX file “html/introduction/definition.xml” in bundle 11111111-1111-1111-1111-111111111111, bundle version 5:

bundle-olx:11111111-1111-1111-1111-111111111111:5:html:html/introduction/definition.xml

The ‘problem’ type OLX file “problem324234.xml” in bundle 22222222-2222-2222-2222-222222222222, draft ‘studio-draft’:

bundle-olx:22222222-2222-2222-2222-222222222222:studio-draft:problem:problem/324234.xml

(The serialized version is somewhat long and verbose because it should rarely be used except for debugging - the in-memory python key instance will be used most of the time, and users will rarely/never see definition keys.)

User state should never be stored using a BundleDefinitionLocator as the key. State should always be stored against a usage locator, which refers to a particular definition being used in a particular context.

Each BundleDefinitionLocator holds the following data
  1. Bundle UUID and [bundle version OR draft name]

  2. Block type (e.g. ‘html’, ‘problem’, etc.)

  3. Path to OLX file

Note that since the data in an .olx file can only ever change in a bundle draft (not in a specific bundle version), an XBlock that is actively making changes to its Scope.content/Scope.settings field values must have a BundleDefinitionLocator with a draft name (not a bundle version).

CANONICAL_NAMESPACE = 'bundle-olx'
CHECKED_INIT = False
KEY_FIELDS = ('bundle_uuid', 'block_type', 'olx_path', '_version_or_draft')
OLX_PATH_REGEXP = re.compile('^[\\w\\-./]+$')
block_type
bundle_uuid
property bundle_version

Get the Blockstore bundle version number, or None if a Blockstore draft name has been specified instead.

property draft_name

Get the Blockstore draft name, or None if a Blockstore bundle version number has been specified instead.

olx_path
class opaque_keys.edx.locator.CheckFieldMixin[source]

Bases: object

Mixin that provides handy methods for checking field types/values.

class opaque_keys.edx.locator.CourseLocator(org=None, course=None, run=None, branch=None, version_guid=None, deprecated=False, **kwargs)[source]

Bases: opaque_keys.edx.locator.BlockLocatorBase, opaque_keys.edx.keys.CourseKey

Examples of valid CourseLocator specifications:

CourseLocator(version_guid=ObjectId(‘519665f6223ebd6980884f2b’)) CourseLocator(org=’mit.eecs’, course=’6.002x’, run=’T2_2014’) CourseLocator(org=’mit.eecs’, course=’6002x’, run=’fall_2014’ branch = ‘published’) CourseLocator.from_string(‘course-v1:version@519665f6223ebd6980884f2b’) CourseLocator.from_string(‘course-v1:mit.eecs+6002x’) CourseLocator.from_string(‘course-v1:mit.eecs+6002x+branch@published’) CourseLocator.from_string(‘course-v1:mit.eecs+6002x+branch@published+version@519665f6223ebd6980884f2b’)

Should have at least a specific org, course, and run with optional ‘branch’, or version_guid (which points to a specific version). Can contain both in which case the persistence layer may raise exceptions if the given version != the current such version of the course.

CANONICAL_NAMESPACE = 'course-v1'
CHECKED_INIT = False
INVALID_CHARS_DEPRECATED = re.compile('[^\\w.%-]')
KEY_FIELDS = ('org', 'course', 'run', 'branch', 'version_guid')
branch
course
course_agnostic()[source]

We only care about the locator’s version not its course. Returns a copy of itself without any course info.

Raises:

ValueError: if the block locator has no version_guid

for_branch(branch)[source]

Return a new CourseLocator for another branch of the same course (also version agnostic)

for_version(version_guid)[source]

Return a new CourseLocator for another version of the same course and branch. Usually used when the head is updated (and thus the course x branch now points to this version)

html_id()[source]

Return an id which can be used on an html page as an id attr of an html element.

To make compatible with old Location object functionality. I don’t believe this behavior fits at this place, but I have no way to override. We should clearly define the purpose and restrictions of this (e.g., I’m assuming periods are fine).

make_asset_key(asset_type, path)[source]

Return an asset key, given the given the specified path.

This function should not actually create any new ids, but should simply return one that already exists.

make_usage_key(block_type, block_id)[source]

Return a usage key, given the given the specified block_type and block_id.

This function should not actually create any new ids, but should simply return one that already exists.

make_usage_key_from_deprecated_string(location_url)[source]

Deprecated mechanism for creating a UsageKey given a CourseKey and a serialized Location.

NOTE: this prejudicially takes the tag, org, and course from the url not self.

Raises:

InvalidKeyError: if the url does not parse

property offering

Deprecated. Use course and run independently.

org
run
property version

Deprecated. The ambiguously named field from CourseLocation which code expects to find. Equivalent to version_guid.

version_agnostic()[source]

We don’t care if the locator’s version is not the current head; so, avoid version conflict by reducing info. Returns a copy of itself without any version info.

Raises:

ValueError: if the block locator has no org & course, run

version_guid
class opaque_keys.edx.locator.DefinitionLocator(block_type, definition_id, deprecated=False)[source]

Bases: opaque_keys.edx.locator.Locator, opaque_keys.edx.keys.DefinitionKey

Container for how to locate a description (the course-independent content).

CANONICAL_NAMESPACE = 'def-v1'
CHECKED_INIT = False
KEY_FIELDS = ('definition_id', 'block_type')
URL_RE = re.compile('^(?P<definition_id>[a-f0-9]+)\\+type@(?P<block_type>[\\w\\-~.:]+)\\Z', re.VERBOSE)
block_type = None
definition_id = None
property version

Returns the ObjectId referencing this specific location.

class opaque_keys.edx.locator.LibraryLocator(org=None, library=None, branch=None, version_guid=None, **kwargs)[source]

Bases: opaque_keys.edx.locator.BlockLocatorBase, opaque_keys.edx.keys.CourseKey

Locates a library. Libraries are XBlock structures with a ‘library’ block at their root.

Libraries are treated analogously to courses for now. Once opaque keys are better supported, they will no longer have the ‘run’ property, and may no longer conform to CourseKey but rather some more general key type.

Examples of valid LibraryLocator specifications:

LibraryLocator(version_guid=ObjectId(‘519665f6223ebd6980884f2b’)) LibraryLocator(org=’UniX’, library=’PhysicsProbs’) LibraryLocator.from_string(‘library-v1:UniX+PhysicsProbs’)

version_guid is optional.

The constructor accepts ‘course’ as a deprecated alias for the ‘library’ attribute.

branch is optional.

CANONICAL_NAMESPACE = 'library-v1'
CHECKED_INIT = False
KEY_FIELDS = ('org', 'library', 'branch', 'version_guid')
RUN = 'library'
branch
property course

Deprecated. Return a ‘course’ for compatibility with CourseLocator.

course_agnostic()[source]

We only care about the locator’s version not its library. Returns a copy of itself without any library info.

Raises:

ValueError: if the block locator has no version_guid

for_branch(branch)[source]

Return a new CourseLocator for another branch of the same library (also version agnostic)

for_version(version_guid)[source]

Return a new LibraryLocator for another version of the same library and branch. Usually used when the head is updated (and thus the library x branch now points to this version)

html_id()[source]

Return an id which can be used on an html page as an id attr of an html element.

is_course = False
library
make_asset_key(asset_type, path)[source]

Return an asset key, given the given the specified path.

This function should not actually create any new ids, but should simply return one that already exists.

make_usage_key(block_type, block_id)[source]

Return a usage key, given the given the specified block_type and block_id.

This function should not actually create any new ids, but should simply return one that already exists.

org
property run

Deprecated. Return a ‘run’ for compatibility with CourseLocator.

property version

Deprecated. The ambiguously named field from CourseLocation which code expects to find. Equivalent to version_guid.

version_agnostic()[source]

We don’t care if the locator’s version is not the current head; so, avoid version conflict by reducing info. Returns a copy of itself without any version info.

Raises:

ValueError: if the block locator has no org & course, run

version_guid
class opaque_keys.edx.locator.LibraryLocatorV2(org, slug)[source]

Bases: opaque_keys.edx.locator.CheckFieldMixin, opaque_keys.edx.keys.LearningContextKey

A key that represents a Blockstore-based content library.

When serialized, these keys look like:

lib:MITx:reallyhardproblems lib:hogwarts:p300-potions-exercises

CANONICAL_NAMESPACE = 'lib'
CHECKED_INIT = False
KEY_FIELDS = ('org', 'slug')
SLUG_REGEXP = re.compile('^[\\w\\-.]+$')
for_branch(branch)[source]

Compatibility helper. Some code calls .for_branch(None) on course keys. By implementing this, it improves backwards compatibility between library keys and course keys.

make_definition_usage(definition_key, usage_id=None)[source]

Return a usage key, given the given the specified definition key and usage_id.

org
slug
class opaque_keys.edx.locator.LibraryUsageLocator(library_key, block_type, block_id, **kwargs)[source]

Bases: opaque_keys.edx.locator.BlockUsageLocator

Just like BlockUsageLocator, but this points to a block stored in a library, not a course.

CANONICAL_NAMESPACE = 'lib-block-v1'
KEY_FIELDS = ('library_key', 'block_type', 'block_id')
block_id = None
block_type = None
property course_key

To enable compatibility with BlockUsageLocator, we provide a read-only course_key property.

for_branch(branch)[source]

Return a UsageLocator for the same block in a different branch of the library.

for_version(version_guid)[source]

Return a UsageLocator for the same block in a different version of the library.

library_key = None
replace(**kwargs)[source]
Return: a new OpaqueKey with KEY_FIELDS specified in kwargs replaced

their corresponding values. Deprecation value is also preserved.

Subclasses should override this if they have required properties that aren’t included in their KEY_FIELDS.

property run

Returns the run for this object’s library_key.

to_deprecated_son(prefix='', tag='i4x')[source]

Disable some deprecated methods of our parent class.

version_agnostic()[source]

We don’t care if the locator’s version is not the current head; so, avoid version conflict by reducing info. Returns a copy of itself without any version info.

Raises:

ValueError: if the block locator has no org, course, and run

class opaque_keys.edx.locator.LibraryUsageLocatorV2(lib_key, block_type, usage_id)[source]

Bases: opaque_keys.edx.locator.CheckFieldMixin, opaque_keys.edx.keys.UsageKeyV2

An XBlock in a Blockstore-based content library.

When serialized, these keys look like:

lb:MITx:reallyhardproblems:problem:problem1

CANONICAL_NAMESPACE = 'lb'
CHECKED_INIT = False
KEY_FIELDS = ('lib_key', 'block_type', 'usage_id')
USAGE_ID_REGEXP = re.compile('^[\\w\\-.]+$')
property block_id

Get the ‘block ID’ which is another name for the usage ID.

block_type
property context_key

Get the learning context key (LearningContextKey) for this XBlock usage. May be a course key, library key, or some other LearningContextKey type.

html_id()[source]

Return an id which can be used on an html page as an id attr of an html element. This is only in here for backwards-compatibility with XModules; don’t use in new code.

lib_key
usage_id
class opaque_keys.edx.locator.LocalId(block_id=None)[source]

Bases: object

Class for local ids for non-persisted xblocks (which can have hardcoded block_ids if necessary)

class opaque_keys.edx.locator.Locator(*args, **kwargs)[source]

Bases: opaque_keys.OpaqueKey

A locator identifies a course resource.

Locator is an abstract base class: do not instantiate

ALLOWED_ID_CHARS = '[\\w\\-~.:]'
BLOCK_TYPE_PREFIX = 'type'
DEPRECATED_ALLOWED_ID_CHARS = '[\\w\\-~.:%]'
VERSION_PREFIX = 'version'
classmethod as_object_id(value)[source]

Attempts to cast value as a bson.objectid.ObjectId.

Raises:

ValueError: if casting fails

deprecated
property version

Returns the ObjectId referencing this specific location.

Raises:

InvalidKeyError: if the instance doesn’t have a complete enough specification.

class opaque_keys.edx.locator.VersionTree(locator, tree_dict=None)[source]

Bases: object

Holds trees of Locators to represent version histories.

Module contents