-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* updated deps * pathlib, Background Tasks, Logging Updates (#38) * updated CacheManager to use pathlib * updated some ops to be background tasks * fixed ruff lint errors * finally fixed logging. logs writing to app.log again * added test for delete file * Updated Endpoint for Downloading File (#39) * updated starter script to use 2 workers * added log messages * added :path to route * added trailing slash to get_file path * updated route and now passing filename through queryparam * updated link generated in FileUploadDTO model to match new route * added gzip middleware * added invoke and helper tasks * fixed delete_file test * updated tests to use new endpoint * updated test client and added logger to cachemanager * updated deps * moved rich to dev dependencies and updated project version to 0.3.0
- Loading branch information
Showing
15 changed files
with
270 additions
and
175 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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "smolvault" | ||
version = "0.2.0" | ||
version = "0.3.0" | ||
description = "" | ||
license = "MIT" | ||
authors = ["Zach Fuller <[email protected]>"] | ||
|
@@ -9,23 +9,24 @@ packages = [{include = "smolvault", from = "src"}] | |
|
||
[tool.poetry.dependencies] | ||
python = "^3.11" | ||
pydantic = "^2.7.4" | ||
pydantic = "^2.8.2" | ||
fastapi = "^0.111.0" | ||
sqlmodel = "^0.0.19" | ||
rich = "^13.7.1" | ||
python-multipart = "^0.0.9" | ||
python-dotenv = "^1.0.1" | ||
pydantic-settings = "^2.3.4" | ||
hypercorn = "^0.17.3" | ||
|
||
[tool.poetry.group.dev.dependencies] | ||
boto3-stubs = {extras = ["essential"], version = "^1.34.136"} | ||
boto3-stubs = {extras = ["essential"], version = "^1.34.144"} | ||
pre-commit = "^3.7.1" | ||
ruff = "^0.5.0" | ||
mypy = "^1.10.1" | ||
pytest = "^8.2.2" | ||
pytest-asyncio = "^0.23.7" | ||
moto = {extras = ["all"], version = "^5.0.10"} | ||
moto = {extras = ["all"], version = "^5.0.11"} | ||
invoke = "^2.2.0" | ||
rich = "^13.7.1" | ||
|
||
[tool.ruff] | ||
line-length = 120 | ||
|
@@ -34,7 +35,7 @@ indent-width = 4 | |
target-version = "py311" | ||
|
||
[tool.ruff.lint] | ||
select = ["E", "F", "W", "C90", "I", "N", "UP", "ASYNC", "S", "B", "ERA", "PLE", "PLW", "PERF", "RUF", "SIM", "PT", "T20"] | ||
select = ["E", "F", "W", "C90", "I", "N", "UP", "ASYNC", "S", "B", "ERA", "PLE", "PLW", "PLC", "PLW", "PERF", "RUF", "SIM", "PT", "T20", "PTH", "LOG", "G"] | ||
ignore = ["E501", "S101"] | ||
|
||
# Allow fix for all enabled rules (when `--fix`) is provided. | ||
|
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#!/bin/bash | ||
|
||
poetry run hypercorn src.smolvault.main:app -b 0.0.0.0 --debug --log-config=logging.conf --log-level=DEBUG --access-logfile=hypercorn.access.log --error-logfile=hypercorn.error.log --keep-alive=120 --workers=1 | ||
poetry run hypercorn src.smolvault.main:app -b 0.0.0.0 --debug --log-config=logging.conf --log-level=DEBUG --access-logfile=hypercorn.access.log --error-logfile=hypercorn.error.log --keep-alive=120 --workers=2 | ||
|
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 |
---|---|---|
|
@@ -17,4 +17,4 @@ fi | |
# create local cache dir | ||
mkdir uploads | ||
|
||
poetry run pytest -vv tests/ | ||
poetry run pytest -vvv tests/ -x |
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 |
---|---|---|
@@ -1,15 +1,24 @@ | ||
import os | ||
import logging | ||
import pathlib | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class CacheManager: | ||
def __init__(self, cache_dir: str) -> None: | ||
self.cache_dir = cache_dir | ||
self.cache_dir = pathlib.Path(cache_dir) | ||
logger.info("Created CacheManager with cache directory %s", self.cache_dir) | ||
|
||
def file_exists(self, filename: str) -> bool: | ||
return os.path.exists(os.path.join(self.cache_dir, filename)) | ||
file_path = self.cache_dir / filename | ||
return file_path.exists() | ||
|
||
def save_file(self, filename: str, data: bytes) -> str: | ||
file_path = os.path.join(self.cache_dir, filename) | ||
with open(file_path, "wb") as f: | ||
file_path = self.cache_dir / filename | ||
with file_path.open("wb") as f: | ||
f.write(data) | ||
return file_path | ||
return file_path.as_posix() | ||
|
||
def delete_file(self, local_path: str) -> None: | ||
file_path = pathlib.Path(local_path) | ||
file_path.unlink(missing_ok=True) |
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
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,24 @@ | ||
import sqlite3 | ||
|
||
from invoke.context import Context | ||
from invoke.tasks import task | ||
from rich import print | ||
|
||
|
||
@task | ||
def lint(c: Context) -> None: | ||
c.run("poetry run ruff check src/smolvault tests", echo=True, pty=True) | ||
|
||
|
||
@task | ||
def fmt(c: Context) -> None: | ||
c.run("poetry run ruff format src/smolvault tests", echo=True, pty=True) | ||
|
||
|
||
@task | ||
def show_table(c: Context) -> None: | ||
conn = sqlite3.connect("file_metadata.db") | ||
cursor = conn.cursor() | ||
cursor.execute("SELECT * FROM filemetadatarecord") | ||
print(cursor.fetchall()) | ||
conn.close() |
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,29 @@ | ||
from typing import Any | ||
from uuid import uuid4 | ||
|
||
import pytest | ||
from httpx import AsyncClient | ||
from smolvault.models import FileUploadDTO | ||
|
||
|
||
@pytest.mark.asyncio() | ||
@pytest.mark.usefixtures("_test_bucket") | ||
async def test_delete_file(client: AsyncClient, camera_img: bytes) -> None: | ||
# first upload the file | ||
filename = f"{uuid4().hex[:6]}-camera.png" | ||
expected_obj = FileUploadDTO(name=filename, size=len(camera_img), content=camera_img, tags="camera,photo") | ||
expected = expected_obj.model_dump(exclude={"content", "upload_timestamp", "tags"}) | ||
response = await client.post( | ||
"/file/upload", files={"file": (filename, camera_img, "image/png")}, data={"tags": "camera,photo"} | ||
) | ||
actual: dict[str, Any] = response.json() | ||
actual.pop("upload_timestamp") | ||
assert response.status_code == 201 | ||
assert actual == expected | ||
|
||
# now delete the file | ||
response = await client.delete(f"/file/{filename}") | ||
actual = response.json() | ||
assert response.status_code == 200 | ||
assert actual["message"] == "File deleted successfully" | ||
assert actual["record"]["file_name"] == filename |
Oops, something went wrong.