summaryrefslogtreecommitdiff
path: root/azurelinuxagent/common/utils/textutil.py
diff options
context:
space:
mode:
Diffstat (limited to 'azurelinuxagent/common/utils/textutil.py')
-rw-r--r--azurelinuxagent/common/utils/textutil.py41
1 files changed, 39 insertions, 2 deletions
diff --git a/azurelinuxagent/common/utils/textutil.py b/azurelinuxagent/common/utils/textutil.py
index f03c7e6..59b8fe7 100644
--- a/azurelinuxagent/common/utils/textutil.py
+++ b/azurelinuxagent/common/utils/textutil.py
@@ -251,8 +251,14 @@ def set_ini_config(config, name, val):
def remove_bom(c):
- if str_to_ord(c[0]) > 128 and str_to_ord(c[1]) > 128 and \
- str_to_ord(c[2]) > 128:
+ '''
+ bom is comprised of a sequence of three chars,0xef, 0xbb, 0xbf, in case of utf-8.
+ '''
+ if not is_str_none_or_whitespace(c) and \
+ len(c) > 2 and \
+ str_to_ord(c[0]) > 128 and \
+ str_to_ord(c[1]) > 128 and \
+ str_to_ord(c[2]) > 128:
c = c[3:]
return c
@@ -277,3 +283,34 @@ def b64encode(s):
if PY_VERSION_MAJOR > 2:
return base64.b64encode(bytes(s, 'utf-8')).decode('utf-8')
return base64.b64encode(s)
+
+
+def b64decode(s):
+ from azurelinuxagent.common.version import PY_VERSION_MAJOR
+ if PY_VERSION_MAJOR > 2:
+ return base64.b64decode(s).decode('utf-8')
+ return base64.b64decode(s)
+
+
+def safe_shlex_split(s):
+ import shlex
+ from azurelinuxagent.common.version import PY_VERSION
+ if PY_VERSION[:2] == (2, 6):
+ return shlex.split(s.encode('utf-8'))
+ return shlex.split(s)
+
+
+def parse_json(json_str):
+ """
+ Parse json string and return a resulting dictionary
+ """
+ # trim null and whitespaces
+ result = None
+ if not is_str_none_or_whitespace(json_str):
+ import json
+ result = json.loads(json_str.rstrip(' \t\r\n\0'))
+
+ return result
+
+def is_str_none_or_whitespace(s):
+ return s is None or len(s) == 0 or s.isspace()