[python] Adding initial pypostal bindings for tokenize so we can remove address_normalizer dependency. Not tested on Python 3.
This commit is contained in:
34
python/postal/text/encoding.py
Normal file
34
python/postal/text/encoding.py
Normal file
@@ -0,0 +1,34 @@
|
||||
import six
|
||||
|
||||
text_type = six.text_type
|
||||
string_types = six.string_types
|
||||
binary_type = six.binary_type
|
||||
|
||||
|
||||
def safe_decode(value, encoding='utf-8', errors='strict'):
|
||||
if isinstance(value, text_type):
|
||||
return value
|
||||
|
||||
if isinstance(value, (string_types, binary_type)):
|
||||
return value.decode(encoding, errors)
|
||||
else:
|
||||
return binary_type(value).decode(encoding, errors)
|
||||
|
||||
|
||||
def safe_encode(value, incoming=None, encoding='utf-8', errors='strict'):
|
||||
if not isinstance(value, (string_types, binary_type)):
|
||||
return binary_type(value)
|
||||
|
||||
if isinstance(value, text_type):
|
||||
return value.encode(encoding, errors)
|
||||
else:
|
||||
if hasattr(incoming, 'lower'):
|
||||
incoming = incoming.lower()
|
||||
if hasattr(encoding, 'lower'):
|
||||
encoding = encoding.lower()
|
||||
|
||||
if value and encoding != incoming:
|
||||
value = safe_decode(value, encoding, errors)
|
||||
return value.encode(encoding, errors)
|
||||
else:
|
||||
return value
|
||||
Reference in New Issue
Block a user