22
33import re
44import fcntl
5+ import binascii
56import logging
67import hashlib
78import unicodedata
@@ -40,13 +41,13 @@ def yes_no_prompt(query, default=None):
4041
4142def chunks (sequence , n ):
4243 """ Yield successive n-sized chunks from sequence. """
43- for i in xrange (0 , len (sequence ), n ):
44+ for i in range (0 , len (sequence ), n ):
4445 yield sequence [i :i + n ]
4546
4647
4748def generate_uid_from_string (value ):
4849 """ Create unique identifier from a string. """
49- return hashlib .sha256 (value ).hexdigest ()
50+ return hashlib .sha256 (value . encode () ).hexdigest ()
5051
5152
5253def slugify (value ):
@@ -59,15 +60,21 @@ def slugify(value):
5960 ---------
6061 https://github.com/django/django/blob/1.7c3/django/utils/text.py#L436
6162 """
62- value = unicodedata .normalize ('NFKD' , unicode (value , "UTF-8" )).encode ('ascii' , 'ignore' ).decode ('ascii' )
63+ try :
64+ value = unicode (value , "UTF-8" )
65+ except NameError :
66+ pass # In Python 3 all strings are already unicode.
67+
68+ value = unicodedata .normalize ('NFKD' , value ).encode ('ascii' , 'ignore' ).decode ('ascii' )
6369 value = re .sub ('[^\w\s-]' , '' , value ).strip ().lower ()
6470 return str (re .sub ('[-\s]+' , '_' , value ))
6571
6672
6773def encode_escaped_characters (text , escaping_character = "\\ " ):
6874 """ Escape the escaped character using its hex representation """
6975 def hexify (match ):
70- return "\\ x{0}" .format (match .group ()[- 1 ].encode ("hex" ))
76+ # Reference: http://stackoverflow.com/questions/18298251/python-hex-values-to-convert-to-a-string-integer
77+ return "\\ x" + binascii .hexlify (match .group ()[- 1 ].encode ()).decode ()
7178
7279 return re .sub (r"\\." , hexify , text )
7380
@@ -78,7 +85,7 @@ def decode_escaped_characters(text):
7885 return ''
7986
8087 def unhexify (match ):
81- return match .group ()[2 :].decode ("hex" )
88+ return binascii . unhexlify ( match .group ()[2 :]) .decode ()
8289
8390 return re .sub (r"\\x.." , unhexify , text )
8491
0 commit comments