11from __future__ import print_function
22
33import re
4+ import binascii
45import hashlib
56import unicodedata
67import json
@@ -37,13 +38,13 @@ def yes_no_prompt(query, default=None):
3738
3839def chunks (sequence , n ):
3940 """ Yield successive n-sized chunks from sequence. """
40- for i in xrange (0 , len (sequence ), n ):
41+ for i in range (0 , len (sequence ), n ):
4142 yield sequence [i :i + n ]
4243
4344
4445def generate_uid_from_string (value ):
4546 """ Create unique identifier from a string. """
46- return hashlib .sha256 (value ).hexdigest ()
47+ return hashlib .sha256 (value . encode () ).hexdigest ()
4748
4849
4950def slugify (value ):
@@ -56,15 +57,21 @@ def slugify(value):
5657 ---------
5758 https://github.com/django/django/blob/1.7c3/django/utils/text.py#L436
5859 """
59- value = unicodedata .normalize ('NFKD' , unicode (value , "UTF-8" )).encode ('ascii' , 'ignore' ).decode ('ascii' )
60+ try :
61+ value = unicode (value , "UTF-8" )
62+ except NameError :
63+ pass # In Python 3 all strings are already unicode.
64+
65+ value = unicodedata .normalize ('NFKD' , value ).encode ('ascii' , 'ignore' ).decode ('ascii' )
6066 value = re .sub ('[^\w\s-]' , '' , value ).strip ().lower ()
6167 return str (re .sub ('[-\s]+' , '_' , value ))
6268
6369
6470def encode_escaped_characters (text , escaping_character = "\\ " ):
6571 """ Escape the escaped character using its hex representation """
6672 def hexify (match ):
67- return "\\ x{0}" .format (match .group ()[- 1 ].encode ("hex" ))
73+ # Reference: http://stackoverflow.com/questions/18298251/python-hex-values-to-convert-to-a-string-integer
74+ return "\\ x" + binascii .hexlify (match .group ()[- 1 ].encode ()).decode ()
6875
6976 return re .sub (r"\\." , hexify , text )
7077
@@ -75,7 +82,7 @@ def decode_escaped_characters(text):
7582 return ''
7683
7784 def unhexify (match ):
78- return match .group ()[2 :].decode ("hex" )
85+ return binascii . unhexlify ( match .group ()[2 :]) .decode ()
7986
8087 return re .sub (r"\\x.." , unhexify , text )
8188
0 commit comments