-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bba233a
commit 4ee36e5
Showing
4 changed files
with
35 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import base64 | ||
import quopri | ||
import re | ||
|
||
|
||
class QuoPriEncoding: | ||
@staticmethod | ||
def decode(encoded_text): | ||
encoded_word_regex = r'=\?{1}(.+)\?{1}([B|Q|b|q])\?{1}(.+)\?{1}=' | ||
matches = re.match(encoded_word_regex, encoded_text) | ||
if matches is None: | ||
return encoded_text | ||
charset, encoding, encoded_text = matches.groups() | ||
if encoding.upper() == 'B': | ||
byte_string = base64.b64decode(encoded_text) | ||
elif encoding.upper() == 'Q': | ||
byte_string = quopri.decodestring(encoded_text) | ||
return byte_string.decode(charset) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from assertpy import assert_that | ||
|
||
from attachment_downloader.encoding import QuoPriEncoding | ||
|
||
|
||
class TestQuoPriEncoding: | ||
def test_decode(self): | ||
assert_that(QuoPriEncoding.decode('=?utf-8?b?2LPZhNin2YU=?=')).is_equal_to('سلام') | ||
assert_that(QuoPriEncoding.decode('=?UTF-8?B?2LPZhNin2YU=?=')).is_equal_to('سلام') | ||
assert_that(QuoPriEncoding.decode('Hello')).is_equal_to('Hello') |