9
9
import re
10
10
import warnings
11
11
from abc import abstractproperty
12
+ from six import string_types , text_type
12
13
13
14
from bson .errors import InvalidId
14
15
from bson .objectid import ObjectId
@@ -45,12 +46,6 @@ class Locator(OpaqueKey):
45
46
ALLOWED_ID_CHARS = r'[\w\-~.:]'
46
47
DEPRECATED_ALLOWED_ID_CHARS = r'[\w\-~.:%]'
47
48
48
- def __str__ (self ):
49
- """
50
- str(self) returns something like this: "mit.eecs.6002x"
51
- """
52
- return unicode (self ).encode ('utf-8' )
53
-
54
49
@abstractproperty
55
50
def version (self ): # pragma: no cover
56
51
"""
@@ -159,9 +154,6 @@ class CourseLocator(BlockLocatorBase, CourseKey): # pylint: disable=abstract-m
159
154
# Characters that are forbidden in the deprecated format
160
155
INVALID_CHARS_DEPRECATED = re .compile (r"[^\w.%-]" , re .UNICODE )
161
156
162
- # stubs to fake out the abstractproperty class instrospection and allow treatment as attrs in instances
163
- org = None
164
-
165
157
def __init__ (self , org = None , course = None , run = None , branch = None , version_guid = None , deprecated = False , ** kwargs ):
166
158
"""
167
159
Construct a CourseLocator
@@ -223,7 +215,7 @@ def __init__(self, org=None, course=None, run=None, branch=None, version_guid=No
223
215
def _check_location_part (cls , val , regexp ): # pylint: disable=missing-docstring
224
216
if val is None :
225
217
return
226
- if not isinstance (val , basestring ):
218
+ if not isinstance (val , string_types ):
227
219
raise InvalidKeyError (cls , "{!r} is not a string" .format (val ))
228
220
if regexp .search (val ) is not None :
229
221
raise InvalidKeyError (cls , "Invalid characters in {!r}." .format (val ))
@@ -278,7 +270,7 @@ def html_id(self):
278
270
place, but I have no way to override. We should clearly define the purpose and restrictions of this
279
271
(e.g., I'm assuming periods are fine).
280
272
"""
281
- return unicode (self )
273
+ return text_type (self )
282
274
283
275
def make_usage_key (self , block_type , block_id ):
284
276
return BlockUsageLocator (
@@ -367,7 +359,7 @@ def to_deprecated_string(self):
367
359
DeprecationWarning ,
368
360
stacklevel = 2
369
361
)
370
- return unicode (self )
362
+ return text_type (self )
371
363
372
364
@classmethod
373
365
def _from_deprecated_string (cls , serialized ):
@@ -421,12 +413,6 @@ class LibraryLocator(BlockLocatorBase, CourseKey):
421
413
__slots__ = KEY_FIELDS
422
414
CHECKED_INIT = False
423
415
424
- # declare our fields explicitly to avoid pylint warnings
425
- org = None
426
- library = None
427
- branch = None
428
- version_guid = None
429
-
430
416
def __init__ (self , org = None , library = None , branch = None , version_guid = None , ** kwargs ):
431
417
"""
432
418
Construct a LibraryLocator
@@ -472,7 +458,7 @@ def __init__(self, org=None, library=None, branch=None, version_guid=None, **kwa
472
458
** kwargs
473
459
)
474
460
475
- if self .version_guid is None and (self .org is None or self .library is None ):
461
+ if self .version_guid is None and (self .org is None or self .library is None ): # pylint: disable=no-member
476
462
raise InvalidKeyError (self .__class__ , "Either version_guid or org and library should be set" )
477
463
478
464
@property
@@ -489,7 +475,7 @@ def course(self):
489
475
Deprecated. Return a 'course' for compatibility with CourseLocator.
490
476
"""
491
477
warnings .warn ("Accessing 'course' on a LibraryLocator is deprecated." , DeprecationWarning , stacklevel = 2 )
492
- return self .library
478
+ return self .library # pylint: disable=no-member
493
479
494
480
@property
495
481
def version (self ):
@@ -502,7 +488,7 @@ def version(self):
502
488
DeprecationWarning ,
503
489
stacklevel = 2
504
490
)
505
- return self .version_guid
491
+ return self .version_guid # pylint: disable=no-member
506
492
507
493
@classmethod
508
494
def _from_string (cls , serialized ):
@@ -526,7 +512,7 @@ def html_id(self):
526
512
"""
527
513
Return an id which can be used on an html page as an id attr of an html element.
528
514
"""
529
- return unicode (self )
515
+ return text_type (self )
530
516
531
517
def make_usage_key (self , block_type , block_id ):
532
518
return LibraryUsageLocator (
@@ -579,12 +565,12 @@ def _to_string(self):
579
565
Return a string representing this location.
580
566
"""
581
567
parts = []
582
- if self .library :
568
+ if self .library : # pylint: disable=no-member
583
569
parts .extend ([self .org , self .course ])
584
- if self .branch :
585
- parts .append (u"{prefix}@{branch}" .format (prefix = self .BRANCH_PREFIX , branch = self .branch ))
586
- if self .version_guid :
587
- parts .append (u"{prefix}@{guid}" .format (prefix = self .VERSION_PREFIX , guid = self .version_guid ))
570
+ if self .branch : # pylint: disable=no-member
571
+ parts .append (u"{prefix}@{branch}" .format (prefix = self .BRANCH_PREFIX , branch = self .branch )) # pylint: disable=no-member
572
+ if self .version_guid : # pylint: disable=no-member
573
+ parts .append (u"{prefix}@{guid}" .format (prefix = self .VERSION_PREFIX , guid = self .version_guid )) # pylint: disable=no-member
588
574
return u"+" .join (parts )
589
575
590
576
def _to_deprecated_string (self ):
@@ -977,7 +963,7 @@ def to_deprecated_string(self):
977
963
DeprecationWarning ,
978
964
stacklevel = 2
979
965
)
980
- return unicode (self )
966
+ return text_type (self )
981
967
982
968
@classmethod
983
969
def _from_deprecated_string (cls , serialized ):
@@ -1176,7 +1162,7 @@ class DefinitionLocator(Locator, DefinitionKey):
1176
1162
definition_id = None
1177
1163
1178
1164
def __init__ (self , block_type , definition_id , deprecated = False ): # pylint: disable=unused-argument
1179
- if isinstance (definition_id , basestring ):
1165
+ if isinstance (definition_id , string_types ):
1180
1166
try :
1181
1167
definition_id = self .as_object_id (definition_id )
1182
1168
except ValueError :
@@ -1188,7 +1174,7 @@ def _to_string(self):
1188
1174
Return a string representing this location.
1189
1175
unicode(self) returns something like this: "519665f6223ebd6980884f2b+type+problem"
1190
1176
"""
1191
- return u"{}+{}@{}" .format (unicode (self .definition_id ), self .BLOCK_TYPE_PREFIX , self .block_type )
1177
+ return u"{}+{}@{}" .format (text_type (self .definition_id ), self .BLOCK_TYPE_PREFIX , self .block_type )
1192
1178
1193
1179
URL_RE = re .compile (
1194
1180
r"^(?P<definition_id>[A-F0-9]+)\+{}@(?P<block_type>{ALLOWED_ID_CHARS}+)$" .format (
@@ -1298,7 +1284,7 @@ def to_deprecated_string(self):
1298
1284
DeprecationWarning ,
1299
1285
stacklevel = 2
1300
1286
)
1301
- return unicode (self )
1287
+ return text_type (self )
1302
1288
1303
1289
@property
1304
1290
def tag (self ):
0 commit comments