-
-
Notifications
You must be signed in to change notification settings - Fork 4
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 #61 from JarbasHiveMind/release-0.4.0a1
Release 0.4.0a1
- Loading branch information
Showing
21 changed files
with
1,302 additions
and
118 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,58 @@ | ||
name: Run UnitTests | ||
on: | ||
pull_request: | ||
branches: | ||
- dev | ||
paths-ignore: | ||
- 'hivemind_bus_client/version.py' | ||
- '.github/**' | ||
- '.gitignore' | ||
- 'LICENSE' | ||
- 'CHANGELOG.md' | ||
- 'MANIFEST.in' | ||
- 'README.md' | ||
push: | ||
branches: | ||
- master | ||
paths-ignore: | ||
- 'hivemind_bus_client/version.py' | ||
- '.github/**' | ||
- '.gitignore' | ||
- 'LICENSE' | ||
- 'CHANGELOG.md' | ||
- 'MANIFEST.in' | ||
- 'README.md' | ||
workflow_dispatch: | ||
|
||
jobs: | ||
unit_tests: | ||
strategy: | ||
matrix: | ||
python-version: ["3.10", "3.11" ] | ||
runs-on: ubuntu-latest | ||
timeout-minutes: 15 | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Set up python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
- name: Install System Dependencies | ||
run: | | ||
sudo apt-get update | ||
sudo apt install python3-dev swig | ||
python -m pip install build wheel | ||
- name: Install repo | ||
run: | | ||
pip install -e . | ||
- name: Install test dependencies | ||
run: | | ||
pip install -r test/requirements.txt | ||
- name: Run unittests | ||
run: | | ||
pytest --cov=hivemind_bus_client --cov-report xml test | ||
- name: Upload coverage | ||
if: "${{ matrix.python-version == '3.11' }}" | ||
env: | ||
CODECOV_TOKEN: ${{secrets.CODECOV_TOKEN}} | ||
uses: codecov/codecov-action@v2 |
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,3 @@ | ||
from hivemind_bus_client.encodings.z85b import Z85B | ||
from hivemind_bus_client.encodings.z85p import Z85P | ||
from hivemind_bus_client.encodings.b91 import B91 |
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,100 @@ | ||
from typing import Union | ||
|
||
|
||
class B91: | ||
ALPHABET = [ | ||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', | ||
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', | ||
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', | ||
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', | ||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '!', '#', '$', | ||
'%', '&', '(', ')', '*', '+', ',', '.', '/', ':', ';', '<', '=', | ||
'>', '?', '@', '[', ']', '^', '_', '`', '{', '|', '}', '~', '"' | ||
] | ||
|
||
DECODE_TABLE = {char: idx for idx, char in enumerate(ALPHABET)} | ||
|
||
@classmethod | ||
def decode(cls, encoded_data: Union[str, bytes], encoding: str = "utf-8") -> bytes: | ||
""" | ||
Decodes a Base91-encoded string into its original binary form. | ||
Args: | ||
encoded_data (Union[str, bytes]): Base91-encoded input data. If `bytes`, it is decoded as UTF-8. | ||
encoding (str): The encoding to use if `encoded_data` is provided as a string. Default is 'utf-8'. | ||
Returns: | ||
bytes: The decoded binary data. | ||
Raises: | ||
ValueError: If the input contains invalid Base91 characters. | ||
""" | ||
if isinstance(encoded_data, bytes): | ||
encoded_data = encoded_data.decode(encoding) | ||
|
||
v = -1 | ||
b = 0 | ||
n = 0 | ||
out = bytearray() | ||
|
||
for char in encoded_data: | ||
if char not in cls.DECODE_TABLE: | ||
raise ValueError(f"Invalid Base91 character: {char}") | ||
c = cls.DECODE_TABLE[char] | ||
if v < 0: | ||
v = c | ||
else: | ||
v += c * 91 | ||
b |= v << n | ||
n += 13 if (v & 8191) > 88 else 14 | ||
while n >= 8: | ||
out.append(b & 255) | ||
b >>= 8 | ||
n -= 8 | ||
v = -1 | ||
|
||
if v >= 0: | ||
out.append((b | v << n) & 255) | ||
|
||
return bytes(out) | ||
|
||
@classmethod | ||
def encode(cls, data: Union[bytes, str], encoding: str = "utf-8") -> bytes: | ||
""" | ||
Encodes binary data into a Base91-encoded string. | ||
Args: | ||
data (Union[bytes, str]): Input binary data to encode. If `str`, it is encoded as UTF-8. | ||
encoding (str): The encoding to use if `data` is provided as a string. Default is 'utf-8'. | ||
Returns: | ||
str: The Base91-encoded string. | ||
""" | ||
if isinstance(data, str): | ||
data = data.encode(encoding) | ||
|
||
b = 0 | ||
n = 0 | ||
out = [] | ||
|
||
for byte in data: | ||
b |= byte << n | ||
n += 8 | ||
if n > 13: | ||
v = b & 8191 | ||
if v > 88: | ||
b >>= 13 | ||
n -= 13 | ||
else: | ||
v = b & 16383 | ||
b >>= 14 | ||
n -= 14 | ||
out.append(cls.ALPHABET[v % 91]) | ||
out.append(cls.ALPHABET[v // 91]) | ||
|
||
if n: | ||
out.append(cls.ALPHABET[b % 91]) | ||
if n > 7 or b > 90: | ||
out.append(cls.ALPHABET[b // 91]) | ||
|
||
return ''.join(out).encode(encoding) |
Oops, something went wrong.