From 3c054306c61575c01a0dd8d9b6b20abac3ac954e Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Wed, 27 Jun 2012 14:17:15 -0400 Subject: [PATCH] Add the ability to specify Keystore engines and default descriptor classes by name from settings --- cms/envs/dev.py | 10 +++++++--- common/lib/keystore/django.py | 13 +++++++------ common/lib/keystore/mongo.py | 11 +++++++---- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/cms/envs/dev.py b/cms/envs/dev.py index 16bed60729..ce775d962a 100644 --- a/cms/envs/dev.py +++ b/cms/envs/dev.py @@ -8,9 +8,13 @@ TEMPLATE_DEBUG = DEBUG KEYSTORE = { 'default': { - 'host': 'localhost', - 'db': 'mongo_base', - 'collection': 'key_store', + 'ENGINE': 'keystore.mongo.MongoModuleStore', + 'OPTIONS': { + 'default_class': 'xmodule.raw_module.RawDescriptor', + 'host': 'localhost', + 'db': 'mongo_base', + 'collection': 'key_store', + } } } diff --git a/common/lib/keystore/django.py b/common/lib/keystore/django.py index 98479a7f7c..89aa9d07b0 100644 --- a/common/lib/keystore/django.py +++ b/common/lib/keystore/django.py @@ -6,9 +6,9 @@ Passes settings.KEYSTORE as kwargs to MongoModuleStore from __future__ import absolute_import +from importlib import import_module + from django.conf import settings -from .mongo import MongoModuleStore -from raw_module import RawDescriptor _KEYSTORES = {} @@ -17,9 +17,10 @@ def keystore(name='default'): global _KEYSTORES if name not in _KEYSTORES: - # TODO (cpennington): Load the default class from a string - _KEYSTORES[name] = MongoModuleStore( - default_class=RawDescriptor, - **settings.KEYSTORE[name]) + class_path = settings.KEYSTORE[name]['ENGINE'] + module_path, _, class_name = class_path.rpartition('.') + class_ = getattr(import_module(module_path), class_name) + _KEYSTORES[name] = class_( + **settings.KEYSTORE[name]['OPTIONS']) return _KEYSTORES[name] diff --git a/common/lib/keystore/mongo.py b/common/lib/keystore/mongo.py index ece8b35b71..4317ce0204 100644 --- a/common/lib/keystore/mongo.py +++ b/common/lib/keystore/mongo.py @@ -1,7 +1,9 @@ import pymongo +from importlib import import_module +from xmodule.x_module import XModuleDescriptor, DescriptorSystem + from . import ModuleStore, Location from .exceptions import ItemNotFoundError, InsufficientSpecificationError -from xmodule.x_module import XModuleDescriptor, DescriptorSystem class MongoModuleStore(ModuleStore): @@ -16,7 +18,10 @@ class MongoModuleStore(ModuleStore): # Force mongo to report errors, at the expense of performance self.collection.safe = True - self.default_class = default_class + + module_path, _, class_name = default_class.rpartition('.') + class_ = getattr(import_module(module_path), class_name) + self.default_class = class_ def get_item(self, location): """ @@ -29,8 +34,6 @@ class MongoModuleStore(ModuleStore): If no object is found at that location, raises keystore.exceptions.ItemNotFoundError location: Something that can be passed to Location - default_class: An XModuleDescriptor subclass to use if no plugin matching the - location is found """ query = {}