Commit 05712da6 by Christina Roberts

Merge pull request #744 from edx/dhm/pep_pylint_cleanups

Add doc strings and fix spaces
parents 4129c1cf 20641bab
...@@ -10,6 +10,7 @@ from xmodule.modulestore.locator import BlockUsageLocator ...@@ -10,6 +10,7 @@ from xmodule.modulestore.locator import BlockUsageLocator
from xmodule.modulestore.mongo import draft from xmodule.modulestore.mongo import draft
from xmodule.modulestore import Location from xmodule.modulestore import Location
class LocMapperStore(object): class LocMapperStore(object):
''' '''
This store persists mappings among the addressing schemes. At this time, it's between the old i4x Location This store persists mappings among the addressing schemes. At this time, it's between the old i4x Location
...@@ -25,23 +26,27 @@ class LocMapperStore(object): ...@@ -25,23 +26,27 @@ class LocMapperStore(object):
or dominant store, but that's not a requirement. This store creates its own connection. or dominant store, but that's not a requirement. This store creates its own connection.
''' '''
# C0103: varnames and attrs must be >= 3 chars, but db defined by long time usage
# pylint: disable = C0103
def __init__(self, host, db, collection, port=27017, user=None, password=None, **kwargs): def __init__(self, host, db, collection, port=27017, user=None, password=None, **kwargs):
''' '''
Constructor Constructor
''' '''
self.db = pymongo.database.Database(pymongo.MongoClient( self.db = pymongo.database.Database(
pymongo.MongoClient(
host=host, host=host,
port=port, port=port,
tz_aware=True, tz_aware=True,
**kwargs **kwargs
), db) ),
db
)
if user is not None and password is not None: if user is not None and password is not None:
self.db.authenticate(user, password) self.db.authenticate(user, password)
self.location_map = self.db[collection + '.location_map'] self.location_map = self.db[collection + '.location_map']
self.location_map.write_concern = {'w': 1} self.location_map.write_concern = {'w': 1}
# location_map functions # location_map functions
def create_map_entry(self, course_location, course_id=None, draft_branch='draft', prod_branch='published', def create_map_entry(self, course_location, course_id=None, draft_branch='draft', prod_branch='published',
block_map=None): block_map=None):
...@@ -96,7 +101,6 @@ class LocMapperStore(object): ...@@ -96,7 +101,6 @@ class LocMapperStore(object):
'block_map': block_map or {}, 'block_map': block_map or {},
}) })
def translate_location(self, old_style_course_id, location, published=True, add_entry_if_missing=True): def translate_location(self, old_style_course_id, location, published=True, add_entry_if_missing=True):
""" """
Translate the given module location to a Locator. If the mapping has the run id in it, then you Translate the given module location to a Locator. If the mapping has the run id in it, then you
...@@ -202,7 +206,6 @@ class LocMapperStore(object): ...@@ -202,7 +206,6 @@ class LocMapperStore(object):
revision) revision)
return None return None
def add_block_location_translator(self, location, old_course_id=None, usage_id=None): def add_block_location_translator(self, location, old_course_id=None, usage_id=None):
""" """
Similar to translate_location which adds an entry if none is found, but this cannot create a new Similar to translate_location which adds an entry if none is found, but this cannot create a new
...@@ -269,7 +272,6 @@ class LocMapperStore(object): ...@@ -269,7 +272,6 @@ class LocMapperStore(object):
return computed_usage_id return computed_usage_id
def update_block_location_translator(self, location, usage_id, old_course_id=None, autogenerated_usage_id=False): def update_block_location_translator(self, location, usage_id, old_course_id=None, autogenerated_usage_id=False):
""" """
Update all existing maps from location's block to the new usage_id. Used for changing the usage_id, Update all existing maps from location's block to the new usage_id. Used for changing the usage_id,
...@@ -307,7 +309,6 @@ class LocMapperStore(object): ...@@ -307,7 +309,6 @@ class LocMapperStore(object):
return usage_id return usage_id
def delete_block_location_translator(self, location, old_course_id=None): def delete_block_location_translator(self, location, old_course_id=None):
""" """
Remove all existing maps from location's block. Remove all existing maps from location's block.
...@@ -349,8 +350,8 @@ class LocMapperStore(object): ...@@ -349,8 +350,8 @@ class LocMapperStore(object):
""" """
if course_id: if course_id:
# re doesn't allow ?P<_id.org> and ilk # re doesn't allow ?P<_id.org> and ilk
m = re.match(r'([^/]+)/([^/]+)/([^/]+)', course_id) matched = re.match(r'([^/]+)/([^/]+)/([^/]+)', course_id)
return dict(zip(['_id.org', '_id.course', '_id.name'], m.groups())) return dict(zip(['_id.org', '_id.course', '_id.name'], matched.groups()))
location_id = {'_id.org': location.org, '_id.course': location.course} location_id = {'_id.org': location.org, '_id.course': location.course}
if location.category == 'course': if location.category == 'course':
...@@ -358,6 +359,9 @@ class LocMapperStore(object): ...@@ -358,6 +359,9 @@ class LocMapperStore(object):
return location_id return location_id
def _block_id_is_guid(self, name): def _block_id_is_guid(self, name):
"""
Does the given name look like it's a guid?
"""
return len(name) == 32 and re.search(r'[^0-9A-Fa-f]', name) is None return len(name) == 32 and re.search(r'[^0-9A-Fa-f]', name) is None
def _verify_uniqueness(self, name, block_map): def _verify_uniqueness(self, name, block_map):
......
...@@ -12,6 +12,9 @@ from xmodule.modulestore.loc_mapper_store import LocMapperStore ...@@ -12,6 +12,9 @@ from xmodule.modulestore.loc_mapper_store import LocMapperStore
class TestLocationMapper(unittest.TestCase): class TestLocationMapper(unittest.TestCase):
"""
Test the location to locator mapper
"""
def setUp(self): def setUp(self):
modulestore_options = { modulestore_options = {
...@@ -23,11 +26,10 @@ class TestLocationMapper(unittest.TestCase): ...@@ -23,11 +26,10 @@ class TestLocationMapper(unittest.TestCase):
# pylint: disable=W0142 # pylint: disable=W0142
TestLocationMapper.loc_store = LocMapperStore(**modulestore_options) TestLocationMapper.loc_store = LocMapperStore(**modulestore_options)
def tearDown(self): def tearDown(self):
db = TestLocationMapper.loc_store.db dbref = TestLocationMapper.loc_store.db
db.drop_collection(TestLocationMapper.loc_store.location_map) dbref.drop_collection(TestLocationMapper.loc_store.location_map)
db.connection.close() dbref.connection.close()
TestLocationMapper.loc_store = None TestLocationMapper.loc_store = None
def test_create_map(self): def test_create_map(self):
...@@ -438,7 +440,6 @@ class TestLocationMapper(unittest.TestCase): ...@@ -438,7 +440,6 @@ class TestLocationMapper(unittest.TestCase):
) )
self.assertEqual(trans_loc.name, '1') self.assertEqual(trans_loc.name, '1')
def test_delete_block(self): def test_delete_block(self):
""" """
test delete_block_location_translator(location, old_course_id=None) test delete_block_location_translator(location, old_course_id=None)
...@@ -490,10 +491,17 @@ class TestLocationMapper(unittest.TestCase): ...@@ -490,10 +491,17 @@ class TestLocationMapper(unittest.TestCase):
) )
self.assertEqual(locator.usage_id, 'problem3') self.assertEqual(locator.usage_id, 'problem3')
#================================== #==================================
# functions to mock existing services # functions to mock existing services
def loc_mapper(): def loc_mapper():
"""
Mocks the global location mapper.
"""
return TestLocationMapper.loc_store return TestLocationMapper.loc_store
def render_to_template_mock(*_args): def render_to_template_mock(*_args):
pass """
Mocks the mako render_to_template w/ a noop
"""
...@@ -252,12 +252,18 @@ class LocatorTest(TestCase): ...@@ -252,12 +252,18 @@ class LocatorTest(TestCase):
def check_course_locn_fields(self, testobj, msg, version_guid=None, def check_course_locn_fields(self, testobj, msg, version_guid=None,
course_id=None, branch=None): course_id=None, branch=None):
"""
Checks the version, course_id, and branch in testobj
"""
self.assertEqual(testobj.version_guid, version_guid, msg) self.assertEqual(testobj.version_guid, version_guid, msg)
self.assertEqual(testobj.course_id, course_id, msg) self.assertEqual(testobj.course_id, course_id, msg)
self.assertEqual(testobj.branch, branch, msg) self.assertEqual(testobj.branch, branch, msg)
def check_block_locn_fields(self, testobj, msg, version_guid=None, def check_block_locn_fields(self, testobj, msg, version_guid=None,
course_id=None, branch=None, block=None): course_id=None, branch=None, block=None):
"""
Does adds a block id check over and above the check_course_locn_fields tests
"""
self.check_course_locn_fields(testobj, msg, version_guid, course_id, self.check_course_locn_fields(testobj, msg, version_guid, course_id,
branch) branch)
self.assertEqual(testobj.usage_id, block) self.assertEqual(testobj.usage_id, block)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment