Skip to content

Commit

Permalink
πŸ—“ Sep 2, 2023 6:47:29 PM
Browse files Browse the repository at this point in the history
✨ to/from upside down
✨ zip compress
✨ zip compress symlink
πŸ€– types added/updated
πŸ’š build steps added/updated
  • Loading branch information
securisec committed Sep 2, 2023
1 parent c4c9005 commit 3ca5eda
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 5 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/tests_multi_os.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jobs:
- name: Install test requirements
run: |
pip install sphinx recommonmark pytest==7.4.0 pytest-cov bandit pyperclip
pip install sphinx recommonmark pytest==7.4.0 pytest-cov pyperclip
- name: Test with pytest
run: |
Expand Down Expand Up @@ -92,9 +92,9 @@ jobs:
pytest --disable-pytest-warnings tests_plugins/
python -c "from chepy import Chepy"
- name: Test with bandit
run: |
bandit --recursive chepy/ --ignore-nosec --skip B101,B413,B303,B310,B112,B304,B320,B410,B404,B608,B311,B324
# - name: Test with bandit
# run: |
# bandit --recursive chepy/ --ignore-nosec --skip B101,B413,B303,B310,B112,B304,B320,B410,B404,B608,B311,B324

# - name: Test docs
# if: ${{ !env.ACT }} && contains(matrix.os, 'ubuntu')
Expand Down
2 changes: 1 addition & 1 deletion TODO
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ New ideas:
☐ ✨ whitespace encoding https://www.dcode.fr/whitespace-language
☐ ✨ huffman encode/decode
☐ ✨ new plugin that can help detect encoding type trained on random data
✨ upside down writing
☐ ✨ upside down writing

Bug:

Expand Down
57 changes: 57 additions & 0 deletions chepy/modules/compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import tarfile
import zipfile
import zlib
import stat
from typing import TypeVar
import lazy_import

Expand Down Expand Up @@ -147,6 +148,62 @@ def create_zip_file(self, file_name: str) -> CompressionT:
self.state = mf.getvalue()
return self

@ChepyDecorators.call_stack
def zip_compress(self, file_name: str) -> CompressionT:
"""Compress the state as a zipfile
Args:
filename (str): File name
Returns:
Chepy: The Chepy object.
"""
out = io.BytesIO()
zipInfo = zipfile.ZipInfo(file_name)
with zipfile.ZipFile(out, mode="w") as z:
z.writestr(zipInfo, self._convert_to_bytes())
self.state = out.getvalue()
return self

@ChepyDecorators.call_stack
def zip_compress_symlink(
self: CompressionT, file_name: str, target_file: str
) -> CompressionT:
"""Create a zipfile with a symlink pointer. For unix
Args:
filename (str): File name
target_file (str): Target system file
Returns:
Chepy: The Chepy object.
"""
out = io.BytesIO()
zipInfo = zipfile.ZipInfo(file_name)
zipInfo.create_system = (
3 # System which created ZIP archive, 3 = Unix; 0 = Windows
)
unix_st_mode = (
stat.S_IFLNK
| stat.S_IRUSR
| stat.S_IWUSR
| stat.S_IXUSR
| stat.S_IRGRP
| stat.S_IWGRP
| stat.S_IXGRP
| stat.S_IROTH
| stat.S_IWOTH
| stat.S_IXOTH
)
zipInfo.external_attr = (
unix_st_mode << 16
) # The Python zipfile module accepts the 16-bit "Mode" field (that stores st_mode field from struct stat, containing user/group/other permissions, setuid/setgid and symlink info, etc) of the ASi extra block for Unix as bits 16-31 of the external_attr
zipOut = zipfile.ZipFile(out, "w", compression=zipfile.ZIP_DEFLATED)
zipOut.writestr(zipInfo, target_file)
zipOut.close()
self.state = out.getvalue()
return self

