-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1409 from A-UNDERSCORE-D/fix/1390/Ensure-we-handl…
…e-`413`-from-EDDN-properly Compress outgoing EDDN data if its large
- Loading branch information
Showing
3 changed files
with
121 additions
and
14 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
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,27 @@ | ||
"""Utilities for dealing with text (and byte representations thereof).""" | ||
from __future__ import annotations | ||
|
||
from gzip import compress | ||
|
||
__all__ = ['gzip'] | ||
|
||
|
||
def gzip(data: str | bytes, max_size: int = 512, encoding='utf-8') -> tuple[bytes, bool]: | ||
""" | ||
Compress the given data if the max size is greater than specified. | ||
The default was chosen somewhat arbitrarily, see eddn.py for some more careful | ||
work towards keeping the data almost always compressed | ||
:param data: The data to compress | ||
:param max_size: The max size of data, in bytes, defaults to 512 | ||
:param encoding: The encoding to use if data is a str, defaults to 'utf-8' | ||
:return: the payload to send, and a bool indicating compression state | ||
""" | ||
if isinstance(data, str): | ||
data = data.encode(encoding=encoding) | ||
|
||
if len(data) <= max_size: | ||
return data, False | ||
|
||
return compress(data), True |