From 28ef2d9f7eb30d31ca11bfde8b4fb3c14823156c Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 13 Jun 2025 08:19:52 +0200 Subject: [PATCH 1/5] wayback.py: Fix undefined name requests used on line 34 % `ruff check --output-format=github --select=E9,F63,F7,F82` ``` Error: server/api/archive.py:342:25: F821 Undefined name `url` Error: server/api/books.py:263:9: F821 Undefined name `self` Error: server/api/books.py:264:23: F821 Undefined name `self` Error: server/views/apis/v1/books.py:154:61: F821 Undefined name `access` Error: server/views/apis/v1/books.py:154:76: F821 Undefined name `secret` Error: server/views/apis/v1/items.py:38:21: F821 Undefined name `iid` Error: server/views/apis/v1/wayback.py:33:13: F821 Undefined name `requests` Error: Process completed with exit code 1. ``` https://docs.astral.sh/ruff --- server/views/apis/v1/wayback.py | 1 + 1 file changed, 1 insertion(+) diff --git a/server/views/apis/v1/wayback.py b/server/views/apis/v1/wayback.py index eedb471..f4e0776 100644 --- a/server/views/apis/v1/wayback.py +++ b/server/views/apis/v1/wayback.py @@ -10,6 +10,7 @@ :license: see LICENSE for more details. """ +import requests from flask import request from flask.views import MethodView from api.archive import wayback_snapshot, wayback_search From fec7f67f9475900bcae4ef80e3b4b565b81f6fc7 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 13 Jun 2025 08:23:49 +0200 Subject: [PATCH 2/5] books.py: The first parameter to methods must be `self`. Fixes undefined names discovered by ruff. --- server/api/books.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/api/books.py b/server/api/books.py index 6366372..e48d2b3 100644 --- a/server/api/books.py +++ b/server/api/books.py @@ -259,7 +259,7 @@ def create_pre_hook(self): self.cover_url = 'https://archive.org/services/img/' + self.archive_id - def add_metadata(d): + def add_metadata(self, d): self.data.update(d) flag_modified(self, 'data') From 278626dea28bfde9bd9f13cbcaa608b70eb8581e Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 13 Jun 2025 09:11:06 +0200 Subject: [PATCH 3/5] archive.py: Comment out unreachable code --- server/api/archive.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/server/api/archive.py b/server/api/archive.py index a5d90fd..dc249cf 100644 --- a/server/api/archive.py +++ b/server/api/archive.py @@ -339,9 +339,10 @@ def librivox(ocaid): if 'librivox.org/' in a['href'] and not a['href'].endswith('.org/'): return a['href'] return None - html = requests.get(url).content - soup = BeautifulSoup(html, 'html.parser') - table = soup.findAll('table', {'class': 'metatable'})[0] + # The following code is unreachable. + # html = requests.get(url).content + # soup = BeautifulSoup(html, 'html.parser') + # table = soup.findAll('table', {'class': 'metatable'})[0] def download(iid, filename, headers=None): From 8b2759608616ffc2d0a09cbae4e47f43c69502dd Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 13 Jun 2025 09:19:00 +0200 Subject: [PATCH 4/5] books.py: Fix undefined names discovered by ruff rule F821 % `ruff rule F821` # undefined-name (F821) Derived from the **Pyflakes** linter. ## What it does Checks for uses of undefined names. ## Why is this bad? An undefined name is likely to raise `NameError` at runtime. ## Example ```python def double(): return n * 2 # raises `NameError` if `n` is undefined when `double` is called ``` Use instead: ```python def double(n): return n * 2 ``` ## Options - [`target-version`]: Can be used to configure which symbols Ruff will understand as being available in the `builtins` namespace. ## References - [Python documentation: Naming and binding](https://docs.python.org/3/reference/executionmodel.html#naming-and-binding) --- server/views/apis/v1/books.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/server/views/apis/v1/books.py b/server/views/apis/v1/books.py index 793d060..64b0091 100644 --- a/server/views/apis/v1/books.py +++ b/server/views/apis/v1/books.py @@ -151,7 +151,8 @@ def get(self, archive_id, page): return redirect(tts_url(archive_id, plaintext)) def post(self, archive_id, page): - data = request.get_json() or request.form or {'access': None, 'secret': None} + data = request.get_json() or request.form or {} + access, secret = data.get('access'), secret=data.get('secret') pageocr = get_bookpage_ocr(archive_id, page, access=access, secret=secret) pagenum = request.args.get('page', '') plaintext = '\n'.join([block[0] for block in pageocr]) From 9c84513e53b3b38c1d0a977628a9a18bf1a8477c Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 13 Jun 2025 09:25:38 +0200 Subject: [PATCH 5/5] items.py: Fix undefined name iid used on line 38 --- server/views/apis/v1/items.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/views/apis/v1/items.py b/server/views/apis/v1/items.py index c979218..5a06176 100644 --- a/server/views/apis/v1/items.py +++ b/server/views/apis/v1/items.py @@ -31,7 +31,7 @@ def get(self, page=1, limit=100): query=query, cursor=cursor, version=version) @rest_api - def post(self): + def post(self, iid): """For Upload API""" # f = request.files['file'] # ia.upload(name, f)