zulip: Add hash_util_decode() to decode server encoded URL excerpts.

This adds hash_util_decode() to decode a hash_util_encode() [present in
zulip/zulip's zerver/lib/url_encoding.py [1]] encoded string.

The intent is to facilitate code sharing among various python clients
(primarily, Zulip Terminal).

The string replacement before the `unquote` is to recoup for the custom
string replacements in zulip/zulip's zerver/lib/url_encoding.py [1].

Test added.

[1] See hash_util_encode() in https://github.com/zulip/zulip/blob/master/zerver/lib/url_encoding.py.
This commit is contained in:
Preet Mishra 2020-08-10 22:02:35 +05:30 committed by Tim Abbott
parent e992f14522
commit 9a4abb9f87
2 changed files with 35 additions and 0 deletions

View file

@ -1574,3 +1574,15 @@ class ZulipStream:
def flush(self) -> None:
pass
def hash_util_decode(string: str) -> str:
"""
Returns a decoded string given a hash_util_encode() [present in zulip/zulip's zerver/lib/url_encoding.py] encoded string.
Example usage:
>>> zulip.hash_util_decode('test.20here')
'test here'
"""
# Acknowledge custom string replacements in zulip/zulip's zerver/lib/url_encoding.py before unquoting.
# NOTE: urllib.parse.unquote already does .replace('%2E', '.').
return urllib.parse.unquote(string.replace('.', '%'))