From 51bb80c7b55b5f332e8d28fc4846b906c0239859 Mon Sep 17 00:00:00 2001 From: Fabio Caccamo Date: Mon, 14 Oct 2019 16:32:01 +0200 Subject: [PATCH] Added io_util.decode_bytes utility method. --- benedict/utils/io_util.py | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/benedict/utils/io_util.py b/benedict/utils/io_util.py index c73b0d7..c2332ab 100644 --- a/benedict/utils/io_util.py +++ b/benedict/utils/io_util.py @@ -14,37 +14,50 @@ import yaml try: # python 3 - from urllib.parse import unquote_plus as urldecode + from urllib.parse import unquote + from urllib.parse import unquote_plus from urllib.parse import urlencode from urllib.parse import parse_qs except ImportError: # python 2 - from urllib import unquote_plus as urldecode + from urllib import unquote + from urllib import unquote_plus from urllib import urlencode from urlparse import parse_qs def decode_base64(s, **kwargs): # fix urlencoded chars - s = urldecode(s) + s = unquote(s) # fix padding m = len(s) % 4 if m != 0: s += '=' * (4 - m) + b = base64.b64decode(s) encoding = kwargs.pop('encoding', 'utf-8') - data = base64.b64decode(s).decode(encoding) - # decode data if format is specified - decode_format = kwargs.pop('format', 'json') - if decode_format: + format = kwargs.pop('format', None) + if encoding: + data = decode_bytes(b, encoding=encoding, format=format) + else: + data = b + return data + + +def decode_bytes(b, encoding='utf-8', format=None, **kwargs): + s = b.decode(encoding) + # fix trailing null chars + s = s.strip().strip('\x00').strip() + if format: decoders = { 'json': decode_json, 'toml': decode_toml, 'yaml': decode_yaml, 'xml': decode_xml, } - decode_func = decoders.get(decode_format.lower(), None) - if decode_func: - data = decode_func(data, **kwargs) + decoder = decoders[format] + data = decoder(s, **kwargs) + else: + data = s return data