@ChepyDecorators.call_stack
def tar_list_files(self, mode: str = "") -> CompressionT:
"""Get file information inside a tar archive
Expand Down
2 changes: 2 additions & 0 deletions chepy/modules/compression.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Compression(ChepyCore):
def unzip_one(self: CompressionT, file: str, password: str=...) -> CompressionT: ...
def unzip_all(self: CompressionT, password: str=...) -> CompressionT: ...
def create_zip_file(self: CompressionT, file_name: str) -> CompressionT: ...
def zip_compress(self: CompressionT, file_name: str) -> CompressionT: ...
def zip_compress_symlink(self: CompressionT, file_name: str, target_file: str) -> CompressionT: ...
def tar_list_files(self: CompressionT, mode: Literal["gz", "bz2", "xz", ""]=...) -> CompressionT: ...
def tar_extract_one(self: CompressionT, filename: str, mode: Literal["gz", "bz2", "xz", ""]=...) -> CompressionT: ...
def tar_extract_all(self: CompressionT, mode: Literal["gz", "bz2", "xz", ""]=...) -> CompressionT: ...
Expand Down
40 changes: 40 additions & 0 deletions chepy/modules/dataformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pickle
import string
from random import randint
from .internal.constants import Encoding

yaml = lazy_import.lazy_module("yaml")
import regex as re
Expand Down Expand Up @@ -1740,3 +1741,42 @@ def from_bacon(
out += mapping.get(d, "")
self.state = out.encode()
return self

@ChepyDecorators.call_stack
def to_upside_down(self, reverse: bool = False):
"""To upside down
Args:
reverse (bool, optional): Reverse order. Defaults to False.
Returns:
Chepy: The Chepy object.
"""
hold = ""
for s in self._convert_to_str():
hold += Encoding.UPSIDE_DOWN.get(s, s)
if reverse:
self.state = hold[::-1]
else:
self.state = hold
return self

@ChepyDecorators.call_stack
def from_upside_down(self, reverse: bool = False):
"""From upside down
Args:
reverse (bool, optional): Reverse order. Defaults to False.
Returns:
Chepy: The Chepy object.
"""
encoding = {v: k for k, v in Encoding.UPSIDE_DOWN.items()}
hold = ""
for s in self._convert_to_str():
hold += encoding.get(s, s)
if reverse:
self.state = hold[::-1]
else:
self.state = hold
return self
2 changes: 2 additions & 0 deletions chepy/modules/dataformat.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,5 @@ class DataFormat(ChepyCore):
def from_pickle(self: DataFormatT, trust: bool=...) -> DataFormatT: ...
def to_bacon(self: DataFormatT, A: Literal['A', '0']=..., B: Literal['B', '1']=..., complete: bool=..., join_by: bytes=..., invert: bool=...) -> DataFormatT: ...
def from_bacon(self: DataFormatT, A: Literal['A', '0']=..., B: Literal['B', '1']=..., complete: bool=..., split_by: bytes=..., invert: bool=...) -> DataFormatT: ...
def to_upside_down(self: DataFormatT, reverse: bool=False) -> DataFormatT: ...
def from_upside_down(self: DataFormatT, reverse: bool=False) -> DataFormatT: ...
99 changes: 99 additions & 0 deletions chepy/modules/internal/constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,103 @@
class Encoding(object):
UPSIDE_DOWN = {
"a": "ɐ",
"b": "q",
"c": "Ι”",
"d": "p",
"e": "ǝ",
"f": "ɟ",
"g": "Ζƒ",
"h": "Ι₯",
"i": "Δ±",
"j": "ΙΎ",
"k": "ʞ",
"l": "Λ₯",
"m": "Ι―",
"n": "u",
"o": "o",
"p": "d",
"q": "b",
"r": "ΙΉ",
"s": "s",
"t": "Κ‡",
"u": "n",
"v": "ʌ",
"w": "ʍ",
"x": "x",
"y": "ʎ",
"z": "z",
"A": "β±―",
"B": "Q",
"C": "Ζ†",
"D": "P",
"E": "Ǝ",
"F": "ɟ",
"G": "Ζ‚",
"H": "Ɥ",
"I": "I",
"J": "ΙΎ",
"K": "Ʞ",
"L": "Λ₯",
"M": "Ɯ",
"N": "U",
"O": "O",
"P": "D",
"Q": "B",
"R": "ΙΉ",
"S": "S",
"T": "Ʇ",
"U": "N",
"V": "Ι…",
"W": "ʍ",
"X": "X",
"Y": "ʎ",
"Z": "Z",
"0": "0",
"1": "Ζ–",
"2": "α„…",
"3": "Ɛ",
"4": "γ„£",
"5": "Ο›",
"6": "9",
"7": "γ„₯",
"8": "8",
"9": "6",
"!": "Β‘",
"@": "@",
"#": "#",
"$": "$",
"%": "%",
"&": "β…‹",
"*": "*",
"(": ")",
")": "(",
"[": "]",
"]": "[",
"{": "}",
"}": "{",
"<": ">",
">": "<",
"?": "ΒΏ",
"/": "\\",
"\\": "/",
"|": "|",
"_": "_",
"-": "-",
"+": "+",
"=": "=",
":": ":",
";": ";",
",": "'",
"'": ",",
'"': '"',
"`": "`",
"~": "~",
".": ".",
"<": ">",
">": "<",
" ": " ",
}

BACON_26 = {
"A": "aaaaa",
"B": "aaaab",
Expand Down
12 changes: 12 additions & 0 deletions tests/test_compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ def test_create_zip():
)


def test_zip_compress():
c = Chepy("some data").zip_compress("file").o
assert c[:2] == b"PK"
assert b"some data" in c


def test_zip_compress_symlink():
c = Chepy("some data").zip_compress_symlink("file", "target").o
assert c[:2] == b"PK"
assert b"target" in c


def test_gzip_compress():
assert Chepy("A").to_hex().gzip_compress().to_hex().slice(0, 6).o == b"1f8b08"

Expand Down
4 changes: 4 additions & 0 deletions tests/test_dataformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,3 +564,7 @@ def test_bacon():
assert out == b"AABBB AABAA ABABB ABABB ABBBA"
assert Chepy(out).from_bacon().o == b"HELLO"
assert Chepy("hello").to_bacon(A="0", B="1").from_bacon(A="0", B="1").o == b"HELLO"

def test_upside_down():
assert Chepy('hello').to_upside_down().from_upside_down().o == b'heLLo'
assert Chepy('hello').to_upside_down(True).from_upside_down(True).o == b'heLLo'

0 comments on commit 3ca5eda

Please sign in to comment.