new base64 helper functions

This commit is contained in:
Mike McLean 2019-03-02 13:27:37 -05:00
parent c41a06b88f
commit 71bc4106b4
2 changed files with 65 additions and 0 deletions

View file

@ -20,6 +20,7 @@
from __future__ import absolute_import
from __future__ import division
import base64
import calendar
import datetime
import hashlib
@ -142,6 +143,42 @@ def printList(l):
return ret
def base64encode(s, as_bytes=False):
"""Helper function to encode string or bytes as base64
This function returns a string unless as_bytes is True
"""
if six.PY2:
return base64.b64encode(s)
if isinstance(s, str):
s = s.encode('utf8')
data = base64.b64encode(s)
if as_bytes:
return data
else:
# ascii is always good enough for base64 encoded data
return data.decode('ascii')
# We don't need a decode wrapper, but we define this for naming consistency
base64decode = base64.b64decode
def decode_bytes(data, fallback='iso8859-15'):
"""Decode a bytes-like object that is expected to be a valid string
First utf8 is tried, then the fallback (defaults to iso8859-15).
The fallback behavior can be disabled by setting the option to None.
"""
try:
return data.decode('utf8')
except UnicodeDecodeError:
if fallback:
return data.decode(fallback)
raise
def multi_fnmatch(s, patterns):
"""Returns true if s matches any pattern in the list