2121"""
2222from __future__ import unicode_literals
2323
24- from decimal import Decimal
25- from bitarray import bitarray
26- import uuid
27- import regex
2824import collections
2925import copy
26+ import uuid
27+ from decimal import Decimal
28+
29+ import regex
3030import six
31+ from bitarray import bitarray
3132
32- # For Python3
33- if six .PY3 :
34- unicode = str
35- basestring = str
3633# Words that could turn up in YANG definition files that are actually
3734# reserved names in Python, such as being builtin types. This list is
3835# not complete, but will probably continue to grow.
@@ -130,7 +127,7 @@ def RestrictedClassType(*args, **kwargs):
130127 type of restriction placed on the class, and the restriction_arg gives
131128 any input data that this function needs.
132129 """
133- base_type = kwargs .pop ("base_type" , unicode )
130+ base_type = kwargs .pop ("base_type" , six . text_type )
134131 restriction_type = kwargs .pop ("restriction_type" , None )
135132 restriction_arg = kwargs .pop ("restriction_arg" , None )
136133 restriction_dict = kwargs .pop ("restriction_dict" , None )
@@ -140,7 +137,7 @@ def RestrictedClassType(*args, **kwargs):
140137 # it must be a list since a restricted class can encapsulate a restricted
141138 # class
142139 current_restricted_class_type = regex .sub ("<(type|class) '(?P<class>.*)'>" ,
143- "\g<class>" , str (base_type ))
140+ "\g<class>" , six . text_type (base_type ))
144141 if hasattr (base_type , "_restricted_class_base" ):
145142 restricted_class_hint = getattr (base_type , "_restricted_class_base" )
146143 restricted_class_hint .append (current_restricted_class_type )
@@ -252,15 +249,15 @@ def range_check(value):
252249
253250 def match_pattern_check (regexp ):
254251 def mp_check (value ):
255- if not isinstance (value , basestring ):
252+ if not isinstance (value , six . string_types + ( six . text_type ,) ):
256253 return False
257254 if regex .match (convert_regexp (regexp ), value ):
258255 return True
259256 return False
260257 return mp_check
261258
262259 def in_dictionary_check (dictionary ):
263- return lambda i : unicode (i ) in dictionary
260+ return lambda i : six . text_type (i ) in dictionary
264261
265262 val = False
266263 try :
@@ -365,7 +362,7 @@ def TypedListType(*args, **kwargs):
365362 certain types (specified by allowed_type kwarg to the function)
366363 can be added to the list.
367364 """
368- allowed_type = kwargs .pop ("allowed_type" , unicode )
365+ allowed_type = kwargs .pop ("allowed_type" , six . text_type )
369366 if not isinstance (allowed_type , list ):
370367 allowed_type = [allowed_type ]
371368
@@ -409,11 +406,11 @@ def check(self, v):
409406 tmp = i (v )
410407 passed = True
411408 break
412- elif i == unicode and isinstance (v , str ):
413- tmp = unicode (v )
409+ elif i == six . text_type and isinstance (v , six . string_types + ( six . text_type ,) ):
410+ tmp = six . text_type (v )
414411 passed = True
415412 break
416- elif i not in [ unicode , str ] :
413+ elif i not in six . string_types + ( six . text_type ,) :
417414 # for anything other than string we try
418415 # and cast. Using things for string or
419416 # unicode gives us strange results because we get
@@ -501,7 +498,7 @@ def YANGListType(*args, **kwargs):
501498 extensions = kwargs .pop ("extensions" , None )
502499
503500 class YANGList (object ):
504- __slots__ = ('_pybind_generated_by' , ' _members' , '_keyval' ,
501+ __slots__ = ('_members' , '_keyval' ,
505502 '_contained_class' , '_path_helper' , '_yang_keys' ,
506503 '_ordered' ,)
507504 _pybind_generated_by = "YANGListType"
@@ -584,7 +581,7 @@ def __set(self, *args, **kwargs):
584581 # this is a list that does not have a key specified, and hence
585582 # we generate a uuid that is used as the key, the method then
586583 # returns the uuid for the upstream process to use
587- k = str (uuid .uuid1 ())
584+ k = six . text_type (uuid .uuid1 ())
588585
589586 update = False
590587 if v is not None :
@@ -714,12 +711,12 @@ def _generate_key(self, *args, **kwargs):
714711 def _extract_key (self , obj ):
715712 kp = self ._keyval .split (" " )
716713 if len (kp ) > 1 :
717- ks = unicode ()
714+ ks = ''
718715 for k in kp :
719716 kv = getattr (obj , "_get_%s" % safe_name (k ), None )
720717 if kv is None :
721718 raise KeyError ("Invalid key attribute specified for object" )
722- ks += "%s " % unicode (kv ())
719+ ks += "%s " % six . text_type (kv ())
723720 return ks .rstrip (" " )
724721 else :
725722 kv = getattr (obj , "_get_%s" % safe_name (self ._keyval ), None )
@@ -918,7 +915,7 @@ def YANGDynClass(*args, **kwargs):
918915 clsslots = ['_default' , '_mchanged' , '_yang_name' , '_choice' , '_parent' ,
919916 '_supplied_register_path' , '_path_helper' , '_base_type' ,
920917 '_is_leaf' , '_is_container' , '_extensionsd' ,
921- '_pybind_base_class' , ' _extmethods' , '_is_keyval' ,
918+ '_extmethods' , '_is_keyval' ,
922919 '_register_paths' , '_namespace' , '_yang_type' ,
923920 '_defining_module' , '_metadata' , '_is_config' , '_cpresent' ,
924921 '_presence' ]
@@ -1176,7 +1173,7 @@ def __init__(self, *args, **kwargs):
11761173 self ._ptr = False
11771174 self ._require_instance = require_instance
11781175 self ._type = "unicode"
1179- self ._utype = unicode
1176+ self ._utype = six . text_type
11801177
11811178 if len (args ):
11821179 value = args [0 ]
@@ -1228,15 +1225,15 @@ def __init__(self, *args, **kwargs):
12281225 if len (path_chk ) == 1 and is_yang_leaflist (path_chk [0 ]):
12291226 index = 0
12301227 for i in path_chk [0 ]:
1231- if unicode (i ) == unicode (value ):
1228+ if six . text_type (i ) == six . text_type (value ):
12321229 found = True
12331230 self ._referenced_object = path_chk [0 ][index ]
12341231 break
12351232 index += 1
12361233 else :
12371234 found = False
12381235 for i in path_chk :
1239- if unicode (i ) == unicode (value ):
1236+ if six . text_type (i ) == six . text_type (value ):
12401237 found = True
12411238 self ._referenced_object = i
12421239
0 commit comments