diff --git a/openedx/core/djangoapps/content/block_structure/admin.py b/openedx/core/djangoapps/content/block_structure/admin.py index 95ef3e140b..a1c74f261e 100644 --- a/openedx/core/djangoapps/content/block_structure/admin.py +++ b/openedx/core/djangoapps/content/block_structure/admin.py @@ -1,9 +1,11 @@ """ Django Admin for Block Structures. """ -from django.contrib import admin +from __future__ import absolute_import from config_models.admin import ConfigurationModelAdmin +from django.contrib import admin + from .config.models import BlockStructureConfiguration diff --git a/openedx/core/djangoapps/content/block_structure/api.py b/openedx/core/djangoapps/content/block_structure/api.py index a86b5bc242..eba7936aec 100644 --- a/openedx/core/djangoapps/content/block_structure/api.py +++ b/openedx/core/djangoapps/content/block_structure/api.py @@ -1,7 +1,10 @@ """ Higher order functions built on the BlockStructureManager to interact with a django cache. """ +from __future__ import absolute_import + from django.core.cache import cache + from xmodule.modulestore.django import modulestore from .manager import BlockStructureManager diff --git a/openedx/core/djangoapps/content/block_structure/apps.py b/openedx/core/djangoapps/content/block_structure/apps.py index 4ccdc608c4..d75e61b718 100644 --- a/openedx/core/djangoapps/content/block_structure/apps.py +++ b/openedx/core/djangoapps/content/block_structure/apps.py @@ -2,6 +2,8 @@ Configuration for block_structure djangoapp """ +from __future__ import absolute_import + from django.apps import AppConfig diff --git a/openedx/core/djangoapps/content/block_structure/block_structure.py b/openedx/core/djangoapps/content/block_structure/block_structure.py index 43839c9718..ea1a0e4454 100644 --- a/openedx/core/djangoapps/content/block_structure/block_structure.py +++ b/openedx/core/djangoapps/content/block_structure/block_structure.py @@ -8,15 +8,18 @@ The following internal data structures are implemented: _BlockRelations - Data structure for a single block's relations. _BlockData - Data structure for a single block's data. """ +from __future__ import absolute_import + from copy import deepcopy from functools import partial from logging import getLogger -from openedx.core.lib.graph_traversals import traverse_topologically, traverse_post_order +import six + +from openedx.core.lib.graph_traversals import traverse_post_order, traverse_topologically from .exceptions import TransformerException - logger = getLogger(__name__) # pylint: disable=invalid-name @@ -145,7 +148,7 @@ class BlockStructure(object): iterator(UsageKey) - An iterator of the usage keys of all the blocks in the block structure. """ - return self._block_relations.iterkeys() + return six.iterkeys(self._block_relations) #--- Block structure traversal methods ---# @@ -432,14 +435,14 @@ class BlockStructureBlockData(BlockStructure): Returns iterator of (UsageKey, BlockData) pairs for all blocks in the BlockStructure. """ - return self._block_data_map.iteritems() + return six.iteritems(self._block_data_map) def itervalues(self): """ Returns iterator of BlockData for all blocks in the BlockStructure. """ - return self._block_data_map.itervalues() + return six.itervalues(self._block_data_map) def __getitem__(self, usage_key): """ @@ -835,7 +838,7 @@ class BlockStructureModulestoreData(BlockStructureBlockData): Iterates through all instantiated xBlocks that were added and collects all xBlock fields that were requested. """ - for xblock_usage_key, xblock in self._xblock_map.iteritems(): + for xblock_usage_key, xblock in six.iteritems(self._xblock_map): block_data = self._get_or_create_block(xblock_usage_key) for field_name in self._requested_xblock_fields: self._set_xblock_field(block_data, xblock, field_name) diff --git a/openedx/core/djangoapps/content/block_structure/config/__init__.py b/openedx/core/djangoapps/content/block_structure/config/__init__.py index e6a9494d35..c8ce73aabf 100644 --- a/openedx/core/djangoapps/content/block_structure/config/__init__.py +++ b/openedx/core/djangoapps/content/block_structure/config/__init__.py @@ -9,7 +9,6 @@ from openedx.core.lib.cache_utils import request_cached from .models import BlockStructureConfiguration - # Namespace WAFFLE_NAMESPACE = u'block_structure' diff --git a/openedx/core/djangoapps/content/block_structure/config/models.py b/openedx/core/djangoapps/content/block_structure/config/models.py index 2e68045112..ee7606d349 100644 --- a/openedx/core/djangoapps/content/block_structure/config/models.py +++ b/openedx/core/djangoapps/content/block_structure/config/models.py @@ -3,8 +3,8 @@ Models for configuration of Block Structures. """ from __future__ import absolute_import -from django.db.models import IntegerField from config_models.models import ConfigurationModel +from django.db.models import IntegerField class BlockStructureConfiguration(ConfigurationModel): diff --git a/openedx/core/djangoapps/content/block_structure/factory.py b/openedx/core/djangoapps/content/block_structure/factory.py index dbaaa2cfa0..8be3e72f9b 100644 --- a/openedx/core/djangoapps/content/block_structure/factory.py +++ b/openedx/core/djangoapps/content/block_structure/factory.py @@ -1,7 +1,7 @@ """ Module for factory class for BlockStructure objects. """ -from .block_structure import BlockStructureModulestoreData, BlockStructureBlockData +from .block_structure import BlockStructureBlockData, BlockStructureModulestoreData class BlockStructureFactory(object): diff --git a/openedx/core/djangoapps/content/block_structure/management/commands/generate_course_blocks.py b/openedx/core/djangoapps/content/block_structure/management/commands/generate_course_blocks.py index 3cf2b7d562..a9607b4b23 100644 --- a/openedx/core/djangoapps/content/block_structure/management/commands/generate_course_blocks.py +++ b/openedx/core/djangoapps/content/block_structure/management/commands/generate_course_blocks.py @@ -5,21 +5,21 @@ from __future__ import absolute_import import logging +import six from django.core.management.base import BaseCommand import six from six import text_type -from xmodule.modulestore.django import modulestore import openedx.core.djangoapps.content.block_structure.api as api -from openedx.core.djangoapps.content.block_structure.config import STORAGE_BACKING_FOR_CACHE, waffle -import openedx.core.djangoapps.content.block_structure.tasks as tasks import openedx.core.djangoapps.content.block_structure.store as store +import openedx.core.djangoapps.content.block_structure.tasks as tasks +from openedx.core.djangoapps.content.block_structure.config import STORAGE_BACKING_FOR_CACHE, waffle from openedx.core.lib.command_utils import ( get_mutually_exclusive_required_option, - validate_dependent_option, parse_course_keys, + validate_dependent_option ) - +from xmodule.modulestore.django import modulestore log = logging.getLogger(__name__) diff --git a/openedx/core/djangoapps/content/block_structure/management/commands/tests/test_generate_course_blocks.py b/openedx/core/djangoapps/content/block_structure/management/commands/tests/test_generate_course_blocks.py index 650ee7df61..cc2afc787d 100644 --- a/openedx/core/djangoapps/content/block_structure/management/commands/tests/test_generate_course_blocks.py +++ b/openedx/core/djangoapps/content/block_structure/management/commands/tests/test_generate_course_blocks.py @@ -3,20 +3,22 @@ Tests for generate_course_blocks management command. """ from __future__ import absolute_import +import itertools + import ddt from django.core.management.base import CommandError -import itertools +from mock import patch import six from six.moves import range -from mock import patch -from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase -from xmodule.modulestore.tests.factories import CourseFactory -from .. import generate_course_blocks from openedx.core.djangoapps.content.block_structure.tests.helpers import ( is_course_in_block_structure_cache, - is_course_in_block_structure_storage, + is_course_in_block_structure_storage ) +from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase +from xmodule.modulestore.tests.factories import CourseFactory + +from .. import generate_course_blocks @ddt.ddt diff --git a/openedx/core/djangoapps/content/block_structure/manager.py b/openedx/core/djangoapps/content/block_structure/manager.py index 4e2052e97b..364d0a7a4a 100644 --- a/openedx/core/djangoapps/content/block_structure/manager.py +++ b/openedx/core/djangoapps/content/block_structure/manager.py @@ -2,10 +2,14 @@ Top-level module for the Block Structure framework with a class for managing BlockStructures. """ +from __future__ import absolute_import + from contextlib import contextmanager +import six + from . import config -from .exceptions import UsageKeyNotInBlockStructure, TransformerDataIncompatible, BlockStructureNotFound +from .exceptions import BlockStructureNotFound, TransformerDataIncompatible, UsageKeyNotInBlockStructure from .factory import BlockStructureFactory from .store import BlockStructureStore from .transformers import BlockStructureTransformers @@ -69,8 +73,8 @@ class BlockStructureManager(object): if starting_block_usage_key not in block_structure: raise UsageKeyNotInBlockStructure( u"The requested usage_key '{0}' is not found in the block_structure with root '{1}'", - unicode(starting_block_usage_key), - unicode(self.root_block_usage_key), + six.text_type(starting_block_usage_key), + six.text_type(self.root_block_usage_key), ) block_structure.set_root_block(starting_block_usage_key) transformers.transform(block_structure) diff --git a/openedx/core/djangoapps/content/block_structure/models.py b/openedx/core/djangoapps/content/block_structure/models.py index 2c685ef16f..290f7e3d52 100644 --- a/openedx/core/djangoapps/content/block_structure/models.py +++ b/openedx/core/djangoapps/content/block_structure/models.py @@ -2,22 +2,26 @@ Models used by the block structure framework. """ +from __future__ import absolute_import + from contextlib import contextmanager from datetime import datetime +from logging import getLogger + +import six +from six.moves import map from django.conf import settings from django.core.exceptions import SuspiciousOperation from django.core.files.base import ContentFile from django.db import models, transaction -from logging import getLogger - from model_utils.models import TimeStampedModel + from openedx.core.djangoapps.xmodule_django.models import UsageKeyWithRunField from openedx.core.storage import get_storage from . import config from .exceptions import BlockStructureNotFound - log = getLogger(__name__) @@ -35,7 +39,7 @@ def _directory_name(data_usage_key): """ # replace any '/' in the usage key so they aren't interpreted # as folder separators. - encoded_usage_key = unicode(data_usage_key).replace('/', '_') + encoded_usage_key = six.text_type(data_usage_key).replace('/', '_') return '{}{}'.format( settings.BLOCK_STRUCTURES_SETTINGS.get('DIRECTORY_PREFIX', ''), encoded_usage_key, @@ -219,7 +223,7 @@ class BlockStructureModel(TimeStampedModel): Returns a string representation of this model. """ return u', '.join( - u'{}: {}'.format(field_name, unicode(getattr(self, field_name))) + u'{}: {}'.format(field_name, six.text_type(getattr(self, field_name))) for field_name in self.UNIQUENESS_FIELDS ) @@ -255,7 +259,7 @@ class BlockStructureModel(TimeStampedModel): Deletes the given files from storage. """ storage = _bs_model_storage() - map(storage.delete, files) + list(map(storage.delete, files)) @classmethod def _get_all_files(cls, data_usage_key): diff --git a/openedx/core/djangoapps/content/block_structure/signals.py b/openedx/core/djangoapps/content/block_structure/signals.py index ca312ba740..cfc340b2b6 100644 --- a/openedx/core/djangoapps/content/block_structure/signals.py +++ b/openedx/core/djangoapps/content/block_structure/signals.py @@ -1,13 +1,15 @@ """ Signal handlers for invalidating cached data. """ +from __future__ import absolute_import + +import six from django.conf import settings from django.dispatch.dispatcher import receiver +from opaque_keys.edx.locator import LibraryLocator from xmodule.modulestore.django import SignalHandler -from opaque_keys.edx.locator import LibraryLocator - from . import config from .api import clear_course_from_cache from .tasks import update_course_in_cache_v2 @@ -27,7 +29,7 @@ def update_block_structure_on_course_publish(sender, course_key, **kwargs): # p clear_course_from_cache(course_key) update_course_in_cache_v2.apply_async( - kwargs=dict(course_id=unicode(course_key)), + kwargs=dict(course_id=six.text_type(course_key)), countdown=settings.BLOCK_STRUCTURES_SETTINGS['COURSE_PUBLISH_TASK_DELAY'], ) diff --git a/openedx/core/djangoapps/content/block_structure/store.py b/openedx/core/djangoapps/content/block_structure/store.py index 0ea0d1dcad..204ee3606a 100644 --- a/openedx/core/djangoapps/content/block_structure/store.py +++ b/openedx/core/djangoapps/content/block_structure/store.py @@ -2,8 +2,12 @@ Module for the Storage of BlockStructure objects. """ # pylint: disable=protected-access +from __future__ import absolute_import + from logging import getLogger +import six + from openedx.core.lib.cache_utils import zpickle, zunpickle from . import config @@ -13,7 +17,6 @@ from .factory import BlockStructureFactory from .models import BlockStructureModel from .transformer_registry import TransformerRegistry - logger = getLogger(__name__) # pylint: disable=C0103 @@ -27,7 +30,7 @@ class StubModel(object): self.data_usage_key = root_block_usage_key def __unicode__(self): - return unicode(self.data_usage_key) + return six.text_type(self.data_usage_key) def delete(self): """ @@ -217,12 +220,12 @@ class BlockStructureStore(object): BlockStructureModel or StubModel. """ if _is_storage_backing_enabled(): - return unicode(bs_model) + return six.text_type(bs_model) else: return "v{version}.root.key.{root_usage_key}".format( - version=unicode(BlockStructureBlockData.VERSION), - root_usage_key=unicode(bs_model.data_usage_key), + version=six.text_type(BlockStructureBlockData.VERSION), + root_usage_key=six.text_type(bs_model.data_usage_key), ) @staticmethod @@ -235,7 +238,7 @@ class BlockStructureStore(object): data_version=getattr(root_block, 'course_version', None), data_edit_timestamp=getattr(root_block, 'subtree_edited_on', None), transformers_schema_version=TransformerRegistry.get_write_version_hash(), - block_structure_schema_version=unicode(BlockStructureBlockData.VERSION), + block_structure_schema_version=six.text_type(BlockStructureBlockData.VERSION), ) @staticmethod diff --git a/openedx/core/djangoapps/content/block_structure/tasks.py b/openedx/core/djangoapps/content/block_structure/tasks.py index 9c5e91ce8d..714d73b4c8 100644 --- a/openedx/core/djangoapps/content/block_structure/tasks.py +++ b/openedx/core/djangoapps/content/block_structure/tasks.py @@ -1,19 +1,20 @@ """ Asynchronous tasks related to the Course Blocks sub-application. """ +from __future__ import absolute_import + import logging -from capa.responsetypes import LoncapaProblemError from celery.task import task from django.conf import settings -from lxml.etree import XMLSyntaxError - from edxval.api import ValInternalError +from lxml.etree import XMLSyntaxError from opaque_keys.edx.keys import CourseKey -from xmodule.modulestore.exceptions import ItemNotFoundError +from capa.responsetypes import LoncapaProblemError from openedx.core.djangoapps.content.block_structure import api from openedx.core.djangoapps.content.block_structure.config import STORAGE_BACKING_FOR_CACHE, waffle +from xmodule.modulestore.exceptions import ItemNotFoundError log = logging.getLogger('edx.celery.task') diff --git a/openedx/core/djangoapps/content/block_structure/transformer.py b/openedx/core/djangoapps/content/block_structure/transformer.py index d51ca46a1b..2a68810b84 100644 --- a/openedx/core/djangoapps/content/block_structure/transformer.py +++ b/openedx/core/djangoapps/content/block_structure/transformer.py @@ -2,6 +2,8 @@ This module provides the abstract base class for all Block Structure Transformers. """ +from __future__ import absolute_import + from abc import abstractmethod diff --git a/openedx/core/djangoapps/content/block_structure/transformer_registry.py b/openedx/core/djangoapps/content/block_structure/transformer_registry.py index d90fe57b3f..93464c5ab2 100644 --- a/openedx/core/djangoapps/content/block_structure/transformer_registry.py +++ b/openedx/core/djangoapps/content/block_structure/transformer_registry.py @@ -2,11 +2,15 @@ Block Structure Transformer Registry implemented using the platform's PluginManager. """ +from __future__ import absolute_import + from base64 import b64encode from hashlib import sha1 -from openedx.core.lib.plugins import PluginManager +import six + from openedx.core.lib.cache_utils import process_cached +from openedx.core.lib.plugins import PluginManager class TransformerRegistry(PluginManager): @@ -30,7 +34,7 @@ class TransformerRegistry(PluginManager): registered with the platform's PluginManager. """ if cls.USE_PLUGIN_MANAGER: - return set(cls.get_available_plugins().itervalues()) + return set(six.itervalues(cls.get_available_plugins())) else: return set() diff --git a/openedx/core/djangoapps/content/block_structure/transformers.py b/openedx/core/djangoapps/content/block_structure/transformers.py index aaaf740e7e..3e83295df4 100644 --- a/openedx/core/djangoapps/content/block_structure/transformers.py +++ b/openedx/core/djangoapps/content/block_structure/transformers.py @@ -1,14 +1,15 @@ """ Module for a collection of BlockStructureTransformers. """ +from __future__ import absolute_import + import functools from logging import getLogger -from .exceptions import TransformerException, TransformerDataIncompatible +from .exceptions import TransformerDataIncompatible, TransformerException from .transformer import FilteringTransformerMixin from .transformer_registry import TransformerRegistry - logger = getLogger(__name__) # pylint: disable=C0103