refactor: convert f-string logging to lazy %-style format (G004)
Sweep all 1676 G004 violations across 112 files, converting
`logger.<level>(f"…{x}…")` to `logger.<level>("…%s…", x)`.
Why: ruff rule G004 was added to pyproject.toml to enforce lazy
%-style logging — defers formatting until the log level is enabled
and lets structured log tooling match the unformatted template.
Conversion preserves rendered output byte-for-byte:
- `{x}` → `%s` + `x`
- `{x!r}` / `{x!s}` / `{x!a}` → `%r` / `%s` / `%a`
- Format specs (`{x:.2f}`, `{x:>10}`) → `%s` + `format(x, 'spec')`
(printf-style specs aren't 1:1 with Python format specs, so we
delegate to `format()` to keep identical output)
- Literal `%` → `%%`
- Concatenated f-strings (`f"a {x} " "b"`) flattened
- Trailing kwargs (`exc_info=True`) preserved
Verified:
- `uv run ruff check --select G004` → 0 violations
- `uv run ty check -- nextcloud_mcp_server` → passes
- `uv run pytest tests/unit/` → 1010 passed
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.7
parent
a4e6125d28
commit
665cb9b1eb
@@ -137,7 +137,7 @@ def _sanitize_error_for_client(error: Exception, context: str = "") -> str:
|
||||
Generic error message safe for client consumption
|
||||
"""
|
||||
# Log detailed error for debugging
|
||||
logger.error(f"Error in {context}: {error}", exc_info=True)
|
||||
logger.error("Error in %s: %s", context, error, exc_info=True)
|
||||
|
||||
# Return generic message
|
||||
return "An internal error occurred. Please contact your administrator."
|
||||
@@ -307,7 +307,7 @@ async def get_vector_sync_status(request: Request) -> JSONResponse:
|
||||
indexed_count = count_result.count
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to query Qdrant for indexed count: {e}")
|
||||
logger.warning("Failed to query Qdrant for indexed count: %s", e)
|
||||
# Continue with indexed_count = 0
|
||||
|
||||
# Determine status
|
||||
@@ -355,7 +355,7 @@ async def get_user_session(request: Request) -> JSONResponse:
|
||||
# Verify token user matches requested user
|
||||
if token_user_id != path_user_id:
|
||||
logger.warning(
|
||||
f"User {token_user_id} attempted to access session for {path_user_id}"
|
||||
"User %s attempted to access session for %s", token_user_id, path_user_id
|
||||
)
|
||||
return JSONResponse(
|
||||
{
|
||||
@@ -442,7 +442,7 @@ async def revoke_user_access(request: Request) -> JSONResponse:
|
||||
# Validate OAuth token and extract user
|
||||
token_user_id, validated = await validate_token_and_get_user(request)
|
||||
except Exception as e:
|
||||
logger.warning(f"Unauthorized access to /api/v1/users/{{user_id}}/revoke: {e}")
|
||||
logger.warning("Unauthorized access to /api/v1/users/{{user_id}}/revoke: %s", e)
|
||||
return JSONResponse(
|
||||
{
|
||||
"error": "Unauthorized",
|
||||
@@ -457,7 +457,7 @@ async def revoke_user_access(request: Request) -> JSONResponse:
|
||||
# Verify token user matches requested user
|
||||
if token_user_id != path_user_id:
|
||||
logger.warning(
|
||||
f"User {token_user_id} attempted to revoke access for {path_user_id}"
|
||||
"User %s attempted to revoke access for %s", token_user_id, path_user_id
|
||||
)
|
||||
return JSONResponse(
|
||||
{
|
||||
@@ -492,7 +492,8 @@ async def revoke_user_access(request: Request) -> JSONResponse:
|
||||
await token_broker.cache.invalidate(token_user_id)
|
||||
|
||||
logger.info(
|
||||
f"Revoked background access for user {token_user_id} (cache and storage cleared)"
|
||||
"Revoked background access for user %s (cache and storage cleared)",
|
||||
token_user_id,
|
||||
)
|
||||
|
||||
return JSONResponse(
|
||||
|
||||
@@ -143,7 +143,7 @@ def _extract_basic_auth(
|
||||
# Verify username matches path user_id
|
||||
if username != path_user_id:
|
||||
logger.warning(
|
||||
f"Username mismatch in app password operation for path user {path_user_id}"
|
||||
"Username mismatch in app password operation for path user %s", path_user_id
|
||||
)
|
||||
return (
|
||||
"",
|
||||
@@ -211,7 +211,7 @@ async def provision_app_password(request: Request) -> JSONResponse:
|
||||
is_allowed, retry_after = _check_rate_limit(path_user_id)
|
||||
if not is_allowed:
|
||||
logger.warning(
|
||||
f"Rate limit exceeded for app password provisioning: {path_user_id}"
|
||||
"Rate limit exceeded for app password provisioning: %s", path_user_id
|
||||
)
|
||||
return JSONResponse(
|
||||
{
|
||||
@@ -263,7 +263,8 @@ async def provision_app_password(request: Request) -> JSONResponse:
|
||||
|
||||
if response.status_code != 200:
|
||||
logger.warning(
|
||||
f"App password validation failed for user: HTTP {response.status_code}"
|
||||
"App password validation failed for user: HTTP %s",
|
||||
response.status_code,
|
||||
)
|
||||
_record_rate_limit_attempt(path_user_id, success=False)
|
||||
return JSONResponse(
|
||||
@@ -283,7 +284,7 @@ async def provision_app_password(request: Request) -> JSONResponse:
|
||||
)
|
||||
|
||||
except httpx.RequestError as e:
|
||||
logger.error(f"Failed to validate app password: {e}")
|
||||
logger.error("Failed to validate app password: %s", e)
|
||||
return JSONResponse(
|
||||
{"success": False, "error": "Failed to validate credentials"},
|
||||
status_code=500,
|
||||
@@ -309,7 +310,7 @@ async def provision_app_password(request: Request) -> JSONResponse:
|
||||
invalidate_scope_cache(username)
|
||||
|
||||
_record_rate_limit_attempt(path_user_id, success=True)
|
||||
logger.info(f"Provisioned app password for user: {username}")
|
||||
logger.info("Provisioned app password for user: %s", username)
|
||||
|
||||
return JSONResponse(
|
||||
{
|
||||
@@ -409,7 +410,7 @@ async def delete_app_password(request: Request) -> JSONResponse:
|
||||
status_code=401,
|
||||
)
|
||||
except httpx.RequestError as e:
|
||||
logger.error(f"Failed to validate credentials: {e}")
|
||||
logger.error("Failed to validate credentials: %s", e)
|
||||
return JSONResponse(
|
||||
{"success": False, "error": "Failed to validate credentials"},
|
||||
status_code=500,
|
||||
@@ -420,7 +421,7 @@ async def delete_app_password(request: Request) -> JSONResponse:
|
||||
deleted = await storage.delete_app_password(username)
|
||||
|
||||
if deleted:
|
||||
logger.info(f"Deleted app password for user: {username}")
|
||||
logger.info("Deleted app password for user: %s", username)
|
||||
return JSONResponse(
|
||||
{
|
||||
"success": True,
|
||||
|
||||
@@ -90,7 +90,7 @@ async def unified_search(request: Request) -> JSONResponse:
|
||||
try:
|
||||
user_id, _validated = await validate_token_and_get_user(request)
|
||||
except Exception as e:
|
||||
logger.warning(f"Unauthorized access to /api/v1/search: {e}")
|
||||
logger.warning("Unauthorized access to /api/v1/search: %s", e)
|
||||
return JSONResponse(
|
||||
{
|
||||
"error": "Unauthorized",
|
||||
@@ -268,12 +268,12 @@ async def unified_search(request: Request) -> JSONResponse:
|
||||
)
|
||||
response_data["pca_data"] = pca_data
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to compute PCA for unified search: {e}")
|
||||
logger.warning("Failed to compute PCA for unified search: %s", e)
|
||||
|
||||
return JSONResponse(response_data)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error in unified search: {e}")
|
||||
logger.error("Error in unified search: %s", e)
|
||||
return JSONResponse(
|
||||
{
|
||||
"error": "Internal error",
|
||||
@@ -311,7 +311,7 @@ async def vector_search(request: Request) -> JSONResponse:
|
||||
try:
|
||||
user_id, _validated = await validate_token_and_get_user(request)
|
||||
except Exception as e:
|
||||
logger.warning(f"Unauthorized access to /api/v1/vector-viz/search: {e}")
|
||||
logger.warning("Unauthorized access to /api/v1/vector-viz/search: %s", e)
|
||||
return JSONResponse(
|
||||
{
|
||||
"error": "Unauthorized",
|
||||
@@ -428,7 +428,7 @@ async def vector_search(request: Request) -> JSONResponse:
|
||||
if "pca_variance" in pca_data:
|
||||
response_data["pca_variance"] = pca_data["pca_variance"]
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to compute PCA coordinates: {e}")
|
||||
logger.warning("Failed to compute PCA coordinates: %s", e)
|
||||
response_data["coordinates_3d"] = []
|
||||
response_data["query_coords"] = []
|
||||
elif include_pca:
|
||||
@@ -465,7 +465,7 @@ async def get_chunk_context(request: Request) -> JSONResponse:
|
||||
# Validate OAuth token and extract user
|
||||
user_id, validated = await validate_token_and_get_user(request)
|
||||
except Exception as e:
|
||||
logger.warning(f"Unauthorized access to /api/v1/chunk-context: {e}")
|
||||
logger.warning("Unauthorized access to /api/v1/chunk-context: %s", e)
|
||||
return JSONResponse(
|
||||
{
|
||||
"error": "Unauthorized",
|
||||
@@ -661,14 +661,16 @@ async def get_pdf_preview(request: Request) -> JSONResponse:
|
||||
# Log incoming request
|
||||
file_path_param = request.query_params.get("file_path", "<not provided>")
|
||||
page_param = request.query_params.get("page", "1")
|
||||
logger.info(f"PDF preview request: file_path={file_path_param}, page={page_param}")
|
||||
logger.info(
|
||||
"PDF preview request: file_path=%s, page=%s", file_path_param, page_param
|
||||
)
|
||||
|
||||
try:
|
||||
# Validate OAuth token and extract user
|
||||
user_id, validated = await validate_token_and_get_user(request)
|
||||
logger.info(f"PDF preview authenticated for user: {user_id}")
|
||||
logger.info("PDF preview authenticated for user: %s", user_id)
|
||||
except Exception as e:
|
||||
logger.warning(f"Unauthorized access to /api/v1/pdf-preview: {e}")
|
||||
logger.warning("Unauthorized access to /api/v1/pdf-preview: %s", e)
|
||||
return JSONResponse(
|
||||
{
|
||||
"success": False,
|
||||
@@ -763,8 +765,11 @@ async def get_pdf_preview(request: Request) -> JSONResponse:
|
||||
image_b64 = base64.b64encode(png_bytes).decode("ascii")
|
||||
|
||||
logger.info(
|
||||
f"Rendered PDF preview: {file_path} page {page_num}/{total_pages}, "
|
||||
f"{len(png_bytes):,} bytes"
|
||||
"Rendered PDF preview: %s page %s/%s, %s bytes",
|
||||
file_path,
|
||||
page_num,
|
||||
total_pages,
|
||||
format(len(png_bytes), ","),
|
||||
)
|
||||
|
||||
return JSONResponse(
|
||||
@@ -777,19 +782,19 @@ async def get_pdf_preview(request: Request) -> JSONResponse:
|
||||
)
|
||||
|
||||
except FileNotFoundError:
|
||||
logger.warning(f"PDF file not found: {file_path_param}")
|
||||
logger.warning("PDF file not found: %s", file_path_param)
|
||||
return JSONResponse(
|
||||
{"success": False, "error": "PDF file not found"},
|
||||
status_code=404,
|
||||
)
|
||||
except (pymupdf.FileDataError, pymupdf.EmptyFileError):
|
||||
logger.warning(f"Invalid or corrupted PDF file: {file_path_param}")
|
||||
logger.warning("Invalid or corrupted PDF file: %s", file_path_param)
|
||||
return JSONResponse(
|
||||
{"success": False, "error": "Invalid or corrupted PDF file"},
|
||||
status_code=400,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"PDF preview error: {e}", exc_info=True)
|
||||
logger.error("PDF preview error: %s", e, exc_info=True)
|
||||
error_msg = _sanitize_error_for_client(e, "get_pdf_preview")
|
||||
return JSONResponse(
|
||||
{"success": False, "error": error_msg},
|
||||
|
||||
Reference in New Issue
Block a user