aboutsummaryrefslogtreecommitdiffstats
path: root/netlib/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'netlib/utils.py')
-rw-r--r--netlib/utils.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/netlib/utils.py b/netlib/utils.py
index dda76808..be2701a0 100644
--- a/netlib/utils.py
+++ b/netlib/utils.py
@@ -330,6 +330,8 @@ def unparse_url(scheme, host, port, path=""):
Args:
All args must be str.
"""
+ if path == "*":
+ path = ""
return "%s://%s%s" % (scheme, hostport(scheme, host, port), path)
@@ -429,3 +431,31 @@ def safe_subn(pattern, repl, target, *args, **kwargs):
need a better solution that is aware of the actual content ecoding.
"""
return re.subn(str(pattern), str(repl), target, *args, **kwargs)
+
+
+def bytes_to_escaped_str(data):
+ """
+ Take bytes and return a safe string that can be displayed to the user.
+ """
+ # TODO: We may want to support multi-byte characters without escaping them.
+ # One way to do would be calling .decode("utf8", "backslashreplace") first
+ # and then escaping UTF8 control chars (see clean_bin).
+
+ if not isinstance(data, bytes):
+ raise ValueError("data must be bytes")
+ return repr(data).lstrip("b")[1:-1]
+
+
+def escaped_str_to_bytes(data):
+ """
+ Take an escaped string and return the unescaped bytes equivalent.
+ """
+ if not isinstance(data, str):
+ raise ValueError("data must be str")
+
+ if six.PY2:
+ return data.decode("string-escape")
+
+ # This one is difficult - we use an undocumented Python API here
+ # as per http://stackoverflow.com/a/23151714/934719
+ return codecs.escape_decode(data)[0]