diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 14d5885..611da85 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -25,3 +25,22 @@ jobs: - name: Run tests run: | python -m pytest + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v7 + - name: Set up Python + uses: actions/setup-python@v6 + with: + python-version: "3.13" + cache: pip + cache-dependency-path: pyproject.toml + - name: Install dependencies + run: | + pip install . --group dev + - name: Run ruff check + run: | + ruff check . + - name: Run ruff format + run: | + ruff format --check . diff --git a/Justfile b/Justfile index a780ae7..f79bd74 100644 --- a/Justfile +++ b/Justfile @@ -16,10 +16,10 @@ # Run linters @lint: echo "Linters..." - echo " Black" - pipenv run black . --check - echo " ruff" - pipenv run ruff . + echo " ruff check" + pipenv run ruff check . + echo " ruff format" + pipenv run ruff format --check . # Serve live docs on localhost:8000 @@ -27,14 +27,14 @@ rm -rf docs/_build cd docs && pipenv run make livehtml -# Apply Black -@black: - pipenv run black . +# Apply ruff format +@format: + pipenv run ruff format . # Run automatic fixes @fix: - pipenv run ruff . --fix - pipenv run black . + pipenv run ruff check . --fix + pipenv run ruff format . # Push commit if tests pass @push: test lint diff --git a/datasette_enrichments/__init__.py b/datasette_enrichments/__init__.py index 41030af..fe8b513 100644 --- a/datasette_enrichments/__init__.py +++ b/datasette_enrichments/__init__.py @@ -1,29 +1,32 @@ -from abc import ABC, abstractmethod import asyncio import datetime -from datasette import hookimpl -from datasette.permissions import Action, PermissionSQL -from datasette.resources import DatabaseResource -from datasette.utils import async_call_with_supported_arguments, tilde_encode, sqlite3 -from datasette_secrets import Secret, get_secret import json import secrets import sys import time import traceback import urllib +from abc import ABC, abstractmethod +from typing import TYPE_CHECKING +from urllib.parse import quote + +from datasette import hookimpl +from datasette.permissions import Action from datasette.plugins import pm +from datasette.resources import DatabaseResource +from datasette.utils import ( + async_call_with_supported_arguments, + await_me_maybe, + sqlite3, + tilde_encode, +) +from datasette_secrets import Secret, get_secret from markupsafe import Markup, escape -from . import views from wtforms import PasswordField from wtforms.validators import DataRequired -from .utils import mark_job_complete, pks_for_rows -from urllib.parse import quote -from . import hookspecs -from datasette.utils import await_me_maybe - -from typing import TYPE_CHECKING, List, Optional, Tuple, Union +from . import hookspecs, views +from .utils import mark_job_complete, pks_for_rows if TYPE_CHECKING: from datasette.app import Datasette @@ -32,10 +35,10 @@ pm.add_hookspecs(hookspecs) -IdType = Union[int, str, Tuple[Union[int, str], ...]] +IdType = int | str | tuple[int | str, ...] # Custom epoch to save space in the _enrichment_progress table -JAN_1_2025_EPOCH = int(datetime.datetime(2025, 1, 1).timestamp() * 1000) +JAN_1_2025_EPOCH = int(datetime.datetime(2025, 1, 1).timestamp() * 1000) # noqa: DTZ001 def ms_since_2025_to_datetime(ms_since_2025): @@ -105,8 +108,8 @@ async def set_job_status( db: "Database", job_id: int, status: str, - allowed_statuses: Optional[Tuple[str]] = None, - message: Optional[str] = None, + allowed_statuses: tuple[str] | None = None, + message: str | None = None, ): if allowed_statuses: # First check the current status @@ -146,9 +149,7 @@ async def record_progress(db, job_id, success_count, error_count, message=""): ) values ( :job_id, :timestamp_ms_2025, :success_count, :error_count, {} ) - """.format( - ":message" if message else "null" - ), + """.format(":message" if message else "null"), { "job_id": job_id, "timestamp_ms_2025": int(time.time() * 1000) - JAN_1_2025_EPOCH, @@ -173,7 +174,7 @@ class SecretError(Exception): class Enrichment(ABC): - _subclasses = [] + _subclasses = [] # noqa: RUF012 batch_size: int = 100 # Cancel run after this many errors @@ -181,14 +182,14 @@ class Enrichment(ABC): log_traceback: bool = False class Cancel(Exception): - def __init__(self, reason: Optional[str] = None): + def __init__(self, reason: str | None = None): self.reason = reason def __str__(self) -> str: return self.reason or "Cancelled by enrichment" class Pause(Exception): - def __init__(self, reason: Optional[str] = None): + def __init__(self, reason: str | None = None): self.reason = reason def __str__(self) -> str: @@ -207,14 +208,14 @@ def name(self): ... description: str = "" # Short description of this enrichment - secret: Optional[Secret] = None + secret: Secret | None = None def __init_subclass__(cls, **kwargs): super().__init_subclass__(**kwargs) cls._subclasses.append(cls) def __repr__(self): - return "".format(self.slug) + return f"" async def get_secret(self, datasette: "Datasette", config: dict): if self.secret is None: @@ -228,13 +229,11 @@ async def get_secret(self, datasette: "Datasette", config: dict): stashed_keys = datasette._enrichments_stashed_secrets stash_key = config.get("enrichment_secret") if stash_key not in stashed_keys: - raise SecretError( - "No secret found in stash for {}".format(self.secret.name) - ) + raise SecretError(f"No secret found in stash for {self.secret.name}") return stashed_keys[stash_key] async def log_error( - self, db: "Database", job_id: int, ids: List[IdType], error: str + self, db: "Database", job_id: int, ids: list[IdType], error: str ): if self.log_traceback: error += "\n\n" + traceback.format_exc() @@ -324,8 +323,8 @@ async def enrich_batch( pks: list, config: dict, job_id: int, - actor_id: str = None, - ) -> Optional[int]: + actor_id: str | None = None, + ) -> int | None: raise NotImplementedError async def increment_cost( @@ -347,7 +346,7 @@ async def enqueue( table: str, filter_querystring: str, config: dict, - actor_id: str = None, + actor_id: str | None = None, ) -> int: # Enqueue a job qs = filter_querystring @@ -378,9 +377,7 @@ def _insert(conn): :enrichment, 'pending', :database_name, :table_name, :filter_querystring, :config, datetime('now'), :row_count, 0, 0, 0{} ) - """.format( - ", :actor_id" if actor_id else ", null" - ), + """.format(", :actor_id" if actor_id else ", null"), { "enrichment": self.slug, "database_name": db.name, @@ -435,8 +432,8 @@ async def run_enrichment(): ) qs = job["filter_querystring"] if next_cursor: - qs += "&_next={}".format(next_cursor) - qs += "&_size={}&_shape=objects".format(self.batch_size) + qs += f"&_next={next_cursor}" + qs += f"&_size={self.batch_size}&_shape=objects" response = await datasette.client.get( table_path + "?" + qs, skip_permission_checks=True ) @@ -466,7 +463,7 @@ async def run_enrichment(): except self.Pause as ex: await set_job_status(db, job_id, "paused", message=str(ex)) return - except Exception as ex: + except Exception as ex: # noqa: BLE001 await self.log_error(db, job_id, pks_for_rows(rows, pks), str(ex)) # Update next_cursor next_cursor = response.json()["next"] @@ -553,7 +550,7 @@ async def inner(): database, tilde_encode(table), ( - "?{}".format(request.query_string) + f"?{request.query_string}" if request.query_string else "" ), @@ -575,9 +572,7 @@ async def inner(): items.append( { "href": datasette.urls.path( - "/-/enrich/{}/-/jobs?table={}".format( - database, quote(table) - ), + f"/-/enrich/{database}/-/jobs?table={quote(table)}", ), "label": "Enrichment jobs", "description": "View and manage {} enrichment job{} for this table".format( @@ -616,7 +611,7 @@ async def inner(): return [ { "href": datasette.urls.path( - "/-/enrich/{}/-/jobs".format(database), + f"/-/enrich/{database}/-/jobs", ), "label": "Enrichment jobs", "description": "View and manage {} enrichment job{} for this database".format( @@ -652,11 +647,7 @@ async def inner(): return [ { "href": datasette.urls.path( - "/-/enrich/{}/{}?{}".format( - database, - tilde_encode(table), - query_string, - ) + f"/-/enrich/{database}/{tilde_encode(table)}?{query_string}" ), "label": "Enrich this row", "description": "Run a data cleaning operation against this row", @@ -958,12 +949,10 @@ async def _restart_running_jobs_task(datasette): # Find jobs marked as 'running' running_jobs = ( - await db.execute( - """ + await db.execute(""" SELECT * FROM _enrichment_jobs WHERE status = 'running' - """ - ) + """) ).rows # Grab all known enrichments @@ -980,7 +969,7 @@ async def _restart_running_jobs_task(datasette): # Resume from wherever it left off await enrichment.start_enrichment_in_process(datasette, db, job_id) else: - print("Unknown enrichment: {}".format(enrichment_slug), file=sys.stderr) + print(f"Unknown enrichment: {enrichment_slug}", file=sys.stderr) async def restart_running_jobs(datasette): diff --git a/datasette_enrichments/utils.py b/datasette_enrichments/utils.py index 3921975..3d5a375 100644 --- a/datasette_enrichments/utils.py +++ b/datasette_enrichments/utils.py @@ -1,6 +1,5 @@ import asyncio -import secrets -from typing import Optional, Union, TYPE_CHECKING +from typing import TYPE_CHECKING if TYPE_CHECKING: from datasette.app import Datasette @@ -12,13 +11,13 @@ def __init__(self, job_id, msg): self.msg = msg def __repr__(self): - return "".format(self.job_id, self.msg) + return f"" async def wait_for_job( datasette: "Datasette", - job_id: Union[int, str], - database: Optional[str] = None, + job_id: int | str, + database: str | None = None, timeout=None, ): "Returns when the job has completed, using an asyncio.Event" @@ -49,7 +48,7 @@ async def wait_for_job( async def mark_job_complete( - datasette: "Datasette", job_id: int, database: Optional[str] = None + datasette: "Datasette", job_id: int, database: str | None = None ): _ensure_enrichment_properties(datasette) if not database: diff --git a/datasette_enrichments/views.py b/datasette_enrichments/views.py index 94a9890..ba8b645 100644 --- a/datasette_enrichments/views.py +++ b/datasette_enrichments/views.py @@ -1,13 +1,14 @@ -from datasette import Response, NotFound, Forbidden +import json +import urllib.parse + +from datasette import Forbidden, NotFound, Response from datasette.resources import DatabaseResource from datasette.utils import ( + MultiParams, async_call_with_supported_arguments, path_with_removed_args, - MultiParams, tilde_decode, ) -import json -import urllib.parse async def check_permissions(datasette, request, database): @@ -22,9 +23,9 @@ async def check_permissions(datasette, request, database): async def job_view(datasette, request): "Page showing details of an enrichment job" from . import ( - get_enrichments, - ensure_tables, CUSTOM_ELEMENT_JS, + ensure_tables, + get_enrichments, ms_since_2025_to_datetime, ) @@ -174,7 +175,7 @@ async def enrichment_view(datasette, request): return Response.html( await datasette.render_template( - ["enrichment-{}.html".format(enrichment.slug), "enrichment.html"], + [f"enrichment-{enrichment.slug}.html", "enrichment.html"], { "database": database, "table": table, @@ -212,7 +213,7 @@ async def enrichment_picker(datasette, request): response = await datasette.client.get(url, skip_permission_checks=True) if response.status_code != 200: return Response.text( - "Error fetching data from {}: {}".format(url, response.text), + f"Error fetching data from {url}: {response.text}", status=500, ) filtered_data = response.json() @@ -228,7 +229,7 @@ async def enrichment_picker(datasette, request): "path": path_with_removed_args( request=request, args={"_sort"}, - path="{}/{}".format(request.path, enrichment.slug), + path=f"{request.path}/{enrichment.slug}", ), } ) @@ -280,7 +281,7 @@ async def enrich_data_post(datasette, request, enrichment, filtered_data): if form and not form.validate(): return Response.html( await datasette.render_template( - ["enrichment-{}.html".format(enrichment.slug), "enrichment.html"], + [f"enrichment-{enrichment.slug}.html", "enrichment.html"], { "database": database, "table": table, @@ -321,12 +322,12 @@ async def enrich_data_post(datasette, request, enrichment, filtered_data): datasette.INFO, ) return Response.redirect( - datasette.urls.table(db.name, table) + "?_enrichment_job={}".format(job_id) + datasette.urls.table(db.name, table) + f"?_enrichment_job={job_id}" ) async def job_progress_view(datasette, request): - from . import get_enrichments, ensure_tables + from . import ensure_tables, get_enrichments job_id = request.url_vars["job_id"] database = request.url_vars["database"] @@ -376,9 +377,7 @@ async def job_progress_view(datasette, request): { "total": job["row_count"], "title": title, - "url": datasette.urls.path( - "/-/enrich/{}/-/jobs/{}".format(database, job_id) - ), + "url": datasette.urls.path(f"/-/enrich/{database}/-/jobs/{job_id}"), "is_complete": is_complete, "sections": sections, } @@ -394,7 +393,7 @@ async def pause_job(db, job_id, message): async def resume_job(datasette, db, job_id, message): - from . import set_job_status, get_enrichments + from . import get_enrichments, set_job_status await set_job_status( db, job_id, "running", allowed_statuses=("paused",), message=message diff --git a/docs/conf.py b/docs/conf.py index 5b40b31..d3e4b43 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -1,6 +1,3 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - from subprocess import PIPE, Popen # This file is execfile()d with the current directory set to its diff --git a/docs/developing.md b/docs/developing.md index 6b64e00..4c07f48 100644 --- a/docs/developing.md +++ b/docs/developing.md @@ -13,6 +13,7 @@ Your enrichment plugin should register new enrichments using the `register_enric ```python from datasette import hookimpl + @hookimpl def register_enrichments(): return [MyEnrichment()] @@ -28,14 +29,10 @@ def register_enrichments(datasette): db = datasette.get_database("mydb") settings = [ row["setting"] - for row in await db.execute( - "select setting from special_settings" - ) - ] - return [ - MyEnrichment(setting) - for setting in settings + for row in await db.execute("select setting from special_settings") ] + return [MyEnrichment(setting) for setting in settings] + return inner ``` @@ -46,6 +43,7 @@ Most of the code you write will be in a subclass of `Enrichment`: ```python from datasette_enrichments import Enrichment + class MyEnrichment(Enrichment): name = "Name of My Enrichment" slug = "my-enrichment" @@ -239,6 +237,7 @@ To define a secret that your plugin needs, add the following code: from datasette_enrichments import Enrichment from datasette_secrets import Secret + # Then later in your enrichments class class TrainEnthusiastsEnrichment(Enrichment): name = "Train Enthusiasts" @@ -248,7 +247,7 @@ class TrainEnthusiastsEnrichment(Enrichment): name="TRAIN_ENTHUSIASTS_API_KEY", description="An API key from train-enthusiasts.doesnt.exist", obtain_url="https://train-enthusiasts.doesnt.exist/api-keys", - obtain_label="Get an API key" + obtain_label="Get an API key", ) ``` Configuring your enrichment like this will result in the following behavior: @@ -278,6 +277,7 @@ from datasette_enrichments.utils import wait_for_job import pytest import sqlite3 + @pytest.mark.asyncio async def test_enrichment(tmpdir): db_path = str(tmpdir / "demo.db") @@ -292,8 +292,7 @@ async def test_enrichment(tmpdir): cookies = {"ds_actor": datasette.sign({"a": {"id": "root"}}, "actor")} csrftoken = ( await datasette.client.get( - "/-/enrich/demo/news/name-of-enrichment", - cookies=cookies + "/-/enrich/demo/news/name-of-enrichment", cookies=cookies ) ).cookies["ds_csrftoken"] cookies["ds_csrftoken"] = csrftoken @@ -323,7 +322,7 @@ async def test_enrichment(tmpdir): assert rows == [ {"body": "example a transformed"}, {"body": "example b transformed"}, - {"body": "example c transformed"} + {"body": "example c transformed"}, ] ``` diff --git a/example-enrichments/enrichments_examples.py b/example-enrichments/enrichments_examples.py index 6d97559..1cb8ec8 100644 --- a/example-enrichments/enrichments_examples.py +++ b/example-enrichments/enrichments_examples.py @@ -1,7 +1,7 @@ from datasette import hookimpl -from uppercase import Uppercase -from openai_embeddings import Embeddings from jinja_sandbox import JinjaSandbox +from openai_embeddings import Embeddings +from uppercase import Uppercase @hookimpl diff --git a/example-enrichments/jinja_sandbox.py b/example-enrichments/jinja_sandbox.py index ea6a81b..af4770d 100644 --- a/example-enrichments/jinja_sandbox.py +++ b/example-enrichments/jinja_sandbox.py @@ -1,12 +1,10 @@ -from datasette_enrichments import Enrichment - -from jinja2.sandbox import SandboxedEnvironment - +import sqlite_utils from datasette.database import Database -from typing import List +from jinja2.sandbox import SandboxedEnvironment from wtforms import Form, StringField, TextAreaField from wtforms.validators import DataRequired -import sqlite_utils + +from datasette_enrichments import Enrichment class JinjaSandbox(Enrichment): @@ -52,11 +50,11 @@ async def enrich_batch( datasette, db: Database, table: str, - rows: List[dict], - pks: List[str], + rows: list[dict], + pks: list[str], config: dict, job_id: int, - actor_id: str = None, + actor_id: str | None = None, ): env = SandboxedEnvironment(enable_async=True) template = env.from_string(config["template"]) @@ -67,7 +65,7 @@ async def enrich_batch( "update [{table}] set [{output_column}] = ? where {wheres}".format( table=table, output_column=output_column, - wheres=" and ".join('"{}" = ?'.format(pk) for pk in pks), + wheres=" and ".join(f'"{pk}" = ?' for pk in pks), ), - [output] + list(row[pk] for pk in pks), + [output] + [row[pk] for pk in pks], ) diff --git a/example-enrichments/openai_embeddings.py b/example-enrichments/openai_embeddings.py index 441f3b5..f808065 100644 --- a/example-enrichments/openai_embeddings.py +++ b/example-enrichments/openai_embeddings.py @@ -1,15 +1,14 @@ -from datasette_enrichments import Enrichment -from datasette.database import Database -from typing import List import math import struct from string import Template -import httpx - -from wtforms import SelectField, Form, TextAreaField, PasswordField -from wtforms.widgets import ListWidget, CheckboxInput +import httpx +from datasette.database import Database +from wtforms import Form, PasswordField, SelectField, TextAreaField from wtforms.validators import DataRequired +from wtforms.widgets import CheckboxInput, ListWidget + +from datasette_enrichments import Enrichment class MultiCheckboxField(SelectField): @@ -38,7 +37,7 @@ async def get_config_form(self, db, table): choices = [(col, col) for col in await db.table_columns(table)] # Default template uses all string columns - default = " ".join("${{{}}}".format(col[0]) for col in choices) + default = " ".join(f"${{{col[0]}}}" for col in choices) class ConfigForm(Form): template = TextAreaField( @@ -55,29 +54,25 @@ class ConfigForm(Form): async def initialize(self, datasette, db, table, config): # Ensure table exists - embeddings_table = "_embeddings_{}".format(table) + embeddings_table = f"_embeddings_{table}" if not await db.table_exists(embeddings_table): # Create it pk_names = await db.primary_keys(table) column_types = { c.name: c.type for c in await db.table_column_details(table) } - sql = ["create table [{}] (".format(embeddings_table)] + sql = [f"create table [{embeddings_table}] ("] create_bits = [] for pk in pk_names: - create_bits.append(" [{}] {}".format(pk, column_types[pk])) + create_bits.append(f" [{pk}] {column_types[pk]}") create_bits.append(" _embedding blob") create_bits.append( - " PRIMARY KEY ({})".format( - ", ".join("[{}]".format(pk) for pk in pk_names) - ) + " PRIMARY KEY ({})".format(", ".join(f"[{pk}]" for pk in pk_names)) ) # If there's only one primary key, set up a foreign key constraint if len(pk_names) == 1: create_bits.append( - " FOREIGN KEY ([{}]) REFERENCES [{}] ({})".format( - pk_names[0], table, pk_names[0] - ) + f" FOREIGN KEY ([{pk_names[0]}]) REFERENCES [{table}] ({pk_names[0]})" ) sql.append(",\n".join(create_bits)) sql.append(")") @@ -88,11 +83,11 @@ async def enrich_batch( datasette, db: Database, table: str, - rows: List[dict], - pks: List[str], + rows: list[dict], + pks: list[str], config: dict, job_id: int, - actor_id: str = None, + actor_id: str | None = None, ): template = SpaceTemplate(config["template"]) texts = [template.safe_substitute(row) for row in rows] @@ -122,7 +117,7 @@ async def enrich_batch( total_cost_rounded_up = math.ceil(total_cost_in_100ths_of_cents) await self.increment_cost(db, job_id, total_cost_rounded_up) - embeddings_table = "_embeddings_{}".format(table) + embeddings_table = f"_embeddings_{table}" # Write results to the table for row, result in zip(rows, results): vector = result["embedding"] @@ -130,8 +125,8 @@ async def enrich_batch( await db.execute_write( "insert or replace into [{embeddings_table}] ({pks}, _embedding) values ({pk_question_marks}, ?)".format( embeddings_table=embeddings_table, - pks=", ".join("[{}]".format(pk) for pk in pks), + pks=", ".join(f"[{pk}]" for pk in pks), pk_question_marks=", ".join("?" for _ in pks), ), - list(row[pk] for pk in pks) + [embedding], + [row[pk] for pk in pks] + [embedding], ) diff --git a/example-enrichments/uppercase.py b/example-enrichments/uppercase.py index 578b1e6..0094f44 100644 --- a/example-enrichments/uppercase.py +++ b/example-enrichments/uppercase.py @@ -1,9 +1,10 @@ -from datasette_enrichments import Enrichment +import asyncio + from datasette.database import Database -from typing import List from wtforms import Form, SelectMultipleField -from wtforms.widgets import ListWidget, CheckboxInput -import asyncio +from wtforms.widgets import CheckboxInput, ListWidget + +from datasette_enrichments import Enrichment class MultiCheckboxField(SelectMultipleField): @@ -30,19 +31,19 @@ async def enrich_batch( datasette, db: Database, table: str, - rows: List[dict], - pks: List[str], + rows: list[dict], + pks: list[str], config: dict, job_id: int, - actor_id: str = None, + actor_id: str | None = None, ): columns = config.get("columns") or [] if not columns: return - wheres = " and ".join('"{}" = ?'.format(pk) for pk in pks) - sets = ", ".join('"{}" = upper("{}")'.format(col, col) for col in columns) + wheres = " and ".join(f'"{pk}" = ?' for pk in pks) + sets = ", ".join(f'"{col}" = upper("{col}")' for col in columns) params = [[row[pk] for pk in pks] for row in rows] await db.execute_write_many( - "update [{}] set {} where {}".format(table, sets, wheres), params + f"update [{table}] set {sets} where {wheres}", params ) await asyncio.sleep(0.3) diff --git a/pyproject.toml b/pyproject.toml index bf8c1f3..04f59b5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,7 +16,13 @@ dependencies = [ ] [dependency-groups] -dev = ["pytest", "pytest-asyncio", "black", "ruff", "packaging"] +dev = [ + "pytest", + "pytest-asyncio", + "packaging", + # Pinned: ruff's default rule set changes between releases + "ruff==0.16.9", +] [build-system] requires = ["setuptools"] @@ -41,6 +47,10 @@ docs = [ "cogapp" ] +[tool.ruff] +# Ruff's default lint rules and black-compatible formatter settings; +# target-version is inferred from requires-python. + [tool.setuptools.packages.find] where = ["."] include = ["datasette_enrichments"] diff --git a/tests/conftest.py b/tests/conftest.py index c483b66..2b060af 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,13 +1,13 @@ import asyncio -from datasette.database import Database import hashlib import json -from typing import List -from wtforms import Form, SelectField, StringField -from wtforms.widgets import ListWidget, CheckboxInput + import pytest -from datasette.plugins import pm from datasette import hookimpl +from datasette.database import Database +from datasette.plugins import pm +from wtforms import Form, SelectField, StringField +from wtforms.widgets import CheckboxInput, ListWidget class MultiCheckboxField(SelectField): @@ -17,9 +17,10 @@ class MultiCheckboxField(SelectField): @pytest.fixture(autouse=True) def load_uppercase_plugin(): - from datasette_enrichments import Enrichment from datasette_secrets import Secret + from datasette_enrichments import Enrichment + class UppercaseDemo(Enrichment): name = "Convert to uppercase" slug = "uppercasedemo" @@ -44,22 +45,22 @@ async def enrich_batch( datasette, db: Database, table: str, - rows: List[dict], - pks: List[str], + rows: list[dict], + pks: list[str], config: dict, job_id: int, - actor_id: str = None, + actor_id: str | None = None, ): if getattr(datasette, "_trigger_enrich_batch_error", None): - raise Exception("Error in enrich_batch()") + raise Exception("Error in enrich_batch()") # noqa: TRY002 columns = config.get("columns") or [] if not columns: return - wheres = " and ".join('"{}" = ?'.format(pk) for pk in pks) - sets = ", ".join('"{}" = upper("{}")'.format(col, col) for col in columns) + wheres = " and ".join(f'"{pk}" = ?' for pk in pks) + sets = ", ".join(f'"{col}" = upper("{col}")' for col in columns) params = [[row[pk] for pk in pks] for row in rows] await db.execute_write_many( - "update [{}] set {} where {}".format(table, sets, wheres), params + f"update [{table}] set {sets} where {wheres}", params ) # Wait 0.3s await asyncio.sleep(0.3) @@ -87,11 +88,11 @@ async def enrich_batch( datasette, db: Database, table: str, - rows: List[dict], - pks: List[str], + rows: list[dict], + pks: list[str], config: dict, job_id: int, - actor_id: str = None, + actor_id: str | None = None, ): secret = await self.get_secret(datasette, config) for row in rows: @@ -99,7 +100,7 @@ async def enrich_batch( "update [{}] set [{}] = ? where {}".format( table, config["column"], - " and ".join('"{}" = ?'.format(pk) for pk in pks), + " and ".join(f'"{pk}" = ?' for pk in pks), ), [row[config["column"]].replace(config["string"], secret)] + [row[pk] for pk in pks], @@ -111,17 +112,15 @@ class HashRows(Enrichment): description = "To demonstrate an enrichment with no config form" async def initialize(self, datasette, db, table, config): - await db.execute_write( - "alter table [{}] add column sha_256 text".format(table) - ) + await db.execute_write(f"alter table [{table}] add column sha_256 text") async def enrich_batch( self, db: Database, table: str, - rows: List[dict], - pks: List[str], - actor_id: str = None, + rows: list[dict], + pks: list[str], + actor_id: str | None = None, ): for row in rows: to_hash = json.dumps(row, default=repr) @@ -129,7 +128,7 @@ async def enrich_batch( await db.execute_write( "update [{}] set sha_256 = ? where {}".format( table, - " and ".join('"{}" = ?'.format(pk) for pk in pks), + " and ".join(f'"{pk}" = ?' for pk in pks), ), [sha_256] + [row[pk] for pk in pks], ) @@ -144,10 +143,10 @@ async def enrich_batch( self, db: Database, table: str, - rows: List[dict], - pks: List[str], + rows: list[dict], + pks: list[str], job_id: int, - actor_id: str = None, + actor_id: str | None = None, ) -> int: assert len(pks) == 1 pk = pks[0] @@ -176,9 +175,9 @@ async def enrich_batch( datasette, db: Database, table: str, - rows: List[dict], - pks: List[str], - actor_id: str = None, + rows: list[dict], + pks: list[str], + actor_id: str | None = None, ): row = rows[0] result = await datasette.enrichment_queue.get() diff --git a/tests/test_enrichments.py b/tests/test_enrichments.py index 9c9cced..535c742 100644 --- a/tests/test_enrichments.py +++ b/tests/test_enrichments.py @@ -1,13 +1,15 @@ import asyncio -from datasette_enrichments.utils import wait_for_job +import random +import sqlite3 + +import pytest +import pytest_asyncio +from datasette import version from datasette.app import Datasette from datasette.utils import tilde_encode -from datasette import version from packaging.version import parse -import pytest -import pytest_asyncio -import random -import sqlite3 + +from datasette_enrichments.utils import wait_for_job @pytest_asyncio.fixture @@ -56,26 +58,22 @@ async def datasette(tmpdir): async def test_uppercase_plugin(datasette, is_root, table): encoded_table = tilde_encode(table) if not is_root: - response1 = await datasette.client.get( - "/-/enrich/data/{}".format(encoded_table) - ) + response1 = await datasette.client.get(f"/-/enrich/data/{encoded_table}") assert response1.status_code == 403 return cookies = {"ds_actor": datasette.sign({"a": {"id": "root"}}, "actor")} response1 = await datasette.client.get( - "/-/enrich/data/{}".format(encoded_table), cookies=cookies + f"/-/enrich/data/{encoded_table}", cookies=cookies ) assert response1.status_code == 200 assert ( - 'Convert to uppercase'.format( - encoded_table - ) + f'Convert to uppercase' in response1.text ) response2 = await datasette.client.get( - "/-/enrich/data/{}/uppercasedemo".format(encoded_table), cookies=cookies + f"/-/enrich/data/{encoded_table}/uppercasedemo", cookies=cookies ) assert "

Convert to uppercase

" in response2.text @@ -86,13 +84,13 @@ async def test_uppercase_plugin(datasette, is_root, table): assert not hasattr(datasette, "_initialize_called_with") response3 = await datasette.client.post( - "/-/enrich/data/{}/uppercasedemo".format(encoded_table), + f"/-/enrich/data/{encoded_table}/uppercasedemo", cookies=cookies, data={"columns": "s", "csrftoken": csrftoken}, ) assert response3.status_code == 302 assert response3.headers["location"].startswith( - "/data/{}?_enrichment_job=".format(encoded_table) + f"/data/{encoded_table}?_enrichment_job=" ) # It should be queued up job_id = response3.headers["location"].split("=")[-1] @@ -175,10 +173,7 @@ async def test_row_actions(datasette, path, expected_path): cookies = {"ds_actor": datasette.sign({"a": {"id": "root"}}, "actor")} response = await datasette.client.get(path, cookies=cookies) assert response.status_code == 200 - assert ( - 'Enrich this row'.format(expected_path) - in response.text - ) + assert f'Enrich this row' in response.text # And check that page offers to enrich just one row enrich_path = "/-/enrich/data/" + response.text.split('