Skip to content

Commit 548c0bc

Browse files
committed
fix: unignore src/readsight/hyphenation/cache/ via .gitignore /cache/
1 parent 73ef4dc commit 548c0bc

5 files changed

Lines changed: 130 additions & 9 deletions

File tree

.github/workflows/ci.yml

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ on:
88

99
jobs:
1010
check:
11-
name: Python ${{ matrix.python-version }}
12-
runs-on: ubuntu-22.04
11+
name: Python ${{ matrix.python-version }} — Lint + Typecheck + Tests
12+
runs-on: ubuntu-latest
1313
strategy:
1414
fail-fast: false
1515
matrix:
@@ -23,11 +23,20 @@ jobs:
2323
with:
2424
python-version: ${{ matrix.python-version }}
2525

26-
- name: Install deps
27-
run: |
28-
python -m pip install --upgrade pip setuptools wheel
29-
python -m pip install -e .
30-
python -m pip install pytest pytest-cov
26+
- name: Install dependencies
27+
run: pip install -e ".[dev]"
3128

32-
- name: Tests
29+
- name: Lint (ruff)
30+
run: ruff check src/ tests/
31+
32+
- name: Type check (mypy)
33+
run: mypy src/
34+
35+
- name: Unit tests
36+
run: pytest tests/unit -v
37+
38+
- name: Integration tests
39+
run: pytest tests/integration -v
40+
41+
- name: Full test run
3342
run: pytest tests/ -v

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ htmlcov/
2929
Thumbs.db
3030

3131
# Project-specific
32-
cache/
32+
/cache/
3333
*.log

src/readsight/hyphenation/cache/__init__.py

Whitespace-only changes.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
from __future__ import annotations
2+
3+
import json
4+
from pathlib import Path
5+
from typing import Any
6+
7+
from ..hyphenation_exceptions_collection import HyphenationExceptionsCollection
8+
from ..hyphenation_override import HyphenationOverride
9+
from ..pattern import Pattern
10+
from ..patterns_collection import PatternsCollection
11+
from .pattern_cache import PatternCache
12+
13+
_CACHE_VERSION = "2.0"
14+
15+
16+
class JsonPatternCache(PatternCache):
17+
def __init__(self, cache_dir: str) -> None:
18+
self._cache_dir = cache_dir
19+
20+
def has(self, language_code: str) -> bool:
21+
return Path(self._get_file_path(language_code)).is_file()
22+
23+
def get(self, language_code: str) -> dict[str, Any] | None:
24+
file_path = self._get_file_path(language_code)
25+
path = Path(file_path)
26+
if not path.is_file():
27+
return None
28+
29+
try:
30+
data: dict[str, Any] = json.loads(path.read_text(encoding="utf-8"))
31+
except (json.JSONDecodeError, OSError):
32+
return None
33+
34+
if data.get("version") != _CACHE_VERSION:
35+
return None
36+
37+
patterns = PatternsCollection()
38+
for p in data["patterns"]:
39+
patterns.add(Pattern(p["chars"], p["weights"]))
40+
41+
exceptions = HyphenationExceptionsCollection()
42+
for word, hyphenated in data.get("exceptions", {}).items():
43+
exceptions.add(HyphenationOverride(str(word), str(hyphenated)))
44+
45+
return {
46+
"patterns": patterns,
47+
"exceptions": exceptions,
48+
"maxPatternLength": data["maxPatternLength"],
49+
}
50+
51+
def set(self, language_code: str, data: dict[str, Any]) -> None:
52+
patterns_col: PatternsCollection = data["patterns"]
53+
exceptions_col: HyphenationExceptionsCollection = data["exceptions"]
54+
55+
payload: dict[str, Any] = {
56+
"version": _CACHE_VERSION,
57+
"patterns": self._serialize_patterns(patterns_col),
58+
"exceptions": exceptions_col.all(),
59+
"maxPatternLength": data["maxPatternLength"],
60+
}
61+
62+
cache_path = Path(self._get_file_path(language_code))
63+
cache_path.parent.mkdir(parents=True, exist_ok=True)
64+
cache_path.write_text(json.dumps(payload, ensure_ascii=False), encoding="utf-8")
65+
66+
def clear(self, language_code: str) -> None:
67+
path = Path(self._get_file_path(language_code))
68+
if path.is_file():
69+
path.unlink()
70+
71+
def clear_all(self) -> None:
72+
cache_dir = Path(self._cache_dir)
73+
if cache_dir.is_dir():
74+
for f in cache_dir.glob("*.json"):
75+
f.unlink()
76+
77+
def _get_file_path(self, language_code: str) -> str:
78+
return str(Path(self._cache_dir) / f"syllable.{language_code}.json")
79+
80+
@staticmethod
81+
def _serialize_patterns(collection: PatternsCollection) -> list[dict[str, Any]]:
82+
result: list[dict[str, Any]] = []
83+
for key, weights in collection.all().items():
84+
chars = list(key)
85+
weight_values = [int(d) for d in weights]
86+
result.append({"chars": chars, "weights": weight_values})
87+
return result
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from __future__ import annotations
2+
3+
from abc import ABC, abstractmethod
4+
5+
6+
class PatternCache(ABC):
7+
@abstractmethod
8+
def has(self, language_code: str) -> bool:
9+
...
10+
11+
@abstractmethod
12+
def get(self, language_code: str) -> dict[str, object] | None:
13+
"""Returns pattern data dict or None"""
14+
15+
@abstractmethod
16+
def set(self, language_code: str, data: dict[str, object]) -> None:
17+
...
18+
19+
@abstractmethod
20+
def clear(self, language_code: str) -> None:
21+
...
22+
23+
@abstractmethod
24+
def clear_all(self) -> None:
25+
...

0 commit comments

Comments
 (0)