Skip to content

Commit

Permalink
rel 2024.1
Browse files Browse the repository at this point in the history
  • Loading branch information
FredHappyface committed Mar 17, 2024
1 parent 6d2b091 commit a42632c
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 46 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ patch-level version changes can be found in [commit messages](../../commits/mast
- use ruff
- code quality improvements
- fix 'Can't download packs with ":" (Colon) in it' - https://github.com/FHPythonUtils/SigStickers/issues/3
- 98% test coverage

## 2024 - 2024/01/07

Expand Down
4 changes: 2 additions & 2 deletions documentation/reference/sigstickers/caching.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ Get the appropriate cache verification function based on version.
#### Returns

-------
- `callable` - Cache verification function
Callable[[dict[str, Any]], bool]: Cache verification function

#### Signature

```python
def _get_verify_function(version: int): ...
def _get_verify_function(version: int) -> Callable[[dict[str, Any]], bool]: ...
```


Expand Down
14 changes: 9 additions & 5 deletions documentation/reference/sigstickers/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

## cli

[Show source in cli.py:12](../../../sigstickers/cli.py#L12)
[Show source in cli.py:17](../../../sigstickers/cli.py#L17)

CLI entry point.

Expand All @@ -24,12 +24,16 @@ def cli() -> None: ...

## main

[Show source in cli.py:35](../../../sigstickers/cli.py#L35)
[Show source in cli.py:40](../../../sigstickers/cli.py#L40)

Main function to download sticker packs.
Download, and convert sticker packs.

#### Signature

```python
def main(packs): ...
```
def main(packs: list[str], cwd: Path = DEFAULT_CWD) -> int: ...
```

#### See also

- [DEFAULT_CWD](./downloader.md#default_cwd)
27 changes: 9 additions & 18 deletions documentation/reference/sigstickers/downloader.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@
- [convert_pack](#convert_pack)
- [convert_with_pil](#convert_with_pil)
- [download_pack](#download_pack)
- [files_to_str](#files_to_str)
- [save_sticker](#save_sticker)

## assure_dir_exists

[Show source in downloader.py:21](../../../sigstickers/downloader.py#L21)
[Show source in downloader.py:22](../../../sigstickers/downloader.py#L22)

Make the directory if it does not exist.

Expand All @@ -38,7 +37,7 @@ def assure_dir_exists(*parts: Path | str) -> Path: ...

## convert_pack

[Show source in downloader.py:141](../../../sigstickers/downloader.py#L141)
[Show source in downloader.py:139](../../../sigstickers/downloader.py#L139)

Convert the webp images into png and gif images.

Expand All @@ -52,14 +51,14 @@ Convert the webp images into png and gif images.
#### Signature

```python
async def convert_pack(swd: Path, pack_name: Path, no_cache=False): ...
async def convert_pack(swd: Path, pack_name: Path, no_cache: bool = False) -> None: ...
```



## convert_with_pil

[Show source in downloader.py:107](../../../sigstickers/downloader.py#L107)
[Show source in downloader.py:105](../../../sigstickers/downloader.py#L105)

Convert the webp file to png.

Expand All @@ -83,7 +82,7 @@ def convert_with_pil(input_path: Path) -> list[str]: ...

## download_pack

[Show source in downloader.py:67](../../../sigstickers/downloader.py#L67)
[Show source in downloader.py:65](../../../sigstickers/downloader.py#L65)

Download a sticker pack.

Expand All @@ -104,27 +103,19 @@ Download a sticker pack.

```python
async def download_pack(
pack_id: str, pack_key: str, cwd: Path = Path.cwd()
pack_id: str, pack_key: str, cwd: Path = DEFAULT_CWD
) -> tuple[Path, Path]: ...
```

#### See also


## files_to_str

[Show source in downloader.py:186](../../../sigstickers/downloader.py#L186)

#### Signature

```python
def files_to_str(files: list[Path]) -> list[str]: ...
```
- [DEFAULT_CWD](#default_cwd)



## save_sticker

[Show source in downloader.py:38](../../../sigstickers/downloader.py#L38)
[Show source in downloader.py:39](../../../sigstickers/downloader.py#L39)

Save a sticker.

Expand Down
5 changes: 2 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "sigstickers"
version = "2024"
version = "2024.1"
license = "mit"
description = "Download sticker packs from Signal"
authors = ["FredHappyface"]
Expand Down Expand Up @@ -37,8 +37,6 @@ handsdown = "^2.1.0"
coverage = "^7.4.4"
pytest-asyncio = "^0.23.5.post1"



[tool.ruff]
line-length = 100
indent-width = 4
Expand Down Expand Up @@ -72,6 +70,7 @@ line-ending = "lf"
[tool.pyright]
venvPath = "."
venv = ".venv"

[tool.tox]
legacy_tox_ini = """
[tox]
Expand Down
6 changes: 3 additions & 3 deletions sigstickers/caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import json
from pathlib import Path
from typing import Any
from typing import Any, Callable

from loguru import logger

Expand Down Expand Up @@ -93,7 +93,7 @@ def create_converted(pack_name: Path, data: dict) -> None:
cache.write_text(json.dumps(data), encoding="utf-8")


def _get_verify_function(version: int):
def _get_verify_function(version: int) -> Callable[[dict[str, Any]], bool]:
"""Get the appropriate cache verification function based on version.
Args:
Expand All @@ -102,7 +102,7 @@ def _get_verify_function(version: int):
Returns:
-------
callable: Cache verification function
Callable[[dict[str, Any]], bool]: Cache verification function
"""
return {
Expand Down
16 changes: 9 additions & 7 deletions sigstickers/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
import asyncio
import functools
import operator
from pathlib import Path
from sys import exit as sysexit
from urllib import parse

from loguru import logger

from sigstickers.downloader import convert_pack, download_pack
from sigstickers.downloader import DEFAULT_CWD, convert_pack, download_pack


def cli() -> None:
def cli() -> None: # pragma: no cover
"""CLI entry point."""
parser = argparse.ArgumentParser("Welcome to SigSticker, providing all of your sticker needs")
parser.add_argument(
Expand All @@ -33,11 +34,11 @@ def cli() -> None:
if not name:
break
packs.append(name)
return main(packs)
sysexit(main(packs))


def main(packs) -> None:
"""Main function to download sticker packs."""
def main(packs: list[str], cwd: Path = DEFAULT_CWD) -> int:
"""Download, and convert sticker packs."""
for pack in packs:
pack_attrs: dict[str, list[str]] = parse.parse_qs(parse.urlparse(pack).fragment)
if {"pack_id", "pack_key"} > pack_attrs.keys():
Expand All @@ -46,10 +47,11 @@ def main(packs) -> None:
"addstickers/#pack_id=9acc9e8aba563d26a4994e69263e3b25&"
"pack_key=5a6dff3948c28efb9b7aaf93ecc375c69fc316e78077ed26867a14d10a0f6a12"
)
sysexit(1)
return 1

downloaded = asyncio.run(
download_pack("".join(pack_attrs["pack_id"]), "".join(pack_attrs["pack_key"]))
download_pack("".join(pack_attrs["pack_id"]), "".join(pack_attrs["pack_key"]), cwd=cwd)
)

asyncio.run(convert_pack(*downloaded))
return 0
12 changes: 7 additions & 5 deletions sigstickers/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from sigstickers.caching import create_converted, verify_converted

UNKNOWN = "🤷‍♂️"
DEFAULT_CWD = Path.cwd()


def assure_dir_exists(*parts: Path | str) -> Path:
Expand Down Expand Up @@ -61,7 +62,7 @@ def _sanitize_filename(filename: str) -> str:
return unicodedata.normalize("NFKD", sanitized_filename).encode("ascii", "ignore").decode()


async def download_pack(pack_id: str, pack_key: str, cwd: Path = Path.cwd()) -> tuple[Path, Path]:
async def download_pack(pack_id: str, pack_key: str, cwd: Path = DEFAULT_CWD) -> tuple[Path, Path]:
"""Download a sticker pack.
Args:
Expand Down Expand Up @@ -135,7 +136,7 @@ def convert_with_pil(input_path: Path) -> list[str]:
return [png_file, gif_file]


async def convert_pack(swd: Path, pack_name: Path, *, no_cache=False) -> None:
async def convert_pack(swd: Path, pack_name: Path, *, no_cache: bool = False) -> None:
"""Convert the webp images into png and gif images.
Args:
Expand Down Expand Up @@ -166,7 +167,8 @@ async def convert_pack(swd: Path, pack_name: Path, *, no_cache=False) -> None:
converted_files.append(converted.result())
end = time.time()
logger.info(
f"Time taken to convert {len(converted_files)}/{len(webp_files)} stickers - {end - start:.3f}s"
f"Time taken to convert {len(converted_files)}/{len(webp_files)} "
f"stickers - {end - start:.3f}s"
)

logger.info("")
Expand All @@ -175,10 +177,10 @@ async def convert_pack(swd: Path, pack_name: Path, *, no_cache=False) -> None:
data={
"version": 2,
"converted_files": converted_files,
"webp_files": files_to_str(webp_files),
"webp_files": _files_to_str(webp_files),
},
)


def files_to_str(files: list[Path]) -> list[str]:
def _files_to_str(files: list[Path]) -> list[str]:
return [str(x.absolute()) for x in files]
40 changes: 40 additions & 0 deletions tests/test_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
file_exists = "pyproject.toml"
file_exists_2 = ".gitignore"

file_not_exists = "file/not/exists"


def test_verify_converted_v1() -> None:
pack_name = "Test_Pack_v1"
Expand All @@ -23,6 +25,18 @@ def test_verify_converted_v1() -> None:
assert verify_converted(Path(pack_name))


def test_verify_converted_v1_not_exists() -> None:
pack_name = "Test_Pack_v1_not_exists"
cache_file = CACHE_DIR / pack_name
cache_data = {
"info": {"swd": file_not_exists},
"converted": {"converted": 10, "total": 10},
}
cache_file.write_text(json.dumps(cache_data))

assert not verify_converted(Path(pack_name))


def test_verify_converted_v2() -> None:
pack_name = "Test_Pack_v2"
cache_file = CACHE_DIR / pack_name
Expand All @@ -36,6 +50,32 @@ def test_verify_converted_v2() -> None:
assert verify_converted(Path(pack_name))


def test_verify_converted_v2_partial_convert() -> None:
pack_name = "Test_Pack_v2_partial_convert"
cache_file = CACHE_DIR / pack_name
cache_data = {
"version": 2,
"webp_files": [file_exists, file_exists_2],
"converted_files": [[file_exists]],
}
cache_file.write_text(json.dumps(cache_data))

assert not verify_converted(Path(pack_name))


def test_verify_converted_v2_not_exists() -> None:
pack_name = "Test_Pack_v2_not_exists"
cache_file = CACHE_DIR / pack_name
cache_data = {
"version": 2,
"webp_files": [file_exists, file_not_exists],
"converted_files": [[file_exists], [file_exists_2]],
}
cache_file.write_text(json.dumps(cache_data))

assert not verify_converted(Path(pack_name))


def test_create_converted() -> None:
pack_name = "Test_Pack"
cache_data = {"example_key": "example_value"}
Expand Down
22 changes: 22 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from __future__ import annotations

import asyncio
import sys
from pathlib import Path

THISDIR = Path(__file__).resolve().parent
PROJECT_DIR = THISDIR.parent
sys.path.insert(0, str(PROJECT_DIR))

from sigstickers.cli import main

cwd = THISDIR / "data"


def test_cli() -> None:
assert main(["https://signal.art/addstickers/#pack_id=b676ec334ee2f771cadff5d095971e8c&pack_key=c957a57000626a2dc3cb69bf0e79c91c6b196b74d4d6ca1cbb830d3ad0ad4e36"], cwd=cwd,) == 0
assert len(list(Path(f"{cwd}/downloads/DonutTheDog/png").iterdir())) == 28


def test_cli_invalid_url() -> None:
assert main(["https://signal.art/addstickers/#pack_key=c957a57000626a2dc3cb69bf0e79c91c6b196b74d4d6ca1cbb830d3ad0ad4e36"], cwd=cwd,) == 1
Loading

0 comments on commit a42632c

Please sign in to comment.