INCR-249: Make compatible with Python 3.x without breaking Python 2.7 support (#20528)

* INCR-249: Make compatible with Python 3.x without breaking Python 2.7 support --> openedx/core/djangoapps/content/block_structure

* INCR-249: Grouped six package

* INCR-249: Remove the duplicate lines for absolute_import
This commit is contained in:
Amit
2019-05-13 19:15:05 +03:00
committed by Michael Youngstrom
parent 3a1c68fb30
commit d0810a5c94
17 changed files with 79 additions and 47 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -2,6 +2,8 @@
Configuration for block_structure djangoapp
"""
from __future__ import absolute_import
from django.apps import AppConfig

View File

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

View File

@@ -9,7 +9,6 @@ from openedx.core.lib.cache_utils import request_cached
from .models import BlockStructureConfiguration
# Namespace
WAFFLE_NAMESPACE = u'block_structure'

View File

@@ -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):

View File

@@ -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):

View File

@@ -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__)

View File

@@ -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

View File

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

View File

@@ -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):

View File

@@ -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'],
)

View File

@@ -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

View File

@@ -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')

View File

@@ -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

View File

@@ -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()

View File

@@ -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