Skip to content

Commit

Permalink
Development QOL
Browse files Browse the repository at this point in the history
  • Loading branch information
Vasily Negrebetskiy committed Aug 29, 2024
1 parent e40b316 commit 9b4383e
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 15 deletions.
2 changes: 0 additions & 2 deletions .flake8

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/development.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
with:
python-version: "3.12"
- name: Check with linter
run: pip install flake8 && flake8
run: pip install ruff==0.5.2 && ruff check

test:
name: Run tests
Expand Down
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.5.2
hooks:
- id: ruff
- id: ruff-format
17 changes: 17 additions & 0 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Developer notes

## Version bumping

Currently project version is tracked in two locations: `pyproject.toml` and `muckraker/__init__.py`.
Those two MUST be synchronised. To do that automatically, use Poetry with
[this plugin](https://pypi.org/project/poetry-bumpversion/).

## Linting

Currently project uses Ruff to perform static checks. To make sure that your commits will be accepted:

- Install `ruff` and `pre-commit` tools.
- Run `pre-commit install` to install our preconfigured hook in your local repo.
- Run `pre-commit run --all-files` to check the whole repo.

Now each time you commit your changes will be checked against the Ruff rules.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ RUN pip install poetry==1.8.3
RUN poetry export --without-hashes --only=main --format=requirements.txt > requirements.txt



FROM python:3.12-slim AS runner

WORKDIR /app
Expand Down
2 changes: 1 addition & 1 deletion muckraker/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.1"
__version__ = "0.1.0"
4 changes: 3 additions & 1 deletion muckraker/md_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ def __init__(self, pattern: str, md: Markdown, image_dir: str = "") -> None:
super().__init__(pattern, md)
self.image_dir = image_dir

def handleMatch(self, m: re.Match[str], data: str) -> tuple[etree.Element | None, int | None, int | None]:
def handleMatch(
self, m: re.Match[str], data: str
) -> tuple[etree.Element | None, int | None, int | None]:
el, start, ind = super().handleMatch(m, data)
src_path = Path(el.get("src"))
src_path = Path(self.image_dir) / src_path
Expand Down
4 changes: 3 additions & 1 deletion muckraker/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ def font_size_css(selector: str, size_pt: int | None):
return f"{selector} {{ font-size: {size_pt}pt !important; }}\n"


def render_issue(page: dict, header: dict, body: str, fonts: dict, output: str, image_dir: str = "") -> None:
def render_issue(
page: dict, header: dict, body: str, fonts: dict, output: str, image_dir: str = ""
) -> None:
# Sanitize Markdown and convert it to HTML
body = nh3.clean(body, tags=TAGS)
md = Markdown(
Expand Down
12 changes: 8 additions & 4 deletions muckraker/sqlcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ async def put_issue(self, issue_id: str, issue_dict: dict) -> None:
async def put_image(self, issue_id: str, name: str, image: bytes) -> None:
async with aiosqlite.connect(self.path) as db:
try:
await db.execute("INSERT INTO images (issue_id, name, image) VALUES (?, ?, ?)", (issue_id, name, image))
query = "INSERT INTO images (issue_id, name, image) VALUES (?, ?, ?)"
await db.execute(query, (issue_id, name, image))
await db.commit()
except aiosqlite.IntegrityError:
raise CacheError(f"Image name duplicates: {name}")
Expand All @@ -52,7 +53,8 @@ async def delete_issue(self, issue_id: str) -> None:

async def get_issue(self, issue_id: str) -> dict:
async with aiosqlite.connect(self.path) as db:
async with db.execute("SELECT data FROM issues WHERE issue_id = ?", (issue_id,)) as cursor:
query = "SELECT data FROM issues WHERE issue_id = ?"
async with db.execute(query, (issue_id,)) as cursor:
row = await cursor.fetchone()
if row:
return json.loads(row[0])
Expand All @@ -61,13 +63,15 @@ async def get_issue(self, issue_id: str) -> dict:

async def count_images(self, issue_id: str) -> None:
async with aiosqlite.connect(self.path) as db:
async with db.execute("SELECT COUNT(*) FROM images WHERE issue_id = ?", (issue_id,)) as cursor:
query = "SELECT COUNT(*) FROM images WHERE issue_id = ?"
async with db.execute(query, (issue_id,)) as cursor:
row = await cursor.fetchone()
return row[0]

async def load_images(self, issue_id: str) -> None:
async with aiosqlite.connect(self.path) as db:
async with db.execute("SELECT name, image FROM images WHERE issue_id = ?", (issue_id,)) as cursor:
query = "SELECT name, image FROM images WHERE issue_id = ?"
async with db.execute(query, (issue_id,)) as cursor:
async for row in cursor:
name = row[0]
image = row[1]
Expand Down
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,27 @@ python-multipart = "0.0.9"
weasyprint = "61.2"
pydyf = "^0.9.0"
uvicorn = "0.29.0"
httpx = "0.26.0"
aiosqlite = "^0.20.0"
nh3 = "^0.2.18"


[tool.poetry.group.dev.dependencies]
pytest = "^8.3.2"
httpx = "0.26.0"

[tool.poetry_bumpversion.file."muckraker/__init__.py"]

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.ruff]
line-length = 120
line-length = 100
indent-width = 4
target-version = "py312"

[tool.ruff.lint]
extend-select = ["E501"]
extend-select = [
"E",
"F",
"W",
]
3 changes: 3 additions & 0 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import os

from muckraker.sqlcache import SQLCache

Expand All @@ -18,3 +19,5 @@ def test_cache(issue_dict, good_image, thick_image):
# Insert some images
asyncio.run(cache.put_image("123", "test1.png", good_image.read()))
asyncio.run(cache.put_image("123", "test2.png", thick_image.read()))

os.remove("test.db")

0 comments on commit 9b4383e

Please sign in to comment.