summaryrefslogtreecommitdiff
path: root/cloudinit/util.py
diff options
context:
space:
mode:
authorBarry Warsaw <barry@python.org>2015-01-27 15:03:52 -0500
committerBarry Warsaw <barry@python.org>2015-01-27 15:03:52 -0500
commit6e742d20e9ed56498925c7c850cd5da65d063b4b (patch)
tree3e584ae2381d72d2b77c6f2d98c9be93bcb9e413 /cloudinit/util.py
parent69c64029997599b3f1764ef48fe571094e2ee5f2 (diff)
downloadvyos-cloud-init-6e742d20e9ed56498925c7c850cd5da65d063b4b.tar.gz
vyos-cloud-init-6e742d20e9ed56498925c7c850cd5da65d063b4b.zip
Respond to review:
- Refactor both the base64 encoding and decoding into utility functions. Also: - Mechanically fix some other broken untested code.
Diffstat (limited to 'cloudinit/util.py')
-rw-r--r--cloudinit/util.py20
1 files changed, 20 insertions, 0 deletions
diff --git a/cloudinit/util.py b/cloudinit/util.py
index 766f8e32..8916cc11 100644
--- a/cloudinit/util.py
+++ b/cloudinit/util.py
@@ -44,6 +44,7 @@ import sys
import tempfile
import time
+from base64 import b64decode, b64encode
from six.moves.urllib import parse as urlparse
import six
@@ -90,6 +91,25 @@ def encode_text(text, encoding='utf-8'):
return text
return text.encode(encoding)
+
+def b64d(source):
+ # Base64 decode some data, accepting bytes or unicode/str, and returning
+ # str/unicode if the result is utf-8 compatible, otherwise returning bytes.
+ decoded = b64decode(source)
+ if isinstance(decoded, bytes):
+ try:
+ return decoded.decode('utf-8')
+ except UnicodeDecodeError:
+ return decoded
+
+def b64e(source):
+ # Base64 encode some data, accepting bytes or unicode/str, and returning
+ # str/unicode if the result is utf-8 compatible, otherwise returning bytes.
+ if not isinstance(source, bytes):
+ source = source.encode('utf-8')
+ return b64encode(source).decode('utf-8')
+
+
# Path for DMI Data
DMI_SYS_PATH = "/sys/class/dmi/id"