Skip to content

Commit 9a4abb9

Browse files
preetmishratimabbott
authored andcommitted
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.
1 parent e992f14 commit 9a4abb9

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

zulip/tests/test_hash_util_decode.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python3
2+
3+
import unittest
4+
import zulip
5+
6+
from unittest import TestCase
7+
8+
class TestHashUtilDecode(TestCase):
9+
def test_hash_util_decode(self) -> None:
10+
tests = [
11+
('topic', 'topic'),
12+
('.2Edot', '.dot'),
13+
('.23stream.20name', '#stream name'),
14+
('(no.20topic)', '(no topic)'),
15+
('.3Cstrong.3Ebold.3C.2Fstrong.3E', '<strong>bold</strong>'),
16+
('.3Asome_emoji.3A', ':some_emoji:'),
17+
]
18+
for encoded_string, decoded_string in tests:
19+
with self.subTest(encoded_string=encoded_string):
20+
self.assertEqual(zulip.hash_util_decode(encoded_string), decoded_string)
21+
22+
if __name__ == '__main__':
23+
unittest.main()

zulip/zulip/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,3 +1574,15 @@ def write(self, content: str) -> None:
15741574

15751575
def flush(self) -> None:
15761576
pass
1577+
1578+
def hash_util_decode(string: str) -> str:
1579+
"""
1580+
Returns a decoded string given a hash_util_encode() [present in zulip/zulip's zerver/lib/url_encoding.py] encoded string.
1581+
1582+
Example usage:
1583+
>>> zulip.hash_util_decode('test.20here')
1584+
'test here'
1585+
"""
1586+
# Acknowledge custom string replacements in zulip/zulip's zerver/lib/url_encoding.py before unquoting.
1587+
# NOTE: urllib.parse.unquote already does .replace('%2E', '.').
1588+
return urllib.parse.unquote(string.replace('.', '%'))

0 commit comments

Comments
 (0)