From 4007d5ebcb6c2e5b16ad15fec7759f23828e3593 Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Tue, 28 Oct 2014 13:37:13 -0400 Subject: [PATCH] Wrap ModuleSystem and DescriptorSystem into a single object, to continue holding up the XModule house of cards [PLAT-191] --- common/lib/xmodule/xmodule/x_module.py | 42 ++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/common/lib/xmodule/xmodule/x_module.py b/common/lib/xmodule/xmodule/x_module.py index 6f35bf1a14..cf5b9fff19 100644 --- a/common/lib/xmodule/xmodule/x_module.py +++ b/common/lib/xmodule/xmodule/x_module.py @@ -203,8 +203,9 @@ class XModuleMixin(XBlockMixin): # so we should use the xmodule_runtime (ModuleSystem) as the runtime. if (not isinstance(self, (XModule, XModuleDescriptor)) and self.xmodule_runtime is not None): - return self.xmodule_runtime - return self._runtime + return PureSystem(self.xmodule_runtime, self._runtime) + else: + return self._runtime @runtime.setter def runtime(self, value): @@ -443,10 +444,9 @@ class XModuleMixin(XBlockMixin): """ Set up this XBlock to act as an XModule instead of an XModuleDescriptor. - :param xmodule_runtime: the runtime to use when accessing student facing methods - :type xmodule_runtime: :class:`ModuleSystem` - :param field_data: The :class:`FieldData` to use for all subsequent data access - :type field_data: :class:`FieldData` + Arguments: + xmodule_runtime (:class:`ModuleSystem'): the runtime to use when accessing student facing methods + field_data (:class:`FieldData`): The :class:`FieldData` to use for all subsequent data access """ # pylint: disable=attribute-defined-outside-init self.xmodule_runtime = xmodule_runtime @@ -1408,6 +1408,36 @@ class ModuleSystem(MetricsMixin, ConfigurableFragmentWrapper, Runtime): # pylin pass +class PureSystem(ModuleSystem, DescriptorSystem): + """ + A subclass of both ModuleSystem and DescriptorSystem to provide pure (non-XModule) XBlocks + a single Runtime interface for both the ModuleSystem and DescriptorSystem, when available. + """ + # This system doesn't override a number of methods that are provided by ModuleSystem and DescriptorSystem, + # namely handler_url, local_resource_url, query, and resource_url. + # + # At runtime, the ModuleSystem and/or DescriptorSystem will define those methods + # + # pylint: disable=abstract-method + def __init__(self, module_system, descriptor_system): + # N.B. This doesn't call super(PureSystem, self).__init__, because it is only inheriting from + # ModuleSystem and DescriptorSystem to pass isinstance checks. + # pylint: disable=super-init-not-called + self._module_system = module_system + self._descriptor_system = descriptor_system + + def __getattr__(self, name): + """ + If the ModuleSystem doesn't have an attribute, try returning the same attribute from the + DescriptorSystem, instead. This allows XModuleDescriptors that are bound as XModules + to still function as XModuleDescriptors. + """ + try: + return getattr(self._module_system, name) + except AttributeError: + return getattr(self._descriptor_system, name) + + class DoNothingCache(object): """A duck-compatible object to use in ModuleSystem when there's no cache.""" def get(self, _key):