mirror of
https://github.com/simonw/datasette.git
synced 2026-10-02 23:04:07 +02:00
Compare commits
328 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cec5e6b2ef |
||
|
|
8e17729ff3 |
||
|
|
90f2f1910f | ||
|
|
34a7c28ba3 | ||
|
|
0361afb12e | ||
|
|
aa8c2e36f7 | ||
|
|
5c9aa3e52a |
||
|
|
e4e6f91962 |
||
|
|
b140f4d734 | ||
|
|
83640cf6e6 | ||
|
|
83cef452ea |
||
|
|
d9f9ed5027 |
||
|
|
4d361cadcd | ||
|
|
96d2baec32 | ||
|
|
287e8ba3b2 | ||
|
|
1bba4d9a67 | ||
|
|
d9457d782e | ||
|
|
2588634358 | ||
|
|
b80535c48a | ||
|
|
29a2e6605d | ||
|
|
05ca128c02 | ||
|
|
8614bb7779 |
||
|
|
2cefab7b4c |
||
|
|
8f49def8cb |
||
|
|
000a023aa0 |
||
|
|
22cdcd39a6 |
||
|
|
3024d950d4 |
||
|
|
383fa1ad0a |
||
|
|
90b727db73 |
||
|
|
9bce2eec7c |
||
|
|
daf1989ebe |
||
|
|
5f1b8f3453 |
||
|
|
74f178cd12 |
||
|
|
70be140197 | ||
|
|
3e9356dde0 | ||
|
|
ad1d0bf6f9 | ||
|
|
269c043da3 | ||
|
|
0ad118ba26 | ||
|
|
2474c45f10 | ||
|
|
8220413a8a | ||
|
|
71600f1c0a | ||
|
|
15d511e2da | ||
|
|
e60d1bfe1c | ||
|
|
3b013b7ea3 | ||
|
|
90f543327e | ||
|
|
814165c8b1 | ||
|
|
c82a98c88a | ||
|
|
de37f1451f | ||
|
|
17b19b4d27 | ||
|
|
328b2e6c6f | ||
|
|
c410ed9555 | ||
|
|
e1f89494f1 | ||
|
|
3239d10b9b | ||
|
|
9cdf95ac2c | ||
|
|
ffd3b8cada | ||
|
|
b661889b62 | ||
|
|
1dbbacedda | ||
|
|
2eb2571fee | ||
|
|
caf238aac8 | ||
|
|
c7e9c52e5b | ||
|
|
d2098e9f84 |
||
|
|
266eaddb73 |
||
|
|
926c6ed2cb |
||
|
|
0d0bb5dd8c |
||
|
|
61fd3461c9 | ||
|
|
6c119323d0 | ||
|
|
6dd5297b34 |
||
|
|
df4c0fde0f |
||
|
|
faff4c8820 | ||
|
|
4bf30aaf8e |
||
|
|
27b7240f30 | ||
|
|
7c8ed1015e | ||
|
|
82ed8d47f9 |
||
|
|
f40d838429 | ||
|
|
920adc6880 |
||
|
|
ea3edcc1b9 | ||
|
|
4fe985945d |
||
|
|
a1b73f830b |
||
|
|
ee5b5728bc | ||
|
|
b09976b924 | ||
|
|
5827feff74 | ||
|
|
32845d88d5 | ||
|
|
d60d88488f |
||
|
|
374b194ff5 |
||
|
|
784695aea6 | ||
|
|
cca08d2886 | ||
|
|
0c31778580 | ||
|
|
b0407867cc | ||
|
|
1538832830 | ||
|
|
063eeae83d |
||
|
|
b338c6f5f6 |
||
|
|
61400fba1a | ||
|
|
186be52863 | ||
|
|
36acd1ea92 | ||
|
|
5e7cdaabbd | ||
|
|
92c7d4b608 | ||
|
|
f70edbfa60 | ||
|
|
b97bb5f016 | ||
|
|
e036907fc3 | ||
|
|
3f8d8417f6 | ||
|
|
d334539a1e | ||
|
|
628cec8f0c | ||
|
|
506c4bb522 | ||
|
|
e429bd2efa | ||
|
|
c6ba7b3298 | ||
|
|
ac2a9a43a5 | ||
|
|
9d3d741620 | ||
|
|
ceef351622 | ||
|
|
7e6039b8df | ||
|
|
d43a04eb54 | ||
|
|
8b10f58e1b | ||
|
|
4b8f3b484d | ||
|
|
1be4df77ac | ||
|
|
6aa58bf4e5 | ||
|
|
22c601b3d0 | ||
|
|
e949ae46de | ||
|
|
a365903d56 | ||
|
|
4c56ce2103 | ||
|
|
158c88f259 | ||
|
|
35232b5c37 | ||
|
|
c01e95f3bd | ||
|
|
bf348a22fc | ||
|
|
59618371e9 | ||
|
|
5de0c1724e | ||
|
|
d06737b6f4 | ||
|
|
6473a7ecb0 | ||
|
|
f6d0f9bd38 | ||
|
|
c899beaebe | ||
|
|
3ae092896d | ||
|
|
5d9a74f370 | ||
|
|
01bf476d51 | ||
|
|
4904249025 | ||
|
|
435e55ff0a | ||
|
|
f8e8e65af7 | ||
|
|
577aeb73f0 | ||
|
|
c280c47424 | ||
|
|
4d0a2f2e84 | ||
|
|
c7944fc454 | ||
|
|
bdaa8cc76c | ||
|
|
7403ae68bb | ||
|
|
bdc9731740 |
||
|
|
3e018bb1b5 |
||
|
|
e78b8a2e6a |
||
|
|
0337fba234 |
||
|
|
12b25affb5 | ||
|
|
eb6c2b96b9 | ||
|
|
e889403d3b |
||
|
|
481df7ff6d | ||
|
|
2ffd8a860e | ||
|
|
8b7c942d5e | ||
|
|
591b909a4d |
||
|
|
9cfc252394 |
||
|
|
7f0a8b38ae |
||
|
|
10088dfa1d |
||
|
|
ccace40e5a |
||
|
|
db82123108 | ||
|
|
52ae7d1b6d | ||
|
|
a31673c90b | ||
|
|
54597f22fa | ||
|
|
bf3e277c98 |
||
|
|
211e70d4e1 |
||
|
|
617acedd38 | ||
|
|
7f37205e76 | ||
|
|
a926ab392e | ||
|
|
db7ba1d30c | ||
|
|
96e8b85523 | ||
|
|
6f27aa112a |
||
|
|
d2695a0c2f |
||
|
|
ebd013c6ef | ||
|
|
27a5be1326 |
||
|
|
b7bbde04be |
||
|
|
be25d6e3e4 |
||
|
|
4a853cb10c |
||
|
|
57ce1a059f |
||
|
|
b83b12dd7a |
||
|
|
b23fc4ec48 |
||
|
|
610c24d59a |
||
|
|
4874c29286 |
||
|
|
8b159144a5 |
||
|
|
53ccca5e15 |
||
|
|
0d962deb05 |
||
|
|
60bac9439d |
||
|
|
87cd695ca3 |
||
|
|
022eb6d3a0 |
||
|
|
8154f7149f |
||
|
|
3322e1f528 |
||
|
|
6e17c51361 |
||
|
|
e892c686c2 |
||
|
|
5c418efd7f |
||
|
|
194ee95ae2 |
||
|
|
f4dfd6e0f7 |
||
|
|
b6f5fd5cd0 | ||
|
|
9a0b78b76c | ||
|
|
c833217401 | ||
|
|
557e08c6ef | ||
|
|
6d253d10c8 | ||
|
|
3a0ea58557 |
||
|
|
404ee4c3a7 |
||
|
|
afa7b1ba0d |
||
|
|
5cb2bc6909 |
||
|
|
13dc7a08b7 |
||
|
|
e5e9aca871 |
||
|
|
0bf3a54716 |
||
|
|
e0ba8b3c6a |
||
|
|
9ee95cab3d |
||
|
|
b958d03c0f |
||
|
|
b09dceea88 |
||
|
|
6488b7a30e |
||
|
|
f3f5e891c9 |
||
|
|
ea9c1b1524 |
||
|
|
aaaffe45b8 |
||
|
|
b2cdc81d34 |
||
|
|
e8048e023f |
||
|
|
ae10a99811 |
||
|
|
f091b6dab1 |
||
|
|
23ccdaeffc |
||
|
|
19e54b10d4 |
||
|
|
b74a8e5b12 |
||
|
|
089e96a437 |
||
|
|
bc51c00724 |
||
|
|
0679e04bd3 |
||
|
|
58c07cc264 | ||
|
|
5bcf191e60 |
||
|
|
7ab8b644a7 | ||
|
|
ae26c3372a | ||
|
|
81d6ee69cd | ||
|
|
5f05d33ef7 | ||
|
|
19dde1c860 | ||
|
|
b476218edb | ||
|
|
8856914be8 | ||
|
|
3b24c88e93 | ||
|
|
c9975c1fe1 |
||
|
|
2bcabd8e1f | ||
|
|
8f32a8f134 | ||
|
|
b1d990ceba | ||
|
|
3d67168ee5 | ||
|
|
3aea678eab | ||
|
|
c864bc866d | ||
|
|
ed6235e59a | ||
|
|
141fe194c6 | ||
|
|
4a39e44eb6 | ||
|
|
9c033b7ce9 |
||
|
|
4bd9d41c43 | ||
|
|
2f84ab77f2 |
||
|
|
8985ecf438 |
||
|
|
b759ea5486 | ||
|
|
9ec42b2dad | ||
|
|
e0a138ffbd | ||
|
|
34ab85e664 |
||
|
|
cb622a3dd6 | ||
|
|
7ae601588e | ||
|
|
b7a896a803 | ||
|
|
d621bdfbfe | ||
|
|
fc2922a300 |
||
|
|
488c9cf3d3 | ||
|
|
753fa3b316 | ||
|
|
85be50ac71 | ||
|
|
463eea2bd0 |
||
|
|
a913ba372a | ||
|
|
a5931594de |
||
|
|
22ccd8a087 | ||
|
|
39f1df5997 | ||
|
|
5eca46a4bc |
||
|
|
a4f74d1d2b | ||
|
|
e3ff63b0f9 | ||
|
|
e0cdd38786 |
||
|
|
0c523dda20 | ||
|
|
8276879997 | ||
|
|
0d1c097396 | ||
|
|
34d9a3bf33 | ||
|
|
59ab0c0ca0 | ||
|
|
a43e76c31a | ||
|
|
cda8f7bbef | ||
|
|
17ec88503e | ||
|
|
2680e3c4bd | ||
|
|
4ac795e20c | ||
|
|
29971d9729 | ||
|
|
4d031c8562 | ||
|
|
49b1adba7b | ||
|
|
86ea1d4722 | ||
|
|
f831352551 | ||
|
|
f0645c6ddf |
||
|
|
b3b5c25df8 | ||
|
|
b932d0dc78 | ||
|
|
1717ab02f7 | ||
|
|
2ebae5ed71 | ||
|
|
fa43aba309 | ||
|
|
063b04ad83 | ||
|
|
dada4de172 | ||
|
|
4b219be8bd | ||
|
|
87354cf94e | ||
|
|
a2e75967ce | ||
|
|
084df1fba2 | ||
|
|
c4aead65ee | ||
|
|
17876ccf45 | ||
|
|
b02999bca6 | ||
|
|
a87c4ac555 | ||
|
|
354780a136 | ||
|
|
e834008075 | ||
|
|
c77dad910b | ||
|
|
1f863def5e | ||
|
|
21c156dfb1 | ||
|
|
a6ef65f90d | ||
|
|
2900efb32d | ||
|
|
9d9a2d3ff3 | ||
|
|
9766a9c087 | ||
|
|
1972ba8952 | ||
|
|
8cec528eeb | ||
|
|
4115213e17 | ||
|
|
c9c79fdfc8 | ||
|
|
15a3ac58cc | ||
|
|
fdd1b61a3e | ||
|
|
b40665dd14 | ||
|
|
2d3c85dfc0 | ||
|
|
57e7bba38f | ||
|
|
bccb7f17e5 | ||
|
|
387f4dd4bc | ||
|
|
ad6fe47a95 | ||
|
|
f673e7416f |
||
|
|
dfd5b95ec8 | ||
|
|
8e01542fe9 | ||
|
|
3cc0fc07b4 | ||
|
|
8b89a3aca8 | ||
|
|
63995ce823 | ||
|
|
3ea7ed8606 | ||
|
|
a55ae2adfc | ||
|
|
6a1b237b39 | ||
|
|
435ff7fa88 |
243 changed files with 36117 additions and 6773 deletions
39
.github/actions/setup-sqlite-version/action.yml
vendored
Normal file
39
.github/actions/setup-sqlite-version/action.yml
vendored
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
name: "Setup SQLite version"
|
||||||
|
description: "Build and activate a specific SQLite version from its amalgamation archive"
|
||||||
|
inputs:
|
||||||
|
version:
|
||||||
|
description: "The SQLite version to install"
|
||||||
|
required: true
|
||||||
|
cflags:
|
||||||
|
description: "CFLAGS to use when compiling SQLite"
|
||||||
|
required: false
|
||||||
|
default: ""
|
||||||
|
skip-activate:
|
||||||
|
description: "Set to true to skip modifying the library path"
|
||||||
|
required: false
|
||||||
|
default: "false"
|
||||||
|
fallback-urls:
|
||||||
|
description: "Whitespace-separated fallback download URLs to try after sqlite.org"
|
||||||
|
required: false
|
||||||
|
default: ""
|
||||||
|
outputs:
|
||||||
|
sqlite-location:
|
||||||
|
description: "Directory containing the compiled SQLite library"
|
||||||
|
value: ${{ steps.build.outputs.sqlite-location }}
|
||||||
|
runs:
|
||||||
|
using: "composite"
|
||||||
|
steps:
|
||||||
|
- shell: bash
|
||||||
|
run: mkdir -p "$RUNNER_TEMP/sqlite-versions/downloads"
|
||||||
|
- uses: actions/cache@v6
|
||||||
|
with:
|
||||||
|
path: ${{ runner.temp }}/sqlite-versions/downloads
|
||||||
|
key: setup-sqlite-version-${{ inputs.version }}-amalgamation-v1
|
||||||
|
- id: build
|
||||||
|
shell: bash
|
||||||
|
run: bash "$GITHUB_ACTION_PATH/setup-sqlite-version.sh"
|
||||||
|
env:
|
||||||
|
SQLITE_VERSION: ${{ inputs.version }}
|
||||||
|
SQLITE_CFLAGS: ${{ inputs.cflags }}
|
||||||
|
SQLITE_SKIP_ACTIVATE: ${{ inputs.skip-activate }}
|
||||||
|
SQLITE_EXTRA_FALLBACK_URLS: ${{ inputs.fallback-urls }}
|
||||||
144
.github/actions/setup-sqlite-version/setup-sqlite-version.sh
vendored
Normal file
144
.github/actions/setup-sqlite-version/setup-sqlite-version.sh
vendored
Normal file
|
|
@ -0,0 +1,144 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
version_spec="${SQLITE_VERSION:?SQLITE_VERSION is required}"
|
||||||
|
cflags="${SQLITE_CFLAGS:-}"
|
||||||
|
skip_activate="${SQLITE_SKIP_ACTIVATE:-false}"
|
||||||
|
extra_fallback_urls="${SQLITE_EXTRA_FALLBACK_URLS:-}"
|
||||||
|
|
||||||
|
case "$version_spec" in
|
||||||
|
3.46 | 3.46.0)
|
||||||
|
sqlite_version="3.46.0"
|
||||||
|
sqlite_year="2024"
|
||||||
|
amalgamation_id="3460000"
|
||||||
|
builtin_fallback_urls="https://static.simonwillison.net/static/2026/sqlite-amalgamation-3460000.zip"
|
||||||
|
;;
|
||||||
|
3.25 | 3.25.0)
|
||||||
|
sqlite_version="3.25.0"
|
||||||
|
sqlite_year="2018"
|
||||||
|
amalgamation_id="3250000"
|
||||||
|
builtin_fallback_urls="https://static.simonwillison.net/static/2026/sqlite-amalgamation-3250000.zip?v=1"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
echo "::error::Unsupported SQLite version '$version_spec'. Add its release year and amalgamation id to $GITHUB_ACTION_PATH/setup-sqlite-version.sh."
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
case "$(uname -s)" in
|
||||||
|
Linux)
|
||||||
|
library_name="libsqlite3.so.0"
|
||||||
|
library_path_var="LD_LIBRARY_PATH"
|
||||||
|
;;
|
||||||
|
Darwin)
|
||||||
|
library_name="libsqlite3.dylib"
|
||||||
|
library_path_var="DYLD_LIBRARY_PATH"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
echo "::error::Unsupported platform $(uname -s)"
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
runner_temp="${RUNNER_TEMP:-}"
|
||||||
|
if [ -z "$runner_temp" ]; then
|
||||||
|
runner_temp="$(mktemp -d)"
|
||||||
|
fi
|
||||||
|
|
||||||
|
filename="sqlite-amalgamation-${amalgamation_id}"
|
||||||
|
official_url="https://www.sqlite.org/${sqlite_year}/${filename}.zip"
|
||||||
|
download_dir="${runner_temp}/sqlite-versions/downloads"
|
||||||
|
source_root="${runner_temp}/sqlite-versions/source"
|
||||||
|
source_dir="${source_root}/${filename}"
|
||||||
|
build_dir="${runner_temp}/sqlite-versions/build/${sqlite_version}"
|
||||||
|
archive_path="${download_dir}/${filename}.zip"
|
||||||
|
|
||||||
|
mkdir -p "$download_dir" "$source_root" "$build_dir"
|
||||||
|
|
||||||
|
download_archive() {
|
||||||
|
local url
|
||||||
|
local candidate_path="${archive_path}.tmp"
|
||||||
|
local urls=("$official_url")
|
||||||
|
|
||||||
|
for url in $builtin_fallback_urls $extra_fallback_urls; do
|
||||||
|
urls+=("$url")
|
||||||
|
done
|
||||||
|
|
||||||
|
rm -f "$candidate_path"
|
||||||
|
for url in "${urls[@]}"; do
|
||||||
|
echo "Downloading SQLite ${sqlite_version} amalgamation from ${url}"
|
||||||
|
if curl \
|
||||||
|
--fail \
|
||||||
|
--location \
|
||||||
|
--show-error \
|
||||||
|
--retry 5 \
|
||||||
|
--retry-delay 2 \
|
||||||
|
--retry-max-time 180 \
|
||||||
|
--retry-all-errors \
|
||||||
|
--connect-timeout 20 \
|
||||||
|
--max-time 240 \
|
||||||
|
--output "$candidate_path" \
|
||||||
|
"$url"; then
|
||||||
|
mv "$candidate_path" "$archive_path"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "::warning::Download failed from ${url}"
|
||||||
|
rm -f "$candidate_path"
|
||||||
|
done
|
||||||
|
|
||||||
|
echo "::error::Could not download SQLite ${sqlite_version} amalgamation"
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
if [ ! -f "${source_dir}/sqlite3.c" ]; then
|
||||||
|
if [ ! -f "$archive_path" ]; then
|
||||||
|
download_archive
|
||||||
|
fi
|
||||||
|
|
||||||
|
rm -rf "$source_dir"
|
||||||
|
unzip -q "$archive_path" -d "$source_root"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ ! -f "${source_dir}/sqlite3.c" ]; then
|
||||||
|
echo "::error::Expected ${source_dir}/sqlite3.c after extracting ${archive_path}"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
read -r -a cflag_args <<< "$cflags"
|
||||||
|
|
||||||
|
echo "Compiling SQLite ${sqlite_version} to ${build_dir}/${library_name}"
|
||||||
|
gcc \
|
||||||
|
-fPIC \
|
||||||
|
-shared \
|
||||||
|
"${cflag_args[@]}" \
|
||||||
|
"${source_dir}/sqlite3.c" \
|
||||||
|
"-I${source_dir}" \
|
||||||
|
-o "${build_dir}/${library_name}"
|
||||||
|
|
||||||
|
if [ "$library_name" = "libsqlite3.so.0" ]; then
|
||||||
|
ln -sf "$library_name" "${build_dir}/libsqlite3.so"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -n "${GITHUB_OUTPUT:-}" ]; then
|
||||||
|
echo "sqlite-location=${build_dir}" >> "$GITHUB_OUTPUT"
|
||||||
|
else
|
||||||
|
echo "sqlite-location=${build_dir}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
case "$(printf '%s' "$skip_activate" | tr '[:upper:]' '[:lower:]')" in
|
||||||
|
true | 1 | yes)
|
||||||
|
echo "Skipping ${library_path_var} activation"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
existing_value="${!library_path_var:-}"
|
||||||
|
if [ -n "${GITHUB_ENV:-}" ]; then
|
||||||
|
if [ -n "$existing_value" ]; then
|
||||||
|
echo "${library_path_var}=${build_dir}:${existing_value}" >> "$GITHUB_ENV"
|
||||||
|
else
|
||||||
|
echo "${library_path_var}=${build_dir}" >> "$GITHUB_ENV"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
echo "Added ${build_dir} to ${library_path_var}"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
56
.github/workflows/deploy-latest.yml
vendored
56
.github/workflows/deploy-latest.yml
vendored
|
|
@ -14,24 +14,46 @@ jobs:
|
||||||
deploy:
|
deploy:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
|
- name: Check deployment prerequisites
|
||||||
|
id: deployment-prerequisites
|
||||||
|
env:
|
||||||
|
GCP_SA_KEY: ${{ secrets.GCP_SA_KEY }}
|
||||||
|
LATEST_DATASETTE_SECRET: ${{ secrets.LATEST_DATASETTE_SECRET }}
|
||||||
|
run: |
|
||||||
|
missing=()
|
||||||
|
for variable in GCP_SA_KEY LATEST_DATASETTE_SECRET; do
|
||||||
|
if [[ -z "${!variable:-}" ]]; then
|
||||||
|
missing+=("$variable")
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
if (( ${#missing[@]} )); then
|
||||||
|
echo "::notice::Skipping deployment because required environment variables are missing: ${missing[*]}"
|
||||||
|
echo "available=false" >> "$GITHUB_OUTPUT"
|
||||||
|
else
|
||||||
|
echo "available=true" >> "$GITHUB_OUTPUT"
|
||||||
|
fi
|
||||||
- name: Check out datasette
|
- name: Check out datasette
|
||||||
uses: actions/checkout@v6
|
if: ${{ steps.deployment-prerequisites.outputs.available == 'true' }}
|
||||||
|
uses: actions/checkout@v7
|
||||||
- name: Set up Python
|
- name: Set up Python
|
||||||
|
if: ${{ steps.deployment-prerequisites.outputs.available == 'true' }}
|
||||||
uses: actions/setup-python@v6
|
uses: actions/setup-python@v6
|
||||||
with:
|
with:
|
||||||
python-version: "3.13"
|
python-version: "3.13"
|
||||||
cache: pip
|
cache: pip
|
||||||
- name: Install Python dependencies
|
- name: Install Python dependencies
|
||||||
|
if: ${{ steps.deployment-prerequisites.outputs.available == 'true' }}
|
||||||
run: |
|
run: |
|
||||||
python -m pip install --upgrade pip
|
python -m pip install --upgrade pip
|
||||||
python -m pip install . --group dev
|
python -m pip install . --group dev
|
||||||
python -m pip install sphinx-to-sqlite==0.1a1
|
python -m pip install sphinx-to-sqlite==0.1a1 "s3-credentials>=0.17"
|
||||||
- name: Run tests
|
- name: Run tests
|
||||||
if: ${{ github.ref == 'refs/heads/main' }}
|
if: ${{ steps.deployment-prerequisites.outputs.available == 'true' && github.ref == 'refs/heads/main' }}
|
||||||
run: |
|
run: |
|
||||||
pytest -n auto -m "not serial"
|
pytest -n auto -m "not serial"
|
||||||
pytest -m "serial"
|
pytest -m "serial"
|
||||||
- name: Build fixtures.db and other files needed to deploy the demo
|
- name: Build fixtures.db and other files needed to deploy the demo
|
||||||
|
if: ${{ steps.deployment-prerequisites.outputs.available == 'true' }}
|
||||||
run: |-
|
run: |-
|
||||||
python tests/fixtures.py \
|
python tests/fixtures.py \
|
||||||
fixtures.db \
|
fixtures.db \
|
||||||
|
|
@ -39,14 +61,18 @@ jobs:
|
||||||
fixtures-metadata.json \
|
fixtures-metadata.json \
|
||||||
plugins \
|
plugins \
|
||||||
--extra-db-filename extra_database.db
|
--extra-db-filename extra_database.db
|
||||||
|
# Package the config with the plugins, excluding test-only plugin secrets
|
||||||
|
# that reference temporary files outside the deployed container.
|
||||||
|
jq 'del(.plugins)' fixtures-config.json > plugins/fixtures-config.json
|
||||||
- name: Build docs.db
|
- name: Build docs.db
|
||||||
if: ${{ github.ref == 'refs/heads/main' }}
|
if: ${{ steps.deployment-prerequisites.outputs.available == 'true' && github.ref == 'refs/heads/main' }}
|
||||||
run: |-
|
run: |-
|
||||||
cd docs
|
cd docs
|
||||||
DISABLE_SPHINX_INLINE_TABS=1 sphinx-build -b xml . _build
|
DISABLE_SPHINX_INLINE_TABS=1 sphinx-build -b xml . _build
|
||||||
sphinx-to-sqlite ../docs.db _build
|
sphinx-to-sqlite ../docs.db _build
|
||||||
cd ..
|
cd ..
|
||||||
- name: Set up the alternate-route demo
|
- name: Set up the alternate-route demo
|
||||||
|
if: ${{ steps.deployment-prerequisites.outputs.available == 'true' }}
|
||||||
run: |
|
run: |
|
||||||
echo '
|
echo '
|
||||||
from datasette import hookimpl
|
from datasette import hookimpl
|
||||||
|
|
@ -58,6 +84,7 @@ jobs:
|
||||||
' > plugins/alternative_route.py
|
' > plugins/alternative_route.py
|
||||||
cp fixtures.db fixtures2.db
|
cp fixtures.db fixtures2.db
|
||||||
- name: And the counters writable stored query demo
|
- name: And the counters writable stored query demo
|
||||||
|
if: ${{ steps.deployment-prerequisites.outputs.available == 'true' }}
|
||||||
run: |
|
run: |
|
||||||
cat > plugins/counters.py <<EOF
|
cat > plugins/counters.py <<EOF
|
||||||
from datasette import hookimpl
|
from datasette import hookimpl
|
||||||
|
|
@ -97,12 +124,15 @@ jobs:
|
||||||
# cat metadata.json
|
# cat metadata.json
|
||||||
- id: auth
|
- id: auth
|
||||||
name: Authenticate to Google Cloud
|
name: Authenticate to Google Cloud
|
||||||
|
if: ${{ steps.deployment-prerequisites.outputs.available == 'true' }}
|
||||||
uses: google-github-actions/auth@v3
|
uses: google-github-actions/auth@v3
|
||||||
with:
|
with:
|
||||||
credentials_json: ${{ secrets.GCP_SA_KEY }}
|
credentials_json: ${{ secrets.GCP_SA_KEY }}
|
||||||
- name: Set up Cloud SDK
|
- name: Set up Cloud SDK
|
||||||
|
if: ${{ steps.deployment-prerequisites.outputs.available == 'true' }}
|
||||||
uses: google-github-actions/setup-gcloud@v3
|
uses: google-github-actions/setup-gcloud@v3
|
||||||
- name: Deploy to Cloud Run
|
- name: Deploy to Cloud Run
|
||||||
|
if: ${{ steps.deployment-prerequisites.outputs.available == 'true' }}
|
||||||
env:
|
env:
|
||||||
LATEST_DATASETTE_SECRET: ${{ secrets.LATEST_DATASETTE_SECRET }}
|
LATEST_DATASETTE_SECRET: ${{ secrets.LATEST_DATASETTE_SECRET }}
|
||||||
run: |-
|
run: |-
|
||||||
|
|
@ -117,16 +147,16 @@ jobs:
|
||||||
--plugins-dir=plugins \
|
--plugins-dir=plugins \
|
||||||
--branch=$GITHUB_SHA \
|
--branch=$GITHUB_SHA \
|
||||||
--version-note=$GITHUB_SHA \
|
--version-note=$GITHUB_SHA \
|
||||||
--extra-options="--setting template_debug 1 --setting trace_debug 1 --crossdb --root" \
|
--extra-options="--config plugins/fixtures-config.json --setting template_debug 1 --setting trace_debug 1 --crossdb --root" \
|
||||||
--install 'datasette-ephemeral-tables>=0.2.2' \
|
--install 'datasette-ephemeral-tables>=0.2.2' \
|
||||||
--service "datasette-latest$SUFFIX" \
|
--service "datasette-latest$SUFFIX" \
|
||||||
--secret $LATEST_DATASETTE_SECRET
|
--secret $LATEST_DATASETTE_SECRET
|
||||||
- name: Deploy to docs as well (only for main)
|
- name: Upload latest documentation database to S3 (only for main)
|
||||||
if: ${{ github.ref == 'refs/heads/main' }}
|
if: ${{ steps.deployment-prerequisites.outputs.available == 'true' && github.ref == 'refs/heads/main' }}
|
||||||
|
env:
|
||||||
|
AWS_ACCESS_KEY_ID: ${{ secrets.S3_DATASETTE_DOCS_ACCESS_KEY }}
|
||||||
|
AWS_SECRET_ACCESS_KEY: ${{ secrets.S3_DATASETTE_DOCS_SECRET_KEY }}
|
||||||
run: |-
|
run: |-
|
||||||
# Deploy docs.db to a different service
|
# Keep development documentation separate from the stable release database.
|
||||||
datasette publish cloudrun docs.db \
|
s3-credentials put-object datasette-docs latest/docs.db docs.db \
|
||||||
--branch=$GITHUB_SHA \
|
--content-type application/octet-stream
|
||||||
--version-note=$GITHUB_SHA \
|
|
||||||
--extra-options="--setting template_debug 1" \
|
|
||||||
--service=datasette-docs-latest
|
|
||||||
|
|
|
||||||
16
.github/workflows/documentation-links.yml
vendored
16
.github/workflows/documentation-links.yml
vendored
|
|
@ -1,16 +0,0 @@
|
||||||
name: Read the Docs Pull Request Preview
|
|
||||||
on:
|
|
||||||
pull_request:
|
|
||||||
types:
|
|
||||||
- opened
|
|
||||||
|
|
||||||
permissions:
|
|
||||||
pull-requests: write
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
documentation-links:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: readthedocs/actions/preview@v1
|
|
||||||
with:
|
|
||||||
project-slug: "datasette"
|
|
||||||
12
.github/workflows/playwright.yml
vendored
12
.github/workflows/playwright.yml
vendored
|
|
@ -2,9 +2,15 @@ name: Playwright
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
pull_request:
|
pull_request:
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
|
|
||||||
|
concurrency:
|
||||||
|
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
|
||||||
|
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
contents: read
|
||||||
|
|
||||||
|
|
@ -16,7 +22,7 @@ jobs:
|
||||||
matrix:
|
matrix:
|
||||||
browser: [chromium, firefox, webkit]
|
browser: [chromium, firefox, webkit]
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@v7
|
||||||
- name: Set up Python 3.14
|
- name: Set up Python 3.14
|
||||||
uses: actions/setup-python@v6
|
uses: actions/setup-python@v6
|
||||||
with:
|
with:
|
||||||
|
|
@ -25,14 +31,14 @@ jobs:
|
||||||
cache: pip
|
cache: pip
|
||||||
cache-dependency-path: pyproject.toml
|
cache-dependency-path: pyproject.toml
|
||||||
- name: Cache uv
|
- name: Cache uv
|
||||||
uses: actions/cache@v5
|
uses: actions/cache@v6
|
||||||
with:
|
with:
|
||||||
path: ~/.cache/uv
|
path: ~/.cache/uv
|
||||||
key: ${{ runner.os }}-py3.14-uv-${{ hashFiles('pyproject.toml') }}
|
key: ${{ runner.os }}-py3.14-uv-${{ hashFiles('pyproject.toml') }}
|
||||||
restore-keys: |
|
restore-keys: |
|
||||||
${{ runner.os }}-py3.14-uv-
|
${{ runner.os }}-py3.14-uv-
|
||||||
- name: Cache Playwright browsers
|
- name: Cache Playwright browsers
|
||||||
uses: actions/cache@v5
|
uses: actions/cache@v6
|
||||||
with:
|
with:
|
||||||
path: ~/.cache/ms-playwright/
|
path: ~/.cache/ms-playwright/
|
||||||
key: ${{ runner.os }}-playwright-${{ matrix.browser }}-${{ hashFiles('pyproject.toml') }}
|
key: ${{ runner.os }}-playwright-${{ matrix.browser }}-${{ hashFiles('pyproject.toml') }}
|
||||||
|
|
|
||||||
15
.github/workflows/prettier.yml
vendored
15
.github/workflows/prettier.yml
vendored
|
|
@ -1,6 +1,15 @@
|
||||||
name: Check JavaScript for conformance with Prettier
|
name: Check JavaScript for conformance with Prettier
|
||||||
|
|
||||||
on: [push]
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
pull_request:
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
concurrency:
|
||||||
|
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
|
||||||
|
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
contents: read
|
||||||
|
|
@ -10,8 +19,8 @@ jobs:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Check out repo
|
- name: Check out repo
|
||||||
uses: actions/checkout@v6
|
uses: actions/checkout@v7
|
||||||
- uses: actions/cache@v5
|
- uses: actions/cache@v6
|
||||||
name: Configure npm caching
|
name: Configure npm caching
|
||||||
with:
|
with:
|
||||||
path: ~/.npm
|
path: ~/.npm
|
||||||
|
|
|
||||||
32
.github/workflows/publish.yml
vendored
32
.github/workflows/publish.yml
vendored
|
|
@ -2,7 +2,7 @@ name: Publish Python Package
|
||||||
|
|
||||||
on:
|
on:
|
||||||
release:
|
release:
|
||||||
types: [created]
|
types: [published]
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
contents: read
|
||||||
|
|
@ -14,7 +14,7 @@ jobs:
|
||||||
matrix:
|
matrix:
|
||||||
python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
|
python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@v7
|
||||||
- name: Set up Python ${{ matrix.python-version }}
|
- name: Set up Python ${{ matrix.python-version }}
|
||||||
uses: actions/setup-python@v6
|
uses: actions/setup-python@v6
|
||||||
with:
|
with:
|
||||||
|
|
@ -35,7 +35,7 @@ jobs:
|
||||||
permissions:
|
permissions:
|
||||||
id-token: write
|
id-token: write
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@v7
|
||||||
- name: Set up Python
|
- name: Set up Python
|
||||||
uses: actions/setup-python@v6
|
uses: actions/setup-python@v6
|
||||||
with:
|
with:
|
||||||
|
|
@ -51,12 +51,14 @@ jobs:
|
||||||
- name: Publish
|
- name: Publish
|
||||||
uses: pypa/gh-action-pypi-publish@release/v1
|
uses: pypa/gh-action-pypi-publish@release/v1
|
||||||
|
|
||||||
|
# After the first non-prerelease 1.0 release, disable this job on 0.65.x,
|
||||||
|
# even for later 0.65 releases, so they cannot overwrite the 1.0 stable docs.
|
||||||
deploy_static_docs:
|
deploy_static_docs:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
needs: [deploy]
|
needs: [deploy]
|
||||||
if: "!github.event.release.prerelease"
|
if: "!github.event.release.prerelease"
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@v7
|
||||||
- name: Set up Python
|
- name: Set up Python
|
||||||
uses: actions/setup-python@v6
|
uses: actions/setup-python@v6
|
||||||
with:
|
with:
|
||||||
|
|
@ -66,33 +68,27 @@ jobs:
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: |
|
run: |
|
||||||
python -m pip install . --group dev
|
python -m pip install . --group dev
|
||||||
python -m pip install sphinx-to-sqlite==0.1a1
|
python -m pip install sphinx-to-sqlite==0.1a1 "s3-credentials>=0.17"
|
||||||
- name: Build docs.db
|
- name: Build docs.db
|
||||||
run: |-
|
run: |-
|
||||||
cd docs
|
cd docs
|
||||||
DISABLE_SPHINX_INLINE_TABS=1 sphinx-build -b xml . _build
|
DISABLE_SPHINX_INLINE_TABS=1 sphinx-build -b xml . _build
|
||||||
sphinx-to-sqlite ../docs.db _build
|
sphinx-to-sqlite ../docs.db _build
|
||||||
cd ..
|
cd ..
|
||||||
- id: auth
|
- name: Upload stable documentation database to S3
|
||||||
name: Authenticate to Google Cloud
|
env:
|
||||||
uses: google-github-actions/auth@v2
|
AWS_ACCESS_KEY_ID: ${{ secrets.S3_DATASETTE_DOCS_ACCESS_KEY }}
|
||||||
with:
|
AWS_SECRET_ACCESS_KEY: ${{ secrets.S3_DATASETTE_DOCS_SECRET_KEY }}
|
||||||
credentials_json: ${{ secrets.GCP_SA_KEY }}
|
|
||||||
- name: Set up Cloud SDK
|
|
||||||
uses: google-github-actions/setup-gcloud@v3
|
|
||||||
- name: Deploy stable-docs.datasette.io to Cloud Run
|
|
||||||
run: |-
|
run: |-
|
||||||
gcloud config set run/region us-central1
|
s3-credentials put-object datasette-docs docs.db docs.db \
|
||||||
gcloud config set project datasette-222320
|
--content-type application/octet-stream
|
||||||
datasette publish cloudrun docs.db \
|
|
||||||
--service=datasette-docs-stable
|
|
||||||
|
|
||||||
deploy_docker:
|
deploy_docker:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
needs: [deploy]
|
needs: [deploy]
|
||||||
if: "!github.event.release.prerelease"
|
if: "!github.event.release.prerelease"
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@v7
|
||||||
- name: Build and push to Docker Hub
|
- name: Build and push to Docker Hub
|
||||||
env:
|
env:
|
||||||
DOCKER_USER: ${{ secrets.DOCKER_USER }}
|
DOCKER_USER: ${{ secrets.DOCKER_USER }}
|
||||||
|
|
|
||||||
2
.github/workflows/push_docker_tag.yml
vendored
2
.github/workflows/push_docker_tag.yml
vendored
|
|
@ -13,7 +13,7 @@ jobs:
|
||||||
deploy_docker:
|
deploy_docker:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@v7
|
||||||
- name: Build and push to Docker Hub
|
- name: Build and push to Docker Hub
|
||||||
env:
|
env:
|
||||||
DOCKER_USER: ${{ secrets.DOCKER_USER }}
|
DOCKER_USER: ${{ secrets.DOCKER_USER }}
|
||||||
|
|
|
||||||
13
.github/workflows/spellcheck.yml
vendored
13
.github/workflows/spellcheck.yml
vendored
|
|
@ -1,6 +1,15 @@
|
||||||
name: Check spelling in documentation
|
name: Check spelling in documentation
|
||||||
|
|
||||||
on: [push, pull_request]
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
pull_request:
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
concurrency:
|
||||||
|
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
|
||||||
|
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
contents: read
|
||||||
|
|
@ -9,7 +18,7 @@ jobs:
|
||||||
spellcheck:
|
spellcheck:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@v7
|
||||||
- name: Set up Python
|
- name: Set up Python
|
||||||
uses: actions/setup-python@v6
|
uses: actions/setup-python@v6
|
||||||
with:
|
with:
|
||||||
|
|
|
||||||
2
.github/workflows/stable-docs.yml
vendored
2
.github/workflows/stable-docs.yml
vendored
|
|
@ -15,7 +15,7 @@ jobs:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v6
|
uses: actions/checkout@v7
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0 # We need all commits to find docs/ changes
|
fetch-depth: 0 # We need all commits to find docs/ changes
|
||||||
- name: Set up Git user
|
- name: Set up Git user
|
||||||
|
|
|
||||||
40
.github/workflows/test-coverage.yml
vendored
40
.github/workflows/test-coverage.yml
vendored
|
|
@ -1,40 +0,0 @@
|
||||||
name: Calculate test coverage
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- main
|
|
||||||
pull_request:
|
|
||||||
branches:
|
|
||||||
- main
|
|
||||||
permissions:
|
|
||||||
contents: read
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
test:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: Check out datasette
|
|
||||||
uses: actions/checkout@v6
|
|
||||||
- name: Set up Python
|
|
||||||
uses: actions/setup-python@v6
|
|
||||||
with:
|
|
||||||
python-version: '3.12'
|
|
||||||
cache: 'pip'
|
|
||||||
cache-dependency-path: '**/pyproject.toml'
|
|
||||||
- name: Install Python dependencies
|
|
||||||
run: |
|
|
||||||
python -m pip install --upgrade pip
|
|
||||||
python -m pip install . --group dev
|
|
||||||
python -m pip install pytest-cov
|
|
||||||
- name: Run tests
|
|
||||||
run: |-
|
|
||||||
ls -lah
|
|
||||||
cat .coveragerc
|
|
||||||
pytest -m "not serial" --cov=datasette --cov-config=.coveragerc --cov-report xml:coverage.xml --cov-report term -x
|
|
||||||
ls -lah
|
|
||||||
- name: Upload coverage report
|
|
||||||
uses: codecov/codecov-action@v1
|
|
||||||
with:
|
|
||||||
token: ${{ secrets.CODECOV_TOKEN }}
|
|
||||||
file: coverage.xml
|
|
||||||
10
.github/workflows/test-pyodide.yml
vendored
10
.github/workflows/test-pyodide.yml
vendored
|
|
@ -2,9 +2,15 @@ name: Test in Pyodide with shot-scraper
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
pull_request:
|
pull_request:
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
|
|
||||||
|
concurrency:
|
||||||
|
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
|
||||||
|
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
contents: read
|
||||||
|
|
||||||
|
|
@ -12,7 +18,7 @@ jobs:
|
||||||
test:
|
test:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@v7
|
||||||
- name: Set up Python 3.10
|
- name: Set up Python 3.10
|
||||||
uses: actions/setup-python@v6
|
uses: actions/setup-python@v6
|
||||||
with:
|
with:
|
||||||
|
|
@ -20,7 +26,7 @@ jobs:
|
||||||
cache: 'pip'
|
cache: 'pip'
|
||||||
cache-dependency-path: '**/pyproject.toml'
|
cache-dependency-path: '**/pyproject.toml'
|
||||||
- name: Cache Playwright browsers
|
- name: Cache Playwright browsers
|
||||||
uses: actions/cache@v5
|
uses: actions/cache@v6
|
||||||
with:
|
with:
|
||||||
path: ~/.cache/ms-playwright/
|
path: ~/.cache/ms-playwright/
|
||||||
key: ${{ runner.os }}-browsers
|
key: ${{ runner.os }}-browsers
|
||||||
|
|
|
||||||
19
.github/workflows/test-sqlite-support.yml
vendored
19
.github/workflows/test-sqlite-support.yml
vendored
|
|
@ -1,6 +1,15 @@
|
||||||
name: Test SQLite versions
|
name: Test SQLite versions
|
||||||
|
|
||||||
on: [push, pull_request]
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
pull_request:
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
concurrency:
|
||||||
|
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
|
||||||
|
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
contents: read
|
||||||
|
|
@ -12,10 +21,10 @@ jobs:
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
platform: [ubuntu-latest]
|
platform: [ubuntu-latest]
|
||||||
python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
|
python-version: ["3.13"]
|
||||||
sqlite-version: [
|
sqlite-version: [
|
||||||
#"3", # latest version
|
#"3", # latest version
|
||||||
"3.46",
|
#"3.46",
|
||||||
#"3.45",
|
#"3.45",
|
||||||
#"3.27",
|
#"3.27",
|
||||||
#"3.26",
|
#"3.26",
|
||||||
|
|
@ -25,7 +34,7 @@ jobs:
|
||||||
#"3.23.1" # 2018-04-10, before UPSERT
|
#"3.23.1" # 2018-04-10, before UPSERT
|
||||||
]
|
]
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@v7
|
||||||
- name: Set up Python ${{ matrix.python-version }}
|
- name: Set up Python ${{ matrix.python-version }}
|
||||||
uses: actions/setup-python@v6
|
uses: actions/setup-python@v6
|
||||||
with:
|
with:
|
||||||
|
|
@ -34,7 +43,7 @@ jobs:
|
||||||
cache: pip
|
cache: pip
|
||||||
cache-dependency-path: pyproject.toml
|
cache-dependency-path: pyproject.toml
|
||||||
- name: Set up SQLite ${{ matrix.sqlite-version }}
|
- name: Set up SQLite ${{ matrix.sqlite-version }}
|
||||||
uses: asg017/sqlite-versions@71ea0de37ae739c33e447af91ba71dda8fcf22e6
|
uses: ./.github/actions/setup-sqlite-version
|
||||||
with:
|
with:
|
||||||
version: ${{ matrix.sqlite-version }}
|
version: ${{ matrix.sqlite-version }}
|
||||||
cflags: "-DSQLITE_ENABLE_DESERIALIZE -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_RTREE -DSQLITE_ENABLE_JSON1"
|
cflags: "-DSQLITE_ENABLE_DESERIALIZE -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_RTREE -DSQLITE_ENABLE_JSON1"
|
||||||
|
|
|
||||||
41
.github/workflows/test.yml
vendored
41
.github/workflows/test.yml
vendored
|
|
@ -1,6 +1,15 @@
|
||||||
name: Test
|
name: Test
|
||||||
|
|
||||||
on: [push, pull_request]
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
pull_request:
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
concurrency:
|
||||||
|
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
|
||||||
|
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
contents: read
|
||||||
|
|
@ -9,17 +18,22 @@ jobs:
|
||||||
test:
|
test:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
strategy:
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
|
python-version: ["3.10", "3.11", "3.12", "3.13", "3.14", "3.15"]
|
||||||
|
include:
|
||||||
|
- python-version: "3.14"
|
||||||
|
coverage: true
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@v7
|
||||||
- name: Set up Python ${{ matrix.python-version }}
|
- name: Set up Python ${{ matrix.python-version }}
|
||||||
uses: actions/setup-python@v6
|
uses: actions/setup-python@v7
|
||||||
with:
|
with:
|
||||||
python-version: ${{ matrix.python-version }}
|
python-version: ${{ matrix.python-version }}
|
||||||
allow-prereleases: true
|
allow-prereleases: true
|
||||||
cache: pip
|
cache: pip
|
||||||
cache-dependency-path: pyproject.toml
|
cache-dependency-path: pyproject.toml
|
||||||
|
check-latest: true
|
||||||
- name: Build extension for --load-extension test
|
- name: Build extension for --load-extension test
|
||||||
run: |-
|
run: |-
|
||||||
(cd tests && gcc ext.c -fPIC -shared -o ext.so)
|
(cd tests && gcc ext.c -fPIC -shared -o ext.so)
|
||||||
|
|
@ -27,12 +41,27 @@ jobs:
|
||||||
run: |
|
run: |
|
||||||
pip install . --group dev
|
pip install . --group dev
|
||||||
pip freeze
|
pip freeze
|
||||||
|
- name: Install pytest-cov
|
||||||
|
if: ${{ matrix.coverage }}
|
||||||
|
run: pip install pytest-cov
|
||||||
- name: Run tests
|
- name: Run tests
|
||||||
run: |
|
run: |
|
||||||
pytest -n auto -m "not serial"
|
if [ "${{ matrix.coverage }}" = "true" ]; then
|
||||||
pytest -m "serial"
|
COV="--cov=datasette --cov-config=.coveragerc"
|
||||||
|
pytest -n auto -m "not serial" $COV --cov-report=
|
||||||
|
pytest -m "serial" $COV --cov-append --cov-report xml:coverage.xml --cov-report term
|
||||||
|
else
|
||||||
|
pytest -n auto -m "not serial"
|
||||||
|
pytest -m "serial"
|
||||||
|
fi
|
||||||
# And the test that exceeds a localhost HTTPS server
|
# And the test that exceeds a localhost HTTPS server
|
||||||
tests/test_datasette_https_server.sh
|
tests/test_datasette_https_server.sh
|
||||||
|
- name: Upload coverage report
|
||||||
|
if: ${{ matrix.coverage }}
|
||||||
|
uses: codecov/codecov-action@v5
|
||||||
|
with:
|
||||||
|
token: ${{ secrets.CODECOV_TOKEN }}
|
||||||
|
files: coverage.xml
|
||||||
- name: Black
|
- name: Black
|
||||||
run: |
|
run: |
|
||||||
black --version
|
black --version
|
||||||
|
|
|
||||||
2
.github/workflows/tmate-mac.yml
vendored
2
.github/workflows/tmate-mac.yml
vendored
|
|
@ -10,6 +10,6 @@ jobs:
|
||||||
build:
|
build:
|
||||||
runs-on: macos-latest
|
runs-on: macos-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@v7
|
||||||
- name: Setup tmate session
|
- name: Setup tmate session
|
||||||
uses: mxschmitt/action-tmate@v3
|
uses: mxschmitt/action-tmate@v3
|
||||||
|
|
|
||||||
2
.github/workflows/tmate.yml
vendored
2
.github/workflows/tmate.yml
vendored
|
|
@ -11,7 +11,7 @@ jobs:
|
||||||
build:
|
build:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@v7
|
||||||
- name: Setup tmate session
|
- name: Setup tmate session
|
||||||
uses: mxschmitt/action-tmate@v3
|
uses: mxschmitt/action-tmate@v3
|
||||||
env:
|
env:
|
||||||
|
|
|
||||||
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -5,6 +5,8 @@ datasets.json
|
||||||
|
|
||||||
scratchpad
|
scratchpad
|
||||||
|
|
||||||
|
ignored/
|
||||||
|
|
||||||
.vscode
|
.vscode
|
||||||
|
|
||||||
uv.lock
|
uv.lock
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
FROM python:3.11.0-slim-bullseye as build
|
FROM python:3.11-slim-bookworm AS build
|
||||||
|
|
||||||
# Version of Datasette to install, e.g. 0.55
|
# Version of Datasette to install, e.g. 0.55
|
||||||
# docker build . -t datasette --build-arg VERSION=0.55
|
# docker build . -t datasette --build-arg VERSION=0.55
|
||||||
|
|
|
||||||
10
Justfile
10
Justfile
|
|
@ -33,10 +33,11 @@ export DATASETTE_SECRET := "not_a_secret"
|
||||||
uv run codespell datasette -S datasette/static --ignore-words docs/codespell-ignore-words.txt
|
uv run codespell datasette -S datasette/static --ignore-words docs/codespell-ignore-words.txt
|
||||||
uv run codespell tests --ignore-words docs/codespell-ignore-words.txt
|
uv run codespell tests --ignore-words docs/codespell-ignore-words.txt
|
||||||
|
|
||||||
# Run linters: black, ruff, cog
|
# Run linters: black, ruff, prettier, cog
|
||||||
@lint: codespell
|
@lint: codespell
|
||||||
uv run black datasette tests --check
|
uv run black datasette tests --check
|
||||||
uv run ruff check datasette tests
|
uv run ruff check datasette tests
|
||||||
|
npm run prettier -- --check
|
||||||
uv run cog --check README.md docs/*.rst
|
uv run cog --check README.md docs/*.rst
|
||||||
|
|
||||||
# Apply ruff fixes
|
# Apply ruff fixes
|
||||||
|
|
@ -48,13 +49,18 @@ export DATASETTE_SECRET := "not_a_secret"
|
||||||
uv run cog -r README.md docs/*.rst
|
uv run cog -r README.md docs/*.rst
|
||||||
|
|
||||||
# Serve live docs on localhost:8000
|
# Serve live docs on localhost:8000
|
||||||
@docs: cog blacken-docs
|
@docs: shots cog blacken-docs
|
||||||
uv run make -C docs livehtml
|
uv run make -C docs livehtml
|
||||||
|
|
||||||
# Build docs as static HTML
|
# Build docs as static HTML
|
||||||
@docs-build: cog blacken-docs
|
@docs-build: cog blacken-docs
|
||||||
rm -rf docs/_build && cd docs && uv run make html
|
rm -rf docs/_build && cd docs && uv run make html
|
||||||
|
|
||||||
|
# Take any missing documentation screenshots defined in docs/shots.yml
|
||||||
|
@shots:
|
||||||
|
uv run --group shots shot-scraper install
|
||||||
|
cd docs && uv run --group shots shot-scraper multi shots.yml --no-clobber --reduced-motion --retina
|
||||||
|
|
||||||
# Apply Black
|
# Apply Black
|
||||||
@black:
|
@black:
|
||||||
uv run black datasette tests
|
uv run black datasette tests
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,7 @@ You can also install it using `pip` or `pipx`:
|
||||||
|
|
||||||
pip install datasette
|
pip install datasette
|
||||||
|
|
||||||
Datasette requires Python 3.8 or higher. We also have [detailed installation instructions](https://docs.datasette.io/en/stable/installation.html) covering other options such as Docker.
|
Datasette requires Python 3.10 or higher. We also have [detailed installation instructions](https://docs.datasette.io/en/stable/installation.html) covering other options such as Docker.
|
||||||
|
|
||||||
## Basic usage
|
## Basic usage
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,15 @@
|
||||||
from datasette.permissions import Permission # noqa
|
from datasette.permissions import Permission # noqa
|
||||||
from datasette.version import __version_info__, __version__ # noqa
|
from datasette.version import __version_info__, __version__ # noqa
|
||||||
from datasette.events import Event # noqa
|
from datasette.events import Event # noqa
|
||||||
from datasette.tokens import TokenHandler, TokenRestrictions # noqa
|
from datasette.background_tasks import BackgroundTask, BackgroundTaskSupervisor # noqa
|
||||||
from datasette.utils.asgi import Forbidden, NotFound, Request, Response # noqa
|
from datasette.tokens import TokenHandler, TokenInvalid, TokenRestrictions # noqa
|
||||||
|
from datasette.utils.asgi import ( # noqa
|
||||||
|
Forbidden,
|
||||||
|
NotFound,
|
||||||
|
PayloadTooLarge,
|
||||||
|
Request,
|
||||||
|
Response,
|
||||||
|
)
|
||||||
from datasette.utils import actor_matches_allow # noqa
|
from datasette.utils import actor_matches_allow # noqa
|
||||||
from datasette.views import Context # noqa
|
from datasette.views import Context # noqa
|
||||||
from .hookspecs import hookimpl # noqa
|
from .hookspecs import hookimpl # noqa
|
||||||
|
|
|
||||||
|
|
@ -89,7 +89,8 @@ def pytest_runtest_protocol(item, nextitem):
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
ds.close()
|
ds.close()
|
||||||
except Exception as e:
|
except Exception as e: # noqa: BLE001
|
||||||
|
# Surfaced as a pytest warning; teardown must not fail the run
|
||||||
item.warn(
|
item.warn(
|
||||||
pytest.PytestUnraisableExceptionWarning(
|
pytest.PytestUnraisableExceptionWarning(
|
||||||
f"Error closing Datasette instance: {e!r}"
|
f"Error closing Datasette instance: {e!r}"
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,10 @@
|
||||||
from datasette import hookimpl
|
|
||||||
from itsdangerous import BadSignature
|
|
||||||
from datasette.utils import baseconv
|
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
from itsdangerous import BadSignature
|
||||||
|
|
||||||
|
from datasette import hookimpl
|
||||||
|
from datasette.utils import baseconv
|
||||||
|
|
||||||
|
|
||||||
@hookimpl
|
@hookimpl
|
||||||
def actor_from_request(datasette, request):
|
def actor_from_request(datasette, request):
|
||||||
|
|
|
||||||
1160
datasette/app.py
1160
datasette/app.py
File diff suppressed because it is too large
Load diff
227
datasette/background_tasks.py
Normal file
227
datasette/background_tasks.py
Normal file
|
|
@ -0,0 +1,227 @@
|
||||||
|
"""
|
||||||
|
Supervised background-task registration for Datasette core.
|
||||||
|
|
||||||
|
Plugins that need long-lived background work (a polling loop, a queue
|
||||||
|
consumer, a scheduled job runner) register it with
|
||||||
|
``datasette.add_background_task(func, name=None)`` - typically from a
|
||||||
|
``startup`` plugin hook - instead of fire-and-forgetting their own
|
||||||
|
``asyncio.create_task()``. Core owns:
|
||||||
|
|
||||||
|
- **references**: every launched ``asyncio.Task`` is kept alive on a
|
||||||
|
:class:`BackgroundTaskSupervisor`, so it can never be silently garbage
|
||||||
|
collected the way an unreferenced ``create_task()`` call can be;
|
||||||
|
- **launch timing**: registered work is buffered until
|
||||||
|
:meth:`BackgroundTaskSupervisor.launch_all` runs, which core arranges to
|
||||||
|
happen only after *every* plugin's ``startup`` hook has finished - so
|
||||||
|
a task that depends on another plugin having registered something first
|
||||||
|
doesn't need ``tryfirst=True`` ordering tricks;
|
||||||
|
- **crash surfacing**: an unhandled exception in a background task is
|
||||||
|
logged with its full traceback to the ``datasette.background_tasks``
|
||||||
|
logger and recorded on the handle, instead of becoming an "Task
|
||||||
|
exception was never retrieved" warning nobody sees;
|
||||||
|
- **cancellation**: :meth:`BackgroundTaskSupervisor.cancel_all` cancels
|
||||||
|
every task still running and waits (with a grace period) for them to
|
||||||
|
actually stop.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import datetime
|
||||||
|
import functools
|
||||||
|
import logging
|
||||||
|
from collections.abc import Awaitable, Callable
|
||||||
|
|
||||||
|
logger = logging.getLogger("datasette.background_tasks")
|
||||||
|
|
||||||
|
|
||||||
|
def _utcnow_iso() -> str:
|
||||||
|
return datetime.datetime.now(datetime.timezone.utc).isoformat()
|
||||||
|
|
||||||
|
|
||||||
|
def _function_path(func: Callable) -> str:
|
||||||
|
"""Describe the callable without guessing which plugin registered it."""
|
||||||
|
while isinstance(func, functools.partial):
|
||||||
|
func = func.func
|
||||||
|
if not hasattr(func, "__qualname__"):
|
||||||
|
func = type(func).__call__
|
||||||
|
return f"{func.__module__}.{func.__qualname__}"
|
||||||
|
|
||||||
|
|
||||||
|
class BackgroundTask:
|
||||||
|
"""A handle to a single piece of supervised background work.
|
||||||
|
|
||||||
|
States: ``registered`` (added but not yet launched) -> ``running`` ->
|
||||||
|
one of ``completed`` (returned cleanly), ``crashed`` (raised an
|
||||||
|
exception other than ``CancelledError`` - see ``.exception``), or
|
||||||
|
``cancelled`` (``.cancel()`` was called, or it was still running at
|
||||||
|
shutdown).
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
name: str,
|
||||||
|
func: Callable[[object], Awaitable[None]],
|
||||||
|
):
|
||||||
|
self.name = name
|
||||||
|
self.state = "registered"
|
||||||
|
self.task: asyncio.Task | None = None
|
||||||
|
self.exception: BaseException | None = None
|
||||||
|
self.started_at: str | None = None
|
||||||
|
self.function = _function_path(func)
|
||||||
|
self._func = func
|
||||||
|
self._supervisor: BackgroundTaskSupervisor | None = None
|
||||||
|
|
||||||
|
def cancel(self) -> None:
|
||||||
|
"""Cancel this task.
|
||||||
|
|
||||||
|
If it has already been launched, cancels the underlying
|
||||||
|
``asyncio.Task`` - its state becomes ``cancelled`` once the
|
||||||
|
cancellation is observed (asynchronously, via the task's done
|
||||||
|
callback). If it has not been launched yet, this is a no-op as
|
||||||
|
far as asyncio is concerned (there's no task to cancel) but it
|
||||||
|
deregisters the handle from its supervisor so it never runs.
|
||||||
|
"""
|
||||||
|
if self.task is not None:
|
||||||
|
self.task.cancel()
|
||||||
|
elif self._supervisor is not None:
|
||||||
|
self._supervisor._deregister(self)
|
||||||
|
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
return f"<BackgroundTask name={self.name!r} state={self.state!r}>"
|
||||||
|
|
||||||
|
|
||||||
|
class BackgroundTaskSupervisor:
|
||||||
|
"""Owns registration and launch of every :class:`BackgroundTask` for a
|
||||||
|
single ``Datasette`` instance.
|
||||||
|
|
||||||
|
Registration (:meth:`add`) is separate from launch
|
||||||
|
(:meth:`launch_all`): plugins register work whenever convenient
|
||||||
|
(typically from a ``startup`` hook, but request handlers can register
|
||||||
|
dynamic per-job work too), and it either sits buffered until
|
||||||
|
:meth:`launch_all` runs, or - if :meth:`launch_all` has already run -
|
||||||
|
starts immediately.
|
||||||
|
|
||||||
|
Strong references to every :class:`BackgroundTask` (and its
|
||||||
|
``asyncio.Task``) are kept for the life of the instance, by design -
|
||||||
|
that's what makes the enrichments-style "fire-and-forget task gets
|
||||||
|
garbage collected mid-flight" bug impossible here. There is currently
|
||||||
|
no pruning of completed/crashed/cancelled tasks, so a plugin that
|
||||||
|
dynamically registers many short-lived tasks over a long process
|
||||||
|
lifetime (a per-job registration pattern, e.g. one task per queued
|
||||||
|
job) will grow this list without bound. That's an accepted v1
|
||||||
|
trade-off in favour of full introspection (``/-/tasks``); revisit
|
||||||
|
with a pruning or capping policy if unbounded growth is reported in
|
||||||
|
practice.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, datasette):
|
||||||
|
self._datasette = datasette
|
||||||
|
self._tasks: list[BackgroundTask] = []
|
||||||
|
self._names = set()
|
||||||
|
self._launched = False
|
||||||
|
self._lock = asyncio.Lock()
|
||||||
|
|
||||||
|
def add(self, func, name=None) -> BackgroundTask:
|
||||||
|
base_name = name or getattr(func, "__qualname__", None) or repr(func)
|
||||||
|
actual_name = self._unique_name(base_name)
|
||||||
|
handle = BackgroundTask(actual_name, func)
|
||||||
|
handle._supervisor = self
|
||||||
|
self._tasks.append(handle)
|
||||||
|
self._names.add(actual_name)
|
||||||
|
if self._launched:
|
||||||
|
self._launch_one(handle)
|
||||||
|
return handle
|
||||||
|
|
||||||
|
def _unique_name(self, base_name: str) -> str:
|
||||||
|
if base_name not in self._names:
|
||||||
|
return base_name
|
||||||
|
n = 2
|
||||||
|
while f"{base_name}-{n}" in self._names:
|
||||||
|
n += 1
|
||||||
|
return f"{base_name}-{n}"
|
||||||
|
|
||||||
|
def _deregister(self, handle: BackgroundTask) -> None:
|
||||||
|
try:
|
||||||
|
self._tasks.remove(handle)
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
self._names.discard(handle.name)
|
||||||
|
|
||||||
|
def _launch_one(self, handle: BackgroundTask) -> None:
|
||||||
|
handle.state = "running"
|
||||||
|
handle.started_at = _utcnow_iso()
|
||||||
|
handle.task = asyncio.create_task(
|
||||||
|
handle._func(self._datasette), name=handle.name
|
||||||
|
)
|
||||||
|
handle.task.add_done_callback(functools.partial(_on_task_done, handle))
|
||||||
|
|
||||||
|
async def launch_all(self) -> None:
|
||||||
|
"""Launch every currently-registered task that hasn't launched
|
||||||
|
yet. Idempotent and safe to call concurrently: subsequent (or
|
||||||
|
racing) calls are no-ops once the first has set ``self._launched``.
|
||||||
|
"""
|
||||||
|
if self._launched:
|
||||||
|
return
|
||||||
|
async with self._lock:
|
||||||
|
if self._launched:
|
||||||
|
return
|
||||||
|
self._launched = True
|
||||||
|
for handle in list(self._tasks):
|
||||||
|
if handle.task is None:
|
||||||
|
self._launch_one(handle)
|
||||||
|
|
||||||
|
async def cancel_all(self, grace: float = 5.0) -> None:
|
||||||
|
"""Cancel every task that isn't already done, then wait up to
|
||||||
|
``grace`` seconds for them to actually finish. Stragglers still
|
||||||
|
running after that are logged by name (but left to finish or not
|
||||||
|
on their own - this does not forcibly kill them, asyncio has no
|
||||||
|
mechanism for that).
|
||||||
|
"""
|
||||||
|
handles_by_task = {
|
||||||
|
handle.task: handle for handle in self._tasks if handle.task is not None
|
||||||
|
}
|
||||||
|
pending = [task for task in handles_by_task if not task.done()]
|
||||||
|
for task in pending:
|
||||||
|
task.cancel()
|
||||||
|
if not pending:
|
||||||
|
return
|
||||||
|
_done, not_done = await asyncio.wait(pending, timeout=grace)
|
||||||
|
if not_done:
|
||||||
|
names = sorted(handles_by_task[task].name for task in not_done)
|
||||||
|
logger.warning(
|
||||||
|
"%d background task(s) did not finish within the %.1fs grace "
|
||||||
|
"period after cancellation: %s",
|
||||||
|
len(names),
|
||||||
|
grace,
|
||||||
|
", ".join(names),
|
||||||
|
)
|
||||||
|
|
||||||
|
def tasks(self) -> list[BackgroundTask]:
|
||||||
|
"""Return every registered :class:`BackgroundTask`, launched or
|
||||||
|
not, in registration order. Used by the ``/-/tasks`` debug
|
||||||
|
endpoint.
|
||||||
|
"""
|
||||||
|
return list(self._tasks)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def launched(self) -> bool:
|
||||||
|
"""Whether :meth:`launch_all` has run yet - lets ``/-/tasks``
|
||||||
|
distinguish "no tasks registered" from "tasks registered but
|
||||||
|
nothing has armed the launch yet" without reaching for the
|
||||||
|
private ``_launched`` attribute.
|
||||||
|
"""
|
||||||
|
return self._launched
|
||||||
|
|
||||||
|
|
||||||
|
def _on_task_done(handle: BackgroundTask, task: asyncio.Task) -> None:
|
||||||
|
if task.cancelled():
|
||||||
|
handle.state = "cancelled"
|
||||||
|
return
|
||||||
|
exc = task.exception()
|
||||||
|
if exc is not None:
|
||||||
|
handle.state = "crashed"
|
||||||
|
handle.exception = exc
|
||||||
|
logger.error("Background task %r crashed", handle.name, exc_info=exc)
|
||||||
|
return
|
||||||
|
handle.state = "completed"
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
from datasette import hookimpl
|
|
||||||
from datasette.utils.asgi import Response, BadRequest
|
|
||||||
from datasette.utils import to_css_class
|
|
||||||
import hashlib
|
import hashlib
|
||||||
|
|
||||||
|
from datasette import hookimpl
|
||||||
|
from datasette.utils import to_css_class
|
||||||
|
from datasette.utils.asgi import BadRequest, Response
|
||||||
|
|
||||||
_BLOB_COLUMN = "_blob_column"
|
_BLOB_COLUMN = "_blob_column"
|
||||||
_BLOB_HASH = "_blob_hash"
|
_BLOB_HASH = "_blob_hash"
|
||||||
|
|
||||||
|
|
|
||||||
191
datasette/cli.py
191
datasette/cli.py
|
|
@ -1,43 +1,45 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import uvicorn
|
|
||||||
import click
|
|
||||||
from click import formatting
|
|
||||||
from click.types import CompositeParamType
|
|
||||||
from click_default_group import DefaultGroup
|
|
||||||
import functools
|
import functools
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import pathlib
|
import pathlib
|
||||||
from runpy import run_module
|
|
||||||
import shutil
|
import shutil
|
||||||
from subprocess import call
|
|
||||||
import sys
|
import sys
|
||||||
import textwrap
|
import textwrap
|
||||||
import webbrowser
|
import webbrowser
|
||||||
|
from runpy import run_module
|
||||||
|
from subprocess import call
|
||||||
|
|
||||||
|
import click
|
||||||
|
import uvicorn
|
||||||
|
from click import formatting
|
||||||
|
from click.types import CompositeParamType
|
||||||
|
from click_default_group import DefaultGroup
|
||||||
|
|
||||||
from .app import (
|
from .app import (
|
||||||
Datasette,
|
|
||||||
DEFAULT_SETTINGS,
|
DEFAULT_SETTINGS,
|
||||||
SETTINGS,
|
SETTINGS,
|
||||||
SQLITE_LIMIT_ATTACHED,
|
SQLITE_LIMIT_ATTACHED,
|
||||||
|
Datasette,
|
||||||
pm,
|
pm,
|
||||||
)
|
)
|
||||||
from .inspect import inspect_tables
|
from .inspect import inspect_tables
|
||||||
from .utils import (
|
from .utils import (
|
||||||
|
ConnectionProblem,
|
||||||
LoadExtension,
|
LoadExtension,
|
||||||
|
SpatialiteConnectionProblem,
|
||||||
|
SpatialiteNotFound,
|
||||||
StartupError,
|
StartupError,
|
||||||
|
StaticMount,
|
||||||
|
ValueAsBooleanError,
|
||||||
check_connection,
|
check_connection,
|
||||||
deep_dict_update,
|
deep_dict_update,
|
||||||
find_spatialite,
|
find_spatialite,
|
||||||
parse_metadata,
|
|
||||||
ConnectionProblem,
|
|
||||||
SpatialiteConnectionProblem,
|
|
||||||
initial_path_for_datasette,
|
initial_path_for_datasette,
|
||||||
pairs_to_nested_config,
|
pairs_to_nested_config,
|
||||||
|
parse_metadata,
|
||||||
temporary_docker_directory,
|
temporary_docker_directory,
|
||||||
value_as_boolean,
|
value_as_boolean,
|
||||||
SpatialiteNotFound,
|
|
||||||
StaticMount,
|
|
||||||
ValueAsBooleanError,
|
|
||||||
)
|
)
|
||||||
from .utils.sqlite import sqlite3
|
from .utils.sqlite import sqlite3
|
||||||
from .utils.testing import TestClient
|
from .utils.testing import TestClient
|
||||||
|
|
@ -75,7 +77,7 @@ class Setting(CompositeParamType):
|
||||||
# Datasette 1.0, we turn bare setting names into setting.name
|
# Datasette 1.0, we turn bare setting names into setting.name
|
||||||
# Type checking for those older settings
|
# Type checking for those older settings
|
||||||
default = DEFAULT_SETTINGS[name]
|
default = DEFAULT_SETTINGS[name]
|
||||||
name = "settings.{}".format(name)
|
name = f"settings.{name}"
|
||||||
if isinstance(default, bool):
|
if isinstance(default, bool):
|
||||||
try:
|
try:
|
||||||
return name, "true" if value_as_boolean(value) else "false"
|
return name, "true" if value_as_boolean(value) else "false"
|
||||||
|
|
@ -155,7 +157,11 @@ async def inspect_(files, sqlite_extensions):
|
||||||
app = Datasette([], immutables=files, sqlite_extensions=sqlite_extensions)
|
app = Datasette([], immutables=files, sqlite_extensions=sqlite_extensions)
|
||||||
data = {}
|
data = {}
|
||||||
for name, database in app.databases.items():
|
for name, database in app.databases.items():
|
||||||
tables = await database.execute_fn(lambda conn: inspect_tables(conn, {}))
|
|
||||||
|
def _inspect_tables(conn):
|
||||||
|
return inspect_tables(conn, {})
|
||||||
|
|
||||||
|
tables = await database.execute_fn(_inspect_tables)
|
||||||
data[name] = {
|
data[name] = {
|
||||||
"hash": database.hash,
|
"hash": database.hash,
|
||||||
"size": database.size,
|
"size": database.size,
|
||||||
|
|
@ -171,7 +177,6 @@ async def inspect_(files, sqlite_extensions):
|
||||||
@cli.group()
|
@cli.group()
|
||||||
def publish():
|
def publish():
|
||||||
"""Publish specified SQLite database files to the internet along with a Datasette-powered interface and API"""
|
"""Publish specified SQLite database files to the internet along with a Datasette-powered interface and API"""
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
# Register publish plugins
|
# Register publish plugins
|
||||||
|
|
@ -496,6 +501,7 @@ def uninstall(packages, yes):
|
||||||
"--internal",
|
"--internal",
|
||||||
type=click.Path(),
|
type=click.Path(),
|
||||||
help="Path to a persistent Datasette internal SQLite database",
|
help="Path to a persistent Datasette internal SQLite database",
|
||||||
|
envvar="DATASETTE_INTERNAL",
|
||||||
)
|
)
|
||||||
def serve(
|
def serve(
|
||||||
files,
|
files,
|
||||||
|
|
@ -578,27 +584,27 @@ def serve(
|
||||||
# https://github.com/simonw/datasette/issues/2389
|
# https://github.com/simonw/datasette/issues/2389
|
||||||
deep_dict_update(config_data, settings_updates)
|
deep_dict_update(config_data, settings_updates)
|
||||||
|
|
||||||
kwargs = dict(
|
kwargs = {
|
||||||
immutables=immutable,
|
"immutables": immutable,
|
||||||
cache_headers=not reload,
|
"cache_headers": not reload,
|
||||||
cors=cors,
|
"cors": cors,
|
||||||
inspect_data=inspect_data,
|
"inspect_data": inspect_data,
|
||||||
config=config_data,
|
"config": config_data,
|
||||||
metadata=metadata_data,
|
"metadata": metadata_data,
|
||||||
sqlite_extensions=sqlite_extensions,
|
"sqlite_extensions": sqlite_extensions,
|
||||||
template_dir=template_dir,
|
"template_dir": template_dir,
|
||||||
plugins_dir=plugins_dir,
|
"plugins_dir": plugins_dir,
|
||||||
static_mounts=static,
|
"static_mounts": static,
|
||||||
settings=None, # These are passed in config= now
|
"settings": None, # These are passed in config= now
|
||||||
memory=memory,
|
"memory": memory,
|
||||||
secret=secret,
|
"secret": secret,
|
||||||
version_note=version_note,
|
"version_note": version_note,
|
||||||
pdb=pdb,
|
"pdb": pdb,
|
||||||
crossdb=crossdb,
|
"crossdb": crossdb,
|
||||||
nolock=nolock,
|
"nolock": nolock,
|
||||||
internal=internal,
|
"internal": internal,
|
||||||
default_deny=default_deny,
|
"default_deny": default_deny,
|
||||||
)
|
}
|
||||||
|
|
||||||
# Separate directories from files
|
# Separate directories from files
|
||||||
directories = [f for f in files if os.path.isdir(f)]
|
directories = [f for f in files if os.path.isdir(f)]
|
||||||
|
|
@ -621,9 +627,7 @@ def serve(
|
||||||
conn.close()
|
conn.close()
|
||||||
else:
|
else:
|
||||||
raise click.ClickException(
|
raise click.ClickException(
|
||||||
"Invalid value for '[FILES]...': Path '{}' does not exist.".format(
|
f"Invalid value for '[FILES]...': Path '{file}' does not exist."
|
||||||
file
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Check for duplicate files by resolving all paths to their absolute forms
|
# Check for duplicate files by resolving all paths to their absolute forms
|
||||||
|
|
@ -664,16 +668,6 @@ def serve(
|
||||||
# Private utility mechanism for writing unit tests
|
# Private utility mechanism for writing unit tests
|
||||||
return ds
|
return ds
|
||||||
|
|
||||||
# Run async soundness checks before startup hooks, since invoke_startup
|
|
||||||
# now populates internal tables which requires querying each database
|
|
||||||
run_sync(lambda: check_databases(ds))
|
|
||||||
|
|
||||||
# Run the "startup" plugin hooks
|
|
||||||
try:
|
|
||||||
run_sync(ds.invoke_startup)
|
|
||||||
except StartupError as e:
|
|
||||||
raise click.ClickException(e.args[0])
|
|
||||||
|
|
||||||
if headers and not get:
|
if headers and not get:
|
||||||
raise click.ClickException("--headers can only be used with --get")
|
raise click.ClickException("--headers can only be used with --get")
|
||||||
|
|
||||||
|
|
@ -681,10 +675,23 @@ def serve(
|
||||||
raise click.ClickException("--token can only be used with --get")
|
raise click.ClickException("--token can only be used with --get")
|
||||||
|
|
||||||
if get:
|
if get:
|
||||||
|
# --get means we don't run Uvicorn at all
|
||||||
|
run_sync(lambda: check_databases(ds))
|
||||||
|
|
||||||
|
try:
|
||||||
|
run_sync(ds.invoke_startup)
|
||||||
|
except StartupError as e:
|
||||||
|
raise click.ClickException(e.args[0])
|
||||||
|
|
||||||
|
# --get never launches background tasks: TestClient's request below
|
||||||
|
# flows through the full ASGI stack, including the
|
||||||
|
# AsgiRunOnFirstRequest fallback, which would otherwise launch them.
|
||||||
|
ds._suppress_background_tasks = True
|
||||||
|
|
||||||
client = TestClient(ds)
|
client = TestClient(ds)
|
||||||
request_headers = {}
|
request_headers = {}
|
||||||
if token:
|
if token:
|
||||||
request_headers["Authorization"] = "Bearer {}".format(token)
|
request_headers["Authorization"] = f"Bearer {token}"
|
||||||
cookies = {}
|
cookies = {}
|
||||||
if actor:
|
if actor:
|
||||||
cookies["ds_actor"] = client.actor_cookie(json.loads(actor))
|
cookies["ds_actor"] = client.actor_cookie(json.loads(actor))
|
||||||
|
|
@ -705,30 +712,54 @@ def serve(
|
||||||
sys.exit(exit_code)
|
sys.exit(exit_code)
|
||||||
return
|
return
|
||||||
|
|
||||||
# Start the server
|
# check_databases, invoke_startup() and the uvicorn server all run on a
|
||||||
url = None
|
# single event loop, so that anything a plugin's "startup" hook schedules
|
||||||
if root:
|
# on the loop (asyncio.create_task, Lock/Queue/Event objects, ...) is
|
||||||
ds.root_enabled = True
|
# still alive when the server starts handling requests.
|
||||||
url = "http://{}:{}{}?token={}".format(
|
async def _serve_async():
|
||||||
host, port, ds.urls.path("-/auth-token"), ds._root_token
|
# Populate internal catalog tables before invoke_startup
|
||||||
)
|
await check_databases(ds)
|
||||||
click.echo(url)
|
|
||||||
if open_browser:
|
# Run the full startup sequence (immutable-database table-count
|
||||||
if url is None:
|
# precompute + the "startup" plugin hooks) via the same entry point
|
||||||
# Figure out most convenient URL - to table, database or homepage
|
# AsgiLifespan/AsgiRunOnFirstRequest use, so it's not skipped when
|
||||||
path = run_sync(lambda: initial_path_for_datasette(ds))
|
# uvicorn's lifespan.startup fires moments later.
|
||||||
url = f"http://{host}:{port}{path}"
|
try:
|
||||||
webbrowser.open(url)
|
await ds._startup_sequence()
|
||||||
uvicorn_kwargs = dict(
|
except StartupError as e:
|
||||||
host=host, port=port, log_level="info", lifespan="on", workers=1
|
raise click.ClickException(e.args[0])
|
||||||
)
|
|
||||||
if uds:
|
# Start the server
|
||||||
uvicorn_kwargs["uds"] = uds
|
url = None
|
||||||
if ssl_keyfile:
|
if root:
|
||||||
uvicorn_kwargs["ssl_keyfile"] = ssl_keyfile
|
ds.root_enabled = True
|
||||||
if ssl_certfile:
|
url = "http://{}:{}{}?token={}".format(
|
||||||
uvicorn_kwargs["ssl_certfile"] = ssl_certfile
|
host, port, ds.urls.path("-/auth-token"), ds._root_token
|
||||||
uvicorn.run(ds.app(), **uvicorn_kwargs)
|
)
|
||||||
|
click.echo(url)
|
||||||
|
if open_browser:
|
||||||
|
if url is None:
|
||||||
|
# Figure out most convenient URL - to table, database or homepage
|
||||||
|
path = await initial_path_for_datasette(ds)
|
||||||
|
url = f"http://{host}:{port}{path}"
|
||||||
|
webbrowser.open(url)
|
||||||
|
uvicorn_kwargs = {
|
||||||
|
"host": host,
|
||||||
|
"port": port,
|
||||||
|
"log_level": "info",
|
||||||
|
"lifespan": "on",
|
||||||
|
"workers": 1,
|
||||||
|
}
|
||||||
|
if uds:
|
||||||
|
uvicorn_kwargs["uds"] = uds
|
||||||
|
if ssl_keyfile:
|
||||||
|
uvicorn_kwargs["ssl_keyfile"] = ssl_keyfile
|
||||||
|
if ssl_certfile:
|
||||||
|
uvicorn_kwargs["ssl_certfile"] = ssl_certfile
|
||||||
|
server = uvicorn.Server(uvicorn.Config(ds.app(), **uvicorn_kwargs))
|
||||||
|
await server.serve()
|
||||||
|
|
||||||
|
asyncio.run(_serve_async())
|
||||||
|
|
||||||
|
|
||||||
@cli.command()
|
@cli.command()
|
||||||
|
|
@ -885,7 +916,7 @@ async def check_databases(ds):
|
||||||
)
|
)
|
||||||
except ConnectionProblem as e:
|
except ConnectionProblem as e:
|
||||||
raise click.UsageError(
|
raise click.UsageError(
|
||||||
f"Connection to {database.path} failed check: {str(e.args[0])}"
|
f"Connection to {database.path} failed check: {e.args[0]!s}"
|
||||||
)
|
)
|
||||||
# If --crossdb and more than SQLITE_LIMIT_ATTACHED show warning
|
# If --crossdb and more than SQLITE_LIMIT_ATTACHED show warning
|
||||||
if (
|
if (
|
||||||
|
|
@ -893,9 +924,5 @@ async def check_databases(ds):
|
||||||
and len([db for db in ds.databases.values() if not db.is_memory])
|
and len([db for db in ds.databases.values() if not db.is_memory])
|
||||||
> SQLITE_LIMIT_ATTACHED
|
> SQLITE_LIMIT_ATTACHED
|
||||||
):
|
):
|
||||||
msg = (
|
msg = f"Warning: --crossdb only works with the first {SQLITE_LIMIT_ATTACHED} attached databases"
|
||||||
"Warning: --crossdb only works with the first {} attached databases".format(
|
|
||||||
SQLITE_LIMIT_ATTACHED
|
|
||||||
)
|
|
||||||
)
|
|
||||||
click.echo(click.style(msg, bold=True, fg="yellow"), err=True)
|
click.echo(click.style(msg, bold=True, fg="yellow"), err=True)
|
||||||
|
|
|
||||||
|
|
@ -64,14 +64,14 @@ class ColumnType:
|
||||||
Return an HTML string to render this cell value, or None to
|
Return an HTML string to render this cell value, or None to
|
||||||
fall through to the default render_cell plugin hook chain.
|
fall through to the default render_cell plugin hook chain.
|
||||||
"""
|
"""
|
||||||
return None
|
return
|
||||||
|
|
||||||
async def validate(self, value, datasette):
|
async def validate(self, value, datasette):
|
||||||
"""
|
"""
|
||||||
Validate a value before it is written. Return None if valid,
|
Validate a value before it is written. Return None if valid,
|
||||||
or a string error message if invalid.
|
or a string error message if invalid.
|
||||||
"""
|
"""
|
||||||
return None
|
return
|
||||||
|
|
||||||
async def transform_value(self, value, datasette):
|
async def transform_value(self, value, datasette):
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -40,12 +40,12 @@ def _origin_tuple(value):
|
||||||
scheme = (parsed.scheme or "").lower()
|
scheme = (parsed.scheme or "").lower()
|
||||||
host = (parsed.hostname or "").lower()
|
host = (parsed.hostname or "").lower()
|
||||||
if not scheme or not host:
|
if not scheme or not host:
|
||||||
raise ValueError("missing scheme or host in {!r}".format(value))
|
raise ValueError(f"missing scheme or host in {value!r}")
|
||||||
port = parsed.port # may raise ValueError on bad ports
|
port = parsed.port # may raise ValueError on bad ports
|
||||||
if port is None:
|
if port is None:
|
||||||
port = DEFAULT_PORTS.get(scheme)
|
port = DEFAULT_PORTS.get(scheme)
|
||||||
if port is None:
|
if port is None:
|
||||||
raise ValueError("unknown default port for scheme {!r}".format(scheme))
|
raise ValueError(f"unknown default port for scheme {scheme!r}")
|
||||||
return scheme, host, port
|
return scheme, host, port
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -125,9 +125,7 @@ class CrossOriginProtectionMiddleware:
|
||||||
return
|
return
|
||||||
await self._forbid(
|
await self._forbid(
|
||||||
send,
|
send,
|
||||||
"Sec-Fetch-Site was {!r}, expected 'same-origin' or 'none'".format(
|
f"Sec-Fetch-Site was {sec_fetch_site!r}, expected 'same-origin' or 'none'",
|
||||||
sec_fetch_site
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
@ -141,11 +139,11 @@ class CrossOriginProtectionMiddleware:
|
||||||
request_scheme = self._request_scheme(scope)
|
request_scheme = self._request_scheme(scope)
|
||||||
try:
|
try:
|
||||||
origin_tuple = _origin_tuple(origin)
|
origin_tuple = _origin_tuple(origin)
|
||||||
expected_tuple = _origin_tuple("{}://{}".format(request_scheme, host))
|
expected_tuple = _origin_tuple(f"{request_scheme}://{host}")
|
||||||
except ValueError:
|
except ValueError:
|
||||||
await self._forbid(
|
await self._forbid(
|
||||||
send,
|
send,
|
||||||
"Malformed Origin {!r} or Host {!r}".format(origin, host),
|
f"Malformed Origin {origin!r} or Host {host!r}",
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
@ -155,7 +153,7 @@ class CrossOriginProtectionMiddleware:
|
||||||
|
|
||||||
await self._forbid(
|
await self._forbid(
|
||||||
send,
|
send,
|
||||||
"Origin {!r} does not match Host {!r}".format(origin, host),
|
f"Origin {origin!r} does not match Host {host!r}",
|
||||||
)
|
)
|
||||||
|
|
||||||
def _request_scheme(self, scope):
|
def _request_scheme(self, scope):
|
||||||
|
|
@ -163,7 +161,8 @@ class CrossOriginProtectionMiddleware:
|
||||||
try:
|
try:
|
||||||
if self.datasette.setting("force_https_urls"):
|
if self.datasette.setting("force_https_urls"):
|
||||||
return "https"
|
return "https"
|
||||||
except Exception:
|
except Exception: # noqa: BLE001, S110
|
||||||
|
# Settings may not be readable this early; fall back to the ASGI scheme
|
||||||
pass
|
pass
|
||||||
return scope.get("scheme") or "http"
|
return scope.get("scheme") or "http"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,33 +1,71 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import atexit
|
import atexit
|
||||||
from collections import namedtuple
|
import contextvars
|
||||||
import inspect
|
import inspect
|
||||||
import os
|
import os
|
||||||
from pathlib import Path
|
|
||||||
import queue
|
import queue
|
||||||
import sqlite_utils
|
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
import threading
|
import threading
|
||||||
|
import time
|
||||||
import uuid
|
import uuid
|
||||||
|
from collections import namedtuple
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import sqlite_utils
|
||||||
|
from opentelemetry import context as otel_context_api
|
||||||
|
from opentelemetry.trace import Status, StatusCode
|
||||||
|
|
||||||
|
from .inspect import inspect_hash
|
||||||
|
from .telemetry import (
|
||||||
|
callback_name,
|
||||||
|
linked_root_span_kwargs,
|
||||||
|
record_operation_duration,
|
||||||
|
record_query_interrupted,
|
||||||
|
record_write_queue_wait,
|
||||||
|
sql_attribute,
|
||||||
|
sql_operation_name,
|
||||||
|
tracer,
|
||||||
|
)
|
||||||
|
from .telemetry_registry import (
|
||||||
|
CALLBACK,
|
||||||
|
DB_NAMESPACE,
|
||||||
|
DB_OPERATION_NAME,
|
||||||
|
DB_QUERY,
|
||||||
|
DB_QUERY_EXECUTE,
|
||||||
|
DB_QUERY_TEXT,
|
||||||
|
DB_SYSTEM,
|
||||||
|
DB_WRITE_EXECUTE,
|
||||||
|
DB_WRITE_QUEUE_WAIT,
|
||||||
|
EXECUTEMANY,
|
||||||
|
EXECUTESCRIPT,
|
||||||
|
INTERRUPTED,
|
||||||
|
ISOLATED_CONNECTION,
|
||||||
|
PARAM_COUNT,
|
||||||
|
PARAM_SETS,
|
||||||
|
ROWS_RETURNED,
|
||||||
|
SQL_ERROR_SUPPRESSED,
|
||||||
|
TIME_LIMIT_MS,
|
||||||
|
TRANSACTION,
|
||||||
|
TRUNCATED,
|
||||||
|
)
|
||||||
from .tracer import trace
|
from .tracer import trace
|
||||||
from .utils import (
|
from .utils import (
|
||||||
call_with_supported_arguments,
|
call_with_supported_arguments,
|
||||||
detect_fts,
|
detect_fts,
|
||||||
detect_primary_keys,
|
detect_primary_keys,
|
||||||
detect_spatialite,
|
detect_spatialite,
|
||||||
|
escape_sqlite,
|
||||||
get_all_foreign_keys,
|
get_all_foreign_keys,
|
||||||
get_outbound_foreign_keys,
|
get_outbound_foreign_keys,
|
||||||
md5_not_usedforsecurity,
|
md5_not_usedforsecurity,
|
||||||
sqlite_timelimit,
|
|
||||||
sqlite3,
|
sqlite3,
|
||||||
table_columns,
|
sqlite_timelimit,
|
||||||
table_column_details,
|
table_column_details,
|
||||||
|
table_columns,
|
||||||
)
|
)
|
||||||
from .utils.sql_analysis import SQLAnalysis, analyze_sql_tables
|
from .utils.sql_analysis import SQLAnalysis, analyze_sql_tables
|
||||||
from .utils.sqlite import sqlite_hidden_table_names
|
from .utils.sqlite import sqlite_derived_table_dependencies, sqlite_hidden_table_names
|
||||||
from .inspect import inspect_hash
|
|
||||||
|
|
||||||
connections = threading.local()
|
connections = threading.local()
|
||||||
|
|
||||||
|
|
@ -83,6 +121,7 @@ class Database:
|
||||||
self.cached_hash = None
|
self.cached_hash = None
|
||||||
self.cached_size = None
|
self.cached_size = None
|
||||||
self._cached_table_counts = None
|
self._cached_table_counts = None
|
||||||
|
self._cached_derived_table_dependencies = None
|
||||||
self._write_thread = None
|
self._write_thread = None
|
||||||
self._write_queue = None
|
self._write_queue = None
|
||||||
self._closed = False
|
self._closed = False
|
||||||
|
|
@ -91,16 +130,15 @@ class Database:
|
||||||
# These are used when in non-threaded mode:
|
# These are used when in non-threaded mode:
|
||||||
self._read_connection = None
|
self._read_connection = None
|
||||||
self._write_connection = None
|
self._write_connection = None
|
||||||
# This is used to track all file connections so they can be closed
|
# Track file and memory connections, including reads on worker threads,
|
||||||
self._all_file_connections = []
|
# so close() can release all of them from the calling thread.
|
||||||
|
self._all_connections = []
|
||||||
if not is_temp_disk:
|
if not is_temp_disk:
|
||||||
self.mode = mode
|
self.mode = mode
|
||||||
|
|
||||||
def _check_not_closed(self):
|
def _check_not_closed(self):
|
||||||
if self._closed:
|
if self._closed:
|
||||||
raise DatasetteClosedError(
|
raise DatasetteClosedError(f"Database {self.name!r} has been closed")
|
||||||
"Database {!r} has been closed".format(self.name)
|
|
||||||
)
|
|
||||||
|
|
||||||
def _remove_pending_execute_future(self, future):
|
def _remove_pending_execute_future(self, future):
|
||||||
with self._pending_execute_futures_lock:
|
with self._pending_execute_futures_lock:
|
||||||
|
|
@ -139,15 +177,18 @@ class Database:
|
||||||
if write:
|
if write:
|
||||||
extra_kwargs["isolation_level"] = "IMMEDIATE"
|
extra_kwargs["isolation_level"] = "IMMEDIATE"
|
||||||
if self.memory_name:
|
if self.memory_name:
|
||||||
uri = "file:{}?mode=memory&cache=shared".format(self.memory_name)
|
uri = f"file:{self.memory_name}?mode=memory&cache=shared"
|
||||||
conn = sqlite3.connect(
|
conn = sqlite3.connect(
|
||||||
uri, uri=True, check_same_thread=False, **extra_kwargs
|
uri, uri=True, check_same_thread=False, **extra_kwargs
|
||||||
)
|
)
|
||||||
if not write:
|
if not write:
|
||||||
conn.execute("PRAGMA query_only=1")
|
conn.execute("PRAGMA query_only=1")
|
||||||
|
self._all_connections.append(conn)
|
||||||
return conn
|
return conn
|
||||||
if self.is_memory:
|
if self.is_memory:
|
||||||
return sqlite3.connect(":memory:", uri=True)
|
conn = sqlite3.connect(":memory:", uri=True, check_same_thread=False)
|
||||||
|
self._all_connections.append(conn)
|
||||||
|
return conn
|
||||||
|
|
||||||
# mode=ro or immutable=1?
|
# mode=ro or immutable=1?
|
||||||
if self.is_mutable:
|
if self.is_mutable:
|
||||||
|
|
@ -164,7 +205,7 @@ class Database:
|
||||||
conn = sqlite3.connect(
|
conn = sqlite3.connect(
|
||||||
f"file:{self.path}{qs}", uri=True, check_same_thread=False, **extra_kwargs
|
f"file:{self.path}{qs}", uri=True, check_same_thread=False, **extra_kwargs
|
||||||
)
|
)
|
||||||
self._all_file_connections.append(conn)
|
self._all_connections.append(conn)
|
||||||
if self.is_temp_disk and not self._wal_enabled:
|
if self.is_temp_disk and not self._wal_enabled:
|
||||||
conn.execute("PRAGMA journal_mode=WAL")
|
conn.execute("PRAGMA journal_mode=WAL")
|
||||||
self._wal_enabled = True
|
self._wal_enabled = True
|
||||||
|
|
@ -192,23 +233,22 @@ class Database:
|
||||||
write_thread.join(timeout=10)
|
write_thread.join(timeout=10)
|
||||||
if write_thread.is_alive():
|
if write_thread.is_alive():
|
||||||
sys.stderr.write(
|
sys.stderr.write(
|
||||||
"Datasette: write thread for {!r} did not exit within 10s\n".format(
|
f"Datasette: write thread for {self.name!r} did not exit within 10s\n"
|
||||||
self.name
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
sys.stderr.flush()
|
sys.stderr.flush()
|
||||||
for future in pending_execute_futures:
|
for future in pending_execute_futures:
|
||||||
try:
|
try:
|
||||||
future.result()
|
future.result()
|
||||||
except Exception:
|
except Exception: # noqa: BLE001, S110
|
||||||
|
# Shutdown teardown - a failed pending write must not block close()
|
||||||
pass
|
pass
|
||||||
# Close anything still tracked in _all_file_connections
|
# Close anything still tracked in _all_connections
|
||||||
for connection in self._all_file_connections:
|
for connection in self._all_connections:
|
||||||
try:
|
try:
|
||||||
connection.close()
|
connection.close()
|
||||||
except Exception:
|
except Exception: # noqa: BLE001, S110
|
||||||
pass
|
pass
|
||||||
self._all_file_connections = []
|
self._all_connections = []
|
||||||
# Drop per-thread cached read connections we can reach
|
# Drop per-thread cached read connections we can reach
|
||||||
try:
|
try:
|
||||||
delattr(connections, self._thread_local_id)
|
delattr(connections, self._thread_local_id)
|
||||||
|
|
@ -218,13 +258,13 @@ class Database:
|
||||||
if self._read_connection is not None:
|
if self._read_connection is not None:
|
||||||
try:
|
try:
|
||||||
self._read_connection.close()
|
self._read_connection.close()
|
||||||
except Exception:
|
except Exception: # noqa: BLE001, S110
|
||||||
pass
|
pass
|
||||||
self._read_connection = None
|
self._read_connection = None
|
||||||
if self._write_connection is not None:
|
if self._write_connection is not None:
|
||||||
try:
|
try:
|
||||||
self._write_connection.close()
|
self._write_connection.close()
|
||||||
except Exception:
|
except Exception: # noqa: BLE001, S110
|
||||||
pass
|
pass
|
||||||
self._write_connection = None
|
self._write_connection = None
|
||||||
if self.is_temp_disk:
|
if self.is_temp_disk:
|
||||||
|
|
@ -246,19 +286,46 @@ class Database:
|
||||||
request=None,
|
request=None,
|
||||||
return_all=False,
|
return_all=False,
|
||||||
returning_limit=EXECUTE_WRITE_RETURNING_LIMIT,
|
returning_limit=EXECUTE_WRITE_RETURNING_LIMIT,
|
||||||
|
transaction=True,
|
||||||
|
time_limit_ms=2000,
|
||||||
):
|
):
|
||||||
self._check_not_closed()
|
self._check_not_closed()
|
||||||
if returning_limit < 0:
|
if returning_limit < 0:
|
||||||
raise ValueError("returning_limit must be >= 0")
|
raise ValueError("returning_limit must be >= 0")
|
||||||
|
|
||||||
def _inner(conn):
|
def execute_sql(conn):
|
||||||
cursor = conn.execute(sql, params or [])
|
cursor = conn.execute(sql, params or [])
|
||||||
return ExecuteWriteResult.from_cursor(
|
return ExecuteWriteResult.from_cursor(
|
||||||
cursor, return_all=return_all, returning_limit=returning_limit
|
cursor, return_all=return_all, returning_limit=returning_limit
|
||||||
)
|
)
|
||||||
|
|
||||||
with trace("sql", database=self.name, sql=sql.strip(), params=params):
|
def _inner(conn):
|
||||||
results = await self.execute_write_fn(_inner, block=block, request=request)
|
try:
|
||||||
|
if time_limit_ms is None:
|
||||||
|
return execute_sql(conn)
|
||||||
|
with sqlite_timelimit(conn, time_limit_ms):
|
||||||
|
return execute_sql(conn)
|
||||||
|
except (sqlite3.OperationalError, sqlite3.DatabaseError) as e:
|
||||||
|
if e.args == ("interrupted",):
|
||||||
|
raise QueryInterrupted(e, sql, params)
|
||||||
|
raise
|
||||||
|
|
||||||
|
with trace( # noqa: SIM117
|
||||||
|
"sql", database=self.name, sql=sql.strip(), params=params
|
||||||
|
):
|
||||||
|
with tracer.start_as_current_span(DB_QUERY, kind=DB_QUERY.kind) as span:
|
||||||
|
span.set_attribute(DB_SYSTEM, "sqlite")
|
||||||
|
span.set_attribute(DB_NAMESPACE, self.name)
|
||||||
|
span.set_attribute(DB_QUERY_TEXT, sql_attribute(sql))
|
||||||
|
operation_name = sql_operation_name(sql)
|
||||||
|
if operation_name:
|
||||||
|
span.set_attribute(DB_OPERATION_NAME, operation_name)
|
||||||
|
if params:
|
||||||
|
span.set_attribute(PARAM_COUNT, len(params))
|
||||||
|
with record_operation_duration(self.name, "write"):
|
||||||
|
results = await self._execute_write_fn(
|
||||||
|
_inner, block=block, request=request, transaction=transaction
|
||||||
|
)
|
||||||
return results
|
return results
|
||||||
|
|
||||||
async def execute_write_script(self, sql, block=True, request=None):
|
async def execute_write_script(self, sql, block=True, request=None):
|
||||||
|
|
@ -267,10 +334,19 @@ class Database:
|
||||||
def _inner(conn):
|
def _inner(conn):
|
||||||
return conn.executescript(sql)
|
return conn.executescript(sql)
|
||||||
|
|
||||||
with trace("sql", database=self.name, sql=sql.strip(), executescript=True):
|
with trace( # noqa: SIM117
|
||||||
results = await self.execute_write_fn(
|
"sql", database=self.name, sql=sql.strip(), executescript=True
|
||||||
_inner, block=block, transaction=False, request=request
|
):
|
||||||
)
|
# No db.operation.name, since the script can contain multiple statements
|
||||||
|
with tracer.start_as_current_span(DB_QUERY, kind=DB_QUERY.kind) as span:
|
||||||
|
span.set_attribute(DB_SYSTEM, "sqlite")
|
||||||
|
span.set_attribute(DB_NAMESPACE, self.name)
|
||||||
|
span.set_attribute(DB_QUERY_TEXT, sql_attribute(sql))
|
||||||
|
span.set_attribute(EXECUTESCRIPT, True)
|
||||||
|
with record_operation_duration(self.name, "write"):
|
||||||
|
results = await self._execute_write_fn(
|
||||||
|
_inner, block=block, transaction=False, request=request
|
||||||
|
)
|
||||||
return results
|
return results
|
||||||
|
|
||||||
async def execute_write_many(self, sql, params_seq, block=True, request=None):
|
async def execute_write_many(self, sql, params_seq, block=True, request=None):
|
||||||
|
|
@ -290,9 +366,19 @@ class Database:
|
||||||
with trace(
|
with trace(
|
||||||
"sql", database=self.name, sql=sql.strip(), executemany=True
|
"sql", database=self.name, sql=sql.strip(), executemany=True
|
||||||
) as kwargs:
|
) as kwargs:
|
||||||
results, count = await self.execute_write_fn(
|
with tracer.start_as_current_span(DB_QUERY, kind=DB_QUERY.kind) as span:
|
||||||
_inner, block=block, request=request
|
span.set_attribute(DB_SYSTEM, "sqlite")
|
||||||
)
|
span.set_attribute(DB_NAMESPACE, self.name)
|
||||||
|
span.set_attribute(DB_QUERY_TEXT, sql_attribute(sql))
|
||||||
|
span.set_attribute(EXECUTEMANY, True)
|
||||||
|
operation_name = sql_operation_name(sql)
|
||||||
|
if operation_name:
|
||||||
|
span.set_attribute(DB_OPERATION_NAME, operation_name)
|
||||||
|
with record_operation_duration(self.name, "write"):
|
||||||
|
results, count = await self._execute_write_fn(
|
||||||
|
_inner, block=block, request=request
|
||||||
|
)
|
||||||
|
span.set_attribute(PARAM_SETS, count)
|
||||||
kwargs["count"] = count
|
kwargs["count"] = count
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
|
@ -309,31 +395,58 @@ class Database:
|
||||||
finally:
|
finally:
|
||||||
isolated_connection.close()
|
isolated_connection.close()
|
||||||
try:
|
try:
|
||||||
self._all_file_connections.remove(isolated_connection)
|
self._all_connections.remove(isolated_connection)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
# Was probably a memory connection
|
# May already have been cleared by close().
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if self.ds.executor is None:
|
with tracer.start_as_current_span(DB_QUERY, kind=DB_QUERY.kind) as span:
|
||||||
# non-threaded mode
|
span.set_attribute(DB_SYSTEM, "sqlite")
|
||||||
return _run()
|
span.set_attribute(DB_NAMESPACE, self.name)
|
||||||
if not write:
|
span.set_attribute(CALLBACK, callback_name(fn))
|
||||||
# Immutable database - no writes can ever occur, so there is no
|
# Immutable databases run this on the read pool, not the write queue
|
||||||
# write queue to block; run against a fresh read-only connection
|
with record_operation_duration(self.name, "write" if write else "read"):
|
||||||
return await asyncio.get_running_loop().run_in_executor(
|
if self.ds.executor is None:
|
||||||
self.ds.executor, _run
|
# non-threaded mode
|
||||||
)
|
return _run()
|
||||||
# Threaded mode - send to write thread
|
if not write:
|
||||||
return await self._send_to_write_thread(fn, isolated_connection=True)
|
# Immutable database - no writes can ever occur, so there
|
||||||
|
# is no write queue to block; run against a fresh
|
||||||
|
# read-only connection
|
||||||
|
ctx = contextvars.copy_context()
|
||||||
|
return await asyncio.get_running_loop().run_in_executor(
|
||||||
|
self.ds.executor, ctx.run, _run
|
||||||
|
)
|
||||||
|
# Threaded mode - send to write thread
|
||||||
|
return await self._send_to_write_thread(fn, isolated_connection=True)
|
||||||
|
|
||||||
async def analyze_sql(self, sql, params=None) -> SQLAnalysis:
|
async def analyze_sql(self, sql, params=None) -> SQLAnalysis:
|
||||||
self._check_not_closed()
|
self._check_not_closed()
|
||||||
|
|
||||||
return await self.execute_isolated_fn(
|
def _analyze_sql(conn):
|
||||||
lambda conn: analyze_sql_tables(conn, sql, params, database_name=self.name)
|
return analyze_sql_tables(conn, sql, params, database_name=self.name)
|
||||||
)
|
|
||||||
|
return await self.execute_isolated_fn(_analyze_sql)
|
||||||
|
|
||||||
async def execute_write_fn(self, fn, block=True, transaction=True, request=None):
|
async def execute_write_fn(self, fn, block=True, transaction=True, request=None):
|
||||||
|
"""Run `fn(conn)` on the write connection, traced as a `db.query` span.
|
||||||
|
|
||||||
|
The SQL-string write methods call `_execute_write_fn()` directly to
|
||||||
|
avoid creating a second span.
|
||||||
|
"""
|
||||||
|
self._check_not_closed()
|
||||||
|
# Record the name before _wrap_fn_with_hooks() wraps fn
|
||||||
|
name = callback_name(fn)
|
||||||
|
with tracer.start_as_current_span(DB_QUERY, kind=DB_QUERY.kind) as span:
|
||||||
|
span.set_attribute(DB_SYSTEM, "sqlite")
|
||||||
|
span.set_attribute(DB_NAMESPACE, self.name)
|
||||||
|
span.set_attribute(CALLBACK, name)
|
||||||
|
with record_operation_duration(self.name, "write"):
|
||||||
|
return await self._execute_write_fn(
|
||||||
|
fn, block=block, transaction=transaction, request=request
|
||||||
|
)
|
||||||
|
|
||||||
|
async def _execute_write_fn(self, fn, block=True, transaction=True, request=None):
|
||||||
self._check_not_closed()
|
self._check_not_closed()
|
||||||
pending_events = []
|
pending_events = []
|
||||||
|
|
||||||
|
|
@ -348,9 +461,19 @@ class Database:
|
||||||
self.ds._prepare_connection(self._write_connection, self.name)
|
self.ds._prepare_connection(self._write_connection, self.name)
|
||||||
if transaction:
|
if transaction:
|
||||||
with self._write_connection:
|
with self._write_connection:
|
||||||
|
self._write_connection.execute("BEGIN IMMEDIATE")
|
||||||
result = fn(self._write_connection)
|
result = fn(self._write_connection)
|
||||||
else:
|
else:
|
||||||
result = fn(self._write_connection)
|
result = fn(self._write_connection)
|
||||||
|
if not block:
|
||||||
|
# There is no write thread here, so the write has already
|
||||||
|
# finished. Hand back the same (task_id, reply_future) shape
|
||||||
|
# _send_to_write_thread() returns, with the future already
|
||||||
|
# resolved, so the block=False path below is identical in
|
||||||
|
# both modes.
|
||||||
|
reply_future = asyncio.get_running_loop().create_future()
|
||||||
|
reply_future.set_result(result)
|
||||||
|
result = (uuid.uuid4(), reply_future)
|
||||||
else:
|
else:
|
||||||
result = await self._send_to_write_thread(
|
result = await self._send_to_write_thread(
|
||||||
fn, block=block, transaction=transaction
|
fn, block=block, transaction=transaction
|
||||||
|
|
@ -366,7 +489,8 @@ class Database:
|
||||||
async def _dispatch_events_after_write():
|
async def _dispatch_events_after_write():
|
||||||
try:
|
try:
|
||||||
await reply_future
|
await reply_future
|
||||||
except Exception:
|
except Exception: # noqa: BLE001
|
||||||
|
# The write failed; skip success events regardless of why
|
||||||
# if the write failed, don't emit success events
|
# if the write failed, don't emit success events
|
||||||
return
|
return
|
||||||
for event in pending_events:
|
for event in pending_events:
|
||||||
|
|
@ -419,15 +543,24 @@ class Database:
|
||||||
self._write_thread = threading.Thread(
|
self._write_thread = threading.Thread(
|
||||||
target=self._execute_writes, daemon=True
|
target=self._execute_writes, daemon=True
|
||||||
)
|
)
|
||||||
self._write_thread.name = "_execute_writes for database {}".format(
|
self._write_thread.name = f"_execute_writes for database {self.name}"
|
||||||
self.name
|
|
||||||
)
|
|
||||||
self._write_thread.start()
|
self._write_thread.start()
|
||||||
task_id = uuid.uuid5(uuid.NAMESPACE_DNS, "datasette.io")
|
task_id = uuid.uuid4()
|
||||||
loop = asyncio.get_running_loop()
|
loop = asyncio.get_running_loop()
|
||||||
reply_future = loop.create_future()
|
reply_future = loop.create_future()
|
||||||
|
# Capture the OpenTelemetry context and enqueue time for the write thread
|
||||||
self._write_queue.put(
|
self._write_queue.put(
|
||||||
WriteTask(fn, task_id, loop, reply_future, isolated_connection, transaction)
|
WriteTask(
|
||||||
|
fn,
|
||||||
|
task_id,
|
||||||
|
loop,
|
||||||
|
reply_future,
|
||||||
|
isolated_connection,
|
||||||
|
transaction,
|
||||||
|
otel_context_api.get_current(),
|
||||||
|
time.time_ns(),
|
||||||
|
block,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
if block:
|
if block:
|
||||||
return await reply_future
|
return await reply_future
|
||||||
|
|
@ -441,8 +574,11 @@ class Database:
|
||||||
conn = None
|
conn = None
|
||||||
try:
|
try:
|
||||||
conn = self.connect(write=True)
|
conn = self.connect(write=True)
|
||||||
|
# Threads do not inherit the caller's context, so any spans
|
||||||
|
# created by prepare_connection hooks here are root spans
|
||||||
self.ds._prepare_connection(conn, self.name)
|
self.ds._prepare_connection(conn, self.name)
|
||||||
except Exception as e:
|
except Exception as e: # noqa: BLE001
|
||||||
|
# Stored and re-raised to whoever queues the next write
|
||||||
conn_exception = e
|
conn_exception = e
|
||||||
while True:
|
while True:
|
||||||
task = self._write_queue.get()
|
task = self._write_queue.get()
|
||||||
|
|
@ -450,43 +586,105 @@ class Database:
|
||||||
if conn is not None:
|
if conn is not None:
|
||||||
try:
|
try:
|
||||||
conn.close()
|
conn.close()
|
||||||
except Exception:
|
except Exception: # noqa: BLE001, S110
|
||||||
|
# Best-effort close as the write thread exits
|
||||||
pass
|
pass
|
||||||
return
|
return
|
||||||
exception = None
|
# block=True: the caller awaits the result, so the write spans
|
||||||
result = None
|
# are children of the caller's span. The token must be detached
|
||||||
if conn_exception is not None:
|
# in the finally block or the context leaks into later writes.
|
||||||
exception = conn_exception
|
# block=False: the caller may finish first, so the write spans
|
||||||
elif task.isolated_connection:
|
# are root spans with a link back to the caller's span.
|
||||||
try:
|
token = None
|
||||||
isolated_connection = self.connect(write=True)
|
write_span_kwargs = {}
|
||||||
try:
|
if task.block:
|
||||||
result = task.fn(isolated_connection)
|
token = otel_context_api.attach(task.otel_context)
|
||||||
finally:
|
|
||||||
isolated_connection.close()
|
|
||||||
try:
|
|
||||||
self._all_file_connections.remove(isolated_connection)
|
|
||||||
except ValueError:
|
|
||||||
# Was probably a memory connection
|
|
||||||
pass
|
|
||||||
except Exception as e:
|
|
||||||
sys.stderr.write("{}\n".format(e))
|
|
||||||
sys.stderr.flush()
|
|
||||||
exception = e
|
|
||||||
else:
|
else:
|
||||||
try:
|
write_span_kwargs = linked_root_span_kwargs(task.otel_context)
|
||||||
if task.transaction:
|
try:
|
||||||
with conn:
|
exception = None
|
||||||
result = task.fn(conn)
|
result = None
|
||||||
else:
|
# Span covers the time from enqueue to dequeue
|
||||||
result = task.fn(conn)
|
dequeued_at_ns = time.time_ns()
|
||||||
except Exception as e:
|
tracer.start_span(
|
||||||
sys.stderr.write("{}\n".format(e))
|
DB_WRITE_QUEUE_WAIT,
|
||||||
sys.stderr.flush()
|
start_time=task.enqueued_at_ns,
|
||||||
exception = e
|
**write_span_kwargs,
|
||||||
_deliver_write_result(task, result, exception)
|
).end(end_time=dequeued_at_ns)
|
||||||
|
record_write_queue_wait(self.name, dequeued_at_ns - task.enqueued_at_ns)
|
||||||
|
if conn_exception is not None:
|
||||||
|
exception = conn_exception
|
||||||
|
elif task.isolated_connection:
|
||||||
|
try:
|
||||||
|
with tracer.start_as_current_span(
|
||||||
|
DB_WRITE_EXECUTE, **write_span_kwargs
|
||||||
|
) as span:
|
||||||
|
span.set_attribute(
|
||||||
|
ISOLATED_CONNECTION,
|
||||||
|
task.isolated_connection,
|
||||||
|
)
|
||||||
|
span.set_attribute(TRANSACTION, task.transaction)
|
||||||
|
isolated_connection = self.connect(write=True)
|
||||||
|
try:
|
||||||
|
result = task.fn(isolated_connection)
|
||||||
|
finally:
|
||||||
|
isolated_connection.close()
|
||||||
|
try:
|
||||||
|
self._all_connections.remove(isolated_connection)
|
||||||
|
except ValueError:
|
||||||
|
# May already have been cleared by close().
|
||||||
|
pass
|
||||||
|
except Exception as e: # noqa: BLE001
|
||||||
|
# Write thread must survive any task failure or the database wedges
|
||||||
|
sys.stderr.write(f"{e}\n")
|
||||||
|
sys.stderr.flush()
|
||||||
|
exception = e
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
with tracer.start_as_current_span(
|
||||||
|
DB_WRITE_EXECUTE, **write_span_kwargs
|
||||||
|
) as span:
|
||||||
|
span.set_attribute(
|
||||||
|
ISOLATED_CONNECTION,
|
||||||
|
task.isolated_connection,
|
||||||
|
)
|
||||||
|
span.set_attribute(TRANSACTION, task.transaction)
|
||||||
|
if task.transaction:
|
||||||
|
with conn:
|
||||||
|
conn.execute("BEGIN IMMEDIATE")
|
||||||
|
result = task.fn(conn)
|
||||||
|
else:
|
||||||
|
result = task.fn(conn)
|
||||||
|
except Exception as e: # noqa: BLE001
|
||||||
|
sys.stderr.write(f"{e}\n")
|
||||||
|
sys.stderr.flush()
|
||||||
|
exception = e
|
||||||
|
_deliver_write_result(task, result, exception)
|
||||||
|
finally:
|
||||||
|
if token is not None:
|
||||||
|
otel_context_api.detach(token)
|
||||||
|
|
||||||
async def execute_fn(self, fn):
|
async def execute_fn(self, fn):
|
||||||
|
"""Run `fn(conn)` on a read connection, traced as a `db.query` span.
|
||||||
|
|
||||||
|
`execute()` calls `_execute_fn()` directly to avoid creating a second
|
||||||
|
span.
|
||||||
|
"""
|
||||||
|
self._check_not_closed()
|
||||||
|
|
||||||
|
def fn_in_execute_span(conn):
|
||||||
|
# Runs on the worker thread
|
||||||
|
with tracer.start_as_current_span(DB_QUERY_EXECUTE):
|
||||||
|
return fn(conn)
|
||||||
|
|
||||||
|
with tracer.start_as_current_span(DB_QUERY, kind=DB_QUERY.kind) as span:
|
||||||
|
span.set_attribute(DB_SYSTEM, "sqlite")
|
||||||
|
span.set_attribute(DB_NAMESPACE, self.name)
|
||||||
|
span.set_attribute(CALLBACK, callback_name(fn))
|
||||||
|
with record_operation_duration(self.name, "read"):
|
||||||
|
return await self._execute_fn(fn_in_execute_span)
|
||||||
|
|
||||||
|
async def _execute_fn(self, fn):
|
||||||
self._check_not_closed()
|
self._check_not_closed()
|
||||||
if self.ds.executor is None:
|
if self.ds.executor is None:
|
||||||
# non-threaded mode
|
# non-threaded mode
|
||||||
|
|
@ -506,7 +704,11 @@ class Database:
|
||||||
|
|
||||||
with self._pending_execute_futures_lock:
|
with self._pending_execute_futures_lock:
|
||||||
self._check_not_closed()
|
self._check_not_closed()
|
||||||
future = self.ds.executor.submit(in_thread)
|
# Run in a copy of the caller's context so spans created in the
|
||||||
|
# thread have the correct parent. This needs a fresh copy for
|
||||||
|
# each submit, since a Context cannot be entered concurrently.
|
||||||
|
ctx = contextvars.copy_context()
|
||||||
|
future = self.ds.executor.submit(ctx.run, in_thread)
|
||||||
self._pending_execute_futures.add(future)
|
self._pending_execute_futures.add(future)
|
||||||
future.add_done_callback(self._remove_pending_execute_future)
|
future.add_done_callback(self._remove_pending_execute_future)
|
||||||
return await asyncio.wrap_future(future)
|
return await asyncio.wrap_future(future)
|
||||||
|
|
@ -523,46 +725,101 @@ class Database:
|
||||||
"""Executes sql against db_name in a thread"""
|
"""Executes sql against db_name in a thread"""
|
||||||
self._check_not_closed()
|
self._check_not_closed()
|
||||||
page_size = page_size or self.ds.page_size
|
page_size = page_size or self.ds.page_size
|
||||||
|
time_limit_ms = self.ds.sql_time_limit_ms
|
||||||
|
# Callers that pass a shorter custom_time_limit, such as table counts
|
||||||
|
# and facet suggestions, expect timeouts, so they are not span errors
|
||||||
|
timeout_expected = bool(custom_time_limit) and custom_time_limit < time_limit_ms
|
||||||
|
if timeout_expected:
|
||||||
|
time_limit_ms = custom_time_limit
|
||||||
|
|
||||||
def sql_operation_in_thread(conn):
|
def sql_operation_in_thread(conn):
|
||||||
time_limit_ms = self.ds.sql_time_limit_ms
|
# Expected timeouts and errors with log_sql_errors=False are not
|
||||||
if custom_time_limit and custom_time_limit < time_limit_ms:
|
# recorded as span errors, so exceptions are handled explicitly
|
||||||
time_limit_ms = custom_time_limit
|
with tracer.start_as_current_span(
|
||||||
|
DB_QUERY_EXECUTE,
|
||||||
with sqlite_timelimit(conn, time_limit_ms):
|
record_exception=False,
|
||||||
|
set_status_on_exception=False,
|
||||||
|
) as execute_span:
|
||||||
try:
|
try:
|
||||||
cursor = conn.cursor()
|
with sqlite_timelimit(conn, time_limit_ms):
|
||||||
cursor.execute(sql, params if params is not None else {})
|
try:
|
||||||
max_returned_rows = self.ds.max_returned_rows
|
cursor = conn.cursor()
|
||||||
if max_returned_rows == page_size:
|
cursor.execute(sql, params if params is not None else {})
|
||||||
max_returned_rows += 1
|
max_returned_rows = self.ds.max_returned_rows
|
||||||
if max_returned_rows and truncate:
|
if max_returned_rows == page_size:
|
||||||
rows = cursor.fetchmany(max_returned_rows + 1)
|
max_returned_rows += 1
|
||||||
truncated = len(rows) > max_returned_rows
|
if max_returned_rows and truncate:
|
||||||
rows = rows[:max_returned_rows]
|
rows = cursor.fetchmany(max_returned_rows + 1)
|
||||||
else:
|
truncated = len(rows) > max_returned_rows
|
||||||
rows = cursor.fetchall()
|
rows = rows[:max_returned_rows]
|
||||||
truncated = False
|
else:
|
||||||
except (sqlite3.OperationalError, sqlite3.DatabaseError) as e:
|
rows = cursor.fetchall()
|
||||||
if e.args == ("interrupted",):
|
truncated = False
|
||||||
raise QueryInterrupted(e, sql, params)
|
except (sqlite3.OperationalError, sqlite3.DatabaseError) as e:
|
||||||
|
if e.args == ("interrupted",):
|
||||||
|
raise QueryInterrupted(e, sql, params)
|
||||||
|
if log_sql_errors:
|
||||||
|
sys.stderr.write(
|
||||||
|
f"ERROR: conn={conn}, sql = {sql!r}, params = {params}: {e}\n"
|
||||||
|
)
|
||||||
|
sys.stderr.flush()
|
||||||
|
raise
|
||||||
|
except QueryInterrupted as e:
|
||||||
|
if not timeout_expected:
|
||||||
|
execute_span.record_exception(e)
|
||||||
|
execute_span.set_status(Status(StatusCode.ERROR, str(e)))
|
||||||
|
raise
|
||||||
|
except Exception as e:
|
||||||
if log_sql_errors:
|
if log_sql_errors:
|
||||||
sys.stderr.write(
|
execute_span.record_exception(e)
|
||||||
"ERROR: conn={}, sql = {}, params = {}: {}\n".format(
|
execute_span.set_status(Status(StatusCode.ERROR, str(e)))
|
||||||
conn, repr(sql), params, e
|
|
||||||
)
|
|
||||||
)
|
|
||||||
sys.stderr.flush()
|
|
||||||
raise
|
raise
|
||||||
|
|
||||||
if truncate:
|
if truncate:
|
||||||
return Results(rows, truncated, cursor.description)
|
return Results(rows, truncated, cursor.description)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return Results(rows, False, cursor.description)
|
return Results(rows, False, cursor.description)
|
||||||
|
|
||||||
with trace("sql", database=self.name, sql=sql.strip(), params=params):
|
with trace( # noqa: SIM117
|
||||||
results = await self.execute_fn(sql_operation_in_thread)
|
"sql", database=self.name, sql=sql.strip(), params=params
|
||||||
|
):
|
||||||
|
with tracer.start_as_current_span(
|
||||||
|
DB_QUERY,
|
||||||
|
kind=DB_QUERY.kind,
|
||||||
|
record_exception=False,
|
||||||
|
set_status_on_exception=False,
|
||||||
|
) as span:
|
||||||
|
span.set_attribute(DB_SYSTEM, "sqlite")
|
||||||
|
span.set_attribute(DB_NAMESPACE, self.name)
|
||||||
|
span.set_attribute(DB_QUERY_TEXT, sql_attribute(sql))
|
||||||
|
span.set_attribute(TIME_LIMIT_MS, time_limit_ms)
|
||||||
|
operation_name = sql_operation_name(sql)
|
||||||
|
if operation_name:
|
||||||
|
span.set_attribute(DB_OPERATION_NAME, operation_name)
|
||||||
|
if params:
|
||||||
|
span.set_attribute(PARAM_COUNT, len(params))
|
||||||
|
try:
|
||||||
|
with record_operation_duration(self.name, "read"):
|
||||||
|
results = await self._execute_fn(sql_operation_in_thread)
|
||||||
|
except QueryInterrupted as e:
|
||||||
|
span.set_attribute(INTERRUPTED, True)
|
||||||
|
if not timeout_expected:
|
||||||
|
span.set_status(Status(StatusCode.ERROR, str(e)))
|
||||||
|
span.record_exception(e)
|
||||||
|
record_query_interrupted(self.name)
|
||||||
|
raise
|
||||||
|
except Exception as e:
|
||||||
|
# log_sql_errors=False callers, such as facet suggestion,
|
||||||
|
# expect some queries to fail
|
||||||
|
if log_sql_errors:
|
||||||
|
span.record_exception(e)
|
||||||
|
span.set_status(Status(StatusCode.ERROR, str(e)))
|
||||||
|
else:
|
||||||
|
span.set_attribute(SQL_ERROR_SUPPRESSED, True)
|
||||||
|
raise
|
||||||
|
span.set_attribute(TRUNCATED, results.truncated)
|
||||||
|
span.set_attribute(ROWS_RETURNED, len(results.rows))
|
||||||
return results
|
return results
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
@ -603,7 +860,7 @@ class Database:
|
||||||
try:
|
try:
|
||||||
table_count = (
|
table_count = (
|
||||||
await self.execute(
|
await self.execute(
|
||||||
f"select count(*) from (select * from [{table}] limit {self.count_limit + 1})",
|
f"select count(*) from (select * from {escape_sqlite(table)} limit {self.count_limit + 1})",
|
||||||
custom_time_limit=limit,
|
custom_time_limit=limit,
|
||||||
)
|
)
|
||||||
).rows[0][0]
|
).rows[0][0]
|
||||||
|
|
@ -653,17 +910,32 @@ class Database:
|
||||||
)
|
)
|
||||||
return [r[0] for r in results.rows]
|
return [r[0] for r in results.rows]
|
||||||
|
|
||||||
|
# Named functions rather than lambdas give more useful datasette.callback
|
||||||
|
# span attributes
|
||||||
|
|
||||||
async def table_columns(self, table):
|
async def table_columns(self, table):
|
||||||
return await self.execute_fn(lambda conn: table_columns(conn, table))
|
def _table_columns(conn):
|
||||||
|
return table_columns(conn, table)
|
||||||
|
|
||||||
|
return await self.execute_fn(_table_columns)
|
||||||
|
|
||||||
async def table_column_details(self, table):
|
async def table_column_details(self, table):
|
||||||
return await self.execute_fn(lambda conn: table_column_details(conn, table))
|
def _table_column_details(conn):
|
||||||
|
return table_column_details(conn, table)
|
||||||
|
|
||||||
|
return await self.execute_fn(_table_column_details)
|
||||||
|
|
||||||
async def primary_keys(self, table):
|
async def primary_keys(self, table):
|
||||||
return await self.execute_fn(lambda conn: detect_primary_keys(conn, table))
|
def _primary_keys(conn):
|
||||||
|
return detect_primary_keys(conn, table)
|
||||||
|
|
||||||
|
return await self.execute_fn(_primary_keys)
|
||||||
|
|
||||||
async def fts_table(self, table):
|
async def fts_table(self, table):
|
||||||
return await self.execute_fn(lambda conn: detect_fts(conn, table))
|
def _fts_table(conn):
|
||||||
|
return detect_fts(conn, table)
|
||||||
|
|
||||||
|
return await self.execute_fn(_fts_table)
|
||||||
|
|
||||||
async def label_column_for_table(self, table):
|
async def label_column_for_table(self, table):
|
||||||
explicit_label_column = (await self.ds.table_config(self.name, table)).get(
|
explicit_label_column = (await self.ds.table_config(self.name, table)).get(
|
||||||
|
|
@ -707,9 +979,9 @@ class Database:
|
||||||
column_names
|
column_names
|
||||||
and len(column_names) == 2
|
and len(column_names) == 2
|
||||||
and ("id" in column_names or "pk" in column_names)
|
and ("id" in column_names or "pk" in column_names)
|
||||||
and not set(column_names) == {"id", "pk"}
|
and set(column_names) != {"id", "pk"}
|
||||||
):
|
):
|
||||||
return [c for c in column_names if c not in ("id", "pk")][0]
|
return next(c for c in column_names if c not in ("id", "pk"))
|
||||||
# Couldn't find a label:
|
# Couldn't find a label:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
@ -755,6 +1027,17 @@ class Database:
|
||||||
|
|
||||||
return hidden_tables
|
return hidden_tables
|
||||||
|
|
||||||
|
async def derived_table_dependencies(self):
|
||||||
|
"""Return implementation tables and the tables they derive from."""
|
||||||
|
schema_version = (await self.execute("PRAGMA schema_version")).first()[0]
|
||||||
|
if (
|
||||||
|
self._cached_derived_table_dependencies is None
|
||||||
|
or self._cached_derived_table_dependencies[0] != schema_version
|
||||||
|
):
|
||||||
|
dependencies = await self.execute_fn(sqlite_derived_table_dependencies)
|
||||||
|
self._cached_derived_table_dependencies = (schema_version, dependencies)
|
||||||
|
return self._cached_derived_table_dependencies[1]
|
||||||
|
|
||||||
async def view_names(self):
|
async def view_names(self):
|
||||||
results = await self.execute("select name from sqlite_master where type='view'")
|
results = await self.execute("select name from sqlite_master where type='view'")
|
||||||
return [r[0] for r in results.rows]
|
return [r[0] for r in results.rows]
|
||||||
|
|
@ -850,16 +1133,28 @@ def _apply_write_wrapper(fn, wrapper_factory, track_event):
|
||||||
|
|
||||||
class WriteTask:
|
class WriteTask:
|
||||||
__slots__ = (
|
__slots__ = (
|
||||||
|
"block",
|
||||||
|
"enqueued_at_ns",
|
||||||
"fn",
|
"fn",
|
||||||
"task_id",
|
|
||||||
"loop",
|
|
||||||
"reply_future",
|
|
||||||
"isolated_connection",
|
"isolated_connection",
|
||||||
|
"loop",
|
||||||
|
"otel_context",
|
||||||
|
"reply_future",
|
||||||
|
"task_id",
|
||||||
"transaction",
|
"transaction",
|
||||||
)
|
)
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self, fn, task_id, loop, reply_future, isolated_connection, transaction
|
self,
|
||||||
|
fn,
|
||||||
|
task_id,
|
||||||
|
loop,
|
||||||
|
reply_future,
|
||||||
|
isolated_connection,
|
||||||
|
transaction,
|
||||||
|
otel_context,
|
||||||
|
enqueued_at_ns,
|
||||||
|
block,
|
||||||
):
|
):
|
||||||
self.fn = fn
|
self.fn = fn
|
||||||
self.task_id = task_id
|
self.task_id = task_id
|
||||||
|
|
@ -867,6 +1162,9 @@ class WriteTask:
|
||||||
self.reply_future = reply_future
|
self.reply_future = reply_future
|
||||||
self.isolated_connection = isolated_connection
|
self.isolated_connection = isolated_connection
|
||||||
self.transaction = transaction
|
self.transaction = transaction
|
||||||
|
self.otel_context = otel_context
|
||||||
|
self.enqueued_at_ns = enqueued_at_ns
|
||||||
|
self.block = block
|
||||||
|
|
||||||
|
|
||||||
def _deliver_write_result(task, result, exception):
|
def _deliver_write_result(task, result, exception):
|
||||||
|
|
@ -895,7 +1193,7 @@ class QueryInterrupted(Exception):
|
||||||
self.params = params
|
self.params = params
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "QueryInterrupted: {}".format(self.e)
|
return f"QueryInterrupted: {self.e}"
|
||||||
|
|
||||||
|
|
||||||
class MultipleValues(Exception):
|
class MultipleValues(Exception):
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,8 @@ from datasette import hookimpl
|
||||||
from datasette.permissions import Action
|
from datasette.permissions import Action
|
||||||
from datasette.resources import (
|
from datasette.resources import (
|
||||||
DatabaseResource,
|
DatabaseResource,
|
||||||
TableResource,
|
|
||||||
QueryResource,
|
QueryResource,
|
||||||
|
TableResource,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -61,6 +61,12 @@ def register_actions():
|
||||||
description="Create tables",
|
description="Create tables",
|
||||||
resource_class=DatabaseResource,
|
resource_class=DatabaseResource,
|
||||||
),
|
),
|
||||||
|
Action(
|
||||||
|
name="create-view",
|
||||||
|
abbr="cv",
|
||||||
|
description="Create views",
|
||||||
|
resource_class=DatabaseResource,
|
||||||
|
),
|
||||||
Action(
|
Action(
|
||||||
name="store-query",
|
name="store-query",
|
||||||
abbr="sq",
|
abbr="sq",
|
||||||
|
|
@ -111,6 +117,12 @@ def register_actions():
|
||||||
description="Drop tables",
|
description="Drop tables",
|
||||||
resource_class=TableResource,
|
resource_class=TableResource,
|
||||||
),
|
),
|
||||||
|
Action(
|
||||||
|
name="drop-view",
|
||||||
|
abbr="dv",
|
||||||
|
description="Drop views",
|
||||||
|
resource_class=TableResource,
|
||||||
|
),
|
||||||
# Query-level actions (child-level)
|
# Query-level actions (child-level)
|
||||||
Action(
|
Action(
|
||||||
name="view-query",
|
name="view-query",
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,17 @@ import markupsafe
|
||||||
from datasette import hookimpl
|
from datasette import hookimpl
|
||||||
from datasette.column_types import ColumnType, SQLiteType
|
from datasette.column_types import ColumnType, SQLiteType
|
||||||
|
|
||||||
|
_HTTP_URL_RE = re.compile(r"https?://\S+", re.IGNORECASE)
|
||||||
|
|
||||||
|
|
||||||
|
def _normalize_http_url(value):
|
||||||
|
if not isinstance(value, str):
|
||||||
|
return None
|
||||||
|
normalized = value.strip()
|
||||||
|
if not _HTTP_URL_RE.fullmatch(normalized):
|
||||||
|
return None
|
||||||
|
return normalized
|
||||||
|
|
||||||
|
|
||||||
class UrlColumnType(ColumnType):
|
class UrlColumnType(ColumnType):
|
||||||
name = "url"
|
name = "url"
|
||||||
|
|
@ -15,7 +26,10 @@ class UrlColumnType(ColumnType):
|
||||||
async def render_cell(self, value, column, table, database, datasette, request):
|
async def render_cell(self, value, column, table, database, datasette, request):
|
||||||
if not value or not isinstance(value, str):
|
if not value or not isinstance(value, str):
|
||||||
return None
|
return None
|
||||||
escaped = markupsafe.escape(value.strip())
|
normalized = _normalize_http_url(value)
|
||||||
|
if normalized is None:
|
||||||
|
return markupsafe.escape(value.strip())
|
||||||
|
escaped = markupsafe.escape(normalized)
|
||||||
return markupsafe.Markup(f'<a href="{escaped}">{escaped}</a>')
|
return markupsafe.Markup(f'<a href="{escaped}">{escaped}</a>')
|
||||||
|
|
||||||
async def validate(self, value, datasette):
|
async def validate(self, value, datasette):
|
||||||
|
|
@ -23,7 +37,7 @@ class UrlColumnType(ColumnType):
|
||||||
return None
|
return None
|
||||||
if not isinstance(value, str):
|
if not isinstance(value, str):
|
||||||
return "URL must be a string"
|
return "URL must be a string"
|
||||||
if not re.match(r"^https?://\S+$", value.strip()):
|
if _normalize_http_url(value) is None:
|
||||||
return "Invalid URL"
|
return "Invalid URL"
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
from datasette import hookimpl
|
|
||||||
import datetime
|
import datetime
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
from datasette import hookimpl
|
||||||
|
|
||||||
|
|
||||||
def header(key, request):
|
def header(key, request):
|
||||||
key = key.replace("_", "-").encode("utf-8")
|
key = key.replace("_", "-").encode("utf-8")
|
||||||
|
|
|
||||||
|
|
@ -17,18 +17,29 @@ UNION/INTERSECT operations. The order of evaluation is:
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
# Re-export all hooks and public utilities
|
|
||||||
from .restrictions import (
|
|
||||||
actor_restrictions_sql as actor_restrictions_sql,
|
|
||||||
restrictions_allow_action as restrictions_allow_action,
|
|
||||||
ActorRestrictions as ActorRestrictions,
|
|
||||||
)
|
|
||||||
from .root import root_user_permissions_sql as root_user_permissions_sql
|
|
||||||
from .config import config_permissions_sql as config_permissions_sql
|
from .config import config_permissions_sql as config_permissions_sql
|
||||||
|
from .defaults import (
|
||||||
|
DEFAULT_ALLOW_ACTIONS as DEFAULT_ALLOW_ACTIONS,
|
||||||
|
)
|
||||||
|
from .defaults import (
|
||||||
|
default_action_permissions_sql as default_action_permissions_sql,
|
||||||
|
)
|
||||||
from .defaults import (
|
from .defaults import (
|
||||||
# Avoid "datasette.default_permissions" does not explicitly export attribute
|
# Avoid "datasette.default_permissions" does not explicitly export attribute
|
||||||
default_allow_sql_check as default_allow_sql_check,
|
default_allow_sql_check as default_allow_sql_check,
|
||||||
default_action_permissions_sql as default_action_permissions_sql,
|
|
||||||
default_query_permissions_sql as default_query_permissions_sql,
|
|
||||||
DEFAULT_ALLOW_ACTIONS as DEFAULT_ALLOW_ACTIONS,
|
|
||||||
)
|
)
|
||||||
|
from .defaults import (
|
||||||
|
default_query_permissions_sql as default_query_permissions_sql,
|
||||||
|
)
|
||||||
|
from .restrictions import (
|
||||||
|
ActorRestrictions as ActorRestrictions,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Re-export all hooks and public utilities
|
||||||
|
from .restrictions import (
|
||||||
|
actor_restrictions_sql as actor_restrictions_sql,
|
||||||
|
)
|
||||||
|
from .restrictions import (
|
||||||
|
restrictions_allow_action as restrictions_allow_action,
|
||||||
|
)
|
||||||
|
from .root import root_user_permissions_sql as root_user_permissions_sql
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ Applies permission rules from datasette.yaml configuration.
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import TYPE_CHECKING, Any, List, Optional, Set, Tuple
|
from typing import TYPE_CHECKING, Any
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from datasette.app import Datasette
|
from datasette.app import Datasette
|
||||||
|
|
@ -55,8 +55,8 @@ class ConfigPermissionProcessor:
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
datasette: "Datasette",
|
datasette: Datasette,
|
||||||
actor: Optional[dict],
|
actor: dict | None,
|
||||||
action: str,
|
action: str,
|
||||||
):
|
):
|
||||||
self.datasette = datasette
|
self.datasette = datasette
|
||||||
|
|
@ -74,8 +74,8 @@ class ConfigPermissionProcessor:
|
||||||
self.restrictions = actor.get("_r", {}) if actor else {}
|
self.restrictions = actor.get("_r", {}) if actor else {}
|
||||||
|
|
||||||
# Pre-compute restriction info for efficiency
|
# Pre-compute restriction info for efficiency
|
||||||
self.restricted_databases: Set[str] = set()
|
self.restricted_databases: set[str] = set()
|
||||||
self.restricted_tables: Set[Tuple[str, str]] = set()
|
self.restricted_tables: set[tuple[str, str]] = set()
|
||||||
|
|
||||||
if self.has_restrictions:
|
if self.has_restrictions:
|
||||||
self.restricted_databases = {
|
self.restricted_databases = {
|
||||||
|
|
@ -92,16 +92,27 @@ class ConfigPermissionProcessor:
|
||||||
# Tables implicitly reference their parent databases
|
# Tables implicitly reference their parent databases
|
||||||
self.restricted_databases.update(db for db, _ in self.restricted_tables)
|
self.restricted_databases.update(db for db, _ in self.restricted_tables)
|
||||||
|
|
||||||
def evaluate_allow_block(self, allow_block: Any) -> Optional[bool]:
|
# Resolve identity keys once per action, rather than scanning the
|
||||||
|
# restriction allowlist for every configured table's allow block.
|
||||||
|
self.restricted_table_keys = {
|
||||||
|
(db, self.action_obj.normalize_child(table) if self.action_obj else table)
|
||||||
|
for db, table in self.restricted_tables
|
||||||
|
}
|
||||||
|
|
||||||
|
def evaluate_allow_block(self, allow_block: Any) -> bool | None:
|
||||||
"""Evaluate an allow block against the current actor."""
|
"""Evaluate an allow block against the current actor."""
|
||||||
if allow_block is None:
|
if allow_block is None:
|
||||||
return None
|
return None
|
||||||
|
# Values passed using ``-s permissions.* 1`` or ``0`` are parsed as
|
||||||
|
# integers, but should retain the CLI's boolean 1/0 behavior.
|
||||||
|
if isinstance(allow_block, int) and allow_block in (0, 1):
|
||||||
|
return bool(allow_block)
|
||||||
return actor_matches_allow(self.actor, allow_block)
|
return actor_matches_allow(self.actor, allow_block)
|
||||||
|
|
||||||
def is_in_restriction_allowlist(
|
def is_in_restriction_allowlist(
|
||||||
self,
|
self,
|
||||||
parent: Optional[str],
|
parent: str | None,
|
||||||
child: Optional[str],
|
child: str | None,
|
||||||
) -> bool:
|
) -> bool:
|
||||||
"""Check if resource is allowed by actor restrictions."""
|
"""Check if resource is allowed by actor restrictions."""
|
||||||
if not self.has_restrictions:
|
if not self.has_restrictions:
|
||||||
|
|
@ -121,8 +132,10 @@ class ConfigPermissionProcessor:
|
||||||
if parent:
|
if parent:
|
||||||
table_restrictions = (self.restrictions.get("r", {}) or {}).get(parent, {})
|
table_restrictions = (self.restrictions.get("r", {}) or {}).get(parent, {})
|
||||||
if child:
|
if child:
|
||||||
table_actions = table_restrictions.get(child, [])
|
child_key = (
|
||||||
if self.action_checks.intersection(table_actions):
|
self.action_obj.normalize_child(child) if self.action_obj else child
|
||||||
|
)
|
||||||
|
if (parent, child_key) in self.restricted_table_keys:
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
# Parent query should proceed if any child in this database is allowlisted
|
# Parent query should proceed if any child in this database is allowlisted
|
||||||
|
|
@ -143,9 +156,9 @@ class ConfigPermissionProcessor:
|
||||||
|
|
||||||
def add_permissions_rule(
|
def add_permissions_rule(
|
||||||
self,
|
self,
|
||||||
parent: Optional[str],
|
parent: str | None,
|
||||||
child: Optional[str],
|
child: str | None,
|
||||||
permissions_block: Optional[dict],
|
permissions_block: dict | None,
|
||||||
scope_desc: str,
|
scope_desc: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Add a rule from a permissions:{action} block."""
|
"""Add a rule from a permissions:{action} block."""
|
||||||
|
|
@ -165,8 +178,8 @@ class ConfigPermissionProcessor:
|
||||||
|
|
||||||
def add_allow_block_rule(
|
def add_allow_block_rule(
|
||||||
self,
|
self,
|
||||||
parent: Optional[str],
|
parent: str | None,
|
||||||
child: Optional[str],
|
child: str | None,
|
||||||
allow_block: Any,
|
allow_block: Any,
|
||||||
scope_desc: str,
|
scope_desc: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
@ -198,8 +211,8 @@ class ConfigPermissionProcessor:
|
||||||
|
|
||||||
def _add_restriction_gate_denies(
|
def _add_restriction_gate_denies(
|
||||||
self,
|
self,
|
||||||
parent: Optional[str],
|
parent: str | None,
|
||||||
child: Optional[str],
|
child: str | None,
|
||||||
is_allowed: bool,
|
is_allowed: bool,
|
||||||
scope_desc: str,
|
scope_desc: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
@ -231,7 +244,7 @@ class ConfigPermissionProcessor:
|
||||||
if db_name == parent:
|
if db_name == parent:
|
||||||
self.collector.add(db_name, table_name, False, reason)
|
self.collector.add(db_name, table_name, False, reason)
|
||||||
|
|
||||||
def process(self) -> Optional[PermissionSQL]:
|
def process(self) -> PermissionSQL | None:
|
||||||
"""Process all config rules and return combined PermissionSQL."""
|
"""Process all config rules and return combined PermissionSQL."""
|
||||||
self._process_root_permissions()
|
self._process_root_permissions()
|
||||||
self._process_databases()
|
self._process_databases()
|
||||||
|
|
@ -421,10 +434,10 @@ class ConfigPermissionProcessor:
|
||||||
|
|
||||||
@hookimpl(specname="permission_resources_sql")
|
@hookimpl(specname="permission_resources_sql")
|
||||||
async def config_permissions_sql(
|
async def config_permissions_sql(
|
||||||
datasette: "Datasette",
|
datasette: Datasette,
|
||||||
actor: Optional[dict],
|
actor: dict | None,
|
||||||
action: str,
|
action: str,
|
||||||
) -> Optional[List[PermissionSQL]]:
|
) -> list[PermissionSQL] | None:
|
||||||
"""
|
"""
|
||||||
Apply permission rules from datasette.yaml configuration.
|
Apply permission rules from datasette.yaml configuration.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ Provides default allow rules for standard view/execute actions.
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import TYPE_CHECKING, Optional
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from datasette.app import Datasette
|
from datasette.app import Datasette
|
||||||
|
|
@ -29,29 +29,28 @@ DEFAULT_ALLOW_ACTIONS = frozenset(
|
||||||
|
|
||||||
@hookimpl(specname="permission_resources_sql")
|
@hookimpl(specname="permission_resources_sql")
|
||||||
async def default_allow_sql_check(
|
async def default_allow_sql_check(
|
||||||
datasette: "Datasette",
|
datasette: Datasette,
|
||||||
actor: Optional[dict],
|
actor: dict | None,
|
||||||
action: str,
|
action: str,
|
||||||
) -> Optional[PermissionSQL]:
|
) -> PermissionSQL | None:
|
||||||
"""
|
"""
|
||||||
Enforce the default_allow_sql setting.
|
Enforce the default_allow_sql setting.
|
||||||
|
|
||||||
When default_allow_sql is false (the default), execute-sql is denied
|
When default_allow_sql is false (the default), execute-sql is denied
|
||||||
unless explicitly allowed by config or other rules.
|
unless explicitly allowed by config or other rules.
|
||||||
"""
|
"""
|
||||||
if action == "execute-sql":
|
if action == "execute-sql" and not datasette.setting("default_allow_sql"):
|
||||||
if not datasette.setting("default_allow_sql"):
|
return PermissionSQL.deny(reason="default_allow_sql is false")
|
||||||
return PermissionSQL.deny(reason="default_allow_sql is false")
|
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
@hookimpl(specname="permission_resources_sql")
|
@hookimpl(specname="permission_resources_sql")
|
||||||
async def default_action_permissions_sql(
|
async def default_action_permissions_sql(
|
||||||
datasette: "Datasette",
|
datasette: Datasette,
|
||||||
actor: Optional[dict],
|
actor: dict | None,
|
||||||
action: str,
|
action: str,
|
||||||
) -> Optional[PermissionSQL]:
|
) -> PermissionSQL | None:
|
||||||
"""
|
"""
|
||||||
Provide default allow rules for standard view/execute actions.
|
Provide default allow rules for standard view/execute actions.
|
||||||
|
|
||||||
|
|
@ -71,10 +70,10 @@ async def default_action_permissions_sql(
|
||||||
|
|
||||||
@hookimpl(specname="permission_resources_sql")
|
@hookimpl(specname="permission_resources_sql")
|
||||||
async def default_query_permissions_sql(
|
async def default_query_permissions_sql(
|
||||||
datasette: "Datasette",
|
datasette: Datasette,
|
||||||
actor: Optional[dict],
|
actor: dict | None,
|
||||||
action: str,
|
action: str,
|
||||||
) -> Optional[PermissionSQL]:
|
) -> PermissionSQL | None:
|
||||||
actor_id = actor.get("id") if isinstance(actor, dict) else None
|
actor_id = actor.get("id") if isinstance(actor, dict) else None
|
||||||
|
|
||||||
if action not in {"view-query", "update-query", "delete-query"}:
|
if action not in {"view-query", "update-query", "delete-query"}:
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ Shared helper utilities for default permission implementations.
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import TYPE_CHECKING, List, Optional, Set
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from datasette.app import Datasette
|
from datasette.app import Datasette
|
||||||
|
|
@ -13,7 +13,7 @@ if TYPE_CHECKING:
|
||||||
from datasette.permissions import PermissionSQL
|
from datasette.permissions import PermissionSQL
|
||||||
|
|
||||||
|
|
||||||
def get_action_name_variants(datasette: "Datasette", action: str) -> Set[str]:
|
def get_action_name_variants(datasette: Datasette, action: str) -> set[str]:
|
||||||
"""
|
"""
|
||||||
Get all name variants for an action (full name and abbreviation).
|
Get all name variants for an action (full name and abbreviation).
|
||||||
|
|
||||||
|
|
@ -27,7 +27,7 @@ def get_action_name_variants(datasette: "Datasette", action: str) -> Set[str]:
|
||||||
return variants
|
return variants
|
||||||
|
|
||||||
|
|
||||||
def action_in_list(datasette: "Datasette", action: str, action_list: list) -> bool:
|
def action_in_list(datasette: Datasette, action: str, action_list: list) -> bool:
|
||||||
"""Check if an action (or its abbreviation) is in a list."""
|
"""Check if an action (or its abbreviation) is in a list."""
|
||||||
return bool(get_action_name_variants(datasette, action).intersection(action_list))
|
return bool(get_action_name_variants(datasette, action).intersection(action_list))
|
||||||
|
|
||||||
|
|
@ -36,8 +36,8 @@ def action_in_list(datasette: "Datasette", action: str, action_list: list) -> bo
|
||||||
class PermissionRow:
|
class PermissionRow:
|
||||||
"""A single permission rule row."""
|
"""A single permission rule row."""
|
||||||
|
|
||||||
parent: Optional[str]
|
parent: str | None
|
||||||
child: Optional[str]
|
child: str | None
|
||||||
allow: bool
|
allow: bool
|
||||||
reason: str
|
reason: str
|
||||||
|
|
||||||
|
|
@ -46,14 +46,14 @@ class PermissionRowCollector:
|
||||||
"""Collects permission rows and converts them to PermissionSQL."""
|
"""Collects permission rows and converts them to PermissionSQL."""
|
||||||
|
|
||||||
def __init__(self, prefix: str = "row"):
|
def __init__(self, prefix: str = "row"):
|
||||||
self.rows: List[PermissionRow] = []
|
self.rows: list[PermissionRow] = []
|
||||||
self.prefix = prefix
|
self.prefix = prefix
|
||||||
|
|
||||||
def add(
|
def add(
|
||||||
self,
|
self,
|
||||||
parent: Optional[str],
|
parent: str | None,
|
||||||
child: Optional[str],
|
child: str | None,
|
||||||
allow: Optional[bool],
|
allow: bool | None,
|
||||||
reason: str,
|
reason: str,
|
||||||
if_not_none: bool = False,
|
if_not_none: bool = False,
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
@ -62,7 +62,7 @@ class PermissionRowCollector:
|
||||||
return
|
return
|
||||||
self.rows.append(PermissionRow(parent, child, allow, reason))
|
self.rows.append(PermissionRow(parent, child, allow, reason))
|
||||||
|
|
||||||
def to_permission_sql(self) -> Optional[PermissionSQL]:
|
def to_permission_sql(self) -> PermissionSQL | None:
|
||||||
"""Convert collected rows to a PermissionSQL object."""
|
"""Convert collected rows to a PermissionSQL object."""
|
||||||
if not self.rows:
|
if not self.rows:
|
||||||
return None
|
return None
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ contains allowlists of resources the actor can access.
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import TYPE_CHECKING, List, Optional, Set, Tuple
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from datasette.app import Datasette
|
from datasette.app import Datasette
|
||||||
|
|
@ -23,12 +23,12 @@ from .helpers import action_in_list, get_action_name_variants
|
||||||
class ActorRestrictions:
|
class ActorRestrictions:
|
||||||
"""Parsed actor restrictions from the _r key."""
|
"""Parsed actor restrictions from the _r key."""
|
||||||
|
|
||||||
global_actions: List[str] # _r.a - globally allowed actions
|
global_actions: list[str] # _r.a - globally allowed actions
|
||||||
database_actions: dict # _r.d - {db_name: [actions]}
|
database_actions: dict # _r.d - {db_name: [actions]}
|
||||||
table_actions: dict # _r.r - {db_name: {table: [actions]}}
|
table_actions: dict # _r.r - {db_name: {table: [actions]}}
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_actor(cls, actor: Optional[dict]) -> Optional["ActorRestrictions"]:
|
def from_actor(cls, actor: dict | None) -> ActorRestrictions | None:
|
||||||
"""Parse restrictions from actor dict. Returns None if no restrictions."""
|
"""Parse restrictions from actor dict. Returns None if no restrictions."""
|
||||||
if not actor:
|
if not actor:
|
||||||
return None
|
return None
|
||||||
|
|
@ -44,11 +44,11 @@ class ActorRestrictions:
|
||||||
table_actions=restrictions.get("r", {}),
|
table_actions=restrictions.get("r", {}),
|
||||||
)
|
)
|
||||||
|
|
||||||
def is_action_globally_allowed(self, datasette: "Datasette", action: str) -> bool:
|
def is_action_globally_allowed(self, datasette: Datasette, action: str) -> bool:
|
||||||
"""Check if action is in the global allowlist."""
|
"""Check if action is in the global allowlist."""
|
||||||
return action_in_list(datasette, action, self.global_actions)
|
return action_in_list(datasette, action, self.global_actions)
|
||||||
|
|
||||||
def get_allowed_databases(self, datasette: "Datasette", action: str) -> Set[str]:
|
def get_allowed_databases(self, datasette: Datasette, action: str) -> set[str]:
|
||||||
"""Get database names where this action is allowed."""
|
"""Get database names where this action is allowed."""
|
||||||
allowed = set()
|
allowed = set()
|
||||||
for db_name, db_actions in self.database_actions.items():
|
for db_name, db_actions in self.database_actions.items():
|
||||||
|
|
@ -57,8 +57,8 @@ class ActorRestrictions:
|
||||||
return allowed
|
return allowed
|
||||||
|
|
||||||
def get_allowed_tables(
|
def get_allowed_tables(
|
||||||
self, datasette: "Datasette", action: str
|
self, datasette: Datasette, action: str
|
||||||
) -> Set[Tuple[str, str]]:
|
) -> set[tuple[str, str]]:
|
||||||
"""Get (database, table) pairs where this action is allowed."""
|
"""Get (database, table) pairs where this action is allowed."""
|
||||||
allowed = set()
|
allowed = set()
|
||||||
for db_name, tables in self.table_actions.items():
|
for db_name, tables in self.table_actions.items():
|
||||||
|
|
@ -70,10 +70,10 @@ class ActorRestrictions:
|
||||||
|
|
||||||
@hookimpl(specname="permission_resources_sql")
|
@hookimpl(specname="permission_resources_sql")
|
||||||
async def actor_restrictions_sql(
|
async def actor_restrictions_sql(
|
||||||
datasette: "Datasette",
|
datasette: Datasette,
|
||||||
actor: Optional[dict],
|
actor: dict | None,
|
||||||
action: str,
|
action: str,
|
||||||
) -> Optional[List[PermissionSQL]]:
|
) -> list[PermissionSQL] | None:
|
||||||
"""
|
"""
|
||||||
Handle actor restriction-based permission rules.
|
Handle actor restriction-based permission rules.
|
||||||
|
|
||||||
|
|
@ -140,10 +140,10 @@ async def actor_restrictions_sql(
|
||||||
|
|
||||||
|
|
||||||
def restrictions_allow_action(
|
def restrictions_allow_action(
|
||||||
datasette: "Datasette",
|
datasette: Datasette,
|
||||||
restrictions: dict,
|
restrictions: dict,
|
||||||
action: str,
|
action: str,
|
||||||
resource: Optional[str | Tuple[str, str]],
|
resource: str | tuple[str, str] | None,
|
||||||
) -> bool:
|
) -> bool:
|
||||||
"""
|
"""
|
||||||
Check if restrictions allow the requested action on the requested resource.
|
Check if restrictions allow the requested action on the requested resource.
|
||||||
|
|
@ -185,11 +185,15 @@ def restrictions_allow_action(
|
||||||
# Check table/resource level
|
# Check table/resource level
|
||||||
if resource is not None and not isinstance(resource, str) and len(resource) == 2:
|
if resource is not None and not isinstance(resource, str) and len(resource) == 2:
|
||||||
database, table = resource
|
database, table = resource
|
||||||
table_allowed = restrictions.get("r", {}).get(database, {}).get(table)
|
action_obj = datasette.actions.get(action)
|
||||||
if table_allowed is not None:
|
normalize = action_obj.normalize_child if action_obj else lambda name: name
|
||||||
assert isinstance(table_allowed, list)
|
for table_name, table_allowed in (
|
||||||
if to_check.intersection(table_allowed):
|
restrictions.get("r", {}).get(database, {}).items()
|
||||||
return True
|
):
|
||||||
|
if normalize(table_name) == normalize(table):
|
||||||
|
assert isinstance(table_allowed, list)
|
||||||
|
if to_check.intersection(table_allowed):
|
||||||
|
return True
|
||||||
|
|
||||||
# This action is not explicitly allowed, so reject it
|
# This action is not explicitly allowed, so reject it
|
||||||
return False
|
return False
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ Grants full permissions to the root user when --root flag is used.
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import TYPE_CHECKING, Optional
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from datasette.app import Datasette
|
from datasette.app import Datasette
|
||||||
|
|
@ -17,9 +17,9 @@ from datasette.permissions import PermissionSQL
|
||||||
|
|
||||||
@hookimpl(specname="permission_resources_sql")
|
@hookimpl(specname="permission_resources_sql")
|
||||||
async def root_user_permissions_sql(
|
async def root_user_permissions_sql(
|
||||||
datasette: "Datasette",
|
datasette: Datasette,
|
||||||
actor: Optional[dict],
|
actor: dict | None,
|
||||||
) -> Optional[PermissionSQL]:
|
) -> PermissionSQL | None:
|
||||||
"""
|
"""
|
||||||
Grant root user full permissions when --root flag is used.
|
Grant root user full permissions when --root flag is used.
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
25
datasette/default_permissions/sqlite_statistics.py
Normal file
25
datasette/default_permissions/sqlite_statistics.py
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
"""Default table-access policy for SQLite optimizer statistics."""
|
||||||
|
|
||||||
|
import json
|
||||||
|
|
||||||
|
from datasette import hookimpl
|
||||||
|
from datasette.permissions import PermissionSQL
|
||||||
|
|
||||||
|
|
||||||
|
@hookimpl
|
||||||
|
def permission_resources_sql(action):
|
||||||
|
if action != "view-table":
|
||||||
|
return None
|
||||||
|
return PermissionSQL(
|
||||||
|
sql="""
|
||||||
|
SELECT database_name AS parent, value AS child, 0 AS allow,
|
||||||
|
'SQLite statistics tables are denied by default' AS reason
|
||||||
|
FROM catalog_databases
|
||||||
|
CROSS JOIN json_each(:sqlite_statistics_names)
|
||||||
|
""",
|
||||||
|
params={
|
||||||
|
"sqlite_statistics_names": json.dumps(
|
||||||
|
["sqlite_stat1", "sqlite_stat2", "sqlite_stat3", "sqlite_stat4"]
|
||||||
|
)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
@ -7,7 +7,7 @@ to datasette.verify_token() so all registered handlers are tried.
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import TYPE_CHECKING, Optional
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from datasette.app import Datasette
|
from datasette.app import Datasette
|
||||||
|
|
@ -17,15 +17,13 @@ from datasette.tokens import SignedTokenHandler
|
||||||
|
|
||||||
|
|
||||||
@hookimpl
|
@hookimpl
|
||||||
def register_token_handler(datasette: "Datasette"):
|
def register_token_handler(datasette: Datasette):
|
||||||
"""Register the default signed token handler."""
|
"""Register the default signed token handler."""
|
||||||
return SignedTokenHandler()
|
return SignedTokenHandler()
|
||||||
|
|
||||||
|
|
||||||
@hookimpl(specname="actor_from_request")
|
@hookimpl(specname="actor_from_request")
|
||||||
async def actor_from_signed_api_token(
|
async def actor_from_signed_api_token(datasette: Datasette, request) -> dict | None:
|
||||||
datasette: "Datasette", request
|
|
||||||
) -> Optional[dict]:
|
|
||||||
"""
|
"""
|
||||||
Authenticate requests using API tokens by delegating to all registered
|
Authenticate requests using API tokens by delegating to all registered
|
||||||
token handlers via datasette.verify_token().
|
token handlers via datasette.verify_token().
|
||||||
|
|
|
||||||
29
datasette/default_table_actions.py
Normal file
29
datasette/default_table_actions.py
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
from datasette import hookimpl
|
||||||
|
from datasette.resources import TableResource
|
||||||
|
|
||||||
|
|
||||||
|
@hookimpl
|
||||||
|
def table_actions(datasette, actor, database, table, request):
|
||||||
|
async def inner():
|
||||||
|
db = datasette.get_database(database)
|
||||||
|
if not db.is_mutable:
|
||||||
|
return []
|
||||||
|
if not await datasette.allowed(
|
||||||
|
action="alter-table",
|
||||||
|
resource=TableResource(database=database, table=table),
|
||||||
|
actor=actor,
|
||||||
|
):
|
||||||
|
return []
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
"type": "button",
|
||||||
|
"label": "Alter table",
|
||||||
|
"description": "Change columns and primary key for this table.",
|
||||||
|
"attrs": {
|
||||||
|
"aria-label": f"Alter table {table}",
|
||||||
|
"data-table-action": "alter-table",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
return inner
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
from abc import ABC, abstractproperty
|
from abc import ABC, abstractproperty
|
||||||
from dataclasses import asdict, dataclass, field
|
from dataclasses import asdict, dataclass, field
|
||||||
from datasette.hookspecs import hookimpl
|
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
|
|
||||||
|
from datasette.hookspecs import hookimpl
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Event(ABC):
|
class Event(ABC):
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,8 @@ from typing import ClassVar
|
||||||
|
|
||||||
from asyncinject import Registry
|
from asyncinject import Registry
|
||||||
|
|
||||||
|
from datasette.utils.asgi import BadRequest
|
||||||
|
|
||||||
|
|
||||||
def extra_names_from_request(request):
|
def extra_names_from_request(request):
|
||||||
extra_bits = request.args.getlist("_extra")
|
extra_bits = request.args.getlist("_extra")
|
||||||
|
|
@ -81,6 +83,16 @@ class ExtraRegistry:
|
||||||
def public_classes_for_scope(self, scope):
|
def public_classes_for_scope(self, scope):
|
||||||
return self.classes_for_scope(scope, include_internal=False)
|
return self.classes_for_scope(scope, include_internal=False)
|
||||||
|
|
||||||
|
def internal_classes_for_scope(self, scope):
|
||||||
|
# Extras that are available to HTML templates but excluded from
|
||||||
|
# JSON responses - plain Providers are dependency plumbing and
|
||||||
|
# never surface as keys, so they are not included
|
||||||
|
return [
|
||||||
|
cls
|
||||||
|
for cls in self.classes_for_scope(scope)
|
||||||
|
if issubclass(cls, Extra) and not cls.public
|
||||||
|
]
|
||||||
|
|
||||||
def _registry_for_scope(self, scope):
|
def _registry_for_scope(self, scope):
|
||||||
registry = self._scope_registries.get(scope)
|
registry = self._scope_registries.get(scope)
|
||||||
if registry is None:
|
if registry is None:
|
||||||
|
|
@ -103,6 +115,17 @@ class ExtraRegistry:
|
||||||
self._allowed_names[key] = names
|
self._allowed_names[key] = names
|
||||||
return names
|
return names
|
||||||
|
|
||||||
|
def validate_requested(self, requested, scope):
|
||||||
|
"""
|
||||||
|
Raise BadRequest if any requested extra name is not a public extra
|
||||||
|
for this scope. Used by data formats such as .json - HTML pages
|
||||||
|
silently ignore unknown names instead.
|
||||||
|
"""
|
||||||
|
allowed = self._allowed_names_for_scope(scope, include_internal=False)
|
||||||
|
unknown = sorted(name for name in requested if name not in allowed)
|
||||||
|
if unknown:
|
||||||
|
raise BadRequest("Unknown _extra: {}".format(", ".join(unknown)))
|
||||||
|
|
||||||
async def resolve(self, requested, context, scope, include_internal=False):
|
async def resolve(self, requested, context, scope, include_internal=False):
|
||||||
allowed_names = self._allowed_names_for_scope(scope, include_internal)
|
allowed_names = self._allowed_names_for_scope(scope, include_internal)
|
||||||
requested_names = [name for name in requested if name in allowed_names]
|
requested_names = [name for name in requested if name in allowed_names]
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,13 @@
|
||||||
import json
|
import json
|
||||||
import urllib
|
import urllib
|
||||||
|
|
||||||
from datasette import hookimpl
|
from datasette import hookimpl
|
||||||
from datasette.database import QueryInterrupted
|
from datasette.database import QueryInterrupted
|
||||||
from datasette.utils import (
|
from datasette.utils import (
|
||||||
|
detect_json1,
|
||||||
escape_sqlite,
|
escape_sqlite,
|
||||||
path_with_added_args,
|
path_with_added_args,
|
||||||
path_with_removed_args,
|
path_with_removed_args,
|
||||||
detect_json1,
|
|
||||||
sqlite3,
|
sqlite3,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -30,7 +31,7 @@ def load_facet_configs(request, table_config):
|
||||||
assert (
|
assert (
|
||||||
len(facet_config.values()) == 1
|
len(facet_config.values()) == 1
|
||||||
), "Metadata config dicts should be {type: config}"
|
), "Metadata config dicts should be {type: config}"
|
||||||
type, facet_config = list(facet_config.items())[0]
|
type, facet_config = next(iter(facet_config.items()))
|
||||||
if isinstance(facet_config, str):
|
if isinstance(facet_config, str):
|
||||||
facet_config = {"simple": facet_config}
|
facet_config = {"simple": facet_config}
|
||||||
facet_configs.setdefault(type, []).append(
|
facet_configs.setdefault(type, []).append(
|
||||||
|
|
@ -38,7 +39,7 @@ def load_facet_configs(request, table_config):
|
||||||
)
|
)
|
||||||
qs_pairs = urllib.parse.parse_qs(request.query_string, keep_blank_values=True)
|
qs_pairs = urllib.parse.parse_qs(request.query_string, keep_blank_values=True)
|
||||||
for key, values in qs_pairs.items():
|
for key, values in qs_pairs.items():
|
||||||
if key.startswith("_facet"):
|
if key == "_facet" or key.startswith("_facet_"):
|
||||||
# Figure out the facet type
|
# Figure out the facet type
|
||||||
if key == "_facet":
|
if key == "_facet":
|
||||||
type = "column"
|
type = "column"
|
||||||
|
|
@ -85,7 +86,7 @@ class Facet:
|
||||||
self.database = database
|
self.database = database
|
||||||
# For foreign key expansion. Can be None for e.g. stored SQL queries:
|
# For foreign key expansion. Can be None for e.g. stored SQL queries:
|
||||||
self.table = table
|
self.table = table
|
||||||
self.sql = sql or f"select * from [{table}]"
|
self.sql = sql or f"select * from {escape_sqlite(table)}"
|
||||||
self.params = params or []
|
self.params = params or []
|
||||||
self.table_config = table_config
|
self.table_config = table_config
|
||||||
# row_count can be None, in which case we calculate it ourselves:
|
# row_count can be None, in which case we calculate it ourselves:
|
||||||
|
|
@ -160,18 +161,13 @@ class ColumnFacet(Facet):
|
||||||
for column in columns:
|
for column in columns:
|
||||||
if column in already_enabled:
|
if column in already_enabled:
|
||||||
continue
|
continue
|
||||||
suggested_facet_sql = """
|
suggested_facet_sql = f"""
|
||||||
with limited as (select * from ({sql}) limit {suggest_consider})
|
with limited as (select * from ({self.sql}) limit {self.suggest_consider})
|
||||||
select {column} as value, count(*) as n from limited
|
select {escape_sqlite(column)} as value, count(*) as n from limited
|
||||||
where value is not null
|
where value is not null
|
||||||
group by value
|
group by value
|
||||||
limit {limit}
|
limit {facet_size + 1}
|
||||||
""".format(
|
"""
|
||||||
column=escape_sqlite(column),
|
|
||||||
sql=self.sql,
|
|
||||||
limit=facet_size + 1,
|
|
||||||
suggest_consider=self.suggest_consider,
|
|
||||||
)
|
|
||||||
distinct_values = None
|
distinct_values = None
|
||||||
try:
|
try:
|
||||||
distinct_values = await self.ds.execute(
|
distinct_values = await self.ds.execute(
|
||||||
|
|
@ -267,11 +263,16 @@ class ColumnFacet(Facet):
|
||||||
for row in facet_rows:
|
for row in facet_rows:
|
||||||
column_qs = column
|
column_qs = column
|
||||||
if column.startswith("_"):
|
if column.startswith("_"):
|
||||||
column_qs = "{}__exact".format(column)
|
column_qs = f"{column}__exact"
|
||||||
selected = (column_qs, str(row["value"])) in qs_pairs
|
selected_args = {
|
||||||
|
key: str(row["value"])
|
||||||
|
for key in (column_qs, f"{column}__exact")
|
||||||
|
if (key, str(row["value"])) in qs_pairs
|
||||||
|
}
|
||||||
|
selected = bool(selected_args)
|
||||||
if selected:
|
if selected:
|
||||||
toggle_path = path_with_removed_args(
|
toggle_path = path_with_removed_args(
|
||||||
self.request, {column_qs: str(row["value"])}
|
self.request, selected_args
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
toggle_path = path_with_added_args(
|
toggle_path = path_with_added_args(
|
||||||
|
|
@ -342,12 +343,12 @@ class ArrayFacet(Facet):
|
||||||
for v in await self.ds.execute(
|
for v in await self.ds.execute(
|
||||||
self.database,
|
self.database,
|
||||||
(
|
(
|
||||||
"select {column} from ({sql}) "
|
f"select {escape_sqlite(column)} from ({self.sql}) "
|
||||||
"where {column} is not null "
|
f"where {escape_sqlite(column)} is not null "
|
||||||
"and {column} != '' "
|
f"and {escape_sqlite(column)} != '' "
|
||||||
"and json_array_length({column}) > 0 "
|
f"and json_array_length({escape_sqlite(column)}) > 0 "
|
||||||
"limit 100"
|
"limit 100"
|
||||||
).format(column=escape_sqlite(column), sql=self.sql),
|
),
|
||||||
self.params,
|
self.params,
|
||||||
truncate=False,
|
truncate=False,
|
||||||
custom_time_limit=self.ds.setting(
|
custom_time_limit=self.ds.setting(
|
||||||
|
|
@ -388,14 +389,14 @@ class ArrayFacet(Facet):
|
||||||
source = source_and_config["source"]
|
source = source_and_config["source"]
|
||||||
column = config.get("column") or config["simple"]
|
column = config.get("column") or config["simple"]
|
||||||
# https://github.com/simonw/datasette/issues/448
|
# https://github.com/simonw/datasette/issues/448
|
||||||
facet_sql = """
|
facet_sql = f"""
|
||||||
with inner as ({sql}),
|
with inner as ({self.sql}),
|
||||||
deduped_array_items as (
|
deduped_array_items as (
|
||||||
select
|
select
|
||||||
distinct j.value,
|
distinct j.value,
|
||||||
inner.*
|
inner.*
|
||||||
from
|
from
|
||||||
json_each([inner].{col}) j
|
json_each([inner].{escape_sqlite(column)}) j
|
||||||
join inner
|
join inner
|
||||||
)
|
)
|
||||||
select
|
select
|
||||||
|
|
@ -406,12 +407,8 @@ class ArrayFacet(Facet):
|
||||||
group by
|
group by
|
||||||
value
|
value
|
||||||
order by
|
order by
|
||||||
count(*) desc, value limit {limit}
|
count(*) desc, value limit {facet_size + 1}
|
||||||
""".format(
|
"""
|
||||||
col=escape_sqlite(column),
|
|
||||||
sql=self.sql,
|
|
||||||
limit=facet_size + 1,
|
|
||||||
)
|
|
||||||
try:
|
try:
|
||||||
facet_rows_results = await self.ds.execute(
|
facet_rows_results = await self.ds.execute(
|
||||||
self.database,
|
self.database,
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,12 @@
|
||||||
from datasette import hookimpl
|
|
||||||
from datasette.resources import DatabaseResource
|
|
||||||
from datasette.views.base import DatasetteError
|
|
||||||
from datasette.utils.asgi import BadRequest
|
|
||||||
import json
|
import json
|
||||||
|
import math
|
||||||
|
from typing import ClassVar
|
||||||
|
|
||||||
|
from datasette import hookimpl
|
||||||
|
from datasette.resources import DatabaseResource, TableResource
|
||||||
|
from datasette.utils.asgi import BadRequest
|
||||||
|
from datasette.views.base import DatasetteError
|
||||||
|
|
||||||
from .utils import detect_json1, escape_sqlite, path_with_removed_args
|
from .utils import detect_json1, escape_sqlite, path_with_removed_args
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -48,13 +52,20 @@ def search_filters(request, database, table, datasette):
|
||||||
human_descriptions = []
|
human_descriptions = []
|
||||||
extra_context = {}
|
extra_context = {}
|
||||||
|
|
||||||
# Figure out which fts_table to use
|
# Figure out which trusted fts_table to use. Query string parameters can
|
||||||
|
# repeat this mapping (for backwards compatibility), but must not select
|
||||||
|
# a different table or primary key.
|
||||||
table_metadata = await datasette.table_config(database, table)
|
table_metadata = await datasette.table_config(database, table)
|
||||||
db = datasette.get_database(database)
|
db = datasette.get_database(database)
|
||||||
fts_table = request.args.get("_fts_table")
|
fts_table = table_metadata.get("fts_table")
|
||||||
fts_table = fts_table or table_metadata.get("fts_table")
|
|
||||||
fts_table = fts_table or await db.fts_table(table)
|
fts_table = fts_table or await db.fts_table(table)
|
||||||
fts_pk = request.args.get("_fts_pk", table_metadata.get("fts_pk", "rowid"))
|
fts_pk = table_metadata.get("fts_pk", "rowid")
|
||||||
|
requested_fts_table = request.args.get("_fts_table")
|
||||||
|
requested_fts_pk = request.args.get("_fts_pk")
|
||||||
|
if (requested_fts_table and requested_fts_table != fts_table) or (
|
||||||
|
requested_fts_pk and requested_fts_pk != fts_pk
|
||||||
|
):
|
||||||
|
raise BadRequest("Invalid _fts_table or _fts_pk")
|
||||||
search_args = {
|
search_args = {
|
||||||
key: request.args[key]
|
key: request.args[key]
|
||||||
for key in request.args
|
for key in request.args
|
||||||
|
|
@ -72,6 +83,11 @@ def search_filters(request, database, table, datasette):
|
||||||
extra_context["supports_search"] = bool(fts_table)
|
extra_context["supports_search"] = bool(fts_table)
|
||||||
|
|
||||||
if fts_table and search_args:
|
if fts_table and search_args:
|
||||||
|
await datasette.ensure_permission(
|
||||||
|
action="view-table",
|
||||||
|
resource=TableResource(database=database, table=fts_table),
|
||||||
|
actor=request.actor,
|
||||||
|
)
|
||||||
if "_search" in search_args:
|
if "_search" in search_args:
|
||||||
# Simple ?_search=xxx
|
# Simple ?_search=xxx
|
||||||
search = search_args["_search"]
|
search = search_args["_search"]
|
||||||
|
|
@ -99,9 +115,9 @@ def search_filters(request, database, table, datasette):
|
||||||
fts_table=escape_sqlite(fts_table),
|
fts_table=escape_sqlite(fts_table),
|
||||||
search_col=escape_sqlite(search_col),
|
search_col=escape_sqlite(search_col),
|
||||||
match_clause=(
|
match_clause=(
|
||||||
":search_{}".format(i)
|
f":search_{i}"
|
||||||
if search_mode_raw
|
if search_mode_raw
|
||||||
else "escape_fts(:search_{})".format(i)
|
else f"escape_fts(:search_{i})"
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
@ -132,13 +148,18 @@ def through_filters(request, database, table, datasette):
|
||||||
through_table = through_data["table"]
|
through_table = through_data["table"]
|
||||||
other_column = through_data["column"]
|
other_column = through_data["column"]
|
||||||
value = through_data["value"]
|
value = through_data["value"]
|
||||||
|
await datasette.ensure_permission(
|
||||||
|
action="view-table",
|
||||||
|
resource=TableResource(database=database, table=through_table),
|
||||||
|
actor=request.actor,
|
||||||
|
)
|
||||||
db = datasette.get_database(database)
|
db = datasette.get_database(database)
|
||||||
outgoing_foreign_keys = await db.foreign_keys_for_table(through_table)
|
outgoing_foreign_keys = await db.foreign_keys_for_table(through_table)
|
||||||
try:
|
fk_to_us = next(
|
||||||
fk_to_us = [
|
(fk for fk in outgoing_foreign_keys if fk["other_table"] == table),
|
||||||
fk for fk in outgoing_foreign_keys if fk["other_table"] == table
|
None,
|
||||||
][0]
|
)
|
||||||
except IndexError:
|
if fk_to_us is None:
|
||||||
raise DatasetteError(
|
raise DatasetteError(
|
||||||
"Invalid _through - could not find corresponding foreign key"
|
"Invalid _through - could not find corresponding foreign key"
|
||||||
)
|
)
|
||||||
|
|
@ -182,6 +203,17 @@ class Filter:
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
|
def _coerce_numeric_filter_value(value):
|
||||||
|
try:
|
||||||
|
return int(value)
|
||||||
|
except ValueError:
|
||||||
|
try:
|
||||||
|
converted = float(value)
|
||||||
|
except ValueError:
|
||||||
|
return value
|
||||||
|
return converted if math.isfinite(converted) else value
|
||||||
|
|
||||||
|
|
||||||
class TemplatedFilter(Filter):
|
class TemplatedFilter(Filter):
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
|
|
@ -203,13 +235,17 @@ class TemplatedFilter(Filter):
|
||||||
|
|
||||||
def where_clause(self, table, column, value, param_counter):
|
def where_clause(self, table, column, value, param_counter):
|
||||||
converted = self.format.format(value)
|
converted = self.format.format(value)
|
||||||
if self.numeric and converted.isdigit():
|
if self.numeric:
|
||||||
converted = int(converted)
|
converted = _coerce_numeric_filter_value(converted)
|
||||||
if self.no_argument:
|
if self.no_argument:
|
||||||
kwargs = {"c": column}
|
kwargs = {"c": _quote_sqlite_identifier(column)}
|
||||||
converted = None
|
converted = None
|
||||||
else:
|
else:
|
||||||
kwargs = {"c": column, "p": f"p{param_counter}", "t": table}
|
kwargs = {
|
||||||
|
"c": _quote_sqlite_identifier(column),
|
||||||
|
"p": f"p{param_counter}",
|
||||||
|
"t": _quote_sqlite_identifier(table),
|
||||||
|
}
|
||||||
return self.sql_template.format(**kwargs), converted
|
return self.sql_template.format(**kwargs), converted
|
||||||
|
|
||||||
def human_clause(self, column, value):
|
def human_clause(self, column, value):
|
||||||
|
|
@ -223,6 +259,14 @@ class TemplatedFilter(Filter):
|
||||||
return template.format(c=column, v=value)
|
return template.format(c=column, v=value)
|
||||||
|
|
||||||
|
|
||||||
|
def _quote_sqlite_identifier(identifier):
|
||||||
|
# Preserve the historic always-quoted SQL generated by TemplatedFilter.
|
||||||
|
escaped = escape_sqlite(identifier)
|
||||||
|
if escaped == identifier:
|
||||||
|
return f'"{identifier}"'
|
||||||
|
return escaped
|
||||||
|
|
||||||
|
|
||||||
class InFilter(Filter):
|
class InFilter(Filter):
|
||||||
key = "in"
|
key = "in"
|
||||||
display = "in"
|
display = "in"
|
||||||
|
|
@ -264,56 +308,56 @@ class Filters:
|
||||||
TemplatedFilter(
|
TemplatedFilter(
|
||||||
"exact",
|
"exact",
|
||||||
"=",
|
"=",
|
||||||
'"{c}" = :{p}',
|
"{c} = :{p}",
|
||||||
lambda c, v: "{c} = {v}" if v.isdigit() else '{c} = "{v}"',
|
lambda c, v: "{c} = {v}" if v.isdigit() else '{c} = "{v}"',
|
||||||
),
|
),
|
||||||
TemplatedFilter(
|
TemplatedFilter(
|
||||||
"not",
|
"not",
|
||||||
"!=",
|
"!=",
|
||||||
'"{c}" != :{p}',
|
"{c} != :{p}",
|
||||||
lambda c, v: "{c} != {v}" if v.isdigit() else '{c} != "{v}"',
|
lambda c, v: "{c} != {v}" if v.isdigit() else '{c} != "{v}"',
|
||||||
),
|
),
|
||||||
TemplatedFilter(
|
TemplatedFilter(
|
||||||
"contains",
|
"contains",
|
||||||
"contains",
|
"contains",
|
||||||
'"{c}" like :{p}',
|
"{c} like :{p}",
|
||||||
'{c} contains "{v}"',
|
'{c} contains "{v}"',
|
||||||
format="%{}%",
|
format="%{}%",
|
||||||
),
|
),
|
||||||
TemplatedFilter(
|
TemplatedFilter(
|
||||||
"notcontains",
|
"notcontains",
|
||||||
"does not contain",
|
"does not contain",
|
||||||
'"{c}" not like :{p}',
|
"{c} not like :{p}",
|
||||||
'{c} does not contain "{v}"',
|
'{c} does not contain "{v}"',
|
||||||
format="%{}%",
|
format="%{}%",
|
||||||
),
|
),
|
||||||
TemplatedFilter(
|
TemplatedFilter(
|
||||||
"endswith",
|
"endswith",
|
||||||
"ends with",
|
"ends with",
|
||||||
'"{c}" like :{p}',
|
"{c} like :{p}",
|
||||||
'{c} ends with "{v}"',
|
'{c} ends with "{v}"',
|
||||||
format="%{}",
|
format="%{}",
|
||||||
),
|
),
|
||||||
TemplatedFilter(
|
TemplatedFilter(
|
||||||
"startswith",
|
"startswith",
|
||||||
"starts with",
|
"starts with",
|
||||||
'"{c}" like :{p}',
|
"{c} like :{p}",
|
||||||
'{c} starts with "{v}"',
|
'{c} starts with "{v}"',
|
||||||
format="{}%",
|
format="{}%",
|
||||||
),
|
),
|
||||||
TemplatedFilter("gt", ">", '"{c}" > :{p}', "{c} > {v}", numeric=True),
|
TemplatedFilter("gt", ">", "{c} > :{p}", "{c} > {v}", numeric=True),
|
||||||
TemplatedFilter(
|
TemplatedFilter(
|
||||||
"gte", "\u2265", '"{c}" >= :{p}', "{c} \u2265 {v}", numeric=True
|
"gte", "\u2265", "{c} >= :{p}", "{c} \u2265 {v}", numeric=True
|
||||||
),
|
),
|
||||||
TemplatedFilter("lt", "<", '"{c}" < :{p}', "{c} < {v}", numeric=True),
|
TemplatedFilter("lt", "<", "{c} < :{p}", "{c} < {v}", numeric=True),
|
||||||
TemplatedFilter(
|
TemplatedFilter(
|
||||||
"lte", "\u2264", '"{c}" <= :{p}', "{c} \u2264 {v}", numeric=True
|
"lte", "\u2264", "{c} <= :{p}", "{c} \u2264 {v}", numeric=True
|
||||||
),
|
),
|
||||||
TemplatedFilter("like", "like", '"{c}" like :{p}', '{c} like "{v}"'),
|
TemplatedFilter("like", "like", "{c} like :{p}", '{c} like "{v}"'),
|
||||||
TemplatedFilter(
|
TemplatedFilter(
|
||||||
"notlike", "not like", '"{c}" not like :{p}', '{c} not like "{v}"'
|
"notlike", "not like", "{c} not like :{p}", '{c} not like "{v}"'
|
||||||
),
|
),
|
||||||
TemplatedFilter("glob", "glob", '"{c}" glob :{p}', '{c} glob "{v}"'),
|
TemplatedFilter("glob", "glob", "{c} glob :{p}", '{c} glob "{v}"'),
|
||||||
InFilter(),
|
InFilter(),
|
||||||
NotInFilter(),
|
NotInFilter(),
|
||||||
]
|
]
|
||||||
|
|
@ -322,13 +366,13 @@ class Filters:
|
||||||
TemplatedFilter(
|
TemplatedFilter(
|
||||||
"arraycontains",
|
"arraycontains",
|
||||||
"array contains",
|
"array contains",
|
||||||
""":{p} in (select value from json_each([{t}].[{c}]))""",
|
""":{p} in (select value from json_each({t}.{c}))""",
|
||||||
'{c} contains "{v}"',
|
'{c} contains "{v}"',
|
||||||
),
|
),
|
||||||
TemplatedFilter(
|
TemplatedFilter(
|
||||||
"arraynotcontains",
|
"arraynotcontains",
|
||||||
"array does not contain",
|
"array does not contain",
|
||||||
""":{p} not in (select value from json_each([{t}].[{c}]))""",
|
""":{p} not in (select value from json_each({t}.{c}))""",
|
||||||
'{c} does not contain "{v}"',
|
'{c} does not contain "{v}"',
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
@ -336,36 +380,34 @@ class Filters:
|
||||||
else []
|
else []
|
||||||
)
|
)
|
||||||
+ [
|
+ [
|
||||||
|
TemplatedFilter("date", "date", "date({c}) = :{p}", '"{c}" is on date {v}'),
|
||||||
TemplatedFilter(
|
TemplatedFilter(
|
||||||
"date", "date", 'date("{c}") = :{p}', '"{c}" is on date {v}'
|
"isnull", "is null", "{c} is null", "{c} is null", no_argument=True
|
||||||
),
|
|
||||||
TemplatedFilter(
|
|
||||||
"isnull", "is null", '"{c}" is null', "{c} is null", no_argument=True
|
|
||||||
),
|
),
|
||||||
TemplatedFilter(
|
TemplatedFilter(
|
||||||
"notnull",
|
"notnull",
|
||||||
"is not null",
|
"is not null",
|
||||||
'"{c}" is not null',
|
"{c} is not null",
|
||||||
"{c} is not null",
|
"{c} is not null",
|
||||||
no_argument=True,
|
no_argument=True,
|
||||||
),
|
),
|
||||||
TemplatedFilter(
|
TemplatedFilter(
|
||||||
"isblank",
|
"isblank",
|
||||||
"is blank",
|
"is blank",
|
||||||
'("{c}" is null or "{c}" = "")',
|
"({c} is null or {c} = '')",
|
||||||
"{c} is blank",
|
"{c} is blank",
|
||||||
no_argument=True,
|
no_argument=True,
|
||||||
),
|
),
|
||||||
TemplatedFilter(
|
TemplatedFilter(
|
||||||
"notblank",
|
"notblank",
|
||||||
"is not blank",
|
"is not blank",
|
||||||
'("{c}" is not null and "{c}" != "")',
|
"({c} is not null and {c} != '')",
|
||||||
"{c} is not blank",
|
"{c} is not blank",
|
||||||
no_argument=True,
|
no_argument=True,
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
_filters_by_key = {f.key: f for f in _filters}
|
_filters_by_key: ClassVar[dict[str, Filter]] = {f.key: f for f in _filters}
|
||||||
|
|
||||||
def __init__(self, pairs):
|
def __init__(self, pairs):
|
||||||
self.pairs = pairs
|
self.pairs = pairs
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,10 @@
|
||||||
from datasette.utils.sqlite import sqlite3
|
|
||||||
from datasette.utils import documented
|
|
||||||
import itertools
|
import itertools
|
||||||
import random
|
import random
|
||||||
import string
|
import string
|
||||||
|
|
||||||
|
from datasette.utils import documented
|
||||||
|
from datasette.utils.sqlite import sqlite3
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"EXTRA_DATABASE_SQL",
|
"EXTRA_DATABASE_SQL",
|
||||||
"TABLES",
|
"TABLES",
|
||||||
|
|
@ -346,9 +347,7 @@ CREATE VIEW searchable_view_configured_by_metadata AS
|
||||||
+ '\nINSERT INTO no_primary_key VALUES ("RENDER_CELL_DEMO", "a202", "b202", "c202");\n'
|
+ '\nINSERT INTO no_primary_key VALUES ("RENDER_CELL_DEMO", "a202", "b202", "c202");\n'
|
||||||
+ "\n".join(
|
+ "\n".join(
|
||||||
[
|
[
|
||||||
'INSERT INTO compound_three_primary_keys VALUES ("{a}", "{b}", "{c}", "{content}");'.format(
|
f'INSERT INTO compound_three_primary_keys VALUES ("{a}", "{b}", "{c}", "{content}");'
|
||||||
a=a, b=b, c=c, content=content
|
|
||||||
)
|
|
||||||
for a, b, c, content in generate_compound_rows(1001)
|
for a, b, c, content in generate_compound_rows(1001)
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,20 @@
|
||||||
from datasette import hookimpl, Response
|
from datasette import Response, hookimpl
|
||||||
|
|
||||||
|
from .utils import add_cors_headers
|
||||||
|
|
||||||
|
|
||||||
@hookimpl(trylast=True)
|
@hookimpl(trylast=True)
|
||||||
def forbidden(datasette, request, message):
|
def forbidden(datasette, request, message):
|
||||||
async def inner():
|
async def inner():
|
||||||
|
if (
|
||||||
|
request.path.split("?")[0].endswith(".json")
|
||||||
|
or "application/json" in (request.headers.get("accept") or "")
|
||||||
|
or request.headers.get("content-type") == "application/json"
|
||||||
|
):
|
||||||
|
headers = {}
|
||||||
|
if datasette.cors:
|
||||||
|
add_cors_headers(headers)
|
||||||
|
return Response.error(message, 403, headers=headers)
|
||||||
return Response.html(
|
return Response.html(
|
||||||
await datasette.render_template(
|
await datasette.render_template(
|
||||||
"error.html",
|
"error.html",
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,21 @@
|
||||||
from datasette import hookimpl, Response
|
import traceback
|
||||||
from .utils import add_cors_headers
|
|
||||||
|
from markupsafe import Markup
|
||||||
|
|
||||||
|
from datasette import Response, hookimpl
|
||||||
|
|
||||||
|
from .utils import add_cors_headers, error_body
|
||||||
from .utils.asgi import (
|
from .utils.asgi import (
|
||||||
Base400,
|
Base400,
|
||||||
)
|
)
|
||||||
from .views.base import DatasetteError
|
from .views.base import DatasetteError
|
||||||
from markupsafe import Markup
|
|
||||||
import traceback
|
|
||||||
|
|
||||||
|
# Debugger imports are deliberate - they back the "pdb" setting, which drops
|
||||||
|
# into a debugger on unhandled exceptions
|
||||||
try:
|
try:
|
||||||
import ipdb as pdb
|
import ipdb as pdb # noqa: T100
|
||||||
except ImportError:
|
except ImportError:
|
||||||
import pdb
|
import pdb # noqa: T100
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import rich
|
import rich
|
||||||
|
|
@ -28,6 +33,7 @@ def handle_exception(datasette, request, exception):
|
||||||
rich.get_console().print_exception(show_locals=True)
|
rich.get_console().print_exception(show_locals=True)
|
||||||
|
|
||||||
title = None
|
title = None
|
||||||
|
plain_message = None
|
||||||
if isinstance(exception, Base400):
|
if isinstance(exception, Base400):
|
||||||
status = exception.status
|
status = exception.status
|
||||||
info = {}
|
info = {}
|
||||||
|
|
@ -36,6 +42,7 @@ def handle_exception(datasette, request, exception):
|
||||||
status = exception.status
|
status = exception.status
|
||||||
info = exception.error_dict
|
info = exception.error_dict
|
||||||
message = exception.message
|
message = exception.message
|
||||||
|
plain_message = exception.plain_message
|
||||||
if exception.message_is_html:
|
if exception.message_is_html:
|
||||||
message = Markup(message)
|
message = Markup(message)
|
||||||
title = exception.title
|
title = exception.title
|
||||||
|
|
@ -45,6 +52,17 @@ def handle_exception(datasette, request, exception):
|
||||||
message = str(exception)
|
message = str(exception)
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
templates = [f"{status}.html", "error.html"]
|
templates = [f"{status}.html", "error.html"]
|
||||||
|
headers = {}
|
||||||
|
if datasette.cors:
|
||||||
|
add_cors_headers(headers)
|
||||||
|
if request.path.split("?")[0].endswith(".json"):
|
||||||
|
body = dict(info)
|
||||||
|
body.update(error_body(plain_message or message, status))
|
||||||
|
return Response.json(body, status=status, headers=headers)
|
||||||
|
if request.path.split("?")[0].endswith(".csv"):
|
||||||
|
return Response.text(
|
||||||
|
plain_message or message, status=status, headers=headers
|
||||||
|
)
|
||||||
info.update(
|
info.update(
|
||||||
{
|
{
|
||||||
"ok": False,
|
"ok": False,
|
||||||
|
|
@ -53,25 +71,18 @@ def handle_exception(datasette, request, exception):
|
||||||
"title": title,
|
"title": title,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
headers = {}
|
environment = datasette.get_jinja_environment(request)
|
||||||
if datasette.cors:
|
template = environment.select_template(templates)
|
||||||
add_cors_headers(headers)
|
return Response.html(
|
||||||
if request.path.split("?")[0].endswith(".json"):
|
await template.render_async(
|
||||||
return Response.json(info, status=status, headers=headers)
|
dict(
|
||||||
else:
|
info,
|
||||||
environment = datasette.get_jinja_environment(request)
|
urls=datasette.urls,
|
||||||
template = environment.select_template(templates)
|
menu_links=list,
|
||||||
return Response.html(
|
)
|
||||||
await template.render_async(
|
),
|
||||||
dict(
|
status=status,
|
||||||
info,
|
headers=headers,
|
||||||
urls=datasette.urls,
|
)
|
||||||
app_css_hash=datasette.app_css_hash(),
|
|
||||||
menu_links=lambda: [],
|
|
||||||
)
|
|
||||||
),
|
|
||||||
status=status,
|
|
||||||
headers=headers,
|
|
||||||
)
|
|
||||||
|
|
||||||
return inner
|
return inner
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
from pluggy import HookimplMarker
|
from pluggy import HookimplMarker, HookspecMarker
|
||||||
from pluggy import HookspecMarker
|
|
||||||
|
|
||||||
hookspec = HookspecMarker("datasette")
|
hookspec = HookspecMarker("datasette")
|
||||||
hookimpl = HookimplMarker("datasette")
|
hookimpl = HookimplMarker("datasette")
|
||||||
|
|
@ -10,6 +9,11 @@ def startup(datasette):
|
||||||
"""Fires directly after Datasette first starts running"""
|
"""Fires directly after Datasette first starts running"""
|
||||||
|
|
||||||
|
|
||||||
|
@hookspec
|
||||||
|
def shutdown(datasette):
|
||||||
|
"""Called once when the Datasette server is shutting down"""
|
||||||
|
|
||||||
|
|
||||||
@hookspec
|
@hookspec
|
||||||
def asgi_wrapper(datasette):
|
def asgi_wrapper(datasette):
|
||||||
"""Returns an ASGI middleware callable to wrap our ASGI application with"""
|
"""Returns an ASGI middleware callable to wrap our ASGI application with"""
|
||||||
|
|
@ -46,7 +50,7 @@ def extra_body_script(
|
||||||
def extra_template_vars(
|
def extra_template_vars(
|
||||||
template, database, table, columns, view_name, request, datasette
|
template, database, table, columns, view_name, request, datasette
|
||||||
):
|
):
|
||||||
"""Extra template variables to be made available to the template - can return dict or callable or awaitable"""
|
"""Extra template variables to be made available to the template - can return dict, None, callable or awaitable"""
|
||||||
|
|
||||||
|
|
||||||
@hookspec
|
@hookspec
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,13 @@
|
||||||
import hashlib
|
import hashlib
|
||||||
|
|
||||||
from .utils import (
|
from .utils import (
|
||||||
detect_spatialite,
|
|
||||||
detect_fts,
|
detect_fts,
|
||||||
detect_primary_keys,
|
detect_primary_keys,
|
||||||
|
detect_spatialite,
|
||||||
escape_sqlite,
|
escape_sqlite,
|
||||||
get_all_foreign_keys,
|
get_all_foreign_keys,
|
||||||
table_columns,
|
|
||||||
sqlite3,
|
sqlite3,
|
||||||
|
table_columns,
|
||||||
)
|
)
|
||||||
|
|
||||||
HASH_BLOCK_SIZE = 1024 * 1024
|
HASH_BLOCK_SIZE = 1024 * 1024
|
||||||
|
|
@ -95,10 +95,10 @@ def inspect_tables(conn, database_metadata):
|
||||||
""")
|
""")
|
||||||
]
|
]
|
||||||
|
|
||||||
for t in tables.keys():
|
for t, table_info in tables.items():
|
||||||
for hidden_table in hidden_tables:
|
for hidden_table in hidden_tables:
|
||||||
if t == hidden_table or t.startswith(hidden_table):
|
if t == hidden_table or t.startswith(hidden_table):
|
||||||
tables[t]["hidden"] = True
|
table_info["hidden"] = True
|
||||||
continue
|
continue
|
||||||
|
|
||||||
return tables
|
return tables
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ class JumpSQL:
|
||||||
search_text: str | None = None,
|
search_text: str | None = None,
|
||||||
display_name: str | None = None,
|
display_name: str | None = None,
|
||||||
item_type: str = "menu",
|
item_type: str = "menu",
|
||||||
) -> "JumpSQL":
|
) -> JumpSQL:
|
||||||
if search_text is None:
|
if search_text is None:
|
||||||
search_text = " ".join(
|
search_text = " ".join(
|
||||||
text for text in (label, display_name, description) if text is not None
|
text for text in (label, display_name, description) if text is not None
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,11 @@
|
||||||
|
import contextvars
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import Any, NamedTuple
|
from typing import Any, NamedTuple
|
||||||
import contextvars
|
|
||||||
|
_SQLITE_IDENTIFIER_CASE = str.maketrans(
|
||||||
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"
|
||||||
|
)
|
||||||
|
|
||||||
# Context variable to track when permission checks should be skipped
|
# Context variable to track when permission checks should be skipped
|
||||||
_skip_permission_checks = contextvars.ContextVar(
|
_skip_permission_checks = contextvars.ContextVar(
|
||||||
|
|
@ -49,6 +53,15 @@ class Resource(ABC):
|
||||||
# Class-level metadata (subclasses must define these)
|
# Class-level metadata (subclasses must define these)
|
||||||
name: str = None # e.g., "table", "database", "model"
|
name: str = None # e.g., "table", "database", "model"
|
||||||
parent_class: type["Resource"] | None = None # e.g., DatabaseResource for tables
|
parent_class: type["Resource"] | None = None # e.g., DatabaseResource for tables
|
||||||
|
case_insensitive_child: bool = False
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def normalize_child(cls, child: str | None) -> str | None:
|
||||||
|
"""Return a comparison key without changing the resource's display name."""
|
||||||
|
if cls.case_insensitive_child and child is not None:
|
||||||
|
# Match SQLite NOCASE: fold ASCII only, not Unicode lower/casefold.
|
||||||
|
return child.translate(_SQLITE_IDENTIFIER_CASE)
|
||||||
|
return child
|
||||||
|
|
||||||
# Instance-level optional extra attributes
|
# Instance-level optional extra attributes
|
||||||
reasons: list[str] | None = None
|
reasons: list[str] | None = None
|
||||||
|
|
@ -72,8 +85,8 @@ class Resource(ABC):
|
||||||
)
|
)
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return "{}(parent={!r}, child={!r})".format(
|
return (
|
||||||
self.__class__.__name__, self.parent, self.child
|
f"{self.__class__.__name__}(parent={self.parent!r}, child={self.child!r})"
|
||||||
)
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
@ -129,7 +142,6 @@ class Resource(ABC):
|
||||||
|
|
||||||
Must return two columns: parent, child
|
Must return two columns: parent, child
|
||||||
"""
|
"""
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class AllowedResource(NamedTuple):
|
class AllowedResource(NamedTuple):
|
||||||
|
|
@ -147,6 +159,11 @@ class Action:
|
||||||
resource_class: type[Resource] | None = None
|
resource_class: type[Resource] | None = None
|
||||||
also_requires: str | None = None # Optional action name that must also be allowed
|
also_requires: str | None = None # Optional action name that must also be allowed
|
||||||
|
|
||||||
|
def normalize_child(self, child: str | None) -> str | None:
|
||||||
|
if self.resource_class is None:
|
||||||
|
return child
|
||||||
|
return self.resource_class.normalize_child(child)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def takes_parent(self) -> bool:
|
def takes_parent(self) -> bool:
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,14 @@
|
||||||
import importlib
|
import importlib
|
||||||
|
import importlib.metadata as importlib_metadata
|
||||||
|
import importlib.resources as importlib_resources
|
||||||
import os
|
import os
|
||||||
import pluggy
|
|
||||||
from pprint import pprint
|
|
||||||
import sys
|
import sys
|
||||||
|
from pprint import pprint
|
||||||
|
|
||||||
|
import pluggy
|
||||||
|
|
||||||
from . import hookspecs
|
from . import hookspecs
|
||||||
|
|
||||||
if sys.version_info >= (3, 9):
|
|
||||||
import importlib.resources as importlib_resources
|
|
||||||
else:
|
|
||||||
import importlib_resources
|
|
||||||
if sys.version_info >= (3, 10):
|
|
||||||
import importlib.metadata as importlib_metadata
|
|
||||||
else:
|
|
||||||
import importlib_metadata
|
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_PLUGINS = (
|
DEFAULT_PLUGINS = (
|
||||||
"datasette.publish.heroku",
|
"datasette.publish.heroku",
|
||||||
"datasette.publish.cloudrun",
|
"datasette.publish.cloudrun",
|
||||||
|
|
@ -24,6 +18,7 @@ DEFAULT_PLUGINS = (
|
||||||
"datasette.actor_auth_cookie",
|
"datasette.actor_auth_cookie",
|
||||||
"datasette.default_permissions",
|
"datasette.default_permissions",
|
||||||
"datasette.default_permissions.tokens",
|
"datasette.default_permissions.tokens",
|
||||||
|
"datasette.default_permissions.sqlite_statistics",
|
||||||
"datasette.default_actions",
|
"datasette.default_actions",
|
||||||
"datasette.default_column_types",
|
"datasette.default_column_types",
|
||||||
"datasette.default_magic_parameters",
|
"datasette.default_magic_parameters",
|
||||||
|
|
@ -31,6 +26,7 @@ DEFAULT_PLUGINS = (
|
||||||
"datasette.default_debug_menu",
|
"datasette.default_debug_menu",
|
||||||
"datasette.default_jump_items",
|
"datasette.default_jump_items",
|
||||||
"datasette.default_database_actions",
|
"datasette.default_database_actions",
|
||||||
|
"datasette.default_table_actions",
|
||||||
"datasette.default_query_actions",
|
"datasette.default_query_actions",
|
||||||
"datasette.handle_exception",
|
"datasette.handle_exception",
|
||||||
"datasette.forbidden",
|
"datasette.forbidden",
|
||||||
|
|
@ -84,7 +80,7 @@ if DATASETTE_LOAD_PLUGINS is not None:
|
||||||
# Ensure name can be found in plugin_to_distinfo later:
|
# Ensure name can be found in plugin_to_distinfo later:
|
||||||
pm._plugin_distinfo.append((mod, distribution))
|
pm._plugin_distinfo.append((mod, distribution))
|
||||||
except importlib_metadata.PackageNotFoundError:
|
except importlib_metadata.PackageNotFoundError:
|
||||||
sys.stderr.write("Plugin {} could not be found\n".format(package_name))
|
sys.stderr.write(f"Plugin {package_name} could not be found\n")
|
||||||
|
|
||||||
|
|
||||||
# Load default plugins
|
# Load default plugins
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,17 @@
|
||||||
from datasette import hookimpl
|
|
||||||
import click
|
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
from subprocess import CalledProcessError, check_call, check_output
|
from subprocess import CalledProcessError, check_call, check_output
|
||||||
|
|
||||||
|
import click
|
||||||
|
|
||||||
|
from datasette import hookimpl
|
||||||
|
|
||||||
|
from ..utils import temporary_docker_directory
|
||||||
from .common import (
|
from .common import (
|
||||||
add_common_publish_arguments_and_options,
|
add_common_publish_arguments_and_options,
|
||||||
fail_if_publish_binary_not_installed,
|
fail_if_publish_binary_not_installed,
|
||||||
)
|
)
|
||||||
from ..utils import temporary_docker_directory
|
|
||||||
|
|
||||||
|
|
||||||
@hookimpl
|
@hookimpl
|
||||||
|
|
@ -219,7 +221,7 @@ def publish_subcommand(publish):
|
||||||
|
|
||||||
check_call(
|
check_call(
|
||||||
"gcloud builds submit --tag {}{}".format(
|
"gcloud builds submit --tag {}{}".format(
|
||||||
image_id, " --timeout {}".format(timeout) if timeout else ""
|
image_id, f" --timeout {timeout}" if timeout else ""
|
||||||
),
|
),
|
||||||
shell=True,
|
shell=True,
|
||||||
)
|
)
|
||||||
|
|
@ -231,7 +233,7 @@ def publish_subcommand(publish):
|
||||||
("--min-instances", min_instances),
|
("--min-instances", min_instances),
|
||||||
):
|
):
|
||||||
if value is not None:
|
if value is not None:
|
||||||
extra_deploy_options.append("{} {}".format(option, value))
|
extra_deploy_options.append(f"{option} {value}")
|
||||||
check_call(
|
check_call(
|
||||||
"gcloud run deploy --allow-unauthenticated --platform=managed --image {} {}{}".format(
|
"gcloud run deploy --allow-unauthenticated --platform=managed --image {} {}{}".format(
|
||||||
image_id,
|
image_id,
|
||||||
|
|
@ -258,24 +260,16 @@ def _ensure_artifact_registry(artifact_project, artifact_region, artifact_reposi
|
||||||
) from exc
|
) from exc
|
||||||
|
|
||||||
describe_cmd = (
|
describe_cmd = (
|
||||||
"gcloud artifacts repositories describe {repo} --project {project} "
|
f"gcloud artifacts repositories describe {artifact_repository} --project {artifact_project} "
|
||||||
"--location {location} --quiet"
|
f"--location {artifact_region} --quiet"
|
||||||
).format(
|
|
||||||
repo=artifact_repository,
|
|
||||||
project=artifact_project,
|
|
||||||
location=artifact_region,
|
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
check_call(describe_cmd, shell=True)
|
check_call(describe_cmd, shell=True)
|
||||||
return
|
return
|
||||||
except CalledProcessError:
|
except CalledProcessError:
|
||||||
create_cmd = (
|
create_cmd = (
|
||||||
"gcloud artifacts repositories create {repo} --repository-format=docker "
|
f"gcloud artifacts repositories create {artifact_repository} --repository-format=docker "
|
||||||
'--location {location} --project {project} --description "Datasette Cloud Run images" --quiet'
|
f'--location {artifact_region} --project {artifact_project} --description "Datasette Cloud Run images" --quiet'
|
||||||
).format(
|
|
||||||
repo=artifact_repository,
|
|
||||||
location=artifact_region,
|
|
||||||
project=artifact_project,
|
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
check_call(create_cmd, shell=True)
|
check_call(create_cmd, shell=True)
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,11 @@
|
||||||
from ..utils import StaticMount
|
|
||||||
import click
|
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
import click
|
||||||
|
|
||||||
|
from ..utils import StaticMount
|
||||||
|
|
||||||
|
|
||||||
def add_common_publish_arguments_and_options(subcommand):
|
def add_common_publish_arguments_and_options(subcommand):
|
||||||
for decorator in reversed(
|
for decorator in reversed(
|
||||||
|
|
@ -76,9 +78,7 @@ def fail_if_publish_binary_not_installed(binary, publish_target, install_link):
|
||||||
"""Exit (with error message) if ``binary` isn't installed"""
|
"""Exit (with error message) if ``binary` isn't installed"""
|
||||||
if not shutil.which(binary):
|
if not shutil.which(binary):
|
||||||
click.secho(
|
click.secho(
|
||||||
"Publishing to {publish_target} requires {binary} to be installed and configured".format(
|
f"Publishing to {publish_target} requires {binary} to be installed and configured",
|
||||||
publish_target=publish_target, binary=binary
|
|
||||||
),
|
|
||||||
bg="red",
|
bg="red",
|
||||||
fg="white",
|
fg="white",
|
||||||
bold=True,
|
bold=True,
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,21 @@
|
||||||
from contextlib import contextmanager
|
|
||||||
from datasette import hookimpl
|
|
||||||
import click
|
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import pathlib
|
import pathlib
|
||||||
import shlex
|
import shlex
|
||||||
import shutil
|
import shutil
|
||||||
from subprocess import call, check_output
|
|
||||||
import tempfile
|
import tempfile
|
||||||
|
from contextlib import contextmanager
|
||||||
|
from subprocess import call, check_output
|
||||||
|
|
||||||
|
import click
|
||||||
|
|
||||||
|
from datasette import hookimpl
|
||||||
|
from datasette.utils import link_or_copy, link_or_copy_directory, parse_metadata
|
||||||
|
|
||||||
from .common import (
|
from .common import (
|
||||||
add_common_publish_arguments_and_options,
|
add_common_publish_arguments_and_options,
|
||||||
fail_if_publish_binary_not_installed,
|
fail_if_publish_binary_not_installed,
|
||||||
)
|
)
|
||||||
from datasette.utils import link_or_copy, link_or_copy_directory, parse_metadata
|
|
||||||
|
|
||||||
|
|
||||||
@hookimpl
|
@hookimpl
|
||||||
|
|
@ -234,7 +236,7 @@ def temporary_heroku_directory(
|
||||||
extras.extend(["--static", f"{mount_point}:{mount_point}"])
|
extras.extend(["--static", f"{mount_point}:{mount_point}"])
|
||||||
|
|
||||||
quoted_files = " ".join(
|
quoted_files = " ".join(
|
||||||
["-i {}".format(shlex.quote(file_name)) for file_name in file_names]
|
[f"-i {shlex.quote(file_name)}" for file_name in file_names]
|
||||||
)
|
)
|
||||||
procfile_cmd = "web: datasette serve --host 0.0.0.0 {quoted_files} --cors --port $PORT --inspect-file inspect-data.json {extras}".format(
|
procfile_cmd = "web: datasette serve --host 0.0.0.0 {quoted_files} --cors --port $PORT --inspect-file inspect-data.json {extras}".format(
|
||||||
quoted_files=quoted_files, extras=" ".join(extras)
|
quoted_files=quoted_files, extras=" ".join(extras)
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,13 @@
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from datasette.extras import extra_names_from_request
|
from datasette.extras import extra_names_from_request
|
||||||
from datasette.utils import (
|
from datasette.utils import (
|
||||||
value_as_boolean,
|
|
||||||
remove_infinites,
|
|
||||||
CustomJSONEncoder,
|
CustomJSONEncoder,
|
||||||
|
error_body,
|
||||||
path_from_row_pks,
|
path_from_row_pks,
|
||||||
|
remove_infinites,
|
||||||
sqlite3,
|
sqlite3,
|
||||||
|
value_as_boolean,
|
||||||
)
|
)
|
||||||
from datasette.utils.asgi import Response
|
from datasette.utils.asgi import Response
|
||||||
|
|
||||||
|
|
@ -52,8 +54,7 @@ def json_renderer(request, args, data, error, truncated=None):
|
||||||
if error:
|
if error:
|
||||||
shape = "objects"
|
shape = "objects"
|
||||||
status_code = 400
|
status_code = 400
|
||||||
data["error"] = error
|
data.update(error_body(error, status_code))
|
||||||
data["ok"] = False
|
|
||||||
|
|
||||||
if truncated is not None:
|
if truncated is not None:
|
||||||
data["truncated"] = truncated
|
data["truncated"] = truncated
|
||||||
|
|
@ -87,7 +88,8 @@ def json_renderer(request, args, data, error, truncated=None):
|
||||||
object_rows[pk_string] = row
|
object_rows[pk_string] = row
|
||||||
data = object_rows
|
data = object_rows
|
||||||
if shape_error:
|
if shape_error:
|
||||||
data = {"ok": False, "error": shape_error}
|
status_code = 400
|
||||||
|
data = error_body(shape_error, status_code)
|
||||||
elif shape == "array":
|
elif shape == "array":
|
||||||
data = data["rows"]
|
data = data["rows"]
|
||||||
|
|
||||||
|
|
@ -100,12 +102,7 @@ def json_renderer(request, args, data, error, truncated=None):
|
||||||
data["rows"] = [list(row.values()) for row in data["rows"]]
|
data["rows"] = [list(row.values()) for row in data["rows"]]
|
||||||
else:
|
else:
|
||||||
status_code = 400
|
status_code = 400
|
||||||
data = {
|
data = error_body(f"Invalid _shape: {shape}", status_code)
|
||||||
"ok": False,
|
|
||||||
"error": f"Invalid _shape: {shape}",
|
|
||||||
"status": 400,
|
|
||||||
"title": None,
|
|
||||||
}
|
|
||||||
|
|
||||||
# Don't include "columns" in output
|
# Don't include "columns" in output
|
||||||
# https://github.com/simonw/datasette/issues/2136
|
# https://github.com/simonw/datasette/issues/2136
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@ class TableResource(Resource):
|
||||||
|
|
||||||
name = "table"
|
name = "table"
|
||||||
parent_class = DatabaseResource
|
parent_class = DatabaseResource
|
||||||
|
case_insensitive_child = True
|
||||||
|
|
||||||
def __init__(self, database: str, table: str):
|
def __init__(self, database: str, table: str):
|
||||||
super().__init__(parent=database, child=table)
|
super().__init__(parent=database, child=table)
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load diff
|
|
@ -1,7 +1,9 @@
|
||||||
|
let columnChooserInstanceCounter = 0;
|
||||||
|
|
||||||
class ColumnChooser extends HTMLElement {
|
class ColumnChooser extends HTMLElement {
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
this.attachShadow({ mode: "open" });
|
this.titleId = `column-chooser-title-${++columnChooserInstanceCounter}`;
|
||||||
|
|
||||||
// State
|
// State
|
||||||
this._items = [];
|
this._items = [];
|
||||||
|
|
@ -26,375 +28,60 @@ class ColumnChooser extends HTMLElement {
|
||||||
// Bound handlers
|
// Bound handlers
|
||||||
this._onMove = this._onMove.bind(this);
|
this._onMove = this._onMove.bind(this);
|
||||||
this._onUp = this._onUp.bind(this);
|
this._onUp = this._onUp.bind(this);
|
||||||
|
}
|
||||||
|
|
||||||
this.shadowRoot.innerHTML = `
|
connectedCallback() {
|
||||||
<style>
|
if (this._modal) return;
|
||||||
:host {
|
this.innerHTML = `
|
||||||
--ink: #0f0f0f;
|
<datasette-modal><dialog aria-labelledby="${this.titleId}">
|
||||||
--paper: #eef6ff;
|
|
||||||
--muted: #6b6b6b;
|
|
||||||
--rule: #d8e6f5;
|
|
||||||
--accent: #1a56db;
|
|
||||||
--accent-light: #e8effd;
|
|
||||||
--card: #ffffff;
|
|
||||||
}
|
|
||||||
|
|
||||||
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
||||||
|
|
||||||
dialog {
|
|
||||||
border: none;
|
|
||||||
border-radius: var(--modal-border-radius, 0.75rem);
|
|
||||||
padding: 0;
|
|
||||||
margin: auto;
|
|
||||||
width: 100%;
|
|
||||||
max-width: 420px;
|
|
||||||
max-height: min(640px, calc(100vh - 32px));
|
|
||||||
box-shadow: var(--modal-shadow, 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04));
|
|
||||||
animation: slideIn var(--modal-animation-duration, 0.2s) ease-out;
|
|
||||||
overflow: hidden;
|
|
||||||
font-family: system-ui, -apple-system, sans-serif;
|
|
||||||
background: var(--card);
|
|
||||||
-webkit-user-select: none;
|
|
||||||
-webkit-touch-callout: none;
|
|
||||||
-webkit-tap-highlight-color: transparent;
|
|
||||||
}
|
|
||||||
|
|
||||||
dialog[open] {
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
height: min(640px, calc(100vh - 32px));
|
|
||||||
}
|
|
||||||
|
|
||||||
dialog::backdrop {
|
|
||||||
background: var(--modal-backdrop-bg, rgba(0, 0, 0, 0.5));
|
|
||||||
backdrop-filter: var(--modal-backdrop-blur, blur(4px));
|
|
||||||
-webkit-backdrop-filter: var(--modal-backdrop-blur, blur(4px));
|
|
||||||
animation: fadeIn var(--modal-animation-duration, 0.2s) ease-out;
|
|
||||||
}
|
|
||||||
|
|
||||||
@keyframes slideIn {
|
|
||||||
from {
|
|
||||||
opacity: 0;
|
|
||||||
transform: translateY(-20px) scale(0.95);
|
|
||||||
}
|
|
||||||
to {
|
|
||||||
opacity: 1;
|
|
||||||
transform: translateY(0) scale(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@keyframes fadeIn {
|
|
||||||
from { opacity: 0; }
|
|
||||||
to { opacity: 1; }
|
|
||||||
}
|
|
||||||
|
|
||||||
.modal-header {
|
|
||||||
padding: 20px 24px 16px;
|
|
||||||
border-bottom: 1px solid var(--rule);
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
justify-content: space-between;
|
|
||||||
flex-shrink: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.modal-title {
|
|
||||||
font-size: 1rem;
|
|
||||||
font-weight: 600;
|
|
||||||
}
|
|
||||||
|
|
||||||
.modal-meta {
|
|
||||||
font-family: ui-monospace, monospace;
|
|
||||||
font-size: 0.7rem;
|
|
||||||
color: var(--muted);
|
|
||||||
background: var(--paper);
|
|
||||||
padding: 3px 9px;
|
|
||||||
border-radius: 20px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.list-toolbar {
|
|
||||||
padding: 6px 24px;
|
|
||||||
border-bottom: 1px solid var(--rule);
|
|
||||||
display: flex;
|
|
||||||
gap: 12px;
|
|
||||||
flex-shrink: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.list-toolbar button {
|
|
||||||
background: var(--accent-light);
|
|
||||||
border: 1px solid var(--rule);
|
|
||||||
border-radius: 4px;
|
|
||||||
font-family: inherit;
|
|
||||||
font-size: 0.75rem;
|
|
||||||
color: var(--accent);
|
|
||||||
cursor: pointer;
|
|
||||||
padding: 3px 10px;
|
|
||||||
transition: background 0.12s, color 0.12s;
|
|
||||||
}
|
|
||||||
.list-toolbar button:hover { background: var(--accent); color: white; }
|
|
||||||
|
|
||||||
.list-wrap {
|
|
||||||
flex: 1;
|
|
||||||
overflow-y: auto;
|
|
||||||
overflow-x: hidden;
|
|
||||||
position: relative;
|
|
||||||
overscroll-behavior: contain;
|
|
||||||
-webkit-overflow-scrolling: touch;
|
|
||||||
}
|
|
||||||
|
|
||||||
.list-wrap::before,
|
|
||||||
.list-wrap::after {
|
|
||||||
content: '';
|
|
||||||
position: sticky;
|
|
||||||
display: block;
|
|
||||||
left: 0; right: 0;
|
|
||||||
height: 20px;
|
|
||||||
pointer-events: none;
|
|
||||||
z-index: 5;
|
|
||||||
transition: opacity 0.2s;
|
|
||||||
}
|
|
||||||
.list-wrap::before {
|
|
||||||
top: 0;
|
|
||||||
background: linear-gradient(to bottom, rgba(255,255,255,0.9), transparent);
|
|
||||||
}
|
|
||||||
.list-wrap::after {
|
|
||||||
bottom: 0;
|
|
||||||
background: linear-gradient(to top, rgba(255,255,255,0.9), transparent);
|
|
||||||
margin-top: -20px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.scroll-zone {
|
|
||||||
position: absolute;
|
|
||||||
left: 0; right: 0;
|
|
||||||
height: 72px;
|
|
||||||
pointer-events: none;
|
|
||||||
z-index: 10;
|
|
||||||
}
|
|
||||||
.scroll-zone-top { top: 0; }
|
|
||||||
.scroll-zone-bot { bottom: 0; }
|
|
||||||
|
|
||||||
.drag-list {
|
|
||||||
list-style: none;
|
|
||||||
padding: 4px 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.drag-item {
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
background: white;
|
|
||||||
border-bottom: 1px solid var(--rule);
|
|
||||||
user-select: none;
|
|
||||||
-webkit-user-select: none;
|
|
||||||
-webkit-touch-callout: none;
|
|
||||||
position: relative;
|
|
||||||
transition: background 0.08s;
|
|
||||||
}
|
|
||||||
|
|
||||||
.drag-item:last-child { border-bottom: none; }
|
|
||||||
|
|
||||||
.drag-handle {
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
justify-content: center;
|
|
||||||
width: 48px;
|
|
||||||
height: 48px;
|
|
||||||
flex-shrink: 0;
|
|
||||||
cursor: grab;
|
|
||||||
color: #c8c4bc;
|
|
||||||
touch-action: none;
|
|
||||||
transition: color 0.15s;
|
|
||||||
}
|
|
||||||
|
|
||||||
.drag-handle:hover { color: var(--accent); }
|
|
||||||
.drag-handle svg { pointer-events: none; display: block; }
|
|
||||||
|
|
||||||
.drag-item-content {
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
flex: 1;
|
|
||||||
min-width: 0;
|
|
||||||
cursor: pointer;
|
|
||||||
}
|
|
||||||
|
|
||||||
.drag-item-check {
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
width: 32px;
|
|
||||||
height: 48px;
|
|
||||||
flex-shrink: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.drag-item-check input[type="checkbox"] {
|
|
||||||
width: 16px;
|
|
||||||
height: 16px;
|
|
||||||
accent-color: var(--accent);
|
|
||||||
cursor: pointer;
|
|
||||||
}
|
|
||||||
|
|
||||||
.drag-item-label {
|
|
||||||
flex: 1;
|
|
||||||
font-size: 0.9rem;
|
|
||||||
line-height: 48px;
|
|
||||||
padding-right: 16px;
|
|
||||||
white-space: nowrap;
|
|
||||||
overflow: hidden;
|
|
||||||
text-overflow: ellipsis;
|
|
||||||
cursor: default;
|
|
||||||
}
|
|
||||||
|
|
||||||
.drag-item.is-dragging {
|
|
||||||
opacity: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.drop-indicator {
|
|
||||||
position: absolute;
|
|
||||||
left: 48px;
|
|
||||||
right: 0;
|
|
||||||
height: 2px;
|
|
||||||
background: var(--accent);
|
|
||||||
border-radius: 99px;
|
|
||||||
pointer-events: none;
|
|
||||||
z-index: 20;
|
|
||||||
display: none;
|
|
||||||
}
|
|
||||||
.drop-indicator.top { top: -1px; display: block; }
|
|
||||||
.drop-indicator.bottom { bottom: -1px; display: block; }
|
|
||||||
|
|
||||||
.drag-ghost {
|
|
||||||
position: fixed;
|
|
||||||
pointer-events: none;
|
|
||||||
z-index: 9999;
|
|
||||||
background: white;
|
|
||||||
border-radius: 6px;
|
|
||||||
box-shadow: 0 8px 32px rgba(0,0,0,0.18), 0 2px 8px rgba(0,0,0,0.1);
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
border: 1.5px solid var(--accent-light);
|
|
||||||
opacity: 0.97;
|
|
||||||
will-change: transform;
|
|
||||||
font-family: system-ui, -apple-system, sans-serif;
|
|
||||||
}
|
|
||||||
|
|
||||||
.scroll-pulse {
|
|
||||||
position: absolute;
|
|
||||||
left: 50%;
|
|
||||||
transform: translateX(-50%);
|
|
||||||
width: 32px;
|
|
||||||
height: 32px;
|
|
||||||
border-radius: 50%;
|
|
||||||
background: var(--accent);
|
|
||||||
opacity: 0;
|
|
||||||
pointer-events: none;
|
|
||||||
z-index: 10;
|
|
||||||
transition: opacity 0.15s;
|
|
||||||
}
|
|
||||||
.scroll-pulse.top { top: 8px; }
|
|
||||||
.scroll-pulse.bot { bottom: 8px; }
|
|
||||||
.scroll-pulse.active {
|
|
||||||
opacity: 0.18;
|
|
||||||
animation: pulse 0.8s ease-in-out infinite;
|
|
||||||
}
|
|
||||||
|
|
||||||
@keyframes pulse {
|
|
||||||
0%, 100% { transform: translateX(-50%) scale(1); opacity: 0.18; }
|
|
||||||
50% { transform: translateX(-50%) scale(1.5); opacity: 0.07; }
|
|
||||||
}
|
|
||||||
|
|
||||||
.modal-footer {
|
|
||||||
padding: 14px 20px;
|
|
||||||
border-top: 1px solid var(--rule);
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
gap: 10px;
|
|
||||||
flex-shrink: 0;
|
|
||||||
background: var(--paper);
|
|
||||||
}
|
|
||||||
|
|
||||||
.footer-info {
|
|
||||||
flex: 1;
|
|
||||||
font-family: ui-monospace, monospace;
|
|
||||||
font-size: 0.68rem;
|
|
||||||
color: var(--muted);
|
|
||||||
}
|
|
||||||
|
|
||||||
.btn {
|
|
||||||
border: none;
|
|
||||||
border-radius: 5px;
|
|
||||||
padding: 9px 20px;
|
|
||||||
font-size: 0.85rem;
|
|
||||||
font-weight: 500;
|
|
||||||
cursor: pointer;
|
|
||||||
touch-action: manipulation;
|
|
||||||
font-family: inherit;
|
|
||||||
transition: background 0.12s;
|
|
||||||
}
|
|
||||||
|
|
||||||
.btn-primary {
|
|
||||||
background: var(--accent);
|
|
||||||
color: white;
|
|
||||||
}
|
|
||||||
.btn-primary:hover { background: #1448c0; }
|
|
||||||
|
|
||||||
.btn-ghost {
|
|
||||||
background: transparent;
|
|
||||||
color: var(--muted);
|
|
||||||
border: 1px solid var(--rule);
|
|
||||||
}
|
|
||||||
.btn-ghost:hover { background: var(--rule); color: var(--ink); }
|
|
||||||
|
|
||||||
.list-wrap::-webkit-scrollbar { width: 5px; }
|
|
||||||
.list-wrap::-webkit-scrollbar-track { background: transparent; }
|
|
||||||
.list-wrap::-webkit-scrollbar-thumb { background: var(--rule); border-radius: 99px; }
|
|
||||||
|
|
||||||
input, textarea { -webkit-user-select: auto; user-select: auto; }
|
|
||||||
</style>
|
|
||||||
|
|
||||||
<dialog aria-labelledby="modalTitle">
|
|
||||||
<div class="modal-header">
|
<div class="modal-header">
|
||||||
<span class="modal-title" id="modalTitle">Choose columns</span>
|
<span class="modal-title" id="${this.titleId}">Choose columns</span>
|
||||||
<span class="modal-meta" id="selectedCount"></span>
|
<span class="modal-meta"></span>
|
||||||
</div>
|
</div>
|
||||||
<div class="list-toolbar">
|
<div class="list-toolbar">
|
||||||
<button id="selectAllBtn">Select all</button>
|
<button class="select-all">Select all</button>
|
||||||
<button id="deselectAllBtn">Deselect all</button>
|
<button class="deselect-all">Deselect all</button>
|
||||||
</div>
|
</div>
|
||||||
<div class="list-wrap" id="listWrap">
|
<div class="modal-body list-wrap">
|
||||||
<div class="scroll-pulse top" id="pulseTop"></div>
|
<div class="scroll-pulse top"></div>
|
||||||
<div class="scroll-pulse bot" id="pulseBot"></div>
|
<div class="scroll-pulse bot"></div>
|
||||||
<ul class="drag-list" id="dragList"></ul>
|
<ul class="drag-list"></ul>
|
||||||
</div>
|
</div>
|
||||||
<div class="modal-footer">
|
<div class="modal-footer">
|
||||||
<span class="footer-info" id="footerInfo"></span>
|
<span class="footer-info"></span>
|
||||||
<button class="btn btn-ghost" id="cancelBtn">Cancel</button>
|
<button class="modal-btn modal-btn-ghost">Cancel</button>
|
||||||
<button class="btn btn-primary" id="applyBtn">Apply</button>
|
<button class="modal-btn modal-btn-primary">Apply</button>
|
||||||
</div>
|
</div>
|
||||||
</dialog>
|
</dialog></datasette-modal>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
// DOM refs
|
// DOM refs
|
||||||
this._dialog = this.shadowRoot.querySelector("dialog");
|
this._modal = this.querySelector("datasette-modal");
|
||||||
this._listWrap = this.shadowRoot.getElementById("listWrap");
|
this._listWrap = this.querySelector(".list-wrap");
|
||||||
this._dragList = this.shadowRoot.getElementById("dragList");
|
this._dragList = this.querySelector(".drag-list");
|
||||||
this._pulseTop = this.shadowRoot.getElementById("pulseTop");
|
this._pulseTop = this.querySelector(".scroll-pulse.top");
|
||||||
this._pulseBot = this.shadowRoot.getElementById("pulseBot");
|
this._pulseBot = this.querySelector(".scroll-pulse.bot");
|
||||||
this._selectAllBtn = this.shadowRoot.getElementById("selectAllBtn");
|
this._selectAllBtn = this.querySelector(".select-all");
|
||||||
this._deselectAllBtn = this.shadowRoot.getElementById("deselectAllBtn");
|
this._deselectAllBtn = this.querySelector(".deselect-all");
|
||||||
this._cancelBtn = this.shadowRoot.getElementById("cancelBtn");
|
this._cancelBtn = this.querySelector(".modal-btn-ghost");
|
||||||
this._applyBtn = this.shadowRoot.getElementById("applyBtn");
|
this._applyBtn = this.querySelector(".modal-btn-primary");
|
||||||
this._countEl = this.shadowRoot.getElementById("selectedCount");
|
this._countEl = this.querySelector(".modal-meta");
|
||||||
this._footerEl = this.shadowRoot.getElementById("footerInfo");
|
this._footerEl = this.querySelector(".footer-info");
|
||||||
|
|
||||||
// Event listeners
|
// Event listeners
|
||||||
this._selectAllBtn.addEventListener("click", () => this._selectAll());
|
this._selectAllBtn.addEventListener("click", () => this._selectAll());
|
||||||
this._deselectAllBtn.addEventListener("click", () => this._deselectAll());
|
this._deselectAllBtn.addEventListener("click", () => this._deselectAll());
|
||||||
this._cancelBtn.addEventListener("click", () => this._close());
|
this._cancelBtn.addEventListener("click", () =>
|
||||||
|
this._modal.requestClose("cancel"),
|
||||||
|
);
|
||||||
this._applyBtn.addEventListener("click", () => this._apply());
|
this._applyBtn.addEventListener("click", () => this._apply());
|
||||||
this._dialog.addEventListener("click", (e) => {
|
this._modal.beforeClose = () => {
|
||||||
if (e.target === this._dialog) this._close();
|
this._items = this._savedItems ? [...this._savedItems] : this._items;
|
||||||
});
|
this._checked = this._savedChecked
|
||||||
this._dialog.addEventListener("cancel", (e) => {
|
? new Set(this._savedChecked)
|
||||||
e.preventDefault();
|
: this._checked;
|
||||||
this._close();
|
return true;
|
||||||
});
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -414,19 +101,11 @@ class ColumnChooser extends HTMLElement {
|
||||||
this._savedChecked = new Set(this._checked);
|
this._savedChecked = new Set(this._checked);
|
||||||
|
|
||||||
this._render();
|
this._render();
|
||||||
this._dialog.showModal();
|
this._modal.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── Internal methods ──
|
// ── Internal methods ──
|
||||||
|
|
||||||
_close() {
|
|
||||||
this._items = this._savedItems ? [...this._savedItems] : this._items;
|
|
||||||
this._checked = this._savedChecked
|
|
||||||
? new Set(this._savedChecked)
|
|
||||||
: this._checked;
|
|
||||||
this._dialog.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
_selectAll() {
|
_selectAll() {
|
||||||
this._items.forEach((col) => this._checked.add(col));
|
this._items.forEach((col) => this._checked.add(col));
|
||||||
this._dragList.querySelectorAll('input[type="checkbox"]').forEach((cb) => {
|
this._dragList.querySelectorAll('input[type="checkbox"]').forEach((cb) => {
|
||||||
|
|
@ -445,7 +124,7 @@ class ColumnChooser extends HTMLElement {
|
||||||
|
|
||||||
_apply() {
|
_apply() {
|
||||||
const selected = this._items.filter((col) => this._checked.has(col));
|
const selected = this._items.filter((col) => this._checked.has(col));
|
||||||
this._dialog.close();
|
this._modal.close();
|
||||||
if (this._onApply) {
|
if (this._onApply) {
|
||||||
this._onApply(selected);
|
this._onApply(selected);
|
||||||
}
|
}
|
||||||
|
|
@ -472,11 +151,13 @@ class ColumnChooser extends HTMLElement {
|
||||||
<span class="drag-item-check">
|
<span class="drag-item-check">
|
||||||
<input type="checkbox" ${this._checked.has(col) ? "checked" : ""}>
|
<input type="checkbox" ${this._checked.has(col) ? "checked" : ""}>
|
||||||
</span>
|
</span>
|
||||||
<span class="drag-item-label">${col}</span>
|
<span class="drag-item-label"></span>
|
||||||
</label>
|
</label>
|
||||||
<div class="drop-indicator"></div>
|
<div class="drop-indicator"></div>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
li.querySelector(".drag-item-label").textContent = col;
|
||||||
|
|
||||||
li.querySelector("input").addEventListener("change", (e) => {
|
li.querySelector("input").addEventListener("change", (e) => {
|
||||||
e.target.checked ? this._checked.add(col) : this._checked.delete(col);
|
e.target.checked ? this._checked.add(col) : this._checked.delete(col);
|
||||||
this._updateCounts();
|
this._updateCounts();
|
||||||
|
|
@ -509,7 +190,7 @@ class ColumnChooser extends HTMLElement {
|
||||||
this._ghostOffX = e.clientX - rect.left;
|
this._ghostOffX = e.clientX - rect.left;
|
||||||
this._ghostOffY = e.clientY - rect.top;
|
this._ghostOffY = e.clientY - rect.top;
|
||||||
|
|
||||||
// Build ghost inside shadow DOM
|
// Keep the drag preview inside the dialog so it stays above the backdrop.
|
||||||
this._ghost = document.createElement("div");
|
this._ghost = document.createElement("div");
|
||||||
this._ghost.className = "drag-ghost";
|
this._ghost.className = "drag-ghost";
|
||||||
this._ghost.style.width = rect.width + "px";
|
this._ghost.style.width = rect.width + "px";
|
||||||
|
|
@ -518,7 +199,7 @@ class ColumnChooser extends HTMLElement {
|
||||||
this._ghost.querySelector(".drop-indicator")?.remove();
|
this._ghost.querySelector(".drop-indicator")?.remove();
|
||||||
const h = this._ghost.querySelector(".drag-handle");
|
const h = this._ghost.querySelector(".drag-handle");
|
||||||
if (h) h.style.color = "var(--accent)";
|
if (h) h.style.color = "var(--accent)";
|
||||||
this.shadowRoot.appendChild(this._ghost);
|
this._modal.dialog.appendChild(this._ghost);
|
||||||
|
|
||||||
srcEl.classList.add("is-dragging");
|
srcEl.classList.add("is-dragging");
|
||||||
this._positionGhost(e.clientX, e.clientY);
|
this._positionGhost(e.clientX, e.clientY);
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load diff
|
|
@ -1,56 +0,0 @@
|
||||||
/*
|
|
||||||
https://github.com/luyilin/json-format-highlight
|
|
||||||
From https://unpkg.com/json-format-highlight@1.0.1/dist/json-format-highlight.js
|
|
||||||
MIT Licensed
|
|
||||||
*/
|
|
||||||
(function (global, factory) {
|
|
||||||
typeof exports === "object" && typeof module !== "undefined"
|
|
||||||
? (module.exports = factory())
|
|
||||||
: typeof define === "function" && define.amd
|
|
||||||
? define(factory)
|
|
||||||
: (global.jsonFormatHighlight = factory());
|
|
||||||
})(this, function () {
|
|
||||||
"use strict";
|
|
||||||
|
|
||||||
var defaultColors = {
|
|
||||||
keyColor: "dimgray",
|
|
||||||
numberColor: "lightskyblue",
|
|
||||||
stringColor: "lightcoral",
|
|
||||||
trueColor: "lightseagreen",
|
|
||||||
falseColor: "#f66578",
|
|
||||||
nullColor: "cornflowerblue",
|
|
||||||
};
|
|
||||||
|
|
||||||
function index(json, colorOptions) {
|
|
||||||
if (colorOptions === void 0) colorOptions = {};
|
|
||||||
|
|
||||||
if (!json) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (typeof json !== "string") {
|
|
||||||
json = JSON.stringify(json, null, 2);
|
|
||||||
}
|
|
||||||
var colors = Object.assign({}, defaultColors, colorOptions);
|
|
||||||
json = json.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
||||||
return json.replace(
|
|
||||||
/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+]?\d+)?)/g,
|
|
||||||
function (match) {
|
|
||||||
var color = colors.numberColor;
|
|
||||||
if (/^"/.test(match)) {
|
|
||||||
color = /:$/.test(match) ? colors.keyColor : colors.stringColor;
|
|
||||||
} else {
|
|
||||||
color = /true/.test(match)
|
|
||||||
? colors.trueColor
|
|
||||||
: /false/.test(match)
|
|
||||||
? colors.falseColor
|
|
||||||
: /null/.test(match)
|
|
||||||
? colors.nullColor
|
|
||||||
: color;
|
|
||||||
}
|
|
||||||
return '<span style="color: ' + color + '">' + match + "</span>";
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return index;
|
|
||||||
});
|
|
||||||
|
|
@ -66,7 +66,8 @@ function initMobileColumnActions(manager) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var dialog = document.createElement("dialog");
|
var modal = DatasetteModal.create();
|
||||||
|
var dialog = modal.dialog;
|
||||||
dialog.className = "mobile-column-actions-dialog";
|
dialog.className = "mobile-column-actions-dialog";
|
||||||
dialog.id = MOBILE_COLUMN_DIALOG_ID;
|
dialog.id = MOBILE_COLUMN_DIALOG_ID;
|
||||||
dialog.setAttribute("aria-labelledby", MOBILE_COLUMN_DIALOG_TITLE_ID);
|
dialog.setAttribute("aria-labelledby", MOBILE_COLUMN_DIALOG_TITLE_ID);
|
||||||
|
|
@ -75,13 +76,13 @@ function initMobileColumnActions(manager) {
|
||||||
<span class="modal-title" id="${MOBILE_COLUMN_DIALOG_TITLE_ID}">Column actions</span>
|
<span class="modal-title" id="${MOBILE_COLUMN_DIALOG_TITLE_ID}">Column actions</span>
|
||||||
<span class="modal-meta"></span>
|
<span class="modal-meta"></span>
|
||||||
</div>
|
</div>
|
||||||
<div class="list-wrap mobile-column-list"></div>
|
<div class="modal-body list-wrap mobile-column-list"></div>
|
||||||
<div class="modal-footer">
|
<div class="modal-footer">
|
||||||
<span class="footer-info">Tap a column to reveal actions.</span>
|
<span class="footer-info">Tap a column to reveal actions.</span>
|
||||||
<button type="button" class="btn btn-ghost mobile-column-actions-done">Done</button>
|
<button type="button" class="modal-btn modal-btn-ghost mobile-column-actions-done">Done</button>
|
||||||
</div>
|
</div>
|
||||||
`;
|
`;
|
||||||
document.body.appendChild(dialog);
|
document.body.appendChild(modal);
|
||||||
|
|
||||||
triggerButton.setAttribute("aria-haspopup", "dialog");
|
triggerButton.setAttribute("aria-haspopup", "dialog");
|
||||||
triggerButton.setAttribute("aria-controls", MOBILE_COLUMN_DIALOG_ID);
|
triggerButton.setAttribute("aria-controls", MOBILE_COLUMN_DIALOG_ID);
|
||||||
|
|
@ -91,7 +92,6 @@ function initMobileColumnActions(manager) {
|
||||||
var listWrap = dialog.querySelector(".mobile-column-list");
|
var listWrap = dialog.querySelector(".mobile-column-list");
|
||||||
var doneButton = dialog.querySelector(".mobile-column-actions-done");
|
var doneButton = dialog.querySelector(".mobile-column-actions-done");
|
||||||
var expandedSectionId = null;
|
var expandedSectionId = null;
|
||||||
var shouldRestoreFocus = true;
|
|
||||||
|
|
||||||
function updateExpandedSection() {
|
function updateExpandedSection() {
|
||||||
Array.from(dialog.querySelectorAll(".col-header")).forEach((button) => {
|
Array.from(dialog.querySelectorAll(".col-header")).forEach((button) => {
|
||||||
|
|
@ -128,16 +128,7 @@ function initMobileColumnActions(manager) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function closeDialog(options) {
|
function closeDialog(options) {
|
||||||
options = options || {};
|
modal.close(options);
|
||||||
shouldRestoreFocus = options.restoreFocus !== false;
|
|
||||||
if (dialog.open) {
|
|
||||||
dialog.close();
|
|
||||||
} else {
|
|
||||||
triggerButton.setAttribute("aria-expanded", "false");
|
|
||||||
if (shouldRestoreFocus) {
|
|
||||||
triggerButton.focus();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function renderDialog() {
|
function renderDialog() {
|
||||||
|
|
@ -166,7 +157,8 @@ function initMobileColumnActions(manager) {
|
||||||
topActions.className = "mobile-column-top-actions";
|
topActions.className = "mobile-column-top-actions";
|
||||||
|
|
||||||
var showAllColumns = document.createElement("a");
|
var showAllColumns = document.createElement("a");
|
||||||
showAllColumns.className = "btn btn-ghost mobile-column-top-action";
|
showAllColumns.className =
|
||||||
|
"modal-btn modal-btn-ghost mobile-column-top-action";
|
||||||
showAllColumns.href = manager.columnActions.showAllColumnsUrl();
|
showAllColumns.href = manager.columnActions.showAllColumnsUrl();
|
||||||
showAllColumns.textContent = "Show all columns";
|
showAllColumns.textContent = "Show all columns";
|
||||||
|
|
||||||
|
|
@ -265,9 +257,7 @@ function initMobileColumnActions(manager) {
|
||||||
if (!renderDialog()) {
|
if (!renderDialog()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!dialog.open) {
|
modal.show({ returnFocusTo: triggerButton });
|
||||||
dialog.showModal();
|
|
||||||
}
|
|
||||||
triggerButton.setAttribute("aria-expanded", "true");
|
triggerButton.setAttribute("aria-expanded", "true");
|
||||||
var focusTarget =
|
var focusTarget =
|
||||||
dialog.querySelector(".mobile-column-top-action") ||
|
dialog.querySelector(".mobile-column-top-action") ||
|
||||||
|
|
@ -288,22 +278,8 @@ function initMobileColumnActions(manager) {
|
||||||
closeDialog();
|
closeDialog();
|
||||||
});
|
});
|
||||||
|
|
||||||
dialog.addEventListener("click", function (ev) {
|
|
||||||
if (ev.target === dialog) {
|
|
||||||
closeDialog();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
dialog.addEventListener("cancel", function (ev) {
|
|
||||||
ev.preventDefault();
|
|
||||||
closeDialog();
|
|
||||||
});
|
|
||||||
|
|
||||||
dialog.addEventListener("close", function () {
|
dialog.addEventListener("close", function () {
|
||||||
triggerButton.setAttribute("aria-expanded", "false");
|
triggerButton.setAttribute("aria-expanded", "false");
|
||||||
if (shouldRestoreFocus) {
|
|
||||||
triggerButton.focus();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
window.addEventListener("resize", function () {
|
window.addEventListener("resize", function () {
|
||||||
|
|
|
||||||
167
datasette/static/modal.js
Normal file
167
datasette/static/modal.js
Normal file
|
|
@ -0,0 +1,167 @@
|
||||||
|
// Shared lifecycle for native modal dialogs.
|
||||||
|
(() => {
|
||||||
|
class DatasetteModal extends HTMLElement {
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
this.beforeClose = null;
|
||||||
|
this._busy = false;
|
||||||
|
this._restoreFocus = true;
|
||||||
|
this._returnFocusTo = null;
|
||||||
|
this._escapeCleanup = null;
|
||||||
|
this._escapeTimer = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
static create() {
|
||||||
|
const modal = document.createElement("datasette-modal");
|
||||||
|
modal.appendChild(document.createElement("dialog"));
|
||||||
|
return modal;
|
||||||
|
}
|
||||||
|
|
||||||
|
get dialog() {
|
||||||
|
return this.querySelector(":scope > dialog");
|
||||||
|
}
|
||||||
|
|
||||||
|
get busy() {
|
||||||
|
return this._busy;
|
||||||
|
}
|
||||||
|
|
||||||
|
set busy(value) {
|
||||||
|
this._busy = !!value;
|
||||||
|
if (this.dialog) {
|
||||||
|
this.dialog.setAttribute("aria-busy", String(this._busy));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
connectedCallback() {
|
||||||
|
const dialog = this.dialog;
|
||||||
|
if (!dialog) return;
|
||||||
|
dialog.classList.add("datasette-modal");
|
||||||
|
this._listeners?.abort();
|
||||||
|
this._listeners = new AbortController();
|
||||||
|
const options = { signal: this._listeners.signal };
|
||||||
|
let backdropPointerDown = false;
|
||||||
|
const outside = (event) => {
|
||||||
|
const rect = dialog.getBoundingClientRect();
|
||||||
|
return (
|
||||||
|
event.target === dialog &&
|
||||||
|
(event.clientX < rect.left ||
|
||||||
|
event.clientX > rect.right ||
|
||||||
|
event.clientY < rect.top ||
|
||||||
|
event.clientY > rect.bottom)
|
||||||
|
);
|
||||||
|
};
|
||||||
|
dialog.addEventListener(
|
||||||
|
"pointerdown",
|
||||||
|
(event) => {
|
||||||
|
backdropPointerDown = outside(event);
|
||||||
|
},
|
||||||
|
options,
|
||||||
|
);
|
||||||
|
dialog.addEventListener(
|
||||||
|
"click",
|
||||||
|
(event) => {
|
||||||
|
if (backdropPointerDown && outside(event))
|
||||||
|
this.requestClose("backdrop");
|
||||||
|
backdropPointerDown = false;
|
||||||
|
},
|
||||||
|
options,
|
||||||
|
);
|
||||||
|
dialog.addEventListener(
|
||||||
|
"keydown",
|
||||||
|
(event) => {
|
||||||
|
if (event.key !== "Escape" || event.defaultPrevented) return;
|
||||||
|
// A nested native dialog or plugin picker gets first refusal.
|
||||||
|
if (event.target.closest("dialog") !== dialog) return;
|
||||||
|
event.preventDefault();
|
||||||
|
if (this.busy || this._escapeCleanup || this._escapeTimer !== null)
|
||||||
|
return;
|
||||||
|
// Safari can otherwise use this Escape press to cancel confirm() too.
|
||||||
|
// Only keyboard dismissals wait for keyup; native cancel events needn't.
|
||||||
|
const onKeyup = (up) => {
|
||||||
|
if (up.key !== "Escape") return;
|
||||||
|
this._escapeCleanup();
|
||||||
|
this._escapeCleanup = null;
|
||||||
|
this._escapeTimer = setTimeout(() => {
|
||||||
|
this._escapeTimer = null;
|
||||||
|
this.requestClose("escape");
|
||||||
|
}, 0);
|
||||||
|
};
|
||||||
|
this.ownerDocument.addEventListener("keyup", onKeyup, true);
|
||||||
|
this._escapeCleanup = () =>
|
||||||
|
this.ownerDocument.removeEventListener("keyup", onKeyup, true);
|
||||||
|
},
|
||||||
|
options,
|
||||||
|
);
|
||||||
|
dialog.addEventListener(
|
||||||
|
"cancel",
|
||||||
|
(event) => {
|
||||||
|
if (event.target !== dialog) return;
|
||||||
|
event.preventDefault();
|
||||||
|
if (!this._escapeCleanup && this._escapeTimer === null)
|
||||||
|
this.requestClose("escape");
|
||||||
|
},
|
||||||
|
options,
|
||||||
|
);
|
||||||
|
dialog.addEventListener(
|
||||||
|
"close",
|
||||||
|
(event) => {
|
||||||
|
if (event.target !== dialog || dialog.open) return;
|
||||||
|
this._clearPendingClose();
|
||||||
|
this.busy = false;
|
||||||
|
if (this._restoreFocus && this._returnFocusTo?.isConnected) {
|
||||||
|
// Menu actions may have become hidden while the dialog was open.
|
||||||
|
const details = this._returnFocusTo.closest("details:not([open])");
|
||||||
|
const target =
|
||||||
|
details?.querySelector("summary") || this._returnFocusTo;
|
||||||
|
target.focus({ preventScroll: true });
|
||||||
|
}
|
||||||
|
this._returnFocusTo = null;
|
||||||
|
},
|
||||||
|
options,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
disconnectedCallback() {
|
||||||
|
this._listeners?.abort();
|
||||||
|
this._clearPendingClose();
|
||||||
|
this._returnFocusTo = null;
|
||||||
|
if (this.dialog?.open) this.dialog.close();
|
||||||
|
this.busy = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
_clearPendingClose() {
|
||||||
|
this._escapeCleanup?.();
|
||||||
|
this._escapeCleanup = null;
|
||||||
|
clearTimeout(this._escapeTimer);
|
||||||
|
this._escapeTimer = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
show({ returnFocusTo, initialFocus } = {}) {
|
||||||
|
const dialog = this.dialog;
|
||||||
|
if (!dialog.open) {
|
||||||
|
this._clearPendingClose();
|
||||||
|
this._returnFocusTo = returnFocusTo || this.ownerDocument.activeElement;
|
||||||
|
this._restoreFocus = true;
|
||||||
|
dialog.showModal();
|
||||||
|
}
|
||||||
|
if (typeof initialFocus === "function") initialFocus();
|
||||||
|
else initialFocus?.focus();
|
||||||
|
}
|
||||||
|
|
||||||
|
requestClose(source = "cancel") {
|
||||||
|
if (!this.dialog.open || this.busy) return false;
|
||||||
|
if (this.beforeClose && this.beforeClose(source) === false) return false;
|
||||||
|
this.close();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
close({ restoreFocus = true } = {}) {
|
||||||
|
this._clearPendingClose();
|
||||||
|
this._restoreFocus = restoreFocus;
|
||||||
|
this.dialog.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
customElements.define("datasette-modal", DatasetteModal);
|
||||||
|
window.DatasetteModal = DatasetteModal;
|
||||||
|
})();
|
||||||
|
|
@ -10,277 +10,22 @@ class NavigationSearch extends HTMLElement {
|
||||||
this.recentHeadingId = `navigation-search-recent-${this.instanceId}`;
|
this.recentHeadingId = `navigation-search-recent-${this.instanceId}`;
|
||||||
this.statusId = `navigation-search-status-${this.instanceId}`;
|
this.statusId = `navigation-search-status-${this.instanceId}`;
|
||||||
this.titleId = `navigation-search-title-${this.instanceId}`;
|
this.titleId = `navigation-search-title-${this.instanceId}`;
|
||||||
this.attachShadow({ mode: "open" });
|
|
||||||
this.selectedIndex = -1;
|
this.selectedIndex = -1;
|
||||||
this.matches = [];
|
this.matches = [];
|
||||||
this.renderedMatches = [];
|
this.renderedMatches = [];
|
||||||
this.debounceTimer = null;
|
this.debounceTimer = null;
|
||||||
this.restoreFocusTarget = null;
|
}
|
||||||
this.shouldRestoreFocus = true;
|
|
||||||
|
|
||||||
|
connectedCallback() {
|
||||||
|
if (this._initialized) return;
|
||||||
|
this._initialized = true;
|
||||||
this.render();
|
this.render();
|
||||||
this.setupEventListeners();
|
this.setupEventListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
this.shadowRoot.innerHTML = `
|
this.innerHTML = `
|
||||||
<style>
|
<datasette-modal><dialog aria-modal="true" aria-labelledby="${this.titleId}">
|
||||||
:host {
|
|
||||||
display: contents;
|
|
||||||
}
|
|
||||||
|
|
||||||
dialog {
|
|
||||||
border: none;
|
|
||||||
border-radius: var(--modal-border-radius, 0.75rem);
|
|
||||||
padding: 0;
|
|
||||||
max-width: 90vw;
|
|
||||||
width: 600px;
|
|
||||||
max-height: 80vh;
|
|
||||||
box-shadow: var(--modal-shadow, 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04));
|
|
||||||
animation: slideIn var(--modal-animation-duration, 0.2s) ease-out;
|
|
||||||
}
|
|
||||||
|
|
||||||
dialog::backdrop {
|
|
||||||
background: var(--modal-backdrop-bg, rgba(0, 0, 0, 0.5));
|
|
||||||
backdrop-filter: var(--modal-backdrop-blur, blur(4px));
|
|
||||||
-webkit-backdrop-filter: var(--modal-backdrop-blur, blur(4px));
|
|
||||||
animation: fadeIn var(--modal-animation-duration, 0.2s) ease-out;
|
|
||||||
}
|
|
||||||
|
|
||||||
@keyframes slideIn {
|
|
||||||
from {
|
|
||||||
opacity: 0;
|
|
||||||
transform: translateY(-20px) scale(0.95);
|
|
||||||
}
|
|
||||||
to {
|
|
||||||
opacity: 1;
|
|
||||||
transform: translateY(0) scale(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@keyframes fadeIn {
|
|
||||||
from { opacity: 0; }
|
|
||||||
to { opacity: 1; }
|
|
||||||
}
|
|
||||||
|
|
||||||
.search-container {
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
}
|
|
||||||
|
|
||||||
.search-input-wrapper {
|
|
||||||
padding: 1.25rem;
|
|
||||||
border-bottom: 1px solid #e5e7eb;
|
|
||||||
display: flex;
|
|
||||||
gap: 0.5rem;
|
|
||||||
align-items: center;
|
|
||||||
}
|
|
||||||
|
|
||||||
.search-input {
|
|
||||||
width: 100%;
|
|
||||||
flex: 1;
|
|
||||||
min-width: 0;
|
|
||||||
padding: 0.75rem 1rem;
|
|
||||||
font-size: 1rem;
|
|
||||||
border: 2px solid #e5e7eb;
|
|
||||||
border-radius: 0.5rem;
|
|
||||||
outline: none;
|
|
||||||
transition: border-color 0.2s;
|
|
||||||
box-sizing: border-box;
|
|
||||||
}
|
|
||||||
|
|
||||||
.search-input:focus {
|
|
||||||
border-color: #2563eb;
|
|
||||||
}
|
|
||||||
|
|
||||||
.close-search {
|
|
||||||
background: transparent;
|
|
||||||
border: 1px solid transparent;
|
|
||||||
border-radius: 0.375rem;
|
|
||||||
color: #4b5563;
|
|
||||||
cursor: pointer;
|
|
||||||
flex: 0 0 auto;
|
|
||||||
font: inherit;
|
|
||||||
font-size: 1.5rem;
|
|
||||||
height: 2.75rem;
|
|
||||||
line-height: 1;
|
|
||||||
width: 2.75rem;
|
|
||||||
}
|
|
||||||
|
|
||||||
.close-search:hover,
|
|
||||||
.close-search:focus {
|
|
||||||
background-color: #f3f4f6;
|
|
||||||
border-color: #d1d5db;
|
|
||||||
}
|
|
||||||
|
|
||||||
.results-container {
|
|
||||||
overflow-y: auto;
|
|
||||||
height: calc(80vh - 180px);
|
|
||||||
padding: 0.5rem;
|
|
||||||
}
|
|
||||||
|
|
||||||
.results-list:empty {
|
|
||||||
display: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
.result-item {
|
|
||||||
padding: 0.875rem 1rem;
|
|
||||||
cursor: pointer;
|
|
||||||
border-radius: 0.5rem;
|
|
||||||
transition: background-color 0.15s;
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
gap: 0.75rem;
|
|
||||||
}
|
|
||||||
|
|
||||||
.result-item:hover {
|
|
||||||
background-color: #f3f4f6;
|
|
||||||
}
|
|
||||||
|
|
||||||
.result-item.selected {
|
|
||||||
background-color: #dbeafe;
|
|
||||||
}
|
|
||||||
|
|
||||||
.result-item > div {
|
|
||||||
flex: 1;
|
|
||||||
min-width: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.jump-start-content {
|
|
||||||
border-bottom: 1px solid #e5e7eb;
|
|
||||||
margin-bottom: 0.5rem;
|
|
||||||
padding: 0.5rem 0.5rem 1rem;
|
|
||||||
}
|
|
||||||
|
|
||||||
.jump-start-content:empty {
|
|
||||||
display: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
.result-name {
|
|
||||||
font-weight: 500;
|
|
||||||
color: #111827;
|
|
||||||
}
|
|
||||||
|
|
||||||
.result-label {
|
|
||||||
font-size: 0.875rem;
|
|
||||||
color: #4b5563;
|
|
||||||
}
|
|
||||||
|
|
||||||
.result-type {
|
|
||||||
color: #4b5563;
|
|
||||||
font-size: 0.75rem;
|
|
||||||
font-weight: 600;
|
|
||||||
text-transform: uppercase;
|
|
||||||
}
|
|
||||||
|
|
||||||
.result-url {
|
|
||||||
font-size: 0.875rem;
|
|
||||||
color: #6b7280;
|
|
||||||
}
|
|
||||||
|
|
||||||
.result-description {
|
|
||||||
color: #374151;
|
|
||||||
display: -webkit-box;
|
|
||||||
font-size: 0.8125rem;
|
|
||||||
line-height: 1.35;
|
|
||||||
margin-top: 0.35rem;
|
|
||||||
overflow: hidden;
|
|
||||||
-webkit-box-orient: vertical;
|
|
||||||
-webkit-line-clamp: 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
.results-heading {
|
|
||||||
color: #4b5563;
|
|
||||||
font-size: 0.75rem;
|
|
||||||
font-weight: 600;
|
|
||||||
letter-spacing: 0;
|
|
||||||
padding: 0.5rem 1rem 0.25rem;
|
|
||||||
text-transform: uppercase;
|
|
||||||
}
|
|
||||||
|
|
||||||
.recent-actions {
|
|
||||||
padding: 0.25rem 1rem 0.75rem;
|
|
||||||
}
|
|
||||||
|
|
||||||
.clear-recent {
|
|
||||||
background: transparent;
|
|
||||||
border: 0;
|
|
||||||
color: #2563eb;
|
|
||||||
cursor: pointer;
|
|
||||||
font: inherit;
|
|
||||||
font-size: 0.875rem;
|
|
||||||
padding: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.clear-recent:hover {
|
|
||||||
text-decoration: underline;
|
|
||||||
}
|
|
||||||
|
|
||||||
.no-results {
|
|
||||||
padding: 2rem;
|
|
||||||
text-align: center;
|
|
||||||
color: #6b7280;
|
|
||||||
}
|
|
||||||
|
|
||||||
.hint-text {
|
|
||||||
padding: 0.75rem 1.25rem;
|
|
||||||
font-size: 0.875rem;
|
|
||||||
color: #6b7280;
|
|
||||||
border-top: 1px solid #e5e7eb;
|
|
||||||
display: flex;
|
|
||||||
gap: 1rem;
|
|
||||||
flex-wrap: wrap;
|
|
||||||
}
|
|
||||||
|
|
||||||
.hint-text kbd {
|
|
||||||
background: #f3f4f6;
|
|
||||||
padding: 0.125rem 0.375rem;
|
|
||||||
border-radius: 0.25rem;
|
|
||||||
font-size: 0.75rem;
|
|
||||||
border: 1px solid #d1d5db;
|
|
||||||
font-family: monospace;
|
|
||||||
}
|
|
||||||
|
|
||||||
.visually-hidden {
|
|
||||||
border: 0;
|
|
||||||
clip: rect(0 0 0 0);
|
|
||||||
height: 1px;
|
|
||||||
margin: -1px;
|
|
||||||
overflow: hidden;
|
|
||||||
padding: 0;
|
|
||||||
position: absolute;
|
|
||||||
white-space: nowrap;
|
|
||||||
width: 1px;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Mobile optimizations */
|
|
||||||
@media (max-width: 640px) {
|
|
||||||
dialog {
|
|
||||||
width: 95vw;
|
|
||||||
max-height: 85vh;
|
|
||||||
border-radius: 0.5rem;
|
|
||||||
}
|
|
||||||
|
|
||||||
.search-input-wrapper {
|
|
||||||
padding: 1rem;
|
|
||||||
}
|
|
||||||
|
|
||||||
.search-input {
|
|
||||||
font-size: 16px; /* Prevents zoom on iOS */
|
|
||||||
}
|
|
||||||
|
|
||||||
.result-item {
|
|
||||||
padding: 1rem 0.75rem;
|
|
||||||
}
|
|
||||||
|
|
||||||
.hint-text {
|
|
||||||
font-size: 0.8rem;
|
|
||||||
padding: 0.5rem 1rem;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
|
|
||||||
<dialog aria-modal="true" aria-labelledby="${this.titleId}">
|
|
||||||
<div class="search-container">
|
<div class="search-container">
|
||||||
<h2 id="${this.titleId}" class="visually-hidden">Jump to</h2>
|
<h2 id="${this.titleId}" class="visually-hidden">Jump to</h2>
|
||||||
<p id="${this.instructionsId}" class="visually-hidden">Type to search. Use up and down arrow keys to move through results, Enter to select a result, and Escape to close this menu.</p>
|
<p id="${this.instructionsId}" class="visually-hidden">Type to search. Use up and down arrow keys to move through results, Enter to select a result, and Escape to close this menu.</p>
|
||||||
|
|
@ -302,23 +47,22 @@ class NavigationSearch extends HTMLElement {
|
||||||
>
|
>
|
||||||
<button type="button" class="close-search" aria-label="Close jump menu">×</button>
|
<button type="button" class="close-search" aria-label="Close jump menu">×</button>
|
||||||
</div>
|
</div>
|
||||||
<div class="results-container"></div>
|
<div class="modal-body results-container"></div>
|
||||||
<div class="hint-text">
|
<div class="hint-text">
|
||||||
<span><kbd>↑</kbd> <kbd>↓</kbd> Navigate</span>
|
<span><kbd>↑</kbd> <kbd>↓</kbd> Navigate</span>
|
||||||
<span><kbd>Enter</kbd> Select</span>
|
<span><kbd>Enter</kbd> Select</span>
|
||||||
<span><kbd>Esc</kbd> Close</span>
|
<span><kbd>Esc</kbd> Close</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</dialog>
|
</dialog></datasette-modal>
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
setupEventListeners() {
|
setupEventListeners() {
|
||||||
const dialog = this.shadowRoot.querySelector("dialog");
|
const dialog = this.querySelector("dialog");
|
||||||
const input = this.shadowRoot.querySelector(".search-input");
|
const input = this.querySelector(".search-input");
|
||||||
const closeButton = this.shadowRoot.querySelector(".close-search");
|
const closeButton = this.querySelector(".close-search");
|
||||||
const resultsContainer =
|
const resultsContainer = this.querySelector(".results-container");
|
||||||
this.shadowRoot.querySelector(".results-container");
|
|
||||||
|
|
||||||
// Global keyboard listener for "/"
|
// Global keyboard listener for "/"
|
||||||
document.addEventListener("keydown", (e) => {
|
document.addEventListener("keydown", (e) => {
|
||||||
|
|
@ -355,8 +99,6 @@ class NavigationSearch extends HTMLElement {
|
||||||
} else if (e.key === "Enter") {
|
} else if (e.key === "Enter") {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
this.selectCurrentItem();
|
this.selectCurrentItem();
|
||||||
} else if (e.key === "Escape") {
|
|
||||||
this.closeMenu();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -380,18 +122,6 @@ class NavigationSearch extends HTMLElement {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Close on backdrop click
|
|
||||||
dialog.addEventListener("click", (e) => {
|
|
||||||
if (e.target === dialog) {
|
|
||||||
this.closeMenu();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
dialog.addEventListener("cancel", (e) => {
|
|
||||||
e.preventDefault();
|
|
||||||
this.closeMenu();
|
|
||||||
});
|
|
||||||
|
|
||||||
dialog.addEventListener("close", () => {
|
dialog.addEventListener("close", () => {
|
||||||
this.onMenuClosed();
|
this.onMenuClosed();
|
||||||
});
|
});
|
||||||
|
|
@ -432,19 +162,6 @@ class NavigationSearch extends HTMLElement {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
focusRestoreTarget(trigger) {
|
|
||||||
if (trigger && typeof trigger.focus === "function") {
|
|
||||||
return trigger;
|
|
||||||
}
|
|
||||||
if (
|
|
||||||
document.activeElement &&
|
|
||||||
typeof document.activeElement.focus === "function"
|
|
||||||
) {
|
|
||||||
return document.activeElement;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
setNavigationTriggersExpanded(expanded) {
|
setNavigationTriggersExpanded(expanded) {
|
||||||
if (typeof document.querySelectorAll !== "function") {
|
if (typeof document.querySelectorAll !== "function") {
|
||||||
return;
|
return;
|
||||||
|
|
@ -465,8 +182,8 @@ class NavigationSearch extends HTMLElement {
|
||||||
}
|
}
|
||||||
|
|
||||||
updateComboboxState() {
|
updateComboboxState() {
|
||||||
const dialog = this.shadowRoot.querySelector("dialog");
|
const dialog = this.querySelector("dialog");
|
||||||
const input = this.shadowRoot.querySelector(".search-input");
|
const input = this.querySelector(".search-input");
|
||||||
const matches = this.renderedMatches || [];
|
const matches = this.renderedMatches || [];
|
||||||
this.setElementAttribute(
|
this.setElementAttribute(
|
||||||
input,
|
input,
|
||||||
|
|
@ -491,7 +208,7 @@ class NavigationSearch extends HTMLElement {
|
||||||
}
|
}
|
||||||
|
|
||||||
setStatus(message) {
|
setStatus(message) {
|
||||||
const status = this.shadowRoot.querySelector(`#${this.statusId}`);
|
const status = this.querySelector(`#${this.statusId}`);
|
||||||
if (status) {
|
if (status) {
|
||||||
status.textContent = message || "";
|
status.textContent = message || "";
|
||||||
}
|
}
|
||||||
|
|
@ -701,7 +418,7 @@ class NavigationSearch extends HTMLElement {
|
||||||
section.render(node, {
|
section.render(node, {
|
||||||
navigationSearch: this,
|
navigationSearch: this,
|
||||||
container,
|
container,
|
||||||
input: this.shadowRoot.querySelector(".search-input"),
|
input: this.querySelector(".search-input"),
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -740,8 +457,8 @@ class NavigationSearch extends HTMLElement {
|
||||||
}
|
}
|
||||||
|
|
||||||
renderResults() {
|
renderResults() {
|
||||||
const container = this.shadowRoot.querySelector(".results-container");
|
const container = this.querySelector(".results-container");
|
||||||
const input = this.shadowRoot.querySelector(".search-input");
|
const input = this.querySelector(".search-input");
|
||||||
const showStartContent = !input.value.trim();
|
const showStartContent = !input.value.trim();
|
||||||
const jumpSections = showStartContent ? this.jumpSections() : [];
|
const jumpSections = showStartContent ? this.jumpSections() : [];
|
||||||
const startBlock = showStartContent
|
const startBlock = showStartContent
|
||||||
|
|
@ -853,18 +570,15 @@ class NavigationSearch extends HTMLElement {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
openMenu(trigger) {
|
openMenu(returnFocusTo) {
|
||||||
const dialog = this.shadowRoot.querySelector("dialog");
|
const input = this.querySelector(".search-input");
|
||||||
const input = this.shadowRoot.querySelector(".search-input");
|
|
||||||
|
|
||||||
this.restoreFocusTarget = this.focusRestoreTarget(trigger);
|
this.querySelector("datasette-modal").show({
|
||||||
this.shouldRestoreFocus = true;
|
returnFocusTo,
|
||||||
if (!dialog.open) {
|
initialFocus: input,
|
||||||
dialog.showModal();
|
});
|
||||||
}
|
|
||||||
this.setNavigationTriggersExpanded(true);
|
this.setNavigationTriggersExpanded(true);
|
||||||
input.value = "";
|
input.value = "";
|
||||||
input.focus();
|
|
||||||
|
|
||||||
// Reset state, then populate the default jump list.
|
// Reset state, then populate the default jump list.
|
||||||
this.matches = [];
|
this.matches = [];
|
||||||
|
|
@ -874,29 +588,15 @@ class NavigationSearch extends HTMLElement {
|
||||||
}
|
}
|
||||||
|
|
||||||
closeMenu(options = {}) {
|
closeMenu(options = {}) {
|
||||||
const dialog = this.shadowRoot.querySelector("dialog");
|
this.querySelector("datasette-modal").close(options);
|
||||||
this.shouldRestoreFocus = options.restoreFocus !== false;
|
|
||||||
if (dialog.open) {
|
|
||||||
dialog.close();
|
|
||||||
} else {
|
|
||||||
this.onMenuClosed();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onMenuClosed() {
|
onMenuClosed() {
|
||||||
const input = this.shadowRoot.querySelector(".search-input");
|
const input = this.querySelector(".search-input");
|
||||||
this.setElementAttribute(input, "aria-expanded", "false");
|
this.setElementAttribute(input, "aria-expanded", "false");
|
||||||
this.removeElementAttribute(input, "aria-activedescendant");
|
this.removeElementAttribute(input, "aria-activedescendant");
|
||||||
this.setNavigationTriggersExpanded(false);
|
this.setNavigationTriggersExpanded(false);
|
||||||
this.setStatus("");
|
this.setStatus("");
|
||||||
if (
|
|
||||||
this.shouldRestoreFocus &&
|
|
||||||
this.restoreFocusTarget &&
|
|
||||||
typeof this.restoreFocusTarget.focus === "function"
|
|
||||||
) {
|
|
||||||
this.restoreFocusTarget.focus();
|
|
||||||
}
|
|
||||||
this.restoreFocusTarget = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
escapeHtml(text) {
|
escapeHtml(text) {
|
||||||
|
|
|
||||||
|
|
@ -157,6 +157,7 @@ function createSetColumnTypeOption(value, name, description, checked) {
|
||||||
|
|
||||||
function setSetColumnTypeDialogBusy(state, isBusy) {
|
function setSetColumnTypeDialogBusy(state, isBusy) {
|
||||||
state.isBusy = isBusy;
|
state.isBusy = isBusy;
|
||||||
|
state.modal.busy = isBusy;
|
||||||
state.saveButton.disabled = isBusy;
|
state.saveButton.disabled = isBusy;
|
||||||
state.cancelButton.disabled = isBusy;
|
state.cancelButton.disabled = isBusy;
|
||||||
Array.from(
|
Array.from(
|
||||||
|
|
@ -185,7 +186,8 @@ function ensureSetColumnTypeDialog() {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
var dialog = document.createElement("dialog");
|
var modal = DatasetteModal.create();
|
||||||
|
var dialog = modal.dialog;
|
||||||
dialog.id = SET_COLUMN_TYPE_DIALOG_ID;
|
dialog.id = SET_COLUMN_TYPE_DIALOG_ID;
|
||||||
dialog.className = "set-column-type-dialog";
|
dialog.className = "set-column-type-dialog";
|
||||||
dialog.setAttribute("aria-labelledby", "set-column-type-title");
|
dialog.setAttribute("aria-labelledby", "set-column-type-title");
|
||||||
|
|
@ -196,16 +198,17 @@ function ensureSetColumnTypeDialog() {
|
||||||
</div>
|
</div>
|
||||||
<p class="set-column-type-status"></p>
|
<p class="set-column-type-status"></p>
|
||||||
<p class="set-column-type-error" hidden></p>
|
<p class="set-column-type-error" hidden></p>
|
||||||
<div class="set-column-type-options"></div>
|
<div class="modal-body set-column-type-options"></div>
|
||||||
<div class="modal-footer">
|
<div class="modal-footer">
|
||||||
<span class="footer-info"></span>
|
<span class="footer-info"></span>
|
||||||
<button type="button" class="btn btn-ghost set-column-type-cancel">Cancel</button>
|
<button type="button" class="modal-btn modal-btn-ghost set-column-type-cancel">Cancel</button>
|
||||||
<button type="button" class="btn btn-primary set-column-type-save">Save</button>
|
<button type="button" class="modal-btn modal-btn-primary set-column-type-save">Save</button>
|
||||||
</div>
|
</div>
|
||||||
`;
|
`;
|
||||||
document.body.appendChild(dialog);
|
document.body.appendChild(modal);
|
||||||
|
|
||||||
setColumnTypeDialogState = {
|
setColumnTypeDialogState = {
|
||||||
|
modal: modal,
|
||||||
dialog: dialog,
|
dialog: dialog,
|
||||||
meta: dialog.querySelector(".modal-meta"),
|
meta: dialog.querySelector(".modal-meta"),
|
||||||
status: dialog.querySelector(".set-column-type-status"),
|
status: dialog.querySelector(".set-column-type-status"),
|
||||||
|
|
@ -220,21 +223,7 @@ function ensureSetColumnTypeDialog() {
|
||||||
};
|
};
|
||||||
|
|
||||||
setColumnTypeDialogState.cancelButton.addEventListener("click", function () {
|
setColumnTypeDialogState.cancelButton.addEventListener("click", function () {
|
||||||
if (!setColumnTypeDialogState.isBusy) {
|
modal.requestClose("cancel");
|
||||||
dialog.close();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
dialog.addEventListener("click", function (ev) {
|
|
||||||
if (ev.target === dialog && !setColumnTypeDialogState.isBusy) {
|
|
||||||
dialog.close();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
dialog.addEventListener("cancel", function (ev) {
|
|
||||||
if (setColumnTypeDialogState.isBusy) {
|
|
||||||
ev.preventDefault();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
dialog.addEventListener("close", function () {
|
dialog.addEventListener("close", function () {
|
||||||
|
|
@ -242,49 +231,52 @@ function ensureSetColumnTypeDialog() {
|
||||||
setSetColumnTypeDialogBusy(setColumnTypeDialogState, false);
|
setSetColumnTypeDialogBusy(setColumnTypeDialogState, false);
|
||||||
});
|
});
|
||||||
|
|
||||||
setColumnTypeDialogState.saveButton.addEventListener("click", async function () {
|
setColumnTypeDialogState.saveButton.addEventListener(
|
||||||
var state = setColumnTypeDialogState;
|
"click",
|
||||||
var selected = state.dialog.querySelector(
|
async function () {
|
||||||
'input[name="set-column-type-choice"]:checked',
|
var state = setColumnTypeDialogState;
|
||||||
);
|
var selected = state.dialog.querySelector(
|
||||||
var selectedType = selected ? selected.value : "";
|
'input[name="set-column-type-choice"]:checked',
|
||||||
var currentType = state.currentConfig.current
|
);
|
||||||
? state.currentConfig.current.type
|
var selectedType = selected ? selected.value : "";
|
||||||
: "";
|
var currentType = state.currentConfig.current
|
||||||
|
? state.currentConfig.current.type
|
||||||
|
: "";
|
||||||
|
|
||||||
if (selectedType === currentType) {
|
if (selectedType === currentType) {
|
||||||
state.dialog.close();
|
state.modal.close();
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
clearSetColumnTypeDialogError(state);
|
|
||||||
setSetColumnTypeDialogBusy(state, true);
|
|
||||||
|
|
||||||
var payload = {
|
|
||||||
column: state.currentColumn,
|
|
||||||
column_type: selectedType ? { type: selectedType } : null,
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
|
||||||
var response = await fetch(getSetColumnTypeData().path, {
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
Accept: "application/json",
|
|
||||||
},
|
|
||||||
body: JSON.stringify(payload),
|
|
||||||
});
|
|
||||||
var data = await response.json();
|
|
||||||
if (!response.ok || data.ok === false) {
|
|
||||||
var message = (data.errors || ["Request failed"]).join(" ");
|
|
||||||
throw new Error(message);
|
|
||||||
}
|
}
|
||||||
location.reload();
|
|
||||||
} catch (error) {
|
clearSetColumnTypeDialogError(state);
|
||||||
setSetColumnTypeDialogBusy(state, false);
|
setSetColumnTypeDialogBusy(state, true);
|
||||||
showSetColumnTypeDialogError(state, error.message || "Request failed");
|
|
||||||
}
|
var payload = {
|
||||||
});
|
column: state.currentColumn,
|
||||||
|
column_type: selectedType ? { type: selectedType } : null,
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
var response = await fetch(getSetColumnTypeData().path, {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Accept: "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify(payload),
|
||||||
|
});
|
||||||
|
var data = await response.json();
|
||||||
|
if (!response.ok || data.ok === false) {
|
||||||
|
var message = (data.errors || ["Request failed"]).join(" ");
|
||||||
|
throw new Error(message);
|
||||||
|
}
|
||||||
|
location.reload();
|
||||||
|
} catch (error) {
|
||||||
|
setSetColumnTypeDialogBusy(state, false);
|
||||||
|
showSetColumnTypeDialogError(state, error.message || "Request failed");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
return setColumnTypeDialogState;
|
return setColumnTypeDialogState;
|
||||||
}
|
}
|
||||||
|
|
@ -341,9 +333,7 @@ function openSetColumnTypeDialog(th) {
|
||||||
state.optionsWrap.appendChild(emptyState);
|
state.optionsWrap.appendChild(emptyState);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!state.dialog.open) {
|
state.modal.show();
|
||||||
state.dialog.showModal();
|
|
||||||
}
|
|
||||||
var selectedOption = state.dialog.querySelector(
|
var selectedOption = state.dialog.querySelector(
|
||||||
'input[name="set-column-type-choice"]:checked',
|
'input[name="set-column-type-choice"]:checked',
|
||||||
);
|
);
|
||||||
|
|
@ -367,9 +357,10 @@ function shouldShowShowAllColumns() {
|
||||||
|
|
||||||
function hasMultipleVisibleColumns(manager) {
|
function hasMultipleVisibleColumns(manager) {
|
||||||
return (
|
return (
|
||||||
Array.from(document.querySelectorAll(manager.selectors.tableHeaders)).filter(
|
Array.from(
|
||||||
(th) => th.dataset.column && th.dataset.isLinkColumn !== "1",
|
document.querySelectorAll(manager.selectors.tableHeaders),
|
||||||
).length > 1
|
).filter((th) => th.dataset.column && th.dataset.isLinkColumn !== "1")
|
||||||
|
.length > 1
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -633,32 +624,157 @@ const initDatasetteTable = function (manager) {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Add x buttons to the filter rows */
|
function filterRowSelector(manager) {
|
||||||
function addButtonsToFilterRows(manager) {
|
return manager.selectors.filterRows || manager.selectors.filterRow;
|
||||||
var x = "✖";
|
}
|
||||||
var rows = Array.from(
|
|
||||||
document.querySelectorAll(manager.selectors.filterRow),
|
function filterRowsWithControls(manager) {
|
||||||
|
return Array.from(
|
||||||
|
document.querySelectorAll(filterRowSelector(manager)),
|
||||||
).filter((el) => el.querySelector(".filter-op"));
|
).filter((el) => el.querySelector(".filter-op"));
|
||||||
rows.forEach((row) => {
|
}
|
||||||
var a = document.createElement("a");
|
|
||||||
a.setAttribute("href", "#");
|
function filterRowNumberFromName(name) {
|
||||||
a.setAttribute("aria-label", "Remove this filter");
|
var match = name && name.match(/^_filter_column_(\d+)$/);
|
||||||
a.style.textDecoration = "none";
|
return match ? parseInt(match[1], 10) : 0;
|
||||||
a.innerText = x;
|
}
|
||||||
a.addEventListener("click", (ev) => {
|
|
||||||
ev.preventDefault();
|
function nextFilterRowNumber(manager) {
|
||||||
let row = ev.target.closest("div");
|
return (
|
||||||
row.querySelector("select").value = "";
|
filterRowsWithControls(manager).reduce((max, row) => {
|
||||||
row.querySelector(".filter-op select").value = "exact";
|
var column = row.querySelector("select");
|
||||||
row.querySelector("input.filter-value").value = "";
|
return Math.max(max, filterRowNumberFromName(column && column.name));
|
||||||
ev.target.closest("a").style.display = "none";
|
}, 0) + 1
|
||||||
});
|
);
|
||||||
row.appendChild(a);
|
}
|
||||||
|
|
||||||
|
function setFilterRowNumber(row, number) {
|
||||||
|
row.querySelector("select").name = `_filter_column_${number}`;
|
||||||
|
row.querySelector(".filter-op select").name = `_filter_op_${number}`;
|
||||||
|
row.querySelector("input.filter-value").name = `_filter_value_${number}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function resetFilterRow(row) {
|
||||||
|
row.querySelector("select").value = "";
|
||||||
|
row.querySelector(".filter-op select").value = "exact";
|
||||||
|
row.querySelector("input.filter-value").value = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateFilterRowButtons(manager) {
|
||||||
|
var rows = filterRowsWithControls(manager);
|
||||||
|
rows.forEach((row, index) => {
|
||||||
|
var removeButton = row.querySelector(".filter-row-remove");
|
||||||
|
var addButton = row.querySelector(".filter-row-add");
|
||||||
var column = row.querySelector("select");
|
var column = row.querySelector("select");
|
||||||
if (!column.value) {
|
if (removeButton) {
|
||||||
a.style.display = "none";
|
removeButton.hidden = index === 0;
|
||||||
|
}
|
||||||
|
if (addButton) {
|
||||||
|
addButton.hidden = index !== rows.length - 1 || !column.value;
|
||||||
|
}
|
||||||
|
var visibleButtonCount = [removeButton, addButton].filter(
|
||||||
|
function (button) {
|
||||||
|
return button && !button.hidden;
|
||||||
|
},
|
||||||
|
).length;
|
||||||
|
row.classList.toggle(
|
||||||
|
"filter-controls-row-has-buttons",
|
||||||
|
visibleButtonCount > 0,
|
||||||
|
);
|
||||||
|
row.classList.toggle(
|
||||||
|
"filter-controls-row-one-button",
|
||||||
|
visibleButtonCount === 1,
|
||||||
|
);
|
||||||
|
row.classList.toggle(
|
||||||
|
"filter-controls-row-two-buttons",
|
||||||
|
visibleButtonCount === 2,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function cloneFilterRow(row) {
|
||||||
|
var clone = row.cloneNode(true);
|
||||||
|
clone.querySelector("select").name = "_filter_column";
|
||||||
|
clone.querySelector(".filter-op select").name = "_filter_op";
|
||||||
|
clone.querySelector("input.filter-value").name = "_filter_value";
|
||||||
|
resetFilterRow(clone);
|
||||||
|
clone
|
||||||
|
.querySelectorAll(".filter-row-icon")
|
||||||
|
.forEach((button) => button.remove());
|
||||||
|
return clone;
|
||||||
|
}
|
||||||
|
|
||||||
|
var FILTER_REMOVE_ICON_SVG = `<svg class="filter-row-remove-icon" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.1" stroke-linecap="round" stroke-linejoin="round">
|
||||||
|
<path d="M3 6h18"></path>
|
||||||
|
<path d="M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path>
|
||||||
|
<path d="M19 6l-1 14a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2L5 6"></path>
|
||||||
|
<path d="M10 11v6"></path>
|
||||||
|
<path d="M14 11v6"></path>
|
||||||
|
</svg>`;
|
||||||
|
|
||||||
|
var FILTER_ADD_ICON_SVG = `<svg class="filter-row-add-icon" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round" stroke-linejoin="round">
|
||||||
|
<path d="M5 12h14"></path>
|
||||||
|
<path d="M12 5v14"></path>
|
||||||
|
</svg>`;
|
||||||
|
|
||||||
|
function addFilterRowButtons(row, manager) {
|
||||||
|
var removeButton = document.createElement("button");
|
||||||
|
removeButton.type = "button";
|
||||||
|
removeButton.className = "filter-row-icon filter-row-remove";
|
||||||
|
removeButton.setAttribute("aria-label", "Remove this filter");
|
||||||
|
removeButton.title = "Remove this filter";
|
||||||
|
removeButton.tabIndex = 0;
|
||||||
|
removeButton.innerHTML = FILTER_REMOVE_ICON_SVG;
|
||||||
|
removeButton.addEventListener("click", (ev) => {
|
||||||
|
var row = ev.currentTarget.closest(filterRowSelector(manager));
|
||||||
|
var rows = filterRowsWithControls(manager);
|
||||||
|
var rowIndex = rows.indexOf(row);
|
||||||
|
var focusRow = rows[rowIndex + 1] || rows[rowIndex - 1] || null;
|
||||||
|
row.remove();
|
||||||
|
updateFilterRowButtons(manager);
|
||||||
|
if (focusRow) {
|
||||||
|
var focusTarget =
|
||||||
|
focusRow.querySelector(".filter-row-add:not([hidden])") ||
|
||||||
|
focusRow.querySelector("select");
|
||||||
|
if (focusTarget) {
|
||||||
|
focusTarget.focus();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
row.appendChild(removeButton);
|
||||||
|
|
||||||
|
var addButton = document.createElement("button");
|
||||||
|
addButton.type = "button";
|
||||||
|
addButton.className = "filter-row-icon filter-row-add";
|
||||||
|
addButton.setAttribute("aria-label", "Add another filter");
|
||||||
|
addButton.title = "Add another filter";
|
||||||
|
addButton.tabIndex = 0;
|
||||||
|
addButton.innerHTML = FILTER_ADD_ICON_SVG;
|
||||||
|
addButton.addEventListener("click", (ev) => {
|
||||||
|
var row = ev.currentTarget.closest(filterRowSelector(manager));
|
||||||
|
if (row.querySelector("select").name === "_filter_column") {
|
||||||
|
setFilterRowNumber(row, nextFilterRowNumber(manager));
|
||||||
|
}
|
||||||
|
var clone = cloneFilterRow(row);
|
||||||
|
addFilterRowButtons(clone, manager);
|
||||||
|
row.parentNode.insertBefore(clone, row.nextSibling);
|
||||||
|
updateFilterRowButtons(manager);
|
||||||
|
clone.querySelector("select").focus();
|
||||||
|
});
|
||||||
|
row.appendChild(addButton);
|
||||||
|
|
||||||
|
row.querySelector("select").addEventListener("change", () => {
|
||||||
|
updateFilterRowButtons(manager);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Add buttons to the filter rows */
|
||||||
|
function addButtonsToFilterRows(manager) {
|
||||||
|
var rows = filterRowsWithControls(manager);
|
||||||
|
rows.forEach((row) => {
|
||||||
|
addFilterRowButtons(row, manager);
|
||||||
|
});
|
||||||
|
updateFilterRowButtons(manager);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Set up datalist autocomplete for filter values */
|
/* Set up datalist autocomplete for filter values */
|
||||||
|
|
@ -687,11 +803,11 @@ function initAutocompleteForFilterValues(manager) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
createDataLists();
|
createDataLists();
|
||||||
// When any select with name=_filter_column changes, update the datalist
|
// When any filter column select changes, update the datalist
|
||||||
document.body.addEventListener("change", function (event) {
|
document.body.addEventListener("change", function (event) {
|
||||||
if (event.target.name === "_filter_column") {
|
if (event.target.name && event.target.name.startsWith("_filter_column")) {
|
||||||
event.target
|
event.target
|
||||||
.closest(manager.selectors.filterRow)
|
.closest(filterRowSelector(manager))
|
||||||
.querySelector(".filter-value")
|
.querySelector(".filter-value")
|
||||||
.setAttribute("list", "datalist-" + event.target.value);
|
.setAttribute("list", "datalist-" + event.target.value);
|
||||||
}
|
}
|
||||||
|
|
@ -741,10 +857,45 @@ function openColumnChooser() {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function initCountAll() {
|
||||||
|
var button = document.querySelector(".count-all");
|
||||||
|
if (!button) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
button.addEventListener("click", async function () {
|
||||||
|
var count = document.querySelector(".table-count");
|
||||||
|
var error = document.querySelector(".count-error");
|
||||||
|
button.disabled = true;
|
||||||
|
button.textContent = "Counting…";
|
||||||
|
error.textContent = "";
|
||||||
|
try {
|
||||||
|
var response = await fetch(button.dataset.countUrl + location.search, {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
Accept: "application/json",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
var data = await response.json();
|
||||||
|
if (!response.ok || !data.ok) {
|
||||||
|
throw new Error((data.errors || ["Count failed"]).join(" "));
|
||||||
|
}
|
||||||
|
count.textContent =
|
||||||
|
data.count.toLocaleString("en-US") +
|
||||||
|
(data.count === 1 ? " row" : " rows");
|
||||||
|
button.remove();
|
||||||
|
} catch (ex) {
|
||||||
|
error.textContent = ex.message || "Count failed";
|
||||||
|
button.disabled = false;
|
||||||
|
button.textContent = "count all";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Ensures Table UI is initialized only after the Manager is ready.
|
// Ensures Table UI is initialized only after the Manager is ready.
|
||||||
document.addEventListener("datasette_init", function (evt) {
|
document.addEventListener("datasette_init", function (evt) {
|
||||||
const { detail: manager } = evt;
|
const { detail: manager } = evt;
|
||||||
|
|
||||||
|
initCountAll();
|
||||||
initializeColumnActions(manager);
|
initializeColumnActions(manager);
|
||||||
|
|
||||||
// Main table
|
// Main table
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from dataclasses import dataclass
|
|
||||||
import json
|
import json
|
||||||
from typing import Any, Iterable
|
from collections.abc import Iterable
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
from .utils import tilde_encode, urlsafe_components
|
from .utils import tilde_encode, urlsafe_components
|
||||||
|
|
||||||
|
|
@ -62,7 +63,6 @@ def stored_query_to_dict(query: StoredQuery) -> dict[str, Any]:
|
||||||
"description_html": query.description_html,
|
"description_html": query.description_html,
|
||||||
"hide_sql": query.hide_sql,
|
"hide_sql": query.hide_sql,
|
||||||
"fragment": query.fragment,
|
"fragment": query.fragment,
|
||||||
"params": list(query.parameters),
|
|
||||||
"parameters": list(query.parameters),
|
"parameters": list(query.parameters),
|
||||||
"is_write": query.is_write,
|
"is_write": query.is_write,
|
||||||
"is_private": query.is_private,
|
"is_private": query.is_private,
|
||||||
|
|
@ -84,7 +84,6 @@ def stored_query_page_to_dict(page: StoredQueryPage) -> dict[str, Any]:
|
||||||
return {
|
return {
|
||||||
"queries": [stored_query_to_dict(query) for query in page.queries],
|
"queries": [stored_query_to_dict(query) for query in page.queries],
|
||||||
"next": page.next,
|
"next": page.next,
|
||||||
"has_more": page.has_more,
|
|
||||||
"limit": page.limit,
|
"limit": page.limit,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -388,7 +387,7 @@ async def count_queries(
|
||||||
OR q.sql LIKE :query_search
|
OR q.sql LIKE :query_search
|
||||||
)
|
)
|
||||||
""")
|
""")
|
||||||
params["query_search"] = "%{}%".format(q)
|
params["query_search"] = f"%{q}%"
|
||||||
if is_write is not None:
|
if is_write is not None:
|
||||||
where_clauses.append("q.is_write = :query_is_write")
|
where_clauses.append("q.is_write = :query_is_write")
|
||||||
params["query_is_write"] = int(bool(is_write))
|
params["query_is_write"] = int(bool(is_write))
|
||||||
|
|
@ -464,7 +463,7 @@ async def list_queries(
|
||||||
except ValueError:
|
except ValueError:
|
||||||
components = []
|
components = []
|
||||||
if database is None and len(components) == 3:
|
if database is None and len(components) == 3:
|
||||||
where_clauses.append("""
|
where_clauses.append(f"""
|
||||||
(
|
(
|
||||||
q.database_name > :cursor_database
|
q.database_name > :cursor_database
|
||||||
OR (
|
OR (
|
||||||
|
|
@ -478,12 +477,12 @@ async def list_queries(
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
""".format(sort_key_sql=sort_key_sql))
|
""")
|
||||||
params["cursor_database"] = components[0]
|
params["cursor_database"] = components[0]
|
||||||
params["cursor_sort_key"] = components[1]
|
params["cursor_sort_key"] = components[1]
|
||||||
params["cursor_name"] = components[2]
|
params["cursor_name"] = components[2]
|
||||||
elif database is not None and len(components) == 2:
|
elif database is not None and len(components) == 2:
|
||||||
where_clauses.append("""
|
where_clauses.append(f"""
|
||||||
(
|
(
|
||||||
{sort_key_sql} > :cursor_sort_key
|
{sort_key_sql} > :cursor_sort_key
|
||||||
OR (
|
OR (
|
||||||
|
|
@ -491,7 +490,7 @@ async def list_queries(
|
||||||
AND q.name > :cursor_name
|
AND q.name > :cursor_name
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
""".format(sort_key_sql=sort_key_sql))
|
""")
|
||||||
params["cursor_sort_key"] = components[0]
|
params["cursor_sort_key"] = components[0]
|
||||||
params["cursor_name"] = components[1]
|
params["cursor_name"] = components[1]
|
||||||
|
|
||||||
|
|
@ -504,7 +503,7 @@ async def list_queries(
|
||||||
OR q.sql LIKE :query_search
|
OR q.sql LIKE :query_search
|
||||||
)
|
)
|
||||||
""")
|
""")
|
||||||
params["query_search"] = "%{}%".format(q)
|
params["query_search"] = f"%{q}%"
|
||||||
if is_write is not None:
|
if is_write is not None:
|
||||||
where_clauses.append("q.is_write = :query_is_write")
|
where_clauses.append("q.is_write = :query_is_write")
|
||||||
params["query_is_write"] = int(bool(is_write))
|
params["query_is_write"] = int(bool(is_write))
|
||||||
|
|
|
||||||
481
datasette/telemetry.py
Normal file
481
datasette/telemetry.py
Normal file
|
|
@ -0,0 +1,481 @@
|
||||||
|
"""
|
||||||
|
OpenTelemetry integration for Datasette.
|
||||||
|
|
||||||
|
This uses `opentelemetry-api` only. Providers, exporters and sampling are
|
||||||
|
configured by whoever runs Datasette, for example `opentelemetry-instrument`.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import contextvars
|
||||||
|
import re
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
import weakref
|
||||||
|
from contextlib import contextmanager
|
||||||
|
|
||||||
|
from opentelemetry import context as otel_context_api
|
||||||
|
from opentelemetry import metrics as otel_metrics
|
||||||
|
from opentelemetry import trace as otel_trace
|
||||||
|
from opentelemetry.propagate import extract
|
||||||
|
from opentelemetry.propagators.textmap import Getter
|
||||||
|
from opentelemetry.trace import Link, SpanKind, Status, StatusCode, get_current_span
|
||||||
|
|
||||||
|
from .telemetry_registry import (
|
||||||
|
DB_NAMESPACE,
|
||||||
|
DB_SYSTEM,
|
||||||
|
ERROR_TYPE,
|
||||||
|
HTTP_REQUEST_METHOD,
|
||||||
|
HTTP_RESPONSE_STATUS_CODE,
|
||||||
|
INTERNAL_CLIENT,
|
||||||
|
M_CONNECTIONS_OPEN,
|
||||||
|
M_OPERATION_DURATION,
|
||||||
|
M_QUERIES_INTERRUPTED,
|
||||||
|
M_QUERIES_PENDING,
|
||||||
|
M_THREADS_LIMIT,
|
||||||
|
M_THREADS_QUEUE_DEPTH,
|
||||||
|
M_WRITE_QUEUE_DEPTH,
|
||||||
|
M_WRITE_QUEUE_WAIT,
|
||||||
|
OPERATION,
|
||||||
|
SERVER_ADDRESS,
|
||||||
|
URL_PATH,
|
||||||
|
URL_SCHEME,
|
||||||
|
USER_AGENT_ORIGINAL,
|
||||||
|
)
|
||||||
|
from .version import __version__
|
||||||
|
|
||||||
|
# True while code is executing within a datasette.client request. Defined
|
||||||
|
# here rather than in app.py to avoid a circular import.
|
||||||
|
_in_datasette_client = contextvars.ContextVar("in_datasette_client", default=False)
|
||||||
|
|
||||||
|
# The semantic conventions version matching the attribute names used here.
|
||||||
|
# 1.30.0 renamed `db.system` to `db.system.name`, so update this when
|
||||||
|
# renaming attributes to match a newer version.
|
||||||
|
SCHEMA_URL = "https://opentelemetry.io/schemas/1.29.0"
|
||||||
|
|
||||||
|
tracer = otel_trace.get_tracer("datasette", __version__, schema_url=SCHEMA_URL)
|
||||||
|
meter = otel_metrics.get_meter("datasette", __version__, schema_url=SCHEMA_URL)
|
||||||
|
|
||||||
|
MAX_SQL_LENGTH = 2048
|
||||||
|
|
||||||
|
|
||||||
|
def sql_attribute(sql: str) -> str:
|
||||||
|
"Truncate SQL text so it is safe to attach to a span as an attribute."
|
||||||
|
sql = sql.strip()
|
||||||
|
if len(sql) <= MAX_SQL_LENGTH:
|
||||||
|
return sql
|
||||||
|
return sql[:MAX_SQL_LENGTH] + "…[truncated]"
|
||||||
|
|
||||||
|
|
||||||
|
def callback_name(fn) -> str:
|
||||||
|
"""
|
||||||
|
The name recorded as `datasette.callback` for a callback-style call.
|
||||||
|
|
||||||
|
Falls back to the type name for callables such as `functools.partial`
|
||||||
|
that have no `__qualname__`.
|
||||||
|
"""
|
||||||
|
return getattr(fn, "__qualname__", type(fn).__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def linked_root_span_kwargs(context=None):
|
||||||
|
"""
|
||||||
|
Keyword arguments that start a new root span with a ``Link`` back to
|
||||||
|
the current span.
|
||||||
|
|
||||||
|
Use this for work that can outlive the span that caused it, such as a
|
||||||
|
background task or a ``block=False`` write.
|
||||||
|
|
||||||
|
Pass ``context`` to link to the span in a previously captured context
|
||||||
|
instead of the current one. If there is no valid span, no link is added.
|
||||||
|
|
||||||
|
Works with any tracer::
|
||||||
|
|
||||||
|
with my_tracer.start_as_current_span(
|
||||||
|
"myplugin.job", **linked_root_span_kwargs()
|
||||||
|
):
|
||||||
|
...
|
||||||
|
"""
|
||||||
|
cause = get_current_span(context).get_span_context()
|
||||||
|
links = [Link(cause)] if cause.is_valid else []
|
||||||
|
return {"context": otel_context_api.Context(), "links": links}
|
||||||
|
|
||||||
|
|
||||||
|
# Keywords that can be recorded as db.operation.name. SQL can be supplied by
|
||||||
|
# users, so an allowlist keeps the number of distinct values small.
|
||||||
|
DB_OPERATION_ALLOWLIST = frozenset(
|
||||||
|
{
|
||||||
|
"SELECT",
|
||||||
|
"INSERT",
|
||||||
|
"UPDATE",
|
||||||
|
"DELETE",
|
||||||
|
"CREATE",
|
||||||
|
"DROP",
|
||||||
|
"ALTER",
|
||||||
|
"PRAGMA",
|
||||||
|
"EXPLAIN",
|
||||||
|
"REPLACE",
|
||||||
|
"VACUUM",
|
||||||
|
"ANALYZE",
|
||||||
|
"WITH",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
_LEADING_KEYWORD = re.compile(r"^\s*([A-Za-z]+)")
|
||||||
|
|
||||||
|
|
||||||
|
def sql_operation_name(sql: str) -> str | None:
|
||||||
|
"""
|
||||||
|
The statement's leading keyword if it is in the allowlist, else None.
|
||||||
|
|
||||||
|
Statements that start with a comment or "(" return None. Statements
|
||||||
|
starting with a CTE return `WITH`. Only call this for a single statement.
|
||||||
|
"""
|
||||||
|
match = _LEADING_KEYWORD.match(sql)
|
||||||
|
if not match:
|
||||||
|
return None
|
||||||
|
keyword = match.group(1).upper()
|
||||||
|
if keyword in DB_OPERATION_ALLOWLIST:
|
||||||
|
return keyword
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
# --- The HTTP request span ------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
class _ScopeHeadersGetter(Getter):
|
||||||
|
"Read W3C trace context from an ASGI scope's headers."
|
||||||
|
|
||||||
|
def get(self, carrier, key):
|
||||||
|
wanted = key.lower().encode("latin-1")
|
||||||
|
values = [v.decode("latin-1") for k, v in carrier if k.lower() == wanted]
|
||||||
|
return values or None
|
||||||
|
|
||||||
|
def keys(self, carrier):
|
||||||
|
return [k.decode("latin-1") for k, _ in carrier]
|
||||||
|
|
||||||
|
|
||||||
|
_HEADERS_GETTER = _ScopeHeadersGetter()
|
||||||
|
|
||||||
|
|
||||||
|
# Methods defined by RFC 9110 plus PATCH (RFC 5789). Anything else is
|
||||||
|
# recorded as `_OTHER`, as recommended by semantic conventions.
|
||||||
|
_KNOWN_METHODS = frozenset(
|
||||||
|
{"GET", "HEAD", "POST", "PUT", "DELETE", "CONNECT", "OPTIONS", "TRACE", "PATCH"}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def clamp_http_method(method):
|
||||||
|
"The request method if it is one we recognise, else ``_OTHER``."
|
||||||
|
method = (method or "").upper()
|
||||||
|
return method if method in _KNOWN_METHODS else "_OTHER"
|
||||||
|
|
||||||
|
|
||||||
|
def _first_header(headers, name):
|
||||||
|
"The first value of a header, decoded, or None."
|
||||||
|
for key, value in headers:
|
||||||
|
if key.lower() == name:
|
||||||
|
return value.decode("latin-1")
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _url_path(scope):
|
||||||
|
"""
|
||||||
|
The request path, with any query string removed.
|
||||||
|
|
||||||
|
Prefers `raw_path`, which preserves encoded slashes in database and
|
||||||
|
table names. Some clients include the query string in `raw_path`, so
|
||||||
|
that is stripped as well.
|
||||||
|
"""
|
||||||
|
raw_path = scope.get("raw_path")
|
||||||
|
if raw_path:
|
||||||
|
if isinstance(raw_path, bytes):
|
||||||
|
raw_path = raw_path.decode("latin-1")
|
||||||
|
return raw_path.split("?", 1)[0]
|
||||||
|
return scope.get("path", "")
|
||||||
|
|
||||||
|
|
||||||
|
# The request span is passed to the router in the ASGI scope, because a
|
||||||
|
# plugin's asgi_wrapper() middleware may have made its own span current.
|
||||||
|
# Absent if the span is not recording.
|
||||||
|
REQUEST_SPAN_SCOPE_KEY = "datasette.telemetry.request_span"
|
||||||
|
|
||||||
|
|
||||||
|
def request_span(scope):
|
||||||
|
"""
|
||||||
|
The recording request span for an ASGI scope, or None.
|
||||||
|
|
||||||
|
Falls back to the current span, for when Datasette is running under
|
||||||
|
other instrumentation.
|
||||||
|
"""
|
||||||
|
span = scope.get(REQUEST_SPAN_SCOPE_KEY)
|
||||||
|
if span is None:
|
||||||
|
span = otel_trace.get_current_span()
|
||||||
|
return span if span.is_recording() else None
|
||||||
|
|
||||||
|
|
||||||
|
class TelemetryMiddleware:
|
||||||
|
"""
|
||||||
|
One `SpanKind.SERVER` span per HTTP request.
|
||||||
|
|
||||||
|
The span ends after the full response, including any streamed body,
|
||||||
|
has been sent.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, app):
|
||||||
|
self.app = app
|
||||||
|
|
||||||
|
async def __call__(self, scope, receive, send):
|
||||||
|
# Pass lifespan and websocket scopes straight through
|
||||||
|
if scope["type"] != "http":
|
||||||
|
await self.app(scope, receive, send)
|
||||||
|
return
|
||||||
|
headers = scope.get("headers") or []
|
||||||
|
# Uses the global propagator, configured with OTEL_PROPAGATORS
|
||||||
|
context = extract(headers, getter=_HEADERS_GETTER)
|
||||||
|
method = clamp_http_method(scope.get("method", ""))
|
||||||
|
# Renamed to include the route once routing has happened
|
||||||
|
with tracer.start_as_current_span(
|
||||||
|
method, context=context, kind=SpanKind.SERVER
|
||||||
|
) as span:
|
||||||
|
if not span.is_recording():
|
||||||
|
# No provider installed, or the trace was not sampled
|
||||||
|
await self.app(scope, receive, send)
|
||||||
|
return
|
||||||
|
span.set_attribute(HTTP_REQUEST_METHOD, method)
|
||||||
|
span.set_attribute(URL_PATH, _url_path(scope))
|
||||||
|
scheme = scope.get("scheme")
|
||||||
|
if scheme:
|
||||||
|
span.set_attribute(URL_SCHEME, scheme)
|
||||||
|
host = _first_header(headers, b"host")
|
||||||
|
if host:
|
||||||
|
span.set_attribute(SERVER_ADDRESS, host)
|
||||||
|
user_agent = _first_header(headers, b"user-agent")
|
||||||
|
if user_agent:
|
||||||
|
span.set_attribute(USER_AGENT_ORIGINAL, user_agent)
|
||||||
|
if _in_datasette_client.get():
|
||||||
|
span.set_attribute(INTERNAL_CLIENT, True)
|
||||||
|
|
||||||
|
scope = dict(scope, **{REQUEST_SPAN_SCOPE_KEY: span})
|
||||||
|
|
||||||
|
# Some responses are sent without a Response object, so the
|
||||||
|
# status is captured by wrapping send()
|
||||||
|
status_holder = {}
|
||||||
|
|
||||||
|
async def wrapped_send(message):
|
||||||
|
if (
|
||||||
|
message["type"] == "http.response.start"
|
||||||
|
and "status" not in status_holder
|
||||||
|
):
|
||||||
|
status_holder["status"] = message["status"]
|
||||||
|
await send(message)
|
||||||
|
|
||||||
|
escaped = False
|
||||||
|
try:
|
||||||
|
await self.app(scope, receive, wrapped_send)
|
||||||
|
except BaseException as exception:
|
||||||
|
# Includes asyncio.CancelledError when a client disconnects
|
||||||
|
escaped = True
|
||||||
|
span.set_attribute(ERROR_TYPE, type(exception).__name__)
|
||||||
|
span.set_status(Status(StatusCode.ERROR, str(exception)))
|
||||||
|
raise
|
||||||
|
finally:
|
||||||
|
status = status_holder.get("status")
|
||||||
|
if status is not None:
|
||||||
|
span.set_attribute(HTTP_RESPONSE_STATUS_CODE, status)
|
||||||
|
# 4xx responses are not errors for a server span. If an
|
||||||
|
# exception escaped, keep its class name as error.type.
|
||||||
|
if status >= 500 and not escaped:
|
||||||
|
span.set_status(Status(StatusCode.ERROR))
|
||||||
|
span.set_attribute(ERROR_TYPE, str(status))
|
||||||
|
|
||||||
|
|
||||||
|
# --- Metrics --------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
def _duration_attributes(database_name, operation):
|
||||||
|
return {
|
||||||
|
DB_SYSTEM: "sqlite",
|
||||||
|
DB_NAMESPACE: database_name,
|
||||||
|
OPERATION: operation,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Instruments use plain text descriptions. The registry entries have longer
|
||||||
|
# reStructuredText descriptions for the documentation.
|
||||||
|
|
||||||
|
sql_operation_duration = meter.create_histogram(
|
||||||
|
M_OPERATION_DURATION,
|
||||||
|
unit=M_OPERATION_DURATION.unit,
|
||||||
|
description="Duration of a SQL operation issued by Datasette",
|
||||||
|
explicit_bucket_boundaries_advisory=M_OPERATION_DURATION.buckets,
|
||||||
|
)
|
||||||
|
|
||||||
|
write_queue_wait = meter.create_histogram(
|
||||||
|
M_WRITE_QUEUE_WAIT,
|
||||||
|
unit=M_WRITE_QUEUE_WAIT.unit,
|
||||||
|
description=(
|
||||||
|
"Time a write spent queued behind the single write thread for its database"
|
||||||
|
),
|
||||||
|
explicit_bucket_boundaries_advisory=M_WRITE_QUEUE_WAIT.buckets,
|
||||||
|
)
|
||||||
|
|
||||||
|
queries_interrupted = meter.create_counter(
|
||||||
|
M_QUERIES_INTERRUPTED,
|
||||||
|
unit=M_QUERIES_INTERRUPTED.unit,
|
||||||
|
description="Queries cancelled for exceeding sql_time_limit_ms",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def record_operation_duration(database_name, operation):
|
||||||
|
"""
|
||||||
|
Record `db.client.operation.duration` for one SQL operation.
|
||||||
|
|
||||||
|
Sets `error.type` to the exception class on failure. For a `block=False`
|
||||||
|
write this measures the time taken to enqueue the write.
|
||||||
|
"""
|
||||||
|
attributes = _duration_attributes(database_name, operation)
|
||||||
|
started = time.perf_counter()
|
||||||
|
try:
|
||||||
|
yield
|
||||||
|
except BaseException as exception:
|
||||||
|
attributes[ERROR_TYPE] = type(exception).__qualname__
|
||||||
|
raise
|
||||||
|
finally:
|
||||||
|
sql_operation_duration.record(time.perf_counter() - started, attributes)
|
||||||
|
|
||||||
|
|
||||||
|
def record_write_queue_wait(database_name, waited_ns):
|
||||||
|
write_queue_wait.record(waited_ns / 1e9, {DB_NAMESPACE: database_name})
|
||||||
|
|
||||||
|
|
||||||
|
def record_query_interrupted(database_name):
|
||||||
|
queries_interrupted.add(1, {DB_NAMESPACE: database_name})
|
||||||
|
|
||||||
|
|
||||||
|
# Live Datasette instances reported by the gauges below. The lock is needed
|
||||||
|
# because gauge callbacks run on the SDK's collection thread.
|
||||||
|
#
|
||||||
|
# The pool gauges do not identify which instance they came from, so they
|
||||||
|
# are only meaningful for a process running a single Datasette instance.
|
||||||
|
_live_datasettes = weakref.WeakSet()
|
||||||
|
_live_datasettes_lock = threading.Lock()
|
||||||
|
|
||||||
|
|
||||||
|
def register_datasette(ds):
|
||||||
|
"Start reporting pool/queue gauges for this Datasette instance."
|
||||||
|
with _live_datasettes_lock:
|
||||||
|
_live_datasettes.add(ds)
|
||||||
|
|
||||||
|
|
||||||
|
def unregister_datasette(ds):
|
||||||
|
"Stop reporting gauges for an instance that has been closed."
|
||||||
|
with _live_datasettes_lock:
|
||||||
|
_live_datasettes.discard(ds)
|
||||||
|
|
||||||
|
|
||||||
|
def _live_instances():
|
||||||
|
with _live_datasettes_lock:
|
||||||
|
return list(_live_datasettes)
|
||||||
|
|
||||||
|
|
||||||
|
def _databases_of(ds):
|
||||||
|
"Every Database attached to an instance, including the internal database."
|
||||||
|
databases = list(ds.databases.values())
|
||||||
|
internal = getattr(ds, "_internal_database", None)
|
||||||
|
if internal is not None:
|
||||||
|
databases.append(internal)
|
||||||
|
return databases
|
||||||
|
|
||||||
|
|
||||||
|
def observe_sql_thread_limit(options=None):
|
||||||
|
"Size of the shared read-query thread pool (the num_sql_threads setting)."
|
||||||
|
for ds in _live_instances():
|
||||||
|
if ds.executor is None:
|
||||||
|
# num_sql_threads=0 - queries run on the event loop, no pool.
|
||||||
|
continue
|
||||||
|
yield otel_metrics.Observation(ds.setting("num_sql_threads"), {})
|
||||||
|
|
||||||
|
|
||||||
|
def observe_sql_thread_queue_depth(options=None):
|
||||||
|
"""
|
||||||
|
Read queries waiting for a free thread in the shared pool.
|
||||||
|
|
||||||
|
`_work_queue` is a private attribute of ThreadPoolExecutor, so this
|
||||||
|
reports nothing if it is missing.
|
||||||
|
"""
|
||||||
|
for ds in _live_instances():
|
||||||
|
if ds.executor is None:
|
||||||
|
continue
|
||||||
|
work_queue = getattr(ds.executor, "_work_queue", None)
|
||||||
|
if work_queue is None:
|
||||||
|
continue
|
||||||
|
yield otel_metrics.Observation(work_queue.qsize(), {})
|
||||||
|
|
||||||
|
|
||||||
|
def observe_pending_queries(options=None):
|
||||||
|
"""
|
||||||
|
Read queries submitted to the pool and not yet finished, per database.
|
||||||
|
|
||||||
|
Reads `len()` without `_pending_execute_futures_lock` to avoid blocking
|
||||||
|
queries.
|
||||||
|
"""
|
||||||
|
for ds in _live_instances():
|
||||||
|
for db in _databases_of(ds):
|
||||||
|
yield otel_metrics.Observation(
|
||||||
|
len(db._pending_execute_futures), {DB_NAMESPACE: db.name}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def observe_write_queue_depth(options=None):
|
||||||
|
"Writes queued behind the single write thread, per database."
|
||||||
|
for ds in _live_instances():
|
||||||
|
for db in _databases_of(ds):
|
||||||
|
write_queue = db._write_queue
|
||||||
|
if write_queue is None:
|
||||||
|
# No write has ever been queued for this database.
|
||||||
|
continue
|
||||||
|
yield otel_metrics.Observation(write_queue.qsize(), {DB_NAMESPACE: db.name})
|
||||||
|
|
||||||
|
|
||||||
|
def observe_open_connections(options=None):
|
||||||
|
"Open SQLite connections tracked for closing, per database."
|
||||||
|
for ds in _live_instances():
|
||||||
|
for db in _databases_of(ds):
|
||||||
|
yield otel_metrics.Observation(
|
||||||
|
len(db._all_connections), {DB_NAMESPACE: db.name}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
sql_thread_limit_gauge = meter.create_observable_gauge(
|
||||||
|
M_THREADS_LIMIT,
|
||||||
|
callbacks=[observe_sql_thread_limit],
|
||||||
|
unit=M_THREADS_LIMIT.unit,
|
||||||
|
description="Maximum concurrent read queries (the num_sql_threads setting)",
|
||||||
|
)
|
||||||
|
|
||||||
|
sql_thread_queue_depth_gauge = meter.create_observable_gauge(
|
||||||
|
M_THREADS_QUEUE_DEPTH,
|
||||||
|
callbacks=[observe_sql_thread_queue_depth],
|
||||||
|
unit=M_THREADS_QUEUE_DEPTH.unit,
|
||||||
|
description="Read queries waiting for a free thread in the shared SQL pool",
|
||||||
|
)
|
||||||
|
|
||||||
|
pending_queries_gauge = meter.create_observable_gauge(
|
||||||
|
M_QUERIES_PENDING,
|
||||||
|
callbacks=[observe_pending_queries],
|
||||||
|
unit=M_QUERIES_PENDING.unit,
|
||||||
|
description="Read queries submitted to the pool and not yet complete",
|
||||||
|
)
|
||||||
|
|
||||||
|
write_queue_depth_gauge = meter.create_observable_gauge(
|
||||||
|
M_WRITE_QUEUE_DEPTH,
|
||||||
|
callbacks=[observe_write_queue_depth],
|
||||||
|
unit=M_WRITE_QUEUE_DEPTH.unit,
|
||||||
|
description="Writes queued behind a database's single write thread",
|
||||||
|
)
|
||||||
|
|
||||||
|
open_connections_gauge = meter.create_observable_gauge(
|
||||||
|
M_CONNECTIONS_OPEN,
|
||||||
|
callbacks=[observe_open_connections],
|
||||||
|
unit=M_CONNECTIONS_OPEN.unit,
|
||||||
|
description="Open SQLite connections tracked for closing",
|
||||||
|
)
|
||||||
502
datasette/telemetry_registry.py
Normal file
502
datasette/telemetry_registry.py
Normal file
|
|
@ -0,0 +1,502 @@
|
||||||
|
"""
|
||||||
|
Every span, metric and attribute that Datasette emits.
|
||||||
|
|
||||||
|
These entries are used by the instrumentation code, by `docs/telemetry_doc.py`
|
||||||
|
to generate the documentation, and by `tests/test_telemetry_registry.py` to
|
||||||
|
check that the emitted telemetry matches the registry.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from opentelemetry.trace import SpanKind
|
||||||
|
|
||||||
|
|
||||||
|
class Attribute(str):
|
||||||
|
"""
|
||||||
|
A span attribute key, carrying its own documentation.
|
||||||
|
|
||||||
|
Subclasses `str` so it can be handed straight to `set_attribute()`.
|
||||||
|
|
||||||
|
Part of Datasette's public plugin API - plugins declare their own
|
||||||
|
telemetry registries with these classes. See the "Telemetry for plugin
|
||||||
|
authors" documentation.
|
||||||
|
"""
|
||||||
|
|
||||||
|
__slots__ = ("description", "optional", "values")
|
||||||
|
|
||||||
|
def __new__(cls, name, description, optional=False, values=None):
|
||||||
|
self = super().__new__(cls, name)
|
||||||
|
self.description = description
|
||||||
|
self.optional = optional
|
||||||
|
# The allowed values for this attribute, or None to allow any value
|
||||||
|
self.values = frozenset(values) if values is not None else None
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __reduce__(self):
|
||||||
|
# Copies and pickles become a plain str, since __new__ requires the
|
||||||
|
# extra arguments. ConsoleMetricExporter deepcopies attribute keys.
|
||||||
|
return (str, (str(self),))
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f"Attribute({str(self)!r})"
|
||||||
|
|
||||||
|
|
||||||
|
class SpanName(str):
|
||||||
|
"""A span name, carrying its documentation and the attributes it may set.
|
||||||
|
|
||||||
|
Part of Datasette's public plugin API, like `Attribute`.
|
||||||
|
"""
|
||||||
|
|
||||||
|
__slots__ = ("attributes", "description", "dynamic", "kind", "prefix")
|
||||||
|
|
||||||
|
def __new__(
|
||||||
|
cls,
|
||||||
|
name,
|
||||||
|
description,
|
||||||
|
attributes=(),
|
||||||
|
prefix=False,
|
||||||
|
dynamic=False,
|
||||||
|
kind=SpanKind.INTERNAL,
|
||||||
|
):
|
||||||
|
self = super().__new__(cls, name)
|
||||||
|
self.description = description
|
||||||
|
self.attributes = tuple(attributes)
|
||||||
|
# Match emitted names that start with this prefix, for names with a
|
||||||
|
# variable suffix such as SpanName("chat ", ..., prefix=True)
|
||||||
|
self.prefix = prefix
|
||||||
|
# The emitted name is built at runtime, so `span_for()` matches it by
|
||||||
|
# span kind. The entry's string is a template for the documentation.
|
||||||
|
self.dynamic = dynamic
|
||||||
|
self.kind = kind
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __reduce__(self):
|
||||||
|
# See Attribute.__reduce__.
|
||||||
|
return (str, (str(self),))
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f"SpanName({str(self)!r})"
|
||||||
|
|
||||||
|
|
||||||
|
class MetricName(str):
|
||||||
|
"A metric name, carrying its instrument kind, unit and attributes."
|
||||||
|
|
||||||
|
__slots__ = ("attributes", "buckets", "description", "kind", "unit")
|
||||||
|
|
||||||
|
def __new__(cls, name, kind, unit, description, attributes=(), buckets=None):
|
||||||
|
self = super().__new__(cls, name)
|
||||||
|
self.kind = kind
|
||||||
|
self.unit = unit
|
||||||
|
self.description = description
|
||||||
|
self.attributes = tuple(attributes)
|
||||||
|
# Explicit bucket boundaries, for histograms only
|
||||||
|
self.buckets = tuple(buckets) if buckets is not None else None
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __reduce__(self):
|
||||||
|
# See Attribute.__reduce__.
|
||||||
|
return (str, (str(self),))
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f"MetricName({str(self)!r})"
|
||||||
|
|
||||||
|
|
||||||
|
COUNTER = "Counter"
|
||||||
|
UPDOWN_COUNTER = "UpDownCounter"
|
||||||
|
HISTOGRAM = "Histogram"
|
||||||
|
GAUGE = "Observable gauge"
|
||||||
|
|
||||||
|
|
||||||
|
# --- Attributes -----------------------------------------------------------
|
||||||
|
|
||||||
|
HTTP_REQUEST_METHOD = Attribute(
|
||||||
|
"http.request.method",
|
||||||
|
"The HTTP request method. Methods outside the nine defined by RFC 9110 "
|
||||||
|
"and RFC 5789 are recorded as ``_OTHER``.",
|
||||||
|
)
|
||||||
|
HTTP_RESPONSE_STATUS_CODE = Attribute(
|
||||||
|
"http.response.status_code",
|
||||||
|
"The HTTP response status code. Omitted if no response was started.",
|
||||||
|
optional=True,
|
||||||
|
)
|
||||||
|
HTTP_ROUTE = Attribute(
|
||||||
|
"http.route",
|
||||||
|
"The regular expression for the matched route, for example "
|
||||||
|
"``/(?P<database>[^\\/\\.]+)/(?P<table>[^\\/\\.]+)(\\.(?P<format>\\w+))?$`` "
|
||||||
|
"for a table page. Use this attribute to group requests by route. "
|
||||||
|
"Omitted when no route matches.",
|
||||||
|
optional=True,
|
||||||
|
)
|
||||||
|
URL_PATH = Attribute(
|
||||||
|
"url.path",
|
||||||
|
"The URL path, excluding the query string.",
|
||||||
|
)
|
||||||
|
URL_SCHEME = Attribute("url.scheme", "``http`` or ``https``.")
|
||||||
|
SERVER_ADDRESS = Attribute(
|
||||||
|
"server.address",
|
||||||
|
"The ``Host`` header, including any ``:port`` suffix. This value is "
|
||||||
|
"supplied by the client.",
|
||||||
|
optional=True,
|
||||||
|
)
|
||||||
|
USER_AGENT_ORIGINAL = Attribute(
|
||||||
|
"user_agent.original",
|
||||||
|
"The ``User-Agent`` header, verbatim. Omitted if the client sent none.",
|
||||||
|
optional=True,
|
||||||
|
)
|
||||||
|
INTERNAL_CLIENT = Attribute(
|
||||||
|
"datasette.internal_client",
|
||||||
|
"``True`` for requests made through ``datasette.client``. Calls made "
|
||||||
|
"inside another request produce a nested ``SERVER`` span. Filter on "
|
||||||
|
"this attribute to exclude internal requests from request counts. "
|
||||||
|
"Omitted for requests received over the network.",
|
||||||
|
optional=True,
|
||||||
|
)
|
||||||
|
ERROR_TYPE = Attribute(
|
||||||
|
"error.type",
|
||||||
|
"The exception class name for a failed operation. On HTTP spans, also "
|
||||||
|
"set to the status code as a string for 5xx responses. A 4xx response "
|
||||||
|
"alone does not set this attribute or an error status.",
|
||||||
|
optional=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
DB_SYSTEM = Attribute("db.system", "Always ``sqlite``.")
|
||||||
|
DB_NAMESPACE = Attribute("db.namespace", "Name of the database being queried.")
|
||||||
|
OPERATION = Attribute(
|
||||||
|
"datasette.operation",
|
||||||
|
"Whether the operation was a read or a write.",
|
||||||
|
values={"read", "write"},
|
||||||
|
)
|
||||||
|
DB_QUERY_TEXT = Attribute(
|
||||||
|
"db.query.text",
|
||||||
|
"The SQL, truncated to 2048 characters. Bound parameter values are not "
|
||||||
|
"recorded. For callback methods, ``datasette.callback`` is recorded instead.",
|
||||||
|
optional=True,
|
||||||
|
)
|
||||||
|
CALLBACK = Attribute(
|
||||||
|
"datasette.callback",
|
||||||
|
"The qualified name of the Python callable passed to ``execute_fn()``, "
|
||||||
|
"``execute_write_fn()`` or ``execute_isolated_fn()``, for example "
|
||||||
|
"``TableInsertView.post.<locals>.insert_or_upsert_rows``. Set instead of "
|
||||||
|
"``db.query.text``. Lambdas appear as ``<lambda>``; use a named function "
|
||||||
|
"for a more descriptive span.",
|
||||||
|
optional=True,
|
||||||
|
)
|
||||||
|
DB_OPERATION_NAME = Attribute(
|
||||||
|
"db.operation.name",
|
||||||
|
"The statement's leading keyword, such as ``SELECT``, ``INSERT`` or "
|
||||||
|
"``CREATE``, if it matches the supported allowlist. Statements beginning "
|
||||||
|
"with a common table expression report ``WITH``. Omitted for unrecognized "
|
||||||
|
"keywords and ``execute_write_script()``.",
|
||||||
|
optional=True,
|
||||||
|
)
|
||||||
|
PARAM_COUNT = Attribute(
|
||||||
|
"datasette.param_count",
|
||||||
|
"Number of bound parameters. Recorded instead of the values themselves.",
|
||||||
|
optional=True,
|
||||||
|
)
|
||||||
|
PARAM_SETS = Attribute(
|
||||||
|
"datasette.param_sets",
|
||||||
|
"Number of parameter sets consumed by ``execute_write_many()``. "
|
||||||
|
"The parameter values are not recorded.",
|
||||||
|
optional=True,
|
||||||
|
)
|
||||||
|
TIME_LIMIT_MS = Attribute(
|
||||||
|
"datasette.time_limit_ms",
|
||||||
|
"Time limit applied to the read query, in milliseconds: "
|
||||||
|
":ref:`setting_sql_time_limit_ms` or a shorter ``custom_time_limit``.",
|
||||||
|
optional=True,
|
||||||
|
)
|
||||||
|
ROWS_RETURNED = Attribute(
|
||||||
|
"datasette.rows_returned",
|
||||||
|
"Number of rows returned by a successful read query.",
|
||||||
|
optional=True,
|
||||||
|
)
|
||||||
|
TRUNCATED = Attribute(
|
||||||
|
"datasette.truncated",
|
||||||
|
"True if the result was cut short by :ref:`setting_max_returned_rows`.",
|
||||||
|
optional=True,
|
||||||
|
)
|
||||||
|
INTERRUPTED = Attribute(
|
||||||
|
"datasette.interrupted",
|
||||||
|
"True if the query exceeded its time limit. The span status is set to "
|
||||||
|
"``ERROR`` unless the caller used a ``custom_time_limit`` shorter than "
|
||||||
|
":ref:`setting_sql_time_limit_ms`, in which case the status is left unset.",
|
||||||
|
optional=True,
|
||||||
|
)
|
||||||
|
SQL_ERROR_SUPPRESSED = Attribute(
|
||||||
|
"datasette.sql_error_suppressed",
|
||||||
|
"True for a non-timeout SQL error with ``log_sql_errors=False``. The "
|
||||||
|
"exception is still raised, but the span status is left unset.",
|
||||||
|
optional=True,
|
||||||
|
)
|
||||||
|
EXECUTESCRIPT = Attribute(
|
||||||
|
"datasette.executescript",
|
||||||
|
"True for ``execute_write_script()``, which runs multiple statements.",
|
||||||
|
optional=True,
|
||||||
|
)
|
||||||
|
EXECUTEMANY = Attribute(
|
||||||
|
"datasette.executemany",
|
||||||
|
"True for ``execute_write_many()``, which runs one statement against many "
|
||||||
|
"parameter sets.",
|
||||||
|
optional=True,
|
||||||
|
)
|
||||||
|
ISOLATED_CONNECTION = Attribute(
|
||||||
|
"datasette.isolated_connection",
|
||||||
|
"True if the write ran on its own connection rather than the shared write "
|
||||||
|
"connection.",
|
||||||
|
)
|
||||||
|
TRANSACTION = Attribute(
|
||||||
|
"datasette.transaction",
|
||||||
|
"False for statements such as ``VACUUM`` that cannot run inside a transaction.",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# --- Spans ----------------------------------------------------------------
|
||||||
|
|
||||||
|
HTTP_REQUEST = SpanName(
|
||||||
|
"{http.request.method} {http.route}",
|
||||||
|
"One span per HTTP request, containing spans from plugin middleware and "
|
||||||
|
"database operations. Named for the HTTP method and matched route, or "
|
||||||
|
"just the method if no route matches. Incoming ``traceparent`` headers "
|
||||||
|
"are extracted using the global propagator to continue the caller's "
|
||||||
|
"trace. Incoming ``baggage`` is not propagated into plugin or downstream "
|
||||||
|
"context in this release. Set ``OTEL_PROPAGATORS=none`` to disable "
|
||||||
|
"extraction. For public instances, strip trace context headers at your "
|
||||||
|
"proxy if callers should not supply trace context.",
|
||||||
|
(
|
||||||
|
HTTP_REQUEST_METHOD,
|
||||||
|
HTTP_ROUTE,
|
||||||
|
URL_PATH,
|
||||||
|
URL_SCHEME,
|
||||||
|
SERVER_ADDRESS,
|
||||||
|
USER_AGENT_ORIGINAL,
|
||||||
|
HTTP_RESPONSE_STATUS_CODE,
|
||||||
|
ERROR_TYPE,
|
||||||
|
INTERNAL_CLIENT,
|
||||||
|
),
|
||||||
|
dynamic=True,
|
||||||
|
kind=SpanKind.SERVER,
|
||||||
|
)
|
||||||
|
|
||||||
|
DB_QUERY = SpanName(
|
||||||
|
"db.query",
|
||||||
|
"A SQL operation, including time spent queued for a worker thread. For "
|
||||||
|
"``block=False`` writes, the span ends after the write is queued. "
|
||||||
|
"Callback methods record ``datasette.callback`` in place of ``db.query.text``.",
|
||||||
|
(
|
||||||
|
DB_SYSTEM,
|
||||||
|
DB_NAMESPACE,
|
||||||
|
DB_QUERY_TEXT,
|
||||||
|
CALLBACK,
|
||||||
|
DB_OPERATION_NAME,
|
||||||
|
PARAM_COUNT,
|
||||||
|
PARAM_SETS,
|
||||||
|
TIME_LIMIT_MS,
|
||||||
|
ROWS_RETURNED,
|
||||||
|
TRUNCATED,
|
||||||
|
INTERRUPTED,
|
||||||
|
SQL_ERROR_SUPPRESSED,
|
||||||
|
EXECUTESCRIPT,
|
||||||
|
EXECUTEMANY,
|
||||||
|
),
|
||||||
|
kind=SpanKind.CLIENT,
|
||||||
|
)
|
||||||
|
|
||||||
|
DB_QUERY_EXECUTE = SpanName(
|
||||||
|
"db.query.execute",
|
||||||
|
"The read executing inside a SQL worker thread. Child of ``db.query``; the "
|
||||||
|
"gap between the two is time spent waiting for a thread.",
|
||||||
|
)
|
||||||
|
|
||||||
|
DB_WRITE_QUEUE_WAIT = SpanName(
|
||||||
|
"db.write.queue_wait",
|
||||||
|
"Time a write spent waiting in its database's write queue. For "
|
||||||
|
"``block=True``, this is a child of ``db.query``. For ``block=False``, "
|
||||||
|
"it is a root span linked to the span that queued the write, since the "
|
||||||
|
"write can outlive that request.",
|
||||||
|
)
|
||||||
|
|
||||||
|
DB_WRITE_EXECUTE = SpanName(
|
||||||
|
"db.write.execute",
|
||||||
|
"The write executing on the write thread. For ``block=True``, this is "
|
||||||
|
"a child of ``db.query``. For ``block=False``, it is a root span linked "
|
||||||
|
"to the span that queued the write.",
|
||||||
|
(ISOLATED_CONNECTION, TRANSACTION),
|
||||||
|
)
|
||||||
|
|
||||||
|
STARTUP = SpanName(
|
||||||
|
"datasette.startup",
|
||||||
|
"Startup work performed by ``invoke_startup()``, including registration "
|
||||||
|
"hooks, schema catalog updates, saved queries, column type configuration "
|
||||||
|
"and the ``startup`` hook. Runs during instance startup, either before "
|
||||||
|
"serving requests or as part of the first request.",
|
||||||
|
)
|
||||||
|
|
||||||
|
SPANS = (
|
||||||
|
HTTP_REQUEST,
|
||||||
|
DB_QUERY,
|
||||||
|
DB_QUERY_EXECUTE,
|
||||||
|
DB_WRITE_QUEUE_WAIT,
|
||||||
|
DB_WRITE_EXECUTE,
|
||||||
|
STARTUP,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def span_for(emitted_name, kind=None, spans=None):
|
||||||
|
"""
|
||||||
|
Resolve an emitted span name to its registry entry, or None.
|
||||||
|
|
||||||
|
Exact matches take precedence over `prefix=True` entries, which take
|
||||||
|
precedence over `dynamic=True` entries matched by `kind`.
|
||||||
|
|
||||||
|
`spans` defaults to Datasette's own registry.
|
||||||
|
"""
|
||||||
|
if spans is None:
|
||||||
|
spans = SPANS
|
||||||
|
for span in spans:
|
||||||
|
if span.dynamic:
|
||||||
|
continue
|
||||||
|
if emitted_name == span:
|
||||||
|
return span
|
||||||
|
for span in spans:
|
||||||
|
if span.prefix and emitted_name.startswith(span):
|
||||||
|
return span
|
||||||
|
if kind is not None:
|
||||||
|
for span in spans:
|
||||||
|
if span.dynamic and span.kind == kind:
|
||||||
|
return span
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def metric_for(emitted_name, metrics=None):
|
||||||
|
"""
|
||||||
|
Resolve an emitted metric name to its registry entry, or None.
|
||||||
|
|
||||||
|
`metrics` defaults to Datasette's own registry.
|
||||||
|
"""
|
||||||
|
if metrics is None:
|
||||||
|
metrics = METRICS
|
||||||
|
for metric in metrics:
|
||||||
|
if emitted_name == metric:
|
||||||
|
return metric
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def attribute_allowed(entry, emitted_key):
|
||||||
|
"""
|
||||||
|
Whether `emitted_key` is a registered attribute of `entry`.
|
||||||
|
|
||||||
|
`entry` is a `SpanName` or a `MetricName` - both carry `.attributes`.
|
||||||
|
"""
|
||||||
|
if entry is None:
|
||||||
|
return False
|
||||||
|
return emitted_key in entry.attributes
|
||||||
|
|
||||||
|
|
||||||
|
def attribute_value_allowed(entry, emitted_key, value):
|
||||||
|
"""
|
||||||
|
Whether `value` is permitted for `emitted_key` on `entry` (a `SpanName`
|
||||||
|
or a `MetricName`).
|
||||||
|
|
||||||
|
Any value is allowed if the attribute does not declare `values=`.
|
||||||
|
"""
|
||||||
|
if entry is None:
|
||||||
|
return False
|
||||||
|
for attribute in entry.attributes:
|
||||||
|
if attribute == emitted_key:
|
||||||
|
return attribute.values is None or value in attribute.values
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
# --- Metrics --------------------------------------------------------------
|
||||||
|
|
||||||
|
# Bucket boundaries in seconds for every duration histogram. OpenTelemetry's
|
||||||
|
# defaults are designed for milliseconds and would put almost every SQLite
|
||||||
|
# query in the first bucket. These are the semantic conventions' recommended
|
||||||
|
# boundaries for db.client.operation.duration, plus 0.0001 and 0.0005 for
|
||||||
|
# fast in-process SQLite queries.
|
||||||
|
DURATION_BUCKETS = (0.0001, 0.0005, 0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1, 5, 10)
|
||||||
|
|
||||||
|
M_OPERATION_DURATION = MetricName(
|
||||||
|
"db.client.operation.duration",
|
||||||
|
HISTOGRAM,
|
||||||
|
"s",
|
||||||
|
"Duration of a SQL operation, including callback-based calls such as "
|
||||||
|
"``execute_fn()``. For ``block=False`` writes, measures enqueue time.",
|
||||||
|
(DB_SYSTEM, DB_NAMESPACE, OPERATION, ERROR_TYPE),
|
||||||
|
buckets=DURATION_BUCKETS,
|
||||||
|
)
|
||||||
|
|
||||||
|
M_WRITE_QUEUE_WAIT = MetricName(
|
||||||
|
"datasette.write.queue_wait",
|
||||||
|
HISTOGRAM,
|
||||||
|
"s",
|
||||||
|
"Time each write waited in its database's write queue.",
|
||||||
|
(DB_NAMESPACE,),
|
||||||
|
buckets=DURATION_BUCKETS,
|
||||||
|
)
|
||||||
|
|
||||||
|
M_QUERIES_INTERRUPTED = MetricName(
|
||||||
|
"datasette.sql.queries.interrupted",
|
||||||
|
COUNTER,
|
||||||
|
"{query}",
|
||||||
|
"Queries cancelled for exceeding :ref:`setting_sql_time_limit_ms`. A "
|
||||||
|
"rising rate can indicate that queries need optimization or a higher "
|
||||||
|
"time limit. Caller-selected timeouts shorter than this limit, such as "
|
||||||
|
"those used for facet suggestion, are excluded.",
|
||||||
|
(DB_NAMESPACE,),
|
||||||
|
)
|
||||||
|
|
||||||
|
M_THREADS_LIMIT = MetricName(
|
||||||
|
"datasette.sql.threads.limit",
|
||||||
|
GAUGE,
|
||||||
|
"{thread}",
|
||||||
|
"Maximum concurrent read queries, configured by "
|
||||||
|
":ref:`setting_num_sql_threads`. Not reported when ``num_sql_threads`` "
|
||||||
|
"is ``0``.",
|
||||||
|
)
|
||||||
|
|
||||||
|
M_THREADS_QUEUE_DEPTH = MetricName(
|
||||||
|
"datasette.sql.threads.queue_depth",
|
||||||
|
GAUGE,
|
||||||
|
"{query}",
|
||||||
|
"Read queries waiting for a free SQL thread. Sustained values above "
|
||||||
|
"zero indicate a saturated read pool.",
|
||||||
|
)
|
||||||
|
|
||||||
|
M_QUERIES_PENDING = MetricName(
|
||||||
|
"datasette.sql.queries.pending",
|
||||||
|
GAUGE,
|
||||||
|
"{query}",
|
||||||
|
"Read queries submitted to the pool and not yet complete. Sum across "
|
||||||
|
"databases and compare with ``datasette.sql.threads.limit`` to assess "
|
||||||
|
"pool usage.",
|
||||||
|
(DB_NAMESPACE,),
|
||||||
|
)
|
||||||
|
|
||||||
|
M_WRITE_QUEUE_DEPTH = MetricName(
|
||||||
|
"datasette.write.queue_depth",
|
||||||
|
GAUGE,
|
||||||
|
"{write}",
|
||||||
|
"Writes waiting for a database's single write thread. Increasing "
|
||||||
|
"``num_sql_threads`` does not increase write concurrency. Not reported for "
|
||||||
|
"databases that have never been written to.",
|
||||||
|
(DB_NAMESPACE,),
|
||||||
|
)
|
||||||
|
|
||||||
|
M_CONNECTIONS_OPEN = MetricName(
|
||||||
|
"datasette.connections.open",
|
||||||
|
GAUGE,
|
||||||
|
"{connection}",
|
||||||
|
"Open SQLite connections managed by Datasette.",
|
||||||
|
(DB_NAMESPACE,),
|
||||||
|
)
|
||||||
|
|
||||||
|
METRICS = (
|
||||||
|
M_OPERATION_DURATION,
|
||||||
|
M_WRITE_QUEUE_WAIT,
|
||||||
|
M_QUERIES_INTERRUPTED,
|
||||||
|
M_THREADS_LIMIT,
|
||||||
|
M_THREADS_QUEUE_DEPTH,
|
||||||
|
M_QUERIES_PENDING,
|
||||||
|
M_WRITE_QUEUE_DEPTH,
|
||||||
|
M_CONNECTIONS_OPEN,
|
||||||
|
)
|
||||||
427
datasette/telemetry_testing.py
Normal file
427
datasette/telemetry_testing.py
Normal file
|
|
@ -0,0 +1,427 @@
|
||||||
|
"""
|
||||||
|
Pytest helpers for testing OpenTelemetry instrumentation - Datasette's own
|
||||||
|
and any plugin's. Part of Datasette's public plugin API; see the "Telemetry
|
||||||
|
for plugin authors" documentation.
|
||||||
|
|
||||||
|
Usage from a plugin's ``conftest.py``::
|
||||||
|
|
||||||
|
from datasette.telemetry_testing import ( # noqa: F401
|
||||||
|
MetricsCollector,
|
||||||
|
otel_metrics,
|
||||||
|
otel_meter_provider,
|
||||||
|
otel_provider,
|
||||||
|
otel_spans,
|
||||||
|
)
|
||||||
|
|
||||||
|
Tests can then use the ``otel_spans`` and ``otel_metrics`` fixtures. The
|
||||||
|
OpenTelemetry SDK is imported lazily, and the fixtures skip if it is not
|
||||||
|
installed.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from .telemetry_registry import (
|
||||||
|
attribute_allowed,
|
||||||
|
attribute_value_allowed,
|
||||||
|
metric_for,
|
||||||
|
span_for,
|
||||||
|
)
|
||||||
|
|
||||||
|
_span_exporter = None
|
||||||
|
_metric_reader = None
|
||||||
|
|
||||||
|
|
||||||
|
def install_span_exporter():
|
||||||
|
"""
|
||||||
|
Install a TracerProvider + InMemorySpanExporter once per process and
|
||||||
|
return the exporter, or None when the SDK is not installed.
|
||||||
|
|
||||||
|
Uses `SimpleSpanProcessor` so spans are exported as soon as they end.
|
||||||
|
"""
|
||||||
|
global _span_exporter
|
||||||
|
if _span_exporter is not None:
|
||||||
|
return _span_exporter
|
||||||
|
try:
|
||||||
|
from opentelemetry import trace as otel_trace
|
||||||
|
from opentelemetry.sdk.trace import TracerProvider
|
||||||
|
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
|
||||||
|
from opentelemetry.sdk.trace.export.in_memory_span_exporter import (
|
||||||
|
InMemorySpanExporter,
|
||||||
|
)
|
||||||
|
except ImportError:
|
||||||
|
return None
|
||||||
|
exporter = InMemorySpanExporter()
|
||||||
|
provider = TracerProvider()
|
||||||
|
provider.add_span_processor(SimpleSpanProcessor(exporter))
|
||||||
|
otel_trace.set_tracer_provider(provider)
|
||||||
|
# set_tracer_provider() is ignored if a provider was already installed,
|
||||||
|
# in which case the fixtures skip
|
||||||
|
if otel_trace.get_tracer_provider() is not provider:
|
||||||
|
return None
|
||||||
|
_span_exporter = exporter
|
||||||
|
return exporter
|
||||||
|
|
||||||
|
|
||||||
|
def install_metric_reader():
|
||||||
|
"""
|
||||||
|
Install a MeterProvider + InMemoryMetricReader once per process and
|
||||||
|
return the reader, or None when the SDK is not installed.
|
||||||
|
|
||||||
|
Uses delta temporality for counters and histograms, so each collection
|
||||||
|
only reports measurements since the previous one.
|
||||||
|
"""
|
||||||
|
global _metric_reader
|
||||||
|
if _metric_reader is not None:
|
||||||
|
return _metric_reader
|
||||||
|
try:
|
||||||
|
from opentelemetry import metrics as otel_metrics_api
|
||||||
|
from opentelemetry.sdk.metrics import Counter, Histogram, MeterProvider
|
||||||
|
from opentelemetry.sdk.metrics.export import (
|
||||||
|
AggregationTemporality,
|
||||||
|
InMemoryMetricReader,
|
||||||
|
)
|
||||||
|
except ImportError:
|
||||||
|
return None
|
||||||
|
reader = InMemoryMetricReader(
|
||||||
|
preferred_temporality={
|
||||||
|
Counter: AggregationTemporality.DELTA,
|
||||||
|
Histogram: AggregationTemporality.DELTA,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
provider = MeterProvider(metric_readers=[reader])
|
||||||
|
otel_metrics_api.set_meter_provider(provider)
|
||||||
|
if otel_metrics_api.get_meter_provider() is not provider:
|
||||||
|
return None
|
||||||
|
_metric_reader = reader
|
||||||
|
return reader
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="session", autouse=True)
|
||||||
|
def otel_provider():
|
||||||
|
"Install the span exporter once per test session, before any spans are created."
|
||||||
|
install_span_exporter()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="session", autouse=True)
|
||||||
|
def otel_meter_provider():
|
||||||
|
"Install the metric reader once per test session."
|
||||||
|
install_metric_reader()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(autouse=True)
|
||||||
|
def otel_reset():
|
||||||
|
"Clear recorded spans and drain collected metrics after every test."
|
||||||
|
yield
|
||||||
|
if _span_exporter is not None:
|
||||||
|
_span_exporter.clear()
|
||||||
|
if _metric_reader is not None:
|
||||||
|
_metric_reader.get_metrics_data()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def otel_spans():
|
||||||
|
"""
|
||||||
|
The in-memory span exporter, cleared before the test. Call
|
||||||
|
`.get_finished_spans()` to retrieve spans.
|
||||||
|
"""
|
||||||
|
pytest.importorskip("opentelemetry.sdk")
|
||||||
|
exporter = install_span_exporter()
|
||||||
|
if exporter is None:
|
||||||
|
pytest.skip("OpenTelemetry SDK provider was not installed")
|
||||||
|
exporter.clear()
|
||||||
|
yield exporter
|
||||||
|
|
||||||
|
|
||||||
|
class MetricsCollector:
|
||||||
|
"""
|
||||||
|
Wraps an `InMemoryMetricReader`.
|
||||||
|
|
||||||
|
`collect()` runs a collection cycle and stores a snapshot, which
|
||||||
|
`points()` and `point()` then query.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, reader):
|
||||||
|
self.reader = reader
|
||||||
|
self.snapshot = {}
|
||||||
|
# (instrumentation scope name, sdk Metric) pairs from the last collect()
|
||||||
|
self.collected = []
|
||||||
|
|
||||||
|
def collect(self):
|
||||||
|
self.snapshot = {}
|
||||||
|
self.collected = []
|
||||||
|
data = self.reader.get_metrics_data()
|
||||||
|
if data is None:
|
||||||
|
return self.snapshot
|
||||||
|
for resource_metrics in data.resource_metrics:
|
||||||
|
for scope_metrics in resource_metrics.scope_metrics:
|
||||||
|
scope_name = scope_metrics.scope.name if scope_metrics.scope else None
|
||||||
|
for metric in scope_metrics.metrics:
|
||||||
|
self.snapshot.setdefault(metric.name, []).extend(
|
||||||
|
metric.data.data_points
|
||||||
|
)
|
||||||
|
self.collected.append((scope_name, metric))
|
||||||
|
return self.snapshot
|
||||||
|
|
||||||
|
def points(self, name, attributes=None):
|
||||||
|
"Data points for `name` whose attributes are a superset of `attributes`."
|
||||||
|
found = []
|
||||||
|
for point in self.snapshot.get(name, []):
|
||||||
|
point_attributes = dict(point.attributes or {})
|
||||||
|
if all(point_attributes.get(k) == v for k, v in (attributes or {}).items()):
|
||||||
|
found.append(point)
|
||||||
|
return found
|
||||||
|
|
||||||
|
def point(self, name, attributes=None):
|
||||||
|
"The single matching data point, asserting there is exactly one."
|
||||||
|
found = self.points(name, attributes)
|
||||||
|
assert len(found) == 1, (
|
||||||
|
f"expected exactly one {name} point matching {attributes}, "
|
||||||
|
f"got {len(found)}: {found}"
|
||||||
|
)
|
||||||
|
return found[0]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def otel_metrics():
|
||||||
|
"A `MetricsCollector`, drained before the test so counts start from zero."
|
||||||
|
pytest.importorskip("opentelemetry.sdk")
|
||||||
|
reader = install_metric_reader()
|
||||||
|
if reader is None:
|
||||||
|
pytest.skip("OpenTelemetry SDK meter provider was not installed")
|
||||||
|
reader.get_metrics_data()
|
||||||
|
yield MetricsCollector(reader)
|
||||||
|
|
||||||
|
|
||||||
|
def _scoped(finished_spans, scope_name):
|
||||||
|
if scope_name is None:
|
||||||
|
return list(finished_spans)
|
||||||
|
return [
|
||||||
|
span
|
||||||
|
for span in finished_spans
|
||||||
|
if span.instrumentation_scope and span.instrumentation_scope.name == scope_name
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def assert_spans_conform(registry_spans, finished_spans, scope_name=None):
|
||||||
|
"""
|
||||||
|
Assert every finished span is registered in `registry_spans`, sets only
|
||||||
|
registered attributes and uses allowed attribute values.
|
||||||
|
|
||||||
|
Pass `scope_name` to only check spans from that instrumentation scope.
|
||||||
|
"""
|
||||||
|
problems = []
|
||||||
|
for span in _scoped(finished_spans, scope_name):
|
||||||
|
entry = span_for(str(span.name), kind=span.kind, spans=registry_spans)
|
||||||
|
if entry is None:
|
||||||
|
problems.append(f"unregistered span: {span.name!r}")
|
||||||
|
continue
|
||||||
|
for key, value in (span.attributes or {}).items():
|
||||||
|
if not attribute_allowed(entry, str(key)):
|
||||||
|
problems.append(f"{span.name}: unregistered attribute {key!r}")
|
||||||
|
elif not attribute_value_allowed(entry, str(key), value):
|
||||||
|
problems.append(
|
||||||
|
f"{span.name}: {key}={value!r} not in the declared enum"
|
||||||
|
)
|
||||||
|
assert not problems, "\n".join(problems)
|
||||||
|
|
||||||
|
|
||||||
|
def assert_spans_covered(registry_spans, finished_spans, scope_name=None):
|
||||||
|
"""
|
||||||
|
Assert every entry in `registry_spans` was emitted at least once, with
|
||||||
|
each of its attributes that is not `optional=True`.
|
||||||
|
"""
|
||||||
|
spans = _scoped(finished_spans, scope_name)
|
||||||
|
seen_attributes = {}
|
||||||
|
for span in spans:
|
||||||
|
entry = span_for(str(span.name), kind=span.kind, spans=registry_spans)
|
||||||
|
if entry is not None:
|
||||||
|
seen = seen_attributes.setdefault(str(entry), set())
|
||||||
|
seen.update(str(key) for key in (span.attributes or {}))
|
||||||
|
problems = []
|
||||||
|
for entry in registry_spans:
|
||||||
|
if str(entry) not in seen_attributes:
|
||||||
|
problems.append(f"registered span never emitted: {entry!r}")
|
||||||
|
continue
|
||||||
|
required = {
|
||||||
|
str(attribute) for attribute in entry.attributes if not attribute.optional
|
||||||
|
}
|
||||||
|
missing = required - seen_attributes[str(entry)]
|
||||||
|
if missing:
|
||||||
|
problems.append(
|
||||||
|
f"{entry}: registered attributes never emitted: {sorted(missing)}"
|
||||||
|
)
|
||||||
|
assert not problems, "\n".join(problems)
|
||||||
|
|
||||||
|
|
||||||
|
# Registry instrument kinds mapped to the SDK data type collected for them.
|
||||||
|
# Both counter kinds collect as Sum, distinguished by is_monotonic.
|
||||||
|
_KIND_TO_DATA_TYPE = {
|
||||||
|
"Counter": "Sum",
|
||||||
|
"UpDownCounter": "Sum",
|
||||||
|
"Histogram": "Histogram",
|
||||||
|
"Observable gauge": "Gauge",
|
||||||
|
}
|
||||||
|
_KIND_IS_MONOTONIC = {"Counter": True, "UpDownCounter": False}
|
||||||
|
|
||||||
|
|
||||||
|
def _scoped_metrics(collector, scope_name):
|
||||||
|
for scope, metric in collector.collected:
|
||||||
|
if scope_name is None or scope == scope_name:
|
||||||
|
yield metric
|
||||||
|
|
||||||
|
|
||||||
|
def assert_metrics_conform(registry_metrics, collector, scope_name=None):
|
||||||
|
"""
|
||||||
|
Assert every metric in the collector's last `collect()` is registered in
|
||||||
|
`registry_metrics` with a matching instrument kind and unit, sets only
|
||||||
|
registered attributes and uses allowed attribute values.
|
||||||
|
|
||||||
|
Pass `scope_name` to only check metrics from that instrumentation scope.
|
||||||
|
"""
|
||||||
|
problems = set()
|
||||||
|
for metric in _scoped_metrics(collector, scope_name):
|
||||||
|
entry = metric_for(metric.name, metrics=registry_metrics)
|
||||||
|
if entry is None:
|
||||||
|
problems.add(f"unregistered metric: {metric.name!r}")
|
||||||
|
continue
|
||||||
|
expected_data_type = _KIND_TO_DATA_TYPE.get(entry.kind)
|
||||||
|
actual_data_type = type(metric.data).__name__
|
||||||
|
if expected_data_type is not None and actual_data_type != expected_data_type:
|
||||||
|
problems.add(
|
||||||
|
f"{metric.name}: registry declares {entry.kind}, "
|
||||||
|
f"SDK collected {actual_data_type}"
|
||||||
|
)
|
||||||
|
expected_monotonic = _KIND_IS_MONOTONIC.get(entry.kind)
|
||||||
|
actual_monotonic = getattr(metric.data, "is_monotonic", None)
|
||||||
|
if (
|
||||||
|
expected_monotonic is not None
|
||||||
|
and actual_monotonic is not None
|
||||||
|
and actual_monotonic != expected_monotonic
|
||||||
|
):
|
||||||
|
problems.add(
|
||||||
|
f"{metric.name}: registry declares {entry.kind}, but the "
|
||||||
|
f"collected Sum is_monotonic={actual_monotonic}"
|
||||||
|
)
|
||||||
|
if (metric.unit or "") != (entry.unit or ""):
|
||||||
|
problems.add(
|
||||||
|
f"{metric.name}: instrument unit {metric.unit!r} != "
|
||||||
|
f"registry unit {entry.unit!r}"
|
||||||
|
)
|
||||||
|
for point in metric.data.data_points:
|
||||||
|
for key, value in dict(point.attributes or {}).items():
|
||||||
|
if not attribute_allowed(entry, str(key)):
|
||||||
|
problems.add(f"{metric.name}: unregistered attribute {key!r}")
|
||||||
|
elif not attribute_value_allowed(entry, str(key), value):
|
||||||
|
problems.add(
|
||||||
|
f"{metric.name}: {key}={value!r} not in the declared enum"
|
||||||
|
)
|
||||||
|
assert not problems, "\n".join(sorted(problems))
|
||||||
|
|
||||||
|
|
||||||
|
def assert_metrics_covered(registry_metrics, collector, scope_name=None):
|
||||||
|
"""
|
||||||
|
Assert every entry in `registry_metrics` was collected at least once,
|
||||||
|
with each of its attributes that is not `optional=True`.
|
||||||
|
|
||||||
|
Call `collect()` once after the workload and before this check.
|
||||||
|
"""
|
||||||
|
seen_attributes = {}
|
||||||
|
for metric in _scoped_metrics(collector, scope_name):
|
||||||
|
entry = metric_for(metric.name, metrics=registry_metrics)
|
||||||
|
if entry is None:
|
||||||
|
continue
|
||||||
|
seen = seen_attributes.setdefault(str(entry), set())
|
||||||
|
for point in metric.data.data_points:
|
||||||
|
seen.update(str(key) for key in dict(point.attributes or {}))
|
||||||
|
problems = []
|
||||||
|
for entry in registry_metrics:
|
||||||
|
if str(entry) not in seen_attributes:
|
||||||
|
problems.append(f"registered metric never collected: {entry!r}")
|
||||||
|
continue
|
||||||
|
required = {
|
||||||
|
str(attribute) for attribute in entry.attributes if not attribute.optional
|
||||||
|
}
|
||||||
|
missing = required - seen_attributes[str(entry)]
|
||||||
|
if missing:
|
||||||
|
problems.append(
|
||||||
|
f"{entry}: registered attributes never collected: {sorted(missing)}"
|
||||||
|
)
|
||||||
|
assert not problems, "\n".join(problems)
|
||||||
|
|
||||||
|
|
||||||
|
def assert_no_forbidden_values(
|
||||||
|
forbidden, finished_spans=None, collector=None, scope_name=None
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Assert that none of the `forbidden` strings appear anywhere in the
|
||||||
|
emitted telemetry: span names, span attribute values, span event names
|
||||||
|
and attributes, span status descriptions, or metric point attributes.
|
||||||
|
|
||||||
|
Use fake private values such as tokens or email addresses in your test
|
||||||
|
workload, then check that they were not recorded:
|
||||||
|
|
||||||
|
FORBIDDEN = {"secret-token-123", "alice@example.com"}
|
||||||
|
run_workload_using_those_values()
|
||||||
|
assert_no_forbidden_values(
|
||||||
|
FORBIDDEN,
|
||||||
|
finished_spans=otel_spans.get_finished_spans(),
|
||||||
|
collector=otel_metrics,
|
||||||
|
)
|
||||||
|
|
||||||
|
Matches substrings of each value's string form. Empty strings in
|
||||||
|
`forbidden` are ignored. Leave `scope_name` unset to also check
|
||||||
|
Datasette's own telemetry.
|
||||||
|
"""
|
||||||
|
needles = [needle for needle in forbidden if needle]
|
||||||
|
leaks = set()
|
||||||
|
|
||||||
|
def check(value, where):
|
||||||
|
text = str(value)
|
||||||
|
for needle in needles:
|
||||||
|
if needle in text:
|
||||||
|
leaks.add(f"{where} contains {needle!r}")
|
||||||
|
|
||||||
|
if finished_spans is not None:
|
||||||
|
for span in _scoped(finished_spans, scope_name):
|
||||||
|
check(span.name, f"span name {str(span.name)!r}")
|
||||||
|
for key, value in (span.attributes or {}).items():
|
||||||
|
check(value, f"{span.name} attribute {key}")
|
||||||
|
for event in span.events or ():
|
||||||
|
check(event.name, f"{span.name} event name")
|
||||||
|
for key, value in (event.attributes or {}).items():
|
||||||
|
check(value, f"{span.name} event {event.name} attribute {key}")
|
||||||
|
if span.status is not None and span.status.description:
|
||||||
|
check(span.status.description, f"{span.name} status description")
|
||||||
|
if collector is not None:
|
||||||
|
for metric in _scoped_metrics(collector, scope_name):
|
||||||
|
for point in metric.data.data_points:
|
||||||
|
for key, value in dict(point.attributes or {}).items():
|
||||||
|
check(value, f"metric {metric.name} attribute {key}")
|
||||||
|
assert not leaks, "forbidden values leaked into telemetry:\n" + "\n".join(
|
||||||
|
sorted(leaks)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def assert_package_never_imports_sdk(*module_names):
|
||||||
|
"""
|
||||||
|
Import the named modules in a fresh interpreter and assert none of them
|
||||||
|
imported `opentelemetry.sdk`.
|
||||||
|
|
||||||
|
Run the test that calls this early in your suite: on macOS with CPython
|
||||||
|
3.13, starting a subprocess from a process with many threads can crash.
|
||||||
|
"""
|
||||||
|
imports = "; ".join(f"import {name}" for name in module_names)
|
||||||
|
code = (
|
||||||
|
f"import sys; {imports}; "
|
||||||
|
"print([m for m in sys.modules if m.startswith('opentelemetry.sdk')])"
|
||||||
|
)
|
||||||
|
result = subprocess.run(
|
||||||
|
[sys.executable, "-c", code], capture_output=True, text=True, check=True
|
||||||
|
)
|
||||||
|
assert result.stdout.strip() == "[]", (
|
||||||
|
f"importing {module_names} pulled in the OpenTelemetry SDK: "
|
||||||
|
f"{result.stdout.strip()}"
|
||||||
|
)
|
||||||
40
datasette/template_contexts.py
Normal file
40
datasette/template_contexts.py
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
"""
|
||||||
|
Index of the documented template contexts for Datasette's core HTML pages.
|
||||||
|
|
||||||
|
This module deliberately contains no documentation strings of its own -
|
||||||
|
the documentation lives next to the code it describes:
|
||||||
|
|
||||||
|
- Every page renders a Context dataclass defined in its view module
|
||||||
|
(DatabaseContext, QueryContext in views/database.py, TableContext in
|
||||||
|
views/table.py, RowContext in views/row.py). Fields added by view code
|
||||||
|
carry ``help`` metadata; fields declared with from_extra() take their
|
||||||
|
documentation from the description on the matching Extra class in
|
||||||
|
views/table_extras.py.
|
||||||
|
- The keys render_template() adds to every page are documented in
|
||||||
|
TEMPLATE_BASE_CONTEXT in datasette/app.py, next to the code that adds
|
||||||
|
them.
|
||||||
|
|
||||||
|
The contract tests in tests/test_template_context.py assert that the real
|
||||||
|
rendered context for each page exactly matches what is documented, and
|
||||||
|
docs/template_context_doc.py generates docs/template_context.rst from the
|
||||||
|
same classes.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from datasette.app import TEMPLATE_BASE_CONTEXT
|
||||||
|
from datasette.views.database import DatabaseContext, QueryContext
|
||||||
|
from datasette.views.row import RowContext
|
||||||
|
from datasette.views.table import TableContext
|
||||||
|
|
||||||
|
PAGES = {
|
||||||
|
"database": DatabaseContext,
|
||||||
|
"query": QueryContext,
|
||||||
|
"table": TableContext,
|
||||||
|
"row": RowContext,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def documented_context_keys(page_name):
|
||||||
|
"Set of every documented key for the named page, including base context keys"
|
||||||
|
return set(TEMPLATE_BASE_CONTEXT) | {
|
||||||
|
f.name for f in PAGES[page_name].documented_fields()
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
<script src="{{ base_url }}-/static/sql-formatter-2.3.3.min.js" defer></script>
|
<script src="{{ static('sql-formatter-2.3.3.min.js') }}" defer></script>
|
||||||
<script src="{{ base_url }}-/static/cm-editor-6.0.1.bundle.js"></script>
|
<script src="{{ static('cm-editor-6.0.1.bundle.js') }}"></script>
|
||||||
<style>
|
<style>
|
||||||
.cm-editor {
|
.cm-editor {
|
||||||
resize: both;
|
resize: both;
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,20 @@
|
||||||
padding: 1.5em;
|
padding: 1.5em;
|
||||||
margin-bottom: 2em;
|
margin-bottom: 2em;
|
||||||
}
|
}
|
||||||
|
.permission-form form {
|
||||||
|
max-width: 60rem;
|
||||||
|
}
|
||||||
|
.permission-form-grid {
|
||||||
|
display: grid;
|
||||||
|
gap: 1.5rem;
|
||||||
|
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||||
|
}
|
||||||
|
.permission-form-result {
|
||||||
|
margin-top: 1rem;
|
||||||
|
max-width: 60rem;
|
||||||
|
}
|
||||||
.form-section {
|
.form-section {
|
||||||
margin-bottom: 1em;
|
margin-bottom: 1.25em;
|
||||||
}
|
}
|
||||||
.form-section label {
|
.form-section label {
|
||||||
display: block;
|
display: block;
|
||||||
|
|
@ -15,22 +27,51 @@
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
}
|
}
|
||||||
.form-section input[type="text"],
|
.form-section input[type="text"],
|
||||||
.form-section select {
|
.form-section input[type="number"],
|
||||||
width: 100%;
|
.form-section select,
|
||||||
max-width: 500px;
|
.permission-textarea {
|
||||||
padding: 0.5em;
|
background-color: #fff;
|
||||||
|
border: 1px solid #aaa;
|
||||||
|
border-radius: 4px;
|
||||||
box-sizing: border-box;
|
box-sizing: border-box;
|
||||||
border: 1px solid #ccc;
|
box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.08);
|
||||||
border-radius: 3px;
|
color: #222;
|
||||||
|
font-family: inherit;
|
||||||
|
font-size: 1rem;
|
||||||
|
line-height: 1.4;
|
||||||
|
max-width: none;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
.form-section input[type="text"] {
|
||||||
|
height: 3rem;
|
||||||
|
padding: 0.6rem 0.75rem;
|
||||||
|
}
|
||||||
|
.form-section input[type="number"] {
|
||||||
|
height: 3rem;
|
||||||
|
max-width: 7rem;
|
||||||
|
padding: 0.6rem 0.75rem;
|
||||||
|
}
|
||||||
|
.form-section select {
|
||||||
|
height: 3rem;
|
||||||
|
padding: 0.6rem 0.75rem;
|
||||||
|
}
|
||||||
|
.permission-textarea {
|
||||||
|
font-family: monospace;
|
||||||
|
min-height: 12rem;
|
||||||
|
padding: 0.75rem;
|
||||||
|
resize: vertical;
|
||||||
}
|
}
|
||||||
.form-section input[type="text"]:focus,
|
.form-section input[type="text"]:focus,
|
||||||
.form-section select:focus {
|
.form-section input[type="number"]:focus,
|
||||||
outline: 2px solid #0066cc;
|
.form-section select:focus,
|
||||||
|
.permission-textarea:focus {
|
||||||
border-color: #0066cc;
|
border-color: #0066cc;
|
||||||
|
box-shadow: 0 0 0 3px rgba(0, 102, 204, 0.18);
|
||||||
|
outline: none;
|
||||||
}
|
}
|
||||||
.form-section small {
|
.form-section small {
|
||||||
display: block;
|
display: block;
|
||||||
margin-top: 0.3em;
|
margin-top: 0.45em;
|
||||||
color: #666;
|
color: #666;
|
||||||
}
|
}
|
||||||
.form-actions {
|
.form-actions {
|
||||||
|
|
@ -142,4 +183,9 @@
|
||||||
text-align: center;
|
text-align: center;
|
||||||
color: #666;
|
color: #666;
|
||||||
}
|
}
|
||||||
|
@media only screen and (max-width: 576px) {
|
||||||
|
.permission-form-grid {
|
||||||
|
grid-template-columns: minmax(0, 1fr);
|
||||||
|
}
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|
|
||||||
|
|
@ -44,10 +44,10 @@
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
<nav class="permissions-debug-tabs">
|
<nav class="permissions-debug-tabs">
|
||||||
<a href="{{ urls.path('-/permissions') }}" {% if current_tab == "permissions" %}class="active"{% endif %}>Playground</a>
|
<a href="{{ urls.path('-/check') }}{{ query_string }}" {% if current_tab == "check" %}class="active"{% endif %}>Explain</a>
|
||||||
<a href="{{ urls.path('-/check') }}{{ query_string }}" {% if current_tab == "check" %}class="active"{% endif %}>Check</a>
|
<a href="{{ urls.path('-/allowed') }}{{ query_string }}" {% if current_tab == "allowed" %}class="active"{% endif %}>Access map</a>
|
||||||
<a href="{{ urls.path('-/allowed') }}{{ query_string }}" {% if current_tab == "allowed" %}class="active"{% endif %}>Allowed</a>
|
<a href="{{ urls.path('-/rules') }}{{ query_string }}" {% if current_tab == "rules" %}class="active"{% endif %}>Rule explorer</a>
|
||||||
<a href="{{ urls.path('-/rules') }}{{ query_string }}" {% if current_tab == "rules" %}class="active"{% endif %}>Rules</a>
|
<a href="{{ urls.path('-/permissions') }}" {% if current_tab == "permissions" %}class="active"{% endif %}>Activity</a>
|
||||||
<a href="{{ urls.path('-/actions') }}" {% if current_tab == "actions" %}class="active"{% endif %}>Actions</a>
|
<a href="{{ urls.path('-/actions') }}" {% if current_tab == "actions" %}class="active"{% endif %}>Actions</a>
|
||||||
<a href="{{ urls.path('-/allow-debug') }}" {% if current_tab == "allow_debug" %}class="active"{% endif %}>Allow debug</a>
|
<a href="{{ urls.path('-/allow-debug') }}" {% if current_tab == "allow_debug" %}class="active"{% endif %}>Allow debug</a>
|
||||||
</nav>
|
</nav>
|
||||||
|
|
|
||||||
|
|
@ -3,29 +3,11 @@
|
||||||
{% block title %}Debug allow rules{% endblock %}
|
{% block title %}Debug allow rules{% endblock %}
|
||||||
|
|
||||||
{% block extra_head %}
|
{% block extra_head %}
|
||||||
|
{% include "_permission_ui_styles.html" %}
|
||||||
<style>
|
<style>
|
||||||
textarea {
|
|
||||||
height: 10em;
|
|
||||||
width: 95%;
|
|
||||||
box-sizing: border-box;
|
|
||||||
padding: 0.5em;
|
|
||||||
border: 2px dotted black;
|
|
||||||
}
|
|
||||||
.two-col {
|
|
||||||
display: inline-block;
|
|
||||||
width: 48%;
|
|
||||||
}
|
|
||||||
.two-col label {
|
|
||||||
width: 48%;
|
|
||||||
}
|
|
||||||
p.message-warning {
|
p.message-warning {
|
||||||
white-space: pre-wrap;
|
white-space: pre-wrap;
|
||||||
}
|
}
|
||||||
@media only screen and (max-width: 576px) {
|
|
||||||
.two-col {
|
|
||||||
width: 100%;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</style>
|
</style>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
|
|
@ -38,24 +20,28 @@ p.message-warning {
|
||||||
|
|
||||||
<p>Use this tool to try out different actor and allow combinations. See <a href="https://docs.datasette.io/en/stable/authentication.html#defining-permissions-with-allow-blocks">Defining permissions with "allow" blocks</a> for documentation.</p>
|
<p>Use this tool to try out different actor and allow combinations. See <a href="https://docs.datasette.io/en/stable/authentication.html#defining-permissions-with-allow-blocks">Defining permissions with "allow" blocks</a> for documentation.</p>
|
||||||
|
|
||||||
<form class="core" action="{{ urls.path('-/allow-debug') }}" method="get" style="margin-bottom: 1em">
|
<div class="permission-form">
|
||||||
<div class="two-col">
|
<form class="core" action="{{ urls.path('-/allow-debug') }}" method="get">
|
||||||
<p><label>Allow block</label></p>
|
<div class="permission-form-grid">
|
||||||
<textarea name="allow">{{ allow_input }}</textarea>
|
<div class="form-section">
|
||||||
</div>
|
<label for="allow-block">Allow block</label>
|
||||||
<div class="two-col">
|
<textarea class="permission-textarea" id="allow-block" name="allow">{{ allow_input }}</textarea>
|
||||||
<p><label>Actor</label></p>
|
</div>
|
||||||
<textarea name="actor">{{ actor_input }}</textarea>
|
<div class="form-section">
|
||||||
</div>
|
<label for="allow-actor">Actor</label>
|
||||||
<div style="margin-top: 1em;">
|
<textarea class="permission-textarea" id="allow-actor" name="actor">{{ actor_input }}</textarea>
|
||||||
<input type="submit" value="Apply allow block to actor">
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
<div class="form-actions">
|
||||||
|
<button type="submit" class="submit-btn">Apply allow block to actor</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
|
||||||
{% if error %}<p class="message-warning">{{ error }}</p>{% endif %}
|
{% if error %}<p class="message-warning permission-form-result">{{ error }}</p>{% endif %}
|
||||||
|
|
||||||
{% if result == "True" %}<p class="message-info">Result: allow</p>{% endif %}
|
{% if result == "True" %}<p class="message-info permission-form-result">Result: allow</p>{% endif %}
|
||||||
|
|
||||||
{% if result == "False" %}<p class="message-error">Result: deny</p>{% endif %}
|
{% if result == "False" %}<p class="message-error permission-form-result">Result: deny</p>{% endif %}
|
||||||
|
</div>
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@
|
||||||
{% block title %}API Explorer{% endblock %}
|
{% block title %}API Explorer{% endblock %}
|
||||||
|
|
||||||
{% block extra_head %}
|
{% block extra_head %}
|
||||||
<script src="{{ base_url }}-/static/json-format-highlight-1.0.1.js"></script>
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
|
|
@ -126,7 +125,7 @@ getForm.addEventListener("submit", (ev) => {
|
||||||
document.getElementById('response-status').textContent = response.status;
|
document.getElementById('response-status').textContent = response.status;
|
||||||
return response.json();
|
return response.json();
|
||||||
}).then((data) => {
|
}).then((data) => {
|
||||||
output.querySelector('pre').innerHTML = jsonFormatHighlight(data);
|
output.querySelector('pre').textContent = JSON.stringify(data, null, 2);
|
||||||
errorList.style.display = 'none';
|
errorList.style.display = 'none';
|
||||||
}).catch((error) => {
|
}).catch((error) => {
|
||||||
alert(error);
|
alert(error);
|
||||||
|
|
@ -174,7 +173,7 @@ postForm.addEventListener("submit", (ev) => {
|
||||||
} else {
|
} else {
|
||||||
errorList.style.display = 'none';
|
errorList.style.display = 'none';
|
||||||
}
|
}
|
||||||
output.querySelector('pre').innerHTML = jsonFormatHighlight(data);
|
output.querySelector('pre').textContent = JSON.stringify(data, null, 2);
|
||||||
output.style.display = 'block';
|
output.style.display = 'block';
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
alert("Error: " + err);
|
alert("Error: " + err);
|
||||||
|
|
|
||||||
|
|
@ -2,13 +2,14 @@
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<title>{% block title %}{% endblock %}</title>
|
<title>{% block title %}{% endblock %}</title>
|
||||||
<link rel="stylesheet" href="{{ urls.static('app.css') }}?{{ app_css_hash }}">
|
<link rel="stylesheet" href="{{ static('app.css') }}">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||||
{% for url in extra_css_urls %}
|
{% for url in extra_css_urls %}
|
||||||
<link rel="stylesheet" href="{{ url.url }}"{% if url.get("sri") %} integrity="{{ url.sri }}" crossorigin="anonymous"{% endif %}>
|
<link rel="stylesheet" href="{{ url.url }}"{% if url.get("sri") %} integrity="{{ url.sri }}" crossorigin="anonymous"{% endif %}>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
<script>window.datasetteVersion = '{{ datasette_version }}';</script>
|
<script>window.datasetteVersion = '{{ datasette_version }}';</script>
|
||||||
<script src="{{ urls.static('datasette-manager.js') }}" defer></script>
|
<script src="{{ static('modal.js') }}" defer></script>
|
||||||
|
<script src="{{ static('datasette-manager.js') }}" defer></script>
|
||||||
{% for url in extra_js_urls %}
|
{% for url in extra_js_urls %}
|
||||||
<script {% if url.module %}type="module" {% endif %}src="{{ url.url }}"{% if url.get("sri") %} integrity="{{ url.sri }}" crossorigin="anonymous"{% endif %}></script>
|
<script {% if url.module %}type="module" {% endif %}src="{{ url.url }}"{% if url.get("sri") %} integrity="{{ url.sri }}" crossorigin="anonymous"{% endif %}></script>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
@ -70,7 +71,7 @@
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
||||||
{% if select_templates %}<!-- Templates considered: {{ select_templates|join(", ") }} -->{% endif %}
|
{% if select_templates %}<!-- Templates considered: {{ select_templates|join(", ") }} -->{% endif %}
|
||||||
<script src="{{ urls.static('navigation-search.js') }}" defer></script>
|
<script src="{{ static('navigation-search.js') }}" defer></script>
|
||||||
<navigation-search url="{{ urls.path("/-/jump") }}"></navigation-search>
|
<navigation-search url="{{ urls.path("/-/jump") }}"></navigation-search>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,10 @@
|
||||||
{{- super() -}}
|
{{- super() -}}
|
||||||
{% include "_codemirror.html" %}
|
{% include "_codemirror.html" %}
|
||||||
{% include "_sql_parameter_styles.html" %}
|
{% include "_sql_parameter_styles.html" %}
|
||||||
|
{% if database_page_data.createTable %}
|
||||||
|
<script>window._datasetteDatabaseData = {{ database_page_data|tojson }};</script>
|
||||||
|
<script src="{{ static('edit-tools.js') }}" defer></script>
|
||||||
|
{% endif %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block body_class %}db db-{{ database|to_css_class }}{% endblock %}
|
{% block body_class %}db db-{{ database|to_css_class }}{% endblock %}
|
||||||
|
|
@ -72,7 +76,7 @@
|
||||||
<div class="db-table">
|
<div class="db-table">
|
||||||
<h3><a href="{{ urls.table(database, table.name) }}">{{ table.name }}</a>{% if table.private %} 🔒{% endif %}{% if table.hidden %}<em> (hidden)</em>{% endif %}</h3>
|
<h3><a href="{{ urls.table(database, table.name) }}">{{ table.name }}</a>{% if table.private %} 🔒{% endif %}{% if table.hidden %}<em> (hidden)</em>{% endif %}</h3>
|
||||||
<p><em>{% for column in table.columns %}{{ column }}{% if not loop.last %}, {% endif %}{% endfor %}</em></p>
|
<p><em>{% for column in table.columns %}{{ column }}{% if not loop.last %}, {% endif %}{% endfor %}</em></p>
|
||||||
<p>{% if table.count is none %}Many rows{% elif table.count == count_limit + 1 %}>{{ "{:,}".format(count_limit) }} rows{% else %}{{ "{:,}".format(table.count) }} row{% if table.count == 1 %}{% else %}s{% endif %}{% endif %}</p>
|
<p>{% if table.count is none %}Many rows{% elif table.count_truncated %}>{{ "{:,}".format(table.count - 1) }} rows{% else %}{{ "{:,}".format(table.count) }} row{% if table.count == 1 %}{% else %}s{% endif %}{% endif %}</p>
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@
|
||||||
{% include "_permissions_debug_tabs.html" %}
|
{% include "_permissions_debug_tabs.html" %}
|
||||||
|
|
||||||
<p style="margin-bottom: 2em;">
|
<p style="margin-bottom: 2em;">
|
||||||
This Datasette instance has registered {{ data|length }} action{{ data|length != 1 and "s" or "" }}.
|
This Datasette instance has registered {{ data.actions|length }} action{{ data.actions|length != 1 and "s" or "" }}.
|
||||||
Actions are used by the permission system to control access to different features.
|
Actions are used by the permission system to control access to different features.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
|
@ -26,7 +26,7 @@
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{% for action in data %}
|
{% for action in data.actions %}
|
||||||
<tr>
|
<tr>
|
||||||
<td><strong>{{ action.name }}</strong></td>
|
<td><strong>{{ action.name }}</strong></td>
|
||||||
<td>{% if action.abbr %}<code>{{ action.abbr }}</code>{% endif %}</td>
|
<td>{% if action.abbr %}<code>{{ action.abbr }}</code>{% endif %}</td>
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@
|
||||||
{% block title %}Allowed Resources{% endblock %}
|
{% block title %}Allowed Resources{% endblock %}
|
||||||
|
|
||||||
{% block extra_head %}
|
{% block extra_head %}
|
||||||
<script src="{{ base_url }}-/static/json-format-highlight-1.0.1.js"></script>
|
|
||||||
{% include "_permission_ui_styles.html" %}
|
{% include "_permission_ui_styles.html" %}
|
||||||
{% include "_debug_common_functions.html" %}
|
{% include "_debug_common_functions.html" %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
@ -49,7 +48,7 @@
|
||||||
|
|
||||||
<div class="form-section">
|
<div class="form-section">
|
||||||
<label for="page_size">Page size:</label>
|
<label for="page_size">Page size:</label>
|
||||||
<input type="number" id="page_size" name="page_size" value="50" min="1" max="200" style="max-width: 100px;">
|
<input type="number" id="page_size" name="_size" value="50" min="1" max="200">
|
||||||
<small>Number of results per page (max 200)</small>
|
<small>Number of results per page (max 200)</small>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
@ -88,7 +87,7 @@ const hasDebugPermission = {{ 'true' if has_debug_permission else 'false' }};
|
||||||
(function() {
|
(function() {
|
||||||
const params = populateFormFromURL();
|
const params = populateFormFromURL();
|
||||||
const action = params.get('action');
|
const action = params.get('action');
|
||||||
const page = params.get('page');
|
const page = params.get('_page');
|
||||||
if (action) {
|
if (action) {
|
||||||
fetchResults(page ? parseInt(page) : 1);
|
fetchResults(page ? parseInt(page) : 1);
|
||||||
}
|
}
|
||||||
|
|
@ -102,14 +101,14 @@ async function fetchResults(page = 1) {
|
||||||
const params = new URLSearchParams();
|
const params = new URLSearchParams();
|
||||||
|
|
||||||
for (const [key, value] of formData.entries()) {
|
for (const [key, value] of formData.entries()) {
|
||||||
if (value && key !== 'page_size') {
|
if (value && key !== '_size' && key !== '_page') {
|
||||||
params.append(key, value);
|
params.append(key, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const pageSize = document.getElementById('page_size').value || '50';
|
const pageSize = document.getElementById('page_size').value || '50';
|
||||||
params.append('page', page.toString());
|
params.append('_page', page.toString());
|
||||||
params.append('page_size', pageSize);
|
params.append('_size', pageSize);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch('{{ urls.path("-/allowed.json") }}?' + params.toString(), {
|
const response = await fetch('{{ urls.path("-/allowed.json") }}?' + params.toString(), {
|
||||||
|
|
@ -198,7 +197,7 @@ function displayResults(data) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update raw JSON
|
// Update raw JSON
|
||||||
document.getElementById('raw-json').innerHTML = jsonFormatHighlight(data);
|
document.getElementById('raw-json').textContent = JSON.stringify(data, null, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
function displayError(data) {
|
function displayError(data) {
|
||||||
|
|
@ -208,7 +207,7 @@ function displayError(data) {
|
||||||
|
|
||||||
resultsContent.innerHTML = `<div class="error-message">Error: ${escapeHtml(data.error || 'Unknown error')}</div>`;
|
resultsContent.innerHTML = `<div class="error-message">Error: ${escapeHtml(data.error || 'Unknown error')}</div>`;
|
||||||
|
|
||||||
document.getElementById('raw-json').innerHTML = jsonFormatHighlight(data);
|
document.getElementById('raw-json').textContent = JSON.stringify(data, null, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Disable child input if parent is empty
|
// Disable child input if parent is empty
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
{% block extra_head %}
|
{% block extra_head %}
|
||||||
{{ super() }}
|
{{ super() }}
|
||||||
<script src="{{ urls.static('autocomplete.js') }}" defer></script>
|
<script src="{{ static('autocomplete.js') }}" defer></script>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,8 @@
|
||||||
{% extends "base.html" %}
|
{% extends "base.html" %}
|
||||||
|
|
||||||
{% block title %}Permission Check{% endblock %}
|
{% block title %}Explain a permission decision{% endblock %}
|
||||||
|
|
||||||
{% block extra_head %}
|
{% block extra_head %}
|
||||||
<script src="{{ base_url }}-/static/json-format-highlight-1.0.1.js"></script>
|
|
||||||
{% include "_permission_ui_styles.html" %}
|
{% include "_permission_ui_styles.html" %}
|
||||||
{% include "_debug_common_functions.html" %}
|
{% include "_debug_common_functions.html" %}
|
||||||
<style>
|
<style>
|
||||||
|
|
@ -13,29 +12,35 @@
|
||||||
border-radius: 5px;
|
border-radius: 5px;
|
||||||
}
|
}
|
||||||
#output.allowed {
|
#output.allowed {
|
||||||
background-color: #e8f5e9;
|
background-color: #f3fbf4;
|
||||||
border: 2px solid #4caf50;
|
border: 2px solid #4caf50;
|
||||||
}
|
}
|
||||||
#output.denied {
|
#output.denied {
|
||||||
background-color: #ffebee;
|
background-color: #fff7f7;
|
||||||
border: 2px solid #f44336;
|
border: 2px solid #f44336;
|
||||||
}
|
}
|
||||||
#output h2 {
|
#output h2 {
|
||||||
margin-top: 0;
|
margin-top: 0;
|
||||||
}
|
}
|
||||||
#output .result-badge {
|
#output h3 {
|
||||||
|
margin-bottom: 0.5em;
|
||||||
|
}
|
||||||
|
#output .result-badge,
|
||||||
|
.effect-badge,
|
||||||
|
.rule-status {
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
padding: 0.3em 0.8em;
|
padding: 0.2em 0.5em;
|
||||||
border-radius: 3px;
|
border-radius: 3px;
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
font-size: 1.1em;
|
|
||||||
}
|
}
|
||||||
#output .allowed-badge {
|
#output .allowed-badge,
|
||||||
background-color: #4caf50;
|
.effect-allow {
|
||||||
|
background-color: #2e7d32;
|
||||||
color: white;
|
color: white;
|
||||||
}
|
}
|
||||||
#output .denied-badge {
|
#output .denied-badge,
|
||||||
background-color: #f44336;
|
.effect-deny {
|
||||||
|
background-color: #c62828;
|
||||||
color: white;
|
color: white;
|
||||||
}
|
}
|
||||||
.details-section {
|
.details-section {
|
||||||
|
|
@ -48,70 +53,130 @@
|
||||||
.details-section dd {
|
.details-section dd {
|
||||||
margin-left: 1em;
|
margin-left: 1em;
|
||||||
}
|
}
|
||||||
|
.explanation-section {
|
||||||
|
background: rgba(255, 255, 255, 0.75);
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
border-radius: 4px;
|
||||||
|
margin-top: 1em;
|
||||||
|
padding: 0 1em 1em;
|
||||||
|
}
|
||||||
|
.rules-table {
|
||||||
|
border-collapse: collapse;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
.rules-table th,
|
||||||
|
.rules-table td {
|
||||||
|
border-bottom: 1px solid #ddd;
|
||||||
|
padding: 0.5em;
|
||||||
|
text-align: left;
|
||||||
|
vertical-align: top;
|
||||||
|
}
|
||||||
|
.rule-status {
|
||||||
|
background: #e8f5e9;
|
||||||
|
color: #1b5e20;
|
||||||
|
}
|
||||||
|
.rule-ignored {
|
||||||
|
background: #eee;
|
||||||
|
color: #555;
|
||||||
|
font-weight: normal;
|
||||||
|
}
|
||||||
|
.requirement-allowed {
|
||||||
|
color: #1b5e20;
|
||||||
|
}
|
||||||
|
.requirement-denied {
|
||||||
|
color: #b71c1c;
|
||||||
|
}
|
||||||
|
@media only screen and (max-width: 576px) {
|
||||||
|
.rules-table,
|
||||||
|
.rules-table tbody,
|
||||||
|
.rules-table tr,
|
||||||
|
.rules-table td {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
.rules-table thead {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
.rules-table td::before {
|
||||||
|
content: attr(data-label) ": ";
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<h1>Permission check</h1>
|
<h1>Explain a permission decision</h1>
|
||||||
|
|
||||||
{% set current_tab = "check" %}
|
{% set current_tab = "check" %}
|
||||||
{% include "_permissions_debug_tabs.html" %}
|
{% include "_permissions_debug_tabs.html" %}
|
||||||
|
|
||||||
<p>Use this tool to test permission checks for the current actor. It queries the <code>/-/check.json</code> API endpoint.</p>
|
<p>Test an actor, action and resource. The result explains which rules matched, which specificity level won, and whether actor restrictions or required actions changed the verdict.</p>
|
||||||
|
|
||||||
{% if request.actor %}
|
|
||||||
<p>Current actor: <strong>{{ request.actor.get("id", "anonymous") }}</strong></p>
|
|
||||||
{% else %}
|
|
||||||
<p>Current actor: <strong>anonymous (not logged in)</strong></p>
|
|
||||||
{% endif %}
|
|
||||||
|
|
||||||
<div class="permission-form">
|
<div class="permission-form">
|
||||||
<form id="check-form" method="get" action="{{ urls.path("-/check") }}">
|
<form id="check-form" method="get" action="{{ urls.path('-/check') }}">
|
||||||
<div class="form-section">
|
<div class="form-section">
|
||||||
<label for="action">Action (permission name):</label>
|
<label for="actor">Actor JSON:</label>
|
||||||
|
<textarea class="permission-textarea" id="actor" name="actor">{{ actor_json }}</textarea>
|
||||||
|
<small>Use <code>null</code> for an anonymous actor. This actor is simulated; it does not change who you are signed in as.</small>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="form-section">
|
||||||
|
<label for="action">Action:</label>
|
||||||
<select id="action" name="action" required>
|
<select id="action" name="action" required>
|
||||||
<option value="">Select an action...</option>
|
<option value="">Select an action...</option>
|
||||||
{% for action_name in sorted_actions %}
|
{% for action in actions %}
|
||||||
<option value="{{ action_name }}">{{ action_name }}</option>
|
<option value="{{ action.name }}">{{ action.name }}{% if action.description %} — {{ action.description }}{% endif %}</option>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</select>
|
</select>
|
||||||
<small>The permission action to check</small>
|
<small id="action-help">The operation to evaluate</small>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-section">
|
<div class="form-section" id="parent-section">
|
||||||
<label for="parent">Parent resource (optional):</label>
|
<label for="parent">Parent resource:</label>
|
||||||
<input type="text" id="parent" name="parent" placeholder="e.g., database name">
|
<input type="text" id="parent" name="parent" placeholder="e.g., database name">
|
||||||
<small>For database-level permissions, specify the database name</small>
|
<small>The database or other parent resource</small>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-section">
|
<div class="form-section" id="child-section">
|
||||||
<label for="child">Child resource (optional):</label>
|
<label for="child">Child resource:</label>
|
||||||
<input type="text" id="child" name="child" placeholder="e.g., table name">
|
<input type="text" id="child" name="child" placeholder="e.g., table or query name">
|
||||||
<small>For table-level permissions, specify the table name (requires parent)</small>
|
<small>The table, query or other child resource</small>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-actions">
|
<div class="form-actions">
|
||||||
<button type="submit" class="submit-btn" id="submit-btn">Check Permission</button>
|
<button type="submit" class="submit-btn" id="submit-btn">Explain decision</button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div id="output" style="display: none;">
|
<div id="output" style="display: none;">
|
||||||
<h2>Result: <span class="result-badge" id="result-badge"></span></h2>
|
<h2>Result: <span class="result-badge" id="result-badge"></span></h2>
|
||||||
|
<p id="result-summary"></p>
|
||||||
|
|
||||||
<dl class="details-section">
|
<dl class="details-section">
|
||||||
|
<dt>Actor:</dt>
|
||||||
|
<dd><code id="result-actor"></code></dd>
|
||||||
<dt>Action:</dt>
|
<dt>Action:</dt>
|
||||||
<dd id="result-action"></dd>
|
<dd><code id="result-action"></code></dd>
|
||||||
|
<dt>Resource:</dt>
|
||||||
<dt>Resource Path:</dt>
|
<dd><code id="result-resource"></code></dd>
|
||||||
<dd id="result-resource"></dd>
|
|
||||||
|
|
||||||
<dt>Actor ID:</dt>
|
|
||||||
<dd id="result-actor"></dd>
|
|
||||||
|
|
||||||
<div id="additional-details"></div>
|
|
||||||
</dl>
|
</dl>
|
||||||
|
|
||||||
|
<section class="explanation-section">
|
||||||
|
<h3>Matching rules</h3>
|
||||||
|
<div id="matching-rules"></div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<section class="explanation-section" id="restrictions-section">
|
||||||
|
<h3>Actor restrictions</h3>
|
||||||
|
<div id="restriction-results"></div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<section class="explanation-section" id="requirements-section">
|
||||||
|
<h3>Required actions</h3>
|
||||||
|
<div id="requirement-results"></div>
|
||||||
|
</section>
|
||||||
|
|
||||||
<details style="margin-top: 1em;">
|
<details style="margin-top: 1em;">
|
||||||
<summary style="cursor: pointer; font-weight: bold;">Raw JSON response</summary>
|
<summary style="cursor: pointer; font-weight: bold;">Raw JSON response</summary>
|
||||||
<pre id="raw-json" style="margin-top: 1em; padding: 1em; background-color: #f5f5f5; border: 1px solid #ddd; border-radius: 3px; overflow-x: auto;"></pre>
|
<pre id="raw-json" style="margin-top: 1em; padding: 1em; background-color: #f5f5f5; border: 1px solid #ddd; border-radius: 3px; overflow-x: auto;"></pre>
|
||||||
|
|
@ -119,152 +184,134 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
const actions = Object.fromEntries({{ actions|tojson }}.map(action => [action.name, action]));
|
||||||
const form = document.getElementById('check-form');
|
const form = document.getElementById('check-form');
|
||||||
const output = document.getElementById('output');
|
const output = document.getElementById('output');
|
||||||
const submitBtn = document.getElementById('submit-btn');
|
const submitBtn = document.getElementById('submit-btn');
|
||||||
|
const actionSelect = document.getElementById('action');
|
||||||
|
|
||||||
|
function updateResourceFields() {
|
||||||
|
const action = actions[actionSelect.value];
|
||||||
|
document.getElementById('parent-section').style.display = action && action.takes_parent ? 'block' : 'none';
|
||||||
|
document.getElementById('child-section').style.display = action && action.takes_child ? 'block' : 'none';
|
||||||
|
let help = action && action.description ? action.description : 'The operation to evaluate';
|
||||||
|
if (action && action.also_requires) {
|
||||||
|
help += `; also requires ${action.also_requires}`;
|
||||||
|
}
|
||||||
|
document.getElementById('action-help').textContent = help;
|
||||||
|
}
|
||||||
|
|
||||||
async function performCheck() {
|
async function performCheck() {
|
||||||
submitBtn.disabled = true;
|
submitBtn.disabled = true;
|
||||||
submitBtn.textContent = 'Checking...';
|
submitBtn.textContent = 'Explaining...';
|
||||||
|
const params = new URLSearchParams(new FormData(form));
|
||||||
const formData = new FormData(form);
|
|
||||||
const params = new URLSearchParams();
|
|
||||||
|
|
||||||
for (const [key, value] of formData.entries()) {
|
|
||||||
if (value) {
|
|
||||||
params.append(key, value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch('{{ urls.path("-/check.json") }}?' + params.toString(), {
|
const response = await fetch('{{ urls.path("-/check.json") }}?' + params.toString(), {
|
||||||
method: 'GET',
|
headers: {'Accept': 'application/json'}
|
||||||
headers: {
|
|
||||||
'Accept': 'application/json',
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
displayResult(data);
|
displayResult(data);
|
||||||
} else {
|
} else {
|
||||||
displayError(data);
|
displayError(data);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
alert('Error: ' + error.message);
|
displayError({error: error.message});
|
||||||
} finally {
|
} finally {
|
||||||
submitBtn.disabled = false;
|
submitBtn.disabled = false;
|
||||||
submitBtn.textContent = 'Check Permission';
|
submitBtn.textContent = 'Explain decision';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Populate form on initial load
|
|
||||||
(function() {
|
|
||||||
const params = populateFormFromURL();
|
|
||||||
const action = params.get('action');
|
|
||||||
if (action) {
|
|
||||||
performCheck();
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
|
|
||||||
function displayResult(data) {
|
function displayResult(data) {
|
||||||
output.style.display = 'block';
|
output.style.display = 'block';
|
||||||
|
|
||||||
// Set badge and styling
|
|
||||||
const resultBadge = document.getElementById('result-badge');
|
const resultBadge = document.getElementById('result-badge');
|
||||||
if (data.allowed) {
|
output.className = data.allowed ? 'allowed' : 'denied';
|
||||||
output.className = 'allowed';
|
resultBadge.className = `result-badge ${data.allowed ? 'allowed-badge' : 'denied-badge'}`;
|
||||||
resultBadge.className = 'result-badge allowed-badge';
|
resultBadge.textContent = data.allowed ? 'ALLOWED ✓' : 'DENIED ✗';
|
||||||
resultBadge.textContent = 'ALLOWED ✓';
|
document.getElementById('result-summary').textContent = data.explanation.summary;
|
||||||
} else {
|
document.getElementById('result-actor').textContent = data.actor === null ? 'anonymous' : JSON.stringify(data.actor);
|
||||||
output.className = 'denied';
|
document.getElementById('result-action').textContent = data.action;
|
||||||
resultBadge.className = 'result-badge denied-badge';
|
document.getElementById('result-resource').textContent = data.resource.path;
|
||||||
resultBadge.textContent = 'DENIED ✗';
|
displayRules(data.explanation);
|
||||||
|
displayRestrictions(data.explanation.restrictions);
|
||||||
|
displayRequirements(data.explanation.required_actions);
|
||||||
|
document.getElementById('raw-json').textContent = JSON.stringify(data, null, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
function displayRules(explanation) {
|
||||||
|
const container = document.getElementById('matching-rules');
|
||||||
|
if (!explanation.matched_rules.length) {
|
||||||
|
container.innerHTML = '<p>No rules matched. Datasette denies access when there is no matching rule.</p>';
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
let html = '<table class="rules-table"><thead><tr><th>Effect</th><th>Scope</th><th>Source</th><th>Reason</th><th>Role in decision</th></tr></thead><tbody>';
|
||||||
// Basic details
|
for (const rule of explanation.matched_rules) {
|
||||||
document.getElementById('result-action').textContent = data.action || 'N/A';
|
const status = rule.decisive
|
||||||
document.getElementById('result-resource').textContent = data.resource?.path || '/';
|
? '<span class="rule-status">Decisive</span>'
|
||||||
document.getElementById('result-actor').textContent = data.actor_id || 'anonymous';
|
: `<span class="rule-status rule-ignored">${escapeHtml(rule.ignored_because)}</span>`;
|
||||||
|
html += '<tr>';
|
||||||
// Additional details
|
html += `<td data-label="Effect"><span class="effect-badge effect-${rule.effect}">${rule.effect.toUpperCase()}</span></td>`;
|
||||||
const additionalDetails = document.getElementById('additional-details');
|
html += `<td data-label="Scope">${escapeHtml(rule.scope)}</td>`;
|
||||||
additionalDetails.innerHTML = '';
|
html += `<td data-label="Source"><code>${escapeHtml(rule.source || 'unknown')}</code></td>`;
|
||||||
|
html += `<td data-label="Reason">${escapeHtml(rule.reason || 'No reason supplied')}</td>`;
|
||||||
if (data.reason !== undefined) {
|
html += `<td data-label="Role in decision">${status}</td>`;
|
||||||
const dt = document.createElement('dt');
|
html += '</tr>';
|
||||||
dt.textContent = 'Reason:';
|
|
||||||
const dd = document.createElement('dd');
|
|
||||||
dd.textContent = data.reason || 'N/A';
|
|
||||||
additionalDetails.appendChild(dt);
|
|
||||||
additionalDetails.appendChild(dd);
|
|
||||||
}
|
}
|
||||||
|
container.innerHTML = html + '</tbody></table>';
|
||||||
|
}
|
||||||
|
|
||||||
if (data.source_plugin !== undefined) {
|
function displayRestrictions(restrictions) {
|
||||||
const dt = document.createElement('dt');
|
const section = document.getElementById('restrictions-section');
|
||||||
dt.textContent = 'Source Plugin:';
|
const container = document.getElementById('restriction-results');
|
||||||
const dd = document.createElement('dd');
|
section.style.display = restrictions.length ? 'block' : 'none';
|
||||||
dd.textContent = data.source_plugin || 'N/A';
|
container.innerHTML = restrictions.map(restriction => {
|
||||||
additionalDetails.appendChild(dt);
|
const className = restriction.allowed ? 'requirement-allowed' : 'requirement-denied';
|
||||||
additionalDetails.appendChild(dd);
|
const verdict = restriction.allowed ? 'INCLUDED ✓' : 'EXCLUDED ✗';
|
||||||
}
|
return `<p class="${className}"><strong>${verdict}</strong> by <code>${escapeHtml(restriction.source || 'unknown')}</code>: ${escapeHtml(restriction.reason)}</p>`;
|
||||||
|
}).join('');
|
||||||
|
}
|
||||||
|
|
||||||
if (data.used_default !== undefined) {
|
function displayRequirements(requirements) {
|
||||||
const dt = document.createElement('dt');
|
const section = document.getElementById('requirements-section');
|
||||||
dt.textContent = 'Used Default:';
|
const container = document.getElementById('requirement-results');
|
||||||
const dd = document.createElement('dd');
|
section.style.display = requirements.length ? 'block' : 'none';
|
||||||
dd.textContent = data.used_default ? 'Yes' : 'No';
|
container.innerHTML = requirements.map(requirement => {
|
||||||
additionalDetails.appendChild(dt);
|
const className = requirement.allowed ? 'requirement-allowed' : 'requirement-denied';
|
||||||
additionalDetails.appendChild(dd);
|
const verdict = requirement.allowed ? 'ALLOWED ✓' : 'DENIED ✗';
|
||||||
}
|
return `<p class="${className}"><strong>${escapeHtml(requirement.action)}: ${verdict}</strong> — ${escapeHtml(requirement.summary)}</p>`;
|
||||||
|
}).join('');
|
||||||
if (data.depth !== undefined) {
|
|
||||||
const dt = document.createElement('dt');
|
|
||||||
dt.textContent = 'Depth:';
|
|
||||||
const dd = document.createElement('dd');
|
|
||||||
dd.textContent = data.depth;
|
|
||||||
additionalDetails.appendChild(dt);
|
|
||||||
additionalDetails.appendChild(dd);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Raw JSON
|
|
||||||
document.getElementById('raw-json').innerHTML = jsonFormatHighlight(data);
|
|
||||||
|
|
||||||
// Scroll to output
|
|
||||||
output.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function displayError(data) {
|
function displayError(data) {
|
||||||
output.style.display = 'block';
|
output.style.display = 'block';
|
||||||
output.className = 'denied';
|
output.className = 'denied';
|
||||||
|
|
||||||
const resultBadge = document.getElementById('result-badge');
|
const resultBadge = document.getElementById('result-badge');
|
||||||
resultBadge.className = 'result-badge denied-badge';
|
resultBadge.className = 'result-badge denied-badge';
|
||||||
resultBadge.textContent = 'ERROR';
|
resultBadge.textContent = 'ERROR';
|
||||||
|
document.getElementById('result-summary').textContent = data.error || 'Unknown error';
|
||||||
document.getElementById('result-action').textContent = 'N/A';
|
document.getElementById('result-actor').textContent = '—';
|
||||||
document.getElementById('result-resource').textContent = 'N/A';
|
document.getElementById('result-action').textContent = '—';
|
||||||
document.getElementById('result-actor').textContent = 'N/A';
|
document.getElementById('result-resource').textContent = '—';
|
||||||
|
document.getElementById('matching-rules').innerHTML = '';
|
||||||
const additionalDetails = document.getElementById('additional-details');
|
document.getElementById('restrictions-section').style.display = 'none';
|
||||||
additionalDetails.innerHTML = '<dt>Error:</dt><dd>' + (data.error || 'Unknown error') + '</dd>';
|
document.getElementById('requirements-section').style.display = 'none';
|
||||||
|
document.getElementById('raw-json').textContent = JSON.stringify(data, null, 2);
|
||||||
document.getElementById('raw-json').innerHTML = jsonFormatHighlight(data);
|
|
||||||
|
|
||||||
output.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Disable child input if parent is empty
|
form.addEventListener('submit', event => {
|
||||||
const parentInput = document.getElementById('parent');
|
event.preventDefault();
|
||||||
const childInput = document.getElementById('child');
|
performCheck();
|
||||||
|
|
||||||
childInput.addEventListener('focus', () => {
|
|
||||||
if (!parentInput.value) {
|
|
||||||
alert('Please specify a parent resource first before adding a child resource.');
|
|
||||||
parentInput.focus();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
</script>
|
actionSelect.addEventListener('change', updateResourceFields);
|
||||||
|
|
||||||
|
(function initializeFromUrl() {
|
||||||
|
const params = populateFormFromURL();
|
||||||
|
updateResourceFields();
|
||||||
|
if (params.get('action')) {
|
||||||
|
performCheck();
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
</script>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
{% extends "base.html" %}
|
{% extends "base.html" %}
|
||||||
|
|
||||||
{% block title %}Debug permissions{% endblock %}
|
{% block title %}Permission activity{% endblock %}
|
||||||
|
|
||||||
{% block extra_head %}
|
{% block extra_head %}
|
||||||
{% include "_permission_ui_styles.html" %}
|
{% include "_permission_ui_styles.html" %}
|
||||||
|
|
@ -20,60 +20,45 @@
|
||||||
.check-action, .check-when, .check-result {
|
.check-action, .check-when, .check-result {
|
||||||
font-size: 1.3em;
|
font-size: 1.3em;
|
||||||
}
|
}
|
||||||
textarea {
|
|
||||||
height: 10em;
|
|
||||||
width: 95%;
|
|
||||||
box-sizing: border-box;
|
|
||||||
padding: 0.5em;
|
|
||||||
border: 2px dotted black;
|
|
||||||
}
|
|
||||||
.two-col {
|
|
||||||
display: inline-block;
|
|
||||||
width: 48%;
|
|
||||||
}
|
|
||||||
.two-col label {
|
|
||||||
width: 48%;
|
|
||||||
}
|
|
||||||
@media only screen and (max-width: 576px) {
|
|
||||||
.two-col {
|
|
||||||
width: 100%;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</style>
|
</style>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<h1>Permission playground</h1>
|
<h1>Permission activity</h1>
|
||||||
|
|
||||||
{% set current_tab = "permissions" %}
|
{% set current_tab = "permissions" %}
|
||||||
{% include "_permissions_debug_tabs.html" %}
|
{% include "_permissions_debug_tabs.html" %}
|
||||||
|
|
||||||
<p>This tool lets you simulate an actor and a permission check for that actor.</p>
|
<h2>Raw simulator</h2>
|
||||||
|
|
||||||
|
<p>This form runs a hypothetical permission check and returns its raw explanation JSON. Use the <a href="{{ urls.path('-/check') }}">Explain tool</a> for a visual explanation of the same decision.</p>
|
||||||
|
|
||||||
<div class="permission-form">
|
<div class="permission-form">
|
||||||
<form action="{{ urls.path('-/permissions') }}" id="debug-post" method="post">
|
<form action="{{ urls.path('-/permissions') }}" id="debug-post" method="post">
|
||||||
<div class="two-col">
|
<div class="permission-form-grid">
|
||||||
<div class="form-section">
|
<div>
|
||||||
<label>Actor</label>
|
<div class="form-section">
|
||||||
<textarea name="actor">{% if actor_input %}{{ actor_input }}{% else %}{"id": "root"}{% endif %}</textarea>
|
<label for="activity-actor">Actor</label>
|
||||||
|
<textarea class="permission-textarea" id="activity-actor" name="actor">{% if actor_input %}{{ actor_input }}{% else %}{"id": "root"}{% endif %}</textarea>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
<div>
|
||||||
<div class="two-col" style="vertical-align: top">
|
<div class="form-section">
|
||||||
<div class="form-section">
|
<label for="permission">Action</label>
|
||||||
<label for="permission">Action</label>
|
<select name="permission" id="permission">
|
||||||
<select name="permission" id="permission">
|
{% for permission in permissions %}
|
||||||
{% for permission in permissions %}
|
<option value="{{ permission.name }}">{{ permission.name }}</option>
|
||||||
<option value="{{ permission.name }}">{{ permission.name }}</option>
|
{% endfor %}
|
||||||
{% endfor %}
|
</select>
|
||||||
</select>
|
</div>
|
||||||
</div>
|
<div class="form-section">
|
||||||
<div class="form-section">
|
<label for="resource_1">Parent</label>
|
||||||
<label for="resource_1">Parent</label>
|
<input type="text" id="resource_1" name="resource_1" placeholder="e.g., database name">
|
||||||
<input type="text" id="resource_1" name="resource_1" placeholder="e.g., database name">
|
</div>
|
||||||
</div>
|
<div class="form-section">
|
||||||
<div class="form-section">
|
<label for="resource_2">Child</label>
|
||||||
<label for="resource_2">Child</label>
|
<input type="text" id="resource_2" name="resource_2" placeholder="e.g., table name">
|
||||||
<input type="text" id="resource_2" name="resource_2" placeholder="e.g., table name">
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-actions">
|
<div class="form-actions">
|
||||||
|
|
@ -125,7 +110,7 @@ debugPost.addEventListener('submit', function(ev) {
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<h1>Recent permissions checks</h1>
|
<h2>Recent permission checks</h2>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
{% if filter != "all" %}<a href="?filter=all">All</a>{% else %}<strong>All</strong>{% endif %},
|
{% if filter != "all" %}<a href="?filter=all">All</a>{% else %}<strong>All</strong>{% endif %},
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@
|
||||||
{% block title %}Permission Rules{% endblock %}
|
{% block title %}Permission Rules{% endblock %}
|
||||||
|
|
||||||
{% block extra_head %}
|
{% block extra_head %}
|
||||||
<script src="{{ base_url }}-/static/json-format-highlight-1.0.1.js"></script>
|
|
||||||
{% include "_permission_ui_styles.html" %}
|
{% include "_permission_ui_styles.html" %}
|
||||||
{% include "_debug_common_functions.html" %}
|
{% include "_debug_common_functions.html" %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
@ -37,7 +36,7 @@
|
||||||
|
|
||||||
<div class="form-section">
|
<div class="form-section">
|
||||||
<label for="page_size">Page size:</label>
|
<label for="page_size">Page size:</label>
|
||||||
<input type="number" id="page_size" name="page_size" value="50" min="1" max="200" style="max-width: 100px;">
|
<input type="number" id="page_size" name="_size" value="50" min="1" max="200">
|
||||||
<small>Number of results per page (max 200)</small>
|
<small>Number of results per page (max 200)</small>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
@ -75,7 +74,7 @@ const submitBtn = document.getElementById('submit-btn');
|
||||||
(function() {
|
(function() {
|
||||||
const params = populateFormFromURL();
|
const params = populateFormFromURL();
|
||||||
const action = params.get('action');
|
const action = params.get('action');
|
||||||
const page = params.get('page');
|
const page = params.get('_page');
|
||||||
if (action) {
|
if (action) {
|
||||||
fetchResults(page ? parseInt(page) : 1);
|
fetchResults(page ? parseInt(page) : 1);
|
||||||
}
|
}
|
||||||
|
|
@ -89,14 +88,14 @@ async function fetchResults(page = 1) {
|
||||||
const params = new URLSearchParams();
|
const params = new URLSearchParams();
|
||||||
|
|
||||||
for (const [key, value] of formData.entries()) {
|
for (const [key, value] of formData.entries()) {
|
||||||
if (value && key !== 'page_size') {
|
if (value && key !== '_size' && key !== '_page') {
|
||||||
params.append(key, value);
|
params.append(key, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const pageSize = document.getElementById('page_size').value || '50';
|
const pageSize = document.getElementById('page_size').value || '50';
|
||||||
params.append('page', page.toString());
|
params.append('_page', page.toString());
|
||||||
params.append('page_size', pageSize);
|
params.append('_size', pageSize);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch('{{ urls.path("-/rules.json") }}?' + params.toString(), {
|
const response = await fetch('{{ urls.path("-/rules.json") }}?' + params.toString(), {
|
||||||
|
|
@ -185,7 +184,7 @@ function displayResults(data) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update raw JSON
|
// Update raw JSON
|
||||||
document.getElementById('raw-json').innerHTML = jsonFormatHighlight(data);
|
document.getElementById('raw-json').textContent = JSON.stringify(data, null, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
function displayError(data) {
|
function displayError(data) {
|
||||||
|
|
@ -195,7 +194,7 @@ function displayError(data) {
|
||||||
|
|
||||||
resultsContent.innerHTML = `<div class="error-message">Error: ${escapeHtml(data.error || 'Unknown error')}</div>`;
|
resultsContent.innerHTML = `<div class="error-message">Error: ${escapeHtml(data.error || 'Unknown error')}</div>`;
|
||||||
|
|
||||||
document.getElementById('raw-json').innerHTML = jsonFormatHighlight(data);
|
document.getElementById('raw-json').textContent = JSON.stringify(data, null, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
|
||||||
|
|
@ -56,6 +56,11 @@ form.sql.core input[data-execute-write-submit]:disabled {
|
||||||
cursor: not-allowed;
|
cursor: not-allowed;
|
||||||
opacity: 1;
|
opacity: 1;
|
||||||
}
|
}
|
||||||
|
.execute-write form.sql .sql-editor-min-lines .cm-content,
|
||||||
|
.execute-write form.sql .sql-editor-min-lines .cm-gutter {
|
||||||
|
/* Four visible editor lines without adding blank lines to the SQL value. */
|
||||||
|
min-height: calc(5.6em + 8px);
|
||||||
|
}
|
||||||
.execute-write-disabled-reason {
|
.execute-write-disabled-reason {
|
||||||
color: #4f5b6d;
|
color: #4f5b6d;
|
||||||
font-size: 0.85rem;
|
font-size: 0.85rem;
|
||||||
|
|
@ -93,20 +98,25 @@ form.sql.core input[data-execute-write-submit]:disabled {
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
<form class="sql core" action="{{ urls.database(database) }}/-/execute-write" method="post" data-analyze-url="{{ urls.database(database) }}/-/execute-write/analyze">
|
<form class="sql core" action="{{ urls.database(database) }}/-/execute-write" method="post" data-analyze-url="{{ urls.database(database) }}/-/execute-write/analyze">
|
||||||
{% if write_template_tables %}
|
{% if write_create_table_template_sql or write_template_tables %}
|
||||||
<div class="execute-write-template-menu">
|
<div class="execute-write-template-menu">
|
||||||
<details>
|
<details>
|
||||||
<summary>Start with a template</summary>
|
<summary>Start with a template</summary>
|
||||||
<p class="execute-write-template-controls">
|
<p class="execute-write-template-controls">
|
||||||
<label for="execute-write-template-table">Table</label>
|
{% if write_create_table_template_sql %}
|
||||||
<select id="execute-write-template-table">
|
<button type="button" data-sql-template="create" data-template-sql="{{ write_create_table_template_sql }}">Create table</button>
|
||||||
{% for table_name, table in write_template_tables|dictsort %}
|
{% endif %}
|
||||||
<option value="{{ table_name }}"{% for operation, template_sql in table.templates|dictsort %} data-template-{{ operation }}-sql="{{ template_sql }}"{% endfor %}>{{ table_name }}</option>
|
{% if write_template_tables %}
|
||||||
|
<label for="execute-write-template-table">{% if write_create_table_template_sql %}or table:{% else %}Table{% endif %}</label>
|
||||||
|
<select id="execute-write-template-table">
|
||||||
|
{% for table_name, table in write_template_tables|dictsort %}
|
||||||
|
<option value="{{ table_name }}"{% for operation, template_sql in table.templates|dictsort %} data-template-{{ operation }}-sql="{{ template_sql }}"{% endfor %}>{{ table_name }}</option>
|
||||||
|
{% endfor %}
|
||||||
|
</select>
|
||||||
|
{% for operation in write_template_operations %}
|
||||||
|
<button type="button" data-sql-template="{{ operation.name }}">{{ operation.label }}</button>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</select>
|
{% endif %}
|
||||||
{% for operation in write_template_operations %}
|
|
||||||
<button type="button" data-sql-template="{{ operation.name }}">{{ operation.label }}</button>
|
|
||||||
{% endfor %}
|
|
||||||
</p>
|
</p>
|
||||||
</details>
|
</details>
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -114,7 +124,7 @@ form.sql.core input[data-execute-write-submit]:disabled {
|
||||||
<p class="message-warning execute-write-template-unavailable">There are no tables that you can currently edit.</p>
|
<p class="message-warning execute-write-template-unavailable">There are no tables that you can currently edit.</p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
<p class="sql-editor"><textarea id="sql-editor" name="sql"{% if sql %} style="height: {{ sql.split("\n")|length + 2 }}em"{% endif %}>{{ sql }}</textarea></p>
|
<p class="sql-editor{% if not sql %} sql-editor-min-lines{% endif %}"><textarea id="sql-editor" name="sql"{% if sql %} style="height: {{ sql.split("\n")|length + 2 }}em"{% endif %}>{{ sql }}</textarea></p>
|
||||||
|
|
||||||
{% set sql_parameters_section_id = "execute-write-parameters-section" %}
|
{% set sql_parameters_section_id = "execute-write-parameters-section" %}
|
||||||
{% set sql_parameters_allow_expand = true %}
|
{% set sql_parameters_allow_expand = true %}
|
||||||
|
|
@ -159,19 +169,13 @@ form.sql.core input[data-execute-write-submit]:disabled {
|
||||||
</p>
|
</p>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
<script>
|
|
||||||
const executeWriteSqlInput = document.querySelector("textarea#sql-editor");
|
|
||||||
if (executeWriteSqlInput && !executeWriteSqlInput.value) {
|
|
||||||
executeWriteSqlInput.value = "\n\n\n";
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
|
|
||||||
{% include "_codemirror_foot.html" %}
|
{% include "_codemirror_foot.html" %}
|
||||||
{% include "_sql_parameter_scripts.html" %}
|
{% include "_sql_parameter_scripts.html" %}
|
||||||
{% include "_execute_write_analysis_scripts.html" %}
|
{% include "_execute_write_analysis_scripts.html" %}
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
window.addEventListener("DOMContentLoaded", () => {
|
window.addEventListener("DOMContentLoaded", () => {
|
||||||
|
const executeWriteSqlInput = document.querySelector("textarea#sql-editor");
|
||||||
const form = document.querySelector("form.sql.core");
|
const form = document.querySelector("form.sql.core");
|
||||||
const analysisSection = document.querySelector("#execute-write-analysis-section");
|
const analysisSection = document.querySelector("#execute-write-analysis-section");
|
||||||
const submitButton = form
|
const submitButton = form
|
||||||
|
|
@ -252,11 +256,12 @@ window.addEventListener("DOMContentLoaded", () => {
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{% if write_template_tables %}
|
{% if write_create_table_template_sql or write_template_tables %}
|
||||||
<script>
|
<script>
|
||||||
window.addEventListener("DOMContentLoaded", () => {
|
window.addEventListener("DOMContentLoaded", () => {
|
||||||
const tableSelect = document.querySelector("#execute-write-template-table");
|
const tableSelect = document.querySelector("#execute-write-template-table");
|
||||||
const templateButtons = document.querySelectorAll("[data-sql-template]");
|
const templateButtons = document.querySelectorAll("[data-sql-template]");
|
||||||
|
const sqlInput = document.querySelector("textarea#sql-editor");
|
||||||
|
|
||||||
function dataKey(operation) {
|
function dataKey(operation) {
|
||||||
return `template${operation.charAt(0).toUpperCase()}${operation.slice(1)}Sql`;
|
return `template${operation.charAt(0).toUpperCase()}${operation.slice(1)}Sql`;
|
||||||
|
|
@ -266,26 +271,59 @@ window.addEventListener("DOMContentLoaded", () => {
|
||||||
return tableSelect ? tableSelect.options[tableSelect.selectedIndex] : null;
|
return tableSelect ? tableSelect.options[tableSelect.selectedIndex] : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function templateSql(operation) {
|
function templateSql(button) {
|
||||||
|
if (button.dataset.templateSql) {
|
||||||
|
return button.dataset.templateSql;
|
||||||
|
}
|
||||||
|
const operation = button.dataset.sqlTemplate;
|
||||||
const option = selectedOption();
|
const option = selectedOption();
|
||||||
return option ? option.dataset[dataKey(operation)] || "" : "";
|
return option ? option.dataset[dataKey(operation)] || "" : "";
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateTemplateButtons() {
|
function updateTemplateButtons() {
|
||||||
templateButtons.forEach((button) => {
|
templateButtons.forEach((button) => {
|
||||||
button.hidden = !templateSql(button.dataset.sqlTemplate);
|
button.hidden = !templateSql(button);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function updateSqlUrl(sql) {
|
||||||
|
if (!window.history || !window.history.replaceState) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const url = new URL(window.location.href);
|
||||||
|
url.searchParams.set("sql", sql);
|
||||||
|
window.history.replaceState(null, "", url.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
function setEditorSql(sql) {
|
||||||
|
if (window.editor) {
|
||||||
|
window.editor.dispatch({
|
||||||
|
changes: {
|
||||||
|
from: 0,
|
||||||
|
to: window.editor.state.doc.length,
|
||||||
|
insert: sql,
|
||||||
|
},
|
||||||
|
selection: { anchor: sql.length },
|
||||||
|
});
|
||||||
|
window.editor.focus();
|
||||||
|
if (sqlInput) {
|
||||||
|
sqlInput.value = sql;
|
||||||
|
}
|
||||||
|
} else if (sqlInput) {
|
||||||
|
sqlInput.value = sql;
|
||||||
|
sqlInput.dispatchEvent(new Event("input", { bubbles: true }));
|
||||||
|
sqlInput.focus();
|
||||||
|
}
|
||||||
|
updateSqlUrl(sql);
|
||||||
|
}
|
||||||
|
|
||||||
templateButtons.forEach((button) => {
|
templateButtons.forEach((button) => {
|
||||||
button.addEventListener("click", () => {
|
button.addEventListener("click", () => {
|
||||||
const sql = templateSql(button.dataset.sqlTemplate);
|
const sql = templateSql(button);
|
||||||
if (!sql) {
|
if (!sql) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const url = new URL(window.location.href);
|
setEditorSql(sql);
|
||||||
url.searchParams.set("sql", sql);
|
|
||||||
window.location.href = url.toString();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
if (tableSelect) {
|
if (tableSelect) {
|
||||||
|
|
|
||||||
|
|
@ -26,8 +26,7 @@
|
||||||
{% if database.show_table_row_counts %}{{ "{:,}".format(database.hidden_table_rows_sum) }} rows in {% endif %}{{ database.hidden_tables_count }} hidden table{% if database.hidden_tables_count != 1 %}s{% endif -%}
|
{% if database.show_table_row_counts %}{{ "{:,}".format(database.hidden_table_rows_sum) }} rows in {% endif %}{{ database.hidden_tables_count }} hidden table{% if database.hidden_tables_count != 1 %}s{% endif -%}
|
||||||
{% endif -%}
|
{% endif -%}
|
||||||
{% if database.views_count -%}
|
{% if database.views_count -%}
|
||||||
{% if database.tables_count or database.hidden_tables_count %}, {% endif -%}
|
, {{ "{:,}".format(database.views_count) }} view{% if database.views_count != 1 %}s{% endif %}
|
||||||
{{ "{:,}".format(database.views_count) }} view{% if database.views_count != 1 %}s{% endif %}
|
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</p>
|
</p>
|
||||||
<p>{% for table in database.tables_and_views_truncated %}<a href="{{ urls.table(database.name, table.name) }}"{% if table.count %} title="{{ table.count }} rows"{% endif %}>{{ table.name }}</a>{% if table.private %} 🔒{% endif %}{% if not loop.last %}, {% endif %}{% endfor %}{% if database.tables_and_views_more %}, <a href="{{ urls.database(database.name) }}">...</a>{% endif %}</p>
|
<p>{% for table in database.tables_and_views_truncated %}<a href="{{ urls.table(database.name, table.name) }}"{% if table.count %} title="{{ table.count }} rows"{% endif %}>{{ table.name }}</a>{% if table.private %} 🔒{% endif %}{% if not loop.last %}, {% endif %}{% endfor %}{% if database.tables_and_views_more %}, <a href="{{ urls.database(database.name) }}">...</a>{% endif %}</p>
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<title>Datasette: Pattern Portfolio</title>
|
<title>Datasette: Pattern Portfolio</title>
|
||||||
<link rel="stylesheet" href="{{ base_url }}-/static/app.css?{{ app_css_hash }}">
|
<link rel="stylesheet" href="{{ static('app.css') }}">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||||
<meta name="robots" content="noindex">
|
<meta name="robots" content="noindex">
|
||||||
<style></style>
|
<style></style>
|
||||||
|
|
@ -202,9 +202,9 @@
|
||||||
<h3>3 rows
|
<h3>3 rows
|
||||||
where characteristic_id = 2
|
where characteristic_id = 2
|
||||||
</h3>
|
</h3>
|
||||||
<form class="filters" action="{{ base_url }}fixtures/roadside_attraction_characteristics" method="get">
|
<form class="core filters" action="{{ base_url }}fixtures/roadside_attraction_characteristics" method="get">
|
||||||
<div class="search-row"><label for="_search">Search:</label><input id="_search" type="search" name="_search" value=""></div>
|
<div class="search-row"><label for="_search">Search:</label><input id="_search" type="search" name="_search" value=""></div>
|
||||||
<div class="filter-row">
|
<div class="filter-row filter-controls-row">
|
||||||
<div class="select-wrapper">
|
<div class="select-wrapper">
|
||||||
<select name="_filter_column_1">
|
<select name="_filter_column_1">
|
||||||
<option value="">- remove filter -</option>
|
<option value="">- remove filter -</option>
|
||||||
|
|
@ -238,7 +238,7 @@
|
||||||
</select>
|
</select>
|
||||||
</div><input type="text" name="_filter_value_1" class="filter-value" value="2">
|
</div><input type="text" name="_filter_value_1" class="filter-value" value="2">
|
||||||
</div>
|
</div>
|
||||||
<div class="filter-row">
|
<div class="filter-row filter-controls-row">
|
||||||
<div class="select-wrapper">
|
<div class="select-wrapper">
|
||||||
<select name="_filter_column">
|
<select name="_filter_column">
|
||||||
<option value="">- column -</option>
|
<option value="">- column -</option>
|
||||||
|
|
@ -272,8 +272,8 @@
|
||||||
</select>
|
</select>
|
||||||
</div><input type="text" name="_filter_value" class="filter-value">
|
</div><input type="text" name="_filter_value" class="filter-value">
|
||||||
</div>
|
</div>
|
||||||
<div class="filter-row">
|
<div class="filter-row filter-actions-row">
|
||||||
<div class="select-wrapper small-screen-only">
|
<div class="select-wrapper">
|
||||||
<select name="_sort" id="sort_by">
|
<select name="_sort" id="sort_by">
|
||||||
<option value="">Sort...</option>
|
<option value="">Sort...</option>
|
||||||
<option value="rowid" selected>Sort by rowid</option>
|
<option value="rowid" selected>Sort by rowid</option>
|
||||||
|
|
@ -281,8 +281,8 @@
|
||||||
<option value="characteristic_id">Sort by characteristic_id</option>
|
<option value="characteristic_id">Sort by characteristic_id</option>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<label class="sort_by_desc small-screen-only"><input type="checkbox" name="_sort_by_desc"> descending</label>
|
<label class="sort_by_desc"><input type="checkbox" name="_sort_by_desc"> descending</label>
|
||||||
<input type="submit" value="Apply">
|
<input type="submit" value="Apply filters">
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,9 +7,9 @@
|
||||||
{% if row_mutation_ui %}
|
{% if row_mutation_ui %}
|
||||||
<script>window._datasetteTableData = {{ table_page_data|tojson }};</script>
|
<script>window._datasetteTableData = {{ table_page_data|tojson }};</script>
|
||||||
{% if table_page_data.foreignKeys %}
|
{% if table_page_data.foreignKeys %}
|
||||||
<script src="{{ urls.static('autocomplete.js') }}" defer></script>
|
<script src="{{ static('autocomplete.js') }}" defer></script>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<script src="{{ urls.static('edit-tools.js') }}?hash={{ edit_tools_js_hash }}" defer></script>
|
<script src="{{ static('edit-tools.js') }}" defer></script>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<style>
|
<style>
|
||||||
@media only screen and (max-width: 576px) {
|
@media only screen and (max-width: 576px) {
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,17 @@
|
||||||
{% extends "base.html" %}
|
{% extends "base.html" %}
|
||||||
|
|
||||||
{% block title %}{{ database }}: {{ table }}: {% if count or count == 0 %}{{ "{:,}".format(count) }} row{% if count == 1 %}{% else %}s{% endif %}{% endif %}{% if human_description_en %} {{ human_description_en }}{% endif %}{% endblock %}
|
{% block title %}{{ database }}: {{ table }}: {% if count_truncated %}{{ "{:,}".format(count - 1) }}+ rows{% elif count or count == 0 %}{{ "{:,}".format(count) }} row{% if count == 1 %}{% else %}s{% endif %}{% endif %}{% if human_description_en %} {{ human_description_en }}{% endif %}{% endblock %}
|
||||||
|
|
||||||
{% block extra_head %}
|
{% block extra_head %}
|
||||||
{{- super() -}}
|
{{- super() -}}
|
||||||
<script>window._datasetteTableData = {{ table_page_data|tojson }};</script>
|
<script>window._datasetteTableData = {{ table_page_data|tojson }};</script>
|
||||||
<script src="{{ urls.static('column-chooser.js') }}" defer></script>
|
<script src="{{ static('column-chooser.js') }}" defer></script>
|
||||||
{% if table_page_data.foreignKeys %}
|
{% if table_page_data.foreignKeys %}
|
||||||
<script src="{{ urls.static('autocomplete.js') }}" defer></script>
|
<script src="{{ static('autocomplete.js') }}" defer></script>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<script src="{{ urls.static('edit-tools.js') }}?hash={{ edit_tools_js_hash }}" defer></script>
|
<script src="{{ static('edit-tools.js') }}" defer></script>
|
||||||
<script src="{{ urls.static('table.js') }}?hash={{ table_js_hash }}" defer></script>
|
<script src="{{ static('table.js') }}" defer></script>
|
||||||
<script src="{{ urls.static('mobile-column-actions.js') }}" defer></script>
|
<script src="{{ static('mobile-column-actions.js') }}" defer></script>
|
||||||
<script>DATASETTE_ALLOW_FACET = {{ datasette_allow_facet }};</script>
|
<script>DATASETTE_ALLOW_FACET = {{ datasette_allow_facet }};</script>
|
||||||
<style>
|
<style>
|
||||||
@media only screen and (max-width: 576px) {
|
@media only screen and (max-width: 576px) {
|
||||||
|
|
@ -47,20 +47,21 @@
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% if count or human_description_en %}
|
{% if count or human_description_en %}
|
||||||
<h3>
|
<h3 class="table-summary">
|
||||||
{% if count == count_limit + 1 %}>{{ "{:,}".format(count_limit) }} rows
|
{% if count_truncated %}<span class="table-count" aria-live="polite">{{ "{:,}".format(count - 1) }}+ rows</span>
|
||||||
{% if allow_execute_sql and query.sql %} <a class="count-sql" style="font-size: 0.8em;" href="{{ urls.database_query(database, count_sql) }}">count all</a>{% endif %}
|
<button type="button" class="count-all" data-count-url="{{ urls.table(database, table) }}/-/count">count all</button>
|
||||||
|
<span class="count-error" role="alert"></span>
|
||||||
{% elif count or count == 0 %}{{ "{:,}".format(count) }} row{% if count == 1 %}{% else %}s{% endif %}{% endif %}
|
{% elif count or count == 0 %}{{ "{:,}".format(count) }} row{% if count == 1 %}{% else %}s{% endif %}{% endif %}
|
||||||
{% if human_description_en %}{{ human_description_en }}{% endif %}
|
{% if human_description_en %}<span class="table-summary-description">{{ human_description_en }}</span>{% endif %}
|
||||||
</h3>
|
</h3>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
<form class="core" class="filters" action="{{ urls.table(database, table) }}" method="get">
|
<form class="core filters" action="{{ urls.table(database, table) }}" method="get">
|
||||||
{% if supports_search %}
|
{% if supports_search %}
|
||||||
<div class="search-row"><label for="_search">Search:</label><input id="_search" type="search" name="_search" value="{{ search }}"></div>
|
<div class="search-row"><label for="_search">Search:</label><input id="_search" type="search" name="_search" value="{{ search }}"></div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% for column, lookup, value in filters.selections() %}
|
{% for column, lookup, value in filters.selections() %}
|
||||||
<div class="filter-row">
|
<div class="filter-row filter-controls-row">
|
||||||
<div class="select-wrapper">
|
<div class="select-wrapper">
|
||||||
<select name="_filter_column_{{ loop.index }}">
|
<select name="_filter_column_{{ loop.index }}">
|
||||||
<option value="">- remove filter -</option>
|
<option value="">- remove filter -</option>
|
||||||
|
|
@ -77,7 +78,7 @@
|
||||||
</div><input type="text" name="_filter_value_{{ loop.index }}" class="filter-value" value="{{ value }}">
|
</div><input type="text" name="_filter_value_{{ loop.index }}" class="filter-value" value="{{ value }}">
|
||||||
</div>
|
</div>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
<div class="filter-row">
|
<div class="filter-row filter-controls-row">
|
||||||
<div class="select-wrapper">
|
<div class="select-wrapper">
|
||||||
<select name="_filter_column">
|
<select name="_filter_column">
|
||||||
<option value="">- column -</option>
|
<option value="">- column -</option>
|
||||||
|
|
@ -93,9 +94,9 @@
|
||||||
</select>
|
</select>
|
||||||
</div><input type="text" name="_filter_value" class="filter-value">
|
</div><input type="text" name="_filter_value" class="filter-value">
|
||||||
</div>
|
</div>
|
||||||
<div class="filter-row">
|
<div class="filter-row filter-actions-row">
|
||||||
{% if is_sortable %}
|
{% if is_sortable %}
|
||||||
<div class="select-wrapper small-screen-only">
|
<div class="select-wrapper">
|
||||||
<select name="_sort" id="sort_by">
|
<select name="_sort" id="sort_by">
|
||||||
<option value="">Sort...</option>
|
<option value="">Sort...</option>
|
||||||
{% for column in display_columns %}
|
{% for column in display_columns %}
|
||||||
|
|
@ -105,12 +106,12 @@
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<label class="sort_by_desc small-screen-only"><input type="checkbox" name="_sort_by_desc"{% if sort_desc %} checked{% endif %}> descending</label>
|
<label class="sort_by_desc"><input type="checkbox" name="_sort_by_desc" tabindex="0"{% if sort_desc %} checked{% endif %}> descending</label>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% for key, value in form_hidden_args %}
|
{% for key, value in form_hidden_args %}
|
||||||
<input type="hidden" name="{{ key }}" value="{{ value }}">
|
<input type="hidden" name="{{ key }}" value="{{ value }}">
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
<input type="submit" value="Apply">
|
<input type="submit" value="Apply filters" tabindex="0">
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ from __future__ import annotations
|
||||||
|
|
||||||
import dataclasses
|
import dataclasses
|
||||||
import time
|
import time
|
||||||
from typing import TYPE_CHECKING, Optional
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
import itsdangerous
|
import itsdangerous
|
||||||
|
|
||||||
|
|
@ -18,6 +18,21 @@ if TYPE_CHECKING:
|
||||||
from datasette.app import Datasette
|
from datasette.app import Datasette
|
||||||
|
|
||||||
|
|
||||||
|
class TokenInvalid(Exception):
|
||||||
|
"""
|
||||||
|
Raised by a TokenHandler when a token it recognizes is invalid -
|
||||||
|
for example a bad signature, malformed payload or expired token.
|
||||||
|
|
||||||
|
Datasette responds to this with an HTTP 401 error. Handlers should
|
||||||
|
return None instead for tokens they do not recognize at all, so that
|
||||||
|
other registered handlers get a chance to verify them.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, message="Invalid token"):
|
||||||
|
self.message = message
|
||||||
|
super().__init__(message)
|
||||||
|
|
||||||
|
|
||||||
@dataclasses.dataclass
|
@dataclasses.dataclass
|
||||||
class TokenRestrictions:
|
class TokenRestrictions:
|
||||||
"""
|
"""
|
||||||
|
|
@ -35,24 +50,24 @@ class TokenRestrictions:
|
||||||
database: dict[str, list[str]] = dataclasses.field(default_factory=dict)
|
database: dict[str, list[str]] = dataclasses.field(default_factory=dict)
|
||||||
resource: dict[str, dict[str, list[str]]] = dataclasses.field(default_factory=dict)
|
resource: dict[str, dict[str, list[str]]] = dataclasses.field(default_factory=dict)
|
||||||
|
|
||||||
def allow_all(self, action: str) -> "TokenRestrictions":
|
def allow_all(self, action: str) -> TokenRestrictions:
|
||||||
"""Allow an action across all databases and resources."""
|
"""Allow an action across all databases and resources."""
|
||||||
self.all.append(action)
|
self.all.append(action)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def allow_database(self, database: str, action: str) -> "TokenRestrictions":
|
def allow_database(self, database: str, action: str) -> TokenRestrictions:
|
||||||
"""Allow an action on a specific database."""
|
"""Allow an action on a specific database."""
|
||||||
self.database.setdefault(database, []).append(action)
|
self.database.setdefault(database, []).append(action)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def allow_resource(
|
def allow_resource(
|
||||||
self, database: str, resource: str, action: str
|
self, database: str, resource: str, action: str
|
||||||
) -> "TokenRestrictions":
|
) -> TokenRestrictions:
|
||||||
"""Allow an action on a specific resource within a database."""
|
"""Allow an action on a specific resource within a database."""
|
||||||
self.resource.setdefault(database, {}).setdefault(resource, []).append(action)
|
self.resource.setdefault(database, {}).setdefault(resource, []).append(action)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def abbreviated(self, datasette: "Datasette") -> Optional[dict]:
|
def abbreviated(self, datasette: Datasette) -> dict | None:
|
||||||
"""
|
"""
|
||||||
Return the abbreviated ``_r`` dictionary shape for this set of
|
Return the abbreviated ``_r`` dictionary shape for this set of
|
||||||
restrictions, using action abbreviations registered with ``datasette``.
|
restrictions, using action abbreviations registered with ``datasette``.
|
||||||
|
|
@ -97,19 +112,23 @@ class TokenHandler:
|
||||||
|
|
||||||
async def create_token(
|
async def create_token(
|
||||||
self,
|
self,
|
||||||
datasette: "Datasette",
|
datasette: Datasette,
|
||||||
actor_id: str,
|
actor_id: str,
|
||||||
*,
|
*,
|
||||||
expires_after: Optional[int] = None,
|
expires_after: int | None = None,
|
||||||
restrictions: Optional[TokenRestrictions] = None,
|
restrictions: TokenRestrictions | None = None,
|
||||||
) -> str:
|
) -> str:
|
||||||
"""Create and return a token string for the given actor."""
|
"""Create and return a token string for the given actor."""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
async def verify_token(self, datasette: "Datasette", token: str) -> Optional[dict]:
|
async def verify_token(self, datasette: Datasette, token: str) -> dict | None:
|
||||||
"""
|
"""
|
||||||
Verify a token and return an actor dict, or None if this handler
|
Verify a token and return an actor dict.
|
||||||
does not recognize the token.
|
|
||||||
|
Return None if this handler does not recognize the token at all,
|
||||||
|
so other handlers can try it. Raise TokenInvalid if the token is
|
||||||
|
recognized but invalid (bad signature, malformed, expired) - the
|
||||||
|
request will fail with a 401 error.
|
||||||
"""
|
"""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
@ -123,11 +142,11 @@ class SignedTokenHandler(TokenHandler):
|
||||||
|
|
||||||
async def create_token(
|
async def create_token(
|
||||||
self,
|
self,
|
||||||
datasette: "Datasette",
|
datasette: Datasette,
|
||||||
actor_id: str,
|
actor_id: str,
|
||||||
*,
|
*,
|
||||||
expires_after: Optional[int] = None,
|
expires_after: int | None = None,
|
||||||
restrictions: Optional[TokenRestrictions] = None,
|
restrictions: TokenRestrictions | None = None,
|
||||||
) -> str:
|
) -> str:
|
||||||
if not datasette.setting("allow_signed_tokens"):
|
if not datasette.setting("allow_signed_tokens"):
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
|
|
@ -144,32 +163,35 @@ class SignedTokenHandler(TokenHandler):
|
||||||
token["_r"] = abbreviated
|
token["_r"] = abbreviated
|
||||||
return "dstok_{}".format(datasette.sign(token, namespace="token"))
|
return "dstok_{}".format(datasette.sign(token, namespace="token"))
|
||||||
|
|
||||||
async def verify_token(self, datasette: "Datasette", token: str) -> Optional[dict]:
|
async def verify_token(self, datasette: Datasette, token: str) -> dict | None:
|
||||||
prefix = "dstok_"
|
prefix = "dstok_"
|
||||||
|
|
||||||
if not datasette.setting("allow_signed_tokens"):
|
if not token.startswith(prefix):
|
||||||
|
# Not one of our tokens - leave it for other handlers
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
if not datasette.setting("allow_signed_tokens"):
|
||||||
|
raise TokenInvalid(
|
||||||
|
"Signed tokens are not enabled for this Datasette instance"
|
||||||
|
)
|
||||||
|
|
||||||
max_signed_tokens_ttl = datasette.setting("max_signed_tokens_ttl")
|
max_signed_tokens_ttl = datasette.setting("max_signed_tokens_ttl")
|
||||||
|
|
||||||
if not token.startswith(prefix):
|
|
||||||
return None
|
|
||||||
|
|
||||||
raw = token[len(prefix) :]
|
raw = token[len(prefix) :]
|
||||||
try:
|
try:
|
||||||
decoded = datasette.unsign(raw, namespace="token")
|
decoded = datasette.unsign(raw, namespace="token")
|
||||||
except itsdangerous.BadSignature:
|
except itsdangerous.BadSignature:
|
||||||
return None
|
raise TokenInvalid("Invalid token signature")
|
||||||
|
|
||||||
if "t" not in decoded:
|
if "t" not in decoded:
|
||||||
return None
|
raise TokenInvalid("Invalid token: no timestamp")
|
||||||
created = decoded["t"]
|
created = decoded["t"]
|
||||||
if not isinstance(created, int):
|
if not isinstance(created, int):
|
||||||
return None
|
raise TokenInvalid("Invalid token: invalid timestamp")
|
||||||
|
|
||||||
duration = decoded.get("d")
|
duration = decoded.get("d")
|
||||||
if duration is not None and not isinstance(duration, int):
|
if duration is not None and not isinstance(duration, int):
|
||||||
return None
|
raise TokenInvalid("Invalid token: invalid duration")
|
||||||
|
|
||||||
if (duration is None and max_signed_tokens_ttl) or (
|
if (duration is None and max_signed_tokens_ttl) or (
|
||||||
duration is not None
|
duration is not None
|
||||||
|
|
@ -178,9 +200,8 @@ class SignedTokenHandler(TokenHandler):
|
||||||
):
|
):
|
||||||
duration = max_signed_tokens_ttl
|
duration = max_signed_tokens_ttl
|
||||||
|
|
||||||
if duration:
|
if duration and time.time() - created > duration:
|
||||||
if time.time() - created > duration:
|
raise TokenInvalid("Token has expired")
|
||||||
return None
|
|
||||||
|
|
||||||
actor = {"id": decoded["a"], "token": "dstok"}
|
actor = {"id": decoded["a"], "token": "dstok"}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,11 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import json
|
||||||
|
import time
|
||||||
|
import traceback
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
from contextvars import ContextVar
|
from contextvars import ContextVar
|
||||||
|
|
||||||
from markupsafe import escape
|
from markupsafe import escape
|
||||||
import time
|
|
||||||
import json
|
|
||||||
import traceback
|
|
||||||
|
|
||||||
tracers = {}
|
tracers = {}
|
||||||
|
|
||||||
|
|
@ -132,17 +133,17 @@ class AsgiTracer:
|
||||||
"num_traces": len(traces),
|
"num_traces": len(traces),
|
||||||
"traces": traces,
|
"traces": traces,
|
||||||
}
|
}
|
||||||
try:
|
content_type = next(
|
||||||
content_type = [
|
(
|
||||||
v.decode("utf8")
|
v.decode("utf8")
|
||||||
for k, v in response_headers
|
for k, v in response_headers
|
||||||
if k.lower() == b"content-type"
|
if k.lower() == b"content-type"
|
||||||
][0]
|
),
|
||||||
except IndexError:
|
"",
|
||||||
content_type = ""
|
)
|
||||||
if "text/html" in content_type and b"</body>" in accumulated_body:
|
if "text/html" in content_type and b"</body>" in accumulated_body:
|
||||||
extra = escape(json.dumps(trace_info, indent=2))
|
extra = escape(json.dumps(trace_info, indent=2))
|
||||||
extra_html = f"<pre>{extra}</pre></body>".encode("utf8")
|
extra_html = f"<pre>{extra}</pre></body>".encode()
|
||||||
accumulated_body = accumulated_body.replace(b"</body>", extra_html)
|
accumulated_body = accumulated_body.replace(b"</body>", extra_html)
|
||||||
elif "json" in content_type and accumulated_body.startswith(b"{"):
|
elif "json" in content_type and accumulated_body.startswith(b"{"):
|
||||||
data = json.loads(accumulated_body.decode("utf8"))
|
data = json.loads(accumulated_body.decode("utf8"))
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
from .utils import tilde_encode, path_with_format, PrefixedUrlString
|
|
||||||
import urllib
|
import urllib
|
||||||
|
|
||||||
|
from .utils import PrefixedUrlString, path_with_format, tilde_encode
|
||||||
|
|
||||||
|
|
||||||
class Urls:
|
class Urls:
|
||||||
def __init__(self, ds):
|
def __init__(self, ds):
|
||||||
|
|
@ -8,8 +9,7 @@ class Urls:
|
||||||
|
|
||||||
def path(self, path, format=None):
|
def path(self, path, format=None):
|
||||||
if not isinstance(path, PrefixedUrlString):
|
if not isinstance(path, PrefixedUrlString):
|
||||||
if path.startswith("/"):
|
path = path.removeprefix("/")
|
||||||
path = path[1:]
|
|
||||||
path = self.ds.setting("base_url") + path
|
path = self.ds.setting("base_url") + path
|
||||||
if format is not None:
|
if format is not None:
|
||||||
path = path_with_format(path=path, format=format)
|
path = path_with_format(path=path, format=format)
|
||||||
|
|
@ -56,6 +56,7 @@ class Urls:
|
||||||
return PrefixedUrlString(path)
|
return PrefixedUrlString(path)
|
||||||
|
|
||||||
def row_blob(self, database, table, row_path, column):
|
def row_blob(self, database, table, row_path, column):
|
||||||
return self.table(database, table) + "/{}.blob?_blob_column={}".format(
|
return (
|
||||||
row_path, urllib.parse.quote_plus(column)
|
self.table(database, table)
|
||||||
|
+ f"/{row_path}.blob?_blob_column={urllib.parse.quote_plus(column)}"
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -1,28 +1,31 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
from contextlib import contextmanager
|
import base64
|
||||||
import aiofiles
|
import binascii
|
||||||
import click
|
|
||||||
from collections import OrderedDict, namedtuple, Counter
|
|
||||||
import copy
|
import copy
|
||||||
import dataclasses
|
import dataclasses
|
||||||
import base64
|
|
||||||
import hashlib
|
import hashlib
|
||||||
import inspect
|
import inspect
|
||||||
import json
|
import json
|
||||||
import markupsafe
|
|
||||||
import mergedeep
|
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
import secrets
|
||||||
import shlex
|
import shlex
|
||||||
|
import shutil
|
||||||
import tempfile
|
import tempfile
|
||||||
import typing
|
|
||||||
import time
|
import time
|
||||||
import types
|
import types
|
||||||
import secrets
|
import typing
|
||||||
import shutil
|
|
||||||
from typing import Iterable, List, Tuple
|
|
||||||
import urllib
|
import urllib
|
||||||
|
from collections import Counter, OrderedDict, namedtuple
|
||||||
|
from collections.abc import Iterable
|
||||||
|
from contextlib import contextmanager
|
||||||
|
|
||||||
|
import aiofiles
|
||||||
|
import click
|
||||||
|
import markupsafe
|
||||||
|
import mergedeep
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from .shutil_backport import copytree
|
from .shutil_backport import copytree
|
||||||
from .sqlite import sqlite3, supports_table_xinfo
|
from .sqlite import sqlite3, supports_table_xinfo
|
||||||
|
|
||||||
|
|
@ -35,7 +38,7 @@ if typing.TYPE_CHECKING:
|
||||||
class PaginatedResources:
|
class PaginatedResources:
|
||||||
"""Paginated results from allowed_resources query."""
|
"""Paginated results from allowed_resources query."""
|
||||||
|
|
||||||
resources: List["Resource"]
|
resources: list["Resource"]
|
||||||
next: str | None # Keyset token for next page (None if no more results)
|
next: str | None # Keyset token for next page (None if no more results)
|
||||||
_datasette: typing.Any = dataclasses.field(default=None, repr=False)
|
_datasette: typing.Any = dataclasses.field(default=None, repr=False)
|
||||||
_action: str = dataclasses.field(default=None, repr=False)
|
_action: str = dataclasses.field(default=None, repr=False)
|
||||||
|
|
@ -82,22 +85,132 @@ class PaginatedResources:
|
||||||
|
|
||||||
|
|
||||||
# From https://www.sqlite.org/lang_keywords.html
|
# From https://www.sqlite.org/lang_keywords.html
|
||||||
reserved_words = set(
|
reserved_words = {
|
||||||
(
|
"abort",
|
||||||
"abort action add after all alter analyze and as asc attach autoincrement "
|
"action",
|
||||||
"before begin between by cascade case cast check collate column commit "
|
"add",
|
||||||
"conflict constraint create cross current_date current_time "
|
"after",
|
||||||
"current_timestamp database default deferrable deferred delete desc detach "
|
"all",
|
||||||
"distinct drop each else end escape except exclusive exists explain fail "
|
"alter",
|
||||||
"for foreign from full glob group having if ignore immediate in index "
|
"analyze",
|
||||||
"indexed initially inner insert instead intersect into is isnull join key "
|
"and",
|
||||||
"left like limit match natural no not notnull null of offset on or order "
|
"as",
|
||||||
"outer plan pragma primary query raise recursive references regexp reindex "
|
"asc",
|
||||||
"release rename replace restrict right rollback row savepoint select set "
|
"attach",
|
||||||
"table temp temporary then to transaction trigger union unique update using "
|
"autoincrement",
|
||||||
"vacuum values view virtual when where with without"
|
"before",
|
||||||
).split()
|
"begin",
|
||||||
)
|
"between",
|
||||||
|
"by",
|
||||||
|
"cascade",
|
||||||
|
"case",
|
||||||
|
"cast",
|
||||||
|
"check",
|
||||||
|
"collate",
|
||||||
|
"column",
|
||||||
|
"commit",
|
||||||
|
"conflict",
|
||||||
|
"constraint",
|
||||||
|
"create",
|
||||||
|
"cross",
|
||||||
|
"current_date",
|
||||||
|
"current_time",
|
||||||
|
"current_timestamp",
|
||||||
|
"database",
|
||||||
|
"default",
|
||||||
|
"deferrable",
|
||||||
|
"deferred",
|
||||||
|
"delete",
|
||||||
|
"desc",
|
||||||
|
"detach",
|
||||||
|
"distinct",
|
||||||
|
"drop",
|
||||||
|
"each",
|
||||||
|
"else",
|
||||||
|
"end",
|
||||||
|
"escape",
|
||||||
|
"except",
|
||||||
|
"exclusive",
|
||||||
|
"exists",
|
||||||
|
"explain",
|
||||||
|
"fail",
|
||||||
|
"for",
|
||||||
|
"foreign",
|
||||||
|
"from",
|
||||||
|
"full",
|
||||||
|
"glob",
|
||||||
|
"group",
|
||||||
|
"having",
|
||||||
|
"if",
|
||||||
|
"ignore",
|
||||||
|
"immediate",
|
||||||
|
"in",
|
||||||
|
"index",
|
||||||
|
"indexed",
|
||||||
|
"initially",
|
||||||
|
"inner",
|
||||||
|
"insert",
|
||||||
|
"instead",
|
||||||
|
"intersect",
|
||||||
|
"into",
|
||||||
|
"is",
|
||||||
|
"isnull",
|
||||||
|
"join",
|
||||||
|
"key",
|
||||||
|
"left",
|
||||||
|
"like",
|
||||||
|
"limit",
|
||||||
|
"match",
|
||||||
|
"natural",
|
||||||
|
"no",
|
||||||
|
"not",
|
||||||
|
"notnull",
|
||||||
|
"null",
|
||||||
|
"of",
|
||||||
|
"offset",
|
||||||
|
"on",
|
||||||
|
"or",
|
||||||
|
"order",
|
||||||
|
"outer",
|
||||||
|
"plan",
|
||||||
|
"pragma",
|
||||||
|
"primary",
|
||||||
|
"query",
|
||||||
|
"raise",
|
||||||
|
"recursive",
|
||||||
|
"references",
|
||||||
|
"regexp",
|
||||||
|
"reindex",
|
||||||
|
"release",
|
||||||
|
"rename",
|
||||||
|
"replace",
|
||||||
|
"restrict",
|
||||||
|
"right",
|
||||||
|
"rollback",
|
||||||
|
"row",
|
||||||
|
"savepoint",
|
||||||
|
"select",
|
||||||
|
"set",
|
||||||
|
"table",
|
||||||
|
"temp",
|
||||||
|
"temporary",
|
||||||
|
"then",
|
||||||
|
"to",
|
||||||
|
"transaction",
|
||||||
|
"trigger",
|
||||||
|
"union",
|
||||||
|
"unique",
|
||||||
|
"update",
|
||||||
|
"using",
|
||||||
|
"vacuum",
|
||||||
|
"values",
|
||||||
|
"view",
|
||||||
|
"virtual",
|
||||||
|
"when",
|
||||||
|
"where",
|
||||||
|
"with",
|
||||||
|
"without",
|
||||||
|
}
|
||||||
|
|
||||||
APT_GET_DOCKERFILE_EXTRAS = r"""
|
APT_GET_DOCKERFILE_EXTRAS = r"""
|
||||||
RUN apt-get update && \
|
RUN apt-get update && \
|
||||||
|
|
@ -157,7 +270,7 @@ functions_marked_as_documented = []
|
||||||
|
|
||||||
def documented(fn=None, *, label=None):
|
def documented(fn=None, *, label=None):
|
||||||
def decorate(fn):
|
def decorate(fn):
|
||||||
fn._datasette_docs_label = label or "internals_utils_{}".format(fn.__name__)
|
fn._datasette_docs_label = label or f"internals_utils_{fn.__name__}"
|
||||||
functions_marked_as_documented.append(fn)
|
functions_marked_as_documented.append(fn)
|
||||||
return fn
|
return fn
|
||||||
|
|
||||||
|
|
@ -224,24 +337,71 @@ def compound_keys_after_sql(pks, start_index=0):
|
||||||
return "({})".format("\n or\n".join(or_clauses))
|
return "({})".format("\n or\n".join(or_clauses))
|
||||||
|
|
||||||
|
|
||||||
|
@documented
|
||||||
class CustomJSONEncoder(json.JSONEncoder):
|
class CustomJSONEncoder(json.JSONEncoder):
|
||||||
|
"""
|
||||||
|
The CustomJSONEncoder class handles serialization for objects commonly used by Datasette,
|
||||||
|
including SQLite cursors and binary blobs. Datasette uses it internally to serve .json endpoints,
|
||||||
|
and plugins that return JSON can use it to match Datasette's own handling.
|
||||||
|
|
||||||
|
Built-in types (text, numbers, lists, etc) are encoded the same as Python's built-in ``json`` module.
|
||||||
|
|
||||||
|
- ``sqlite3.Row`` becomes a tuple
|
||||||
|
- ``sqlite3.Cursor`` becomes a list
|
||||||
|
|
||||||
|
Binary blobs are encoded as an object, with the actual data base64-encoded,
|
||||||
|
like so: ::
|
||||||
|
|
||||||
|
{
|
||||||
|
"$base64": True,
|
||||||
|
"encoded": ...,
|
||||||
|
}
|
||||||
|
|
||||||
|
Example: https://latest.datasette.io/fixtures/binary_data.json
|
||||||
|
"""
|
||||||
|
|
||||||
def default(self, obj):
|
def default(self, obj):
|
||||||
if isinstance(obj, sqlite3.Row):
|
if isinstance(obj, sqlite3.Row):
|
||||||
return tuple(obj)
|
return tuple(obj)
|
||||||
if isinstance(obj, sqlite3.Cursor):
|
if isinstance(obj, sqlite3.Cursor):
|
||||||
return list(obj)
|
return list(obj)
|
||||||
if isinstance(obj, bytes):
|
if isinstance(obj, bytes):
|
||||||
# Does it encode to utf8?
|
return {
|
||||||
try:
|
"$base64": True,
|
||||||
return obj.decode("utf8")
|
"encoded": base64.b64encode(obj).decode("latin1"),
|
||||||
except UnicodeDecodeError:
|
}
|
||||||
return {
|
|
||||||
"$base64": True,
|
|
||||||
"encoded": base64.b64encode(obj).decode("latin1"),
|
|
||||||
}
|
|
||||||
return json.JSONEncoder.default(self, obj)
|
return json.JSONEncoder.default(self, obj)
|
||||||
|
|
||||||
|
|
||||||
|
class WriteJsonValueError(ValueError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def decode_write_json_cell(value):
|
||||||
|
if not isinstance(value, dict):
|
||||||
|
return value
|
||||||
|
keys = set(value.keys())
|
||||||
|
if keys == {"$raw"}:
|
||||||
|
return value["$raw"]
|
||||||
|
if keys == {"$base64", "encoded"} and value.get("$base64") is True:
|
||||||
|
encoded = value["encoded"]
|
||||||
|
if not isinstance(encoded, str):
|
||||||
|
raise WriteJsonValueError("$base64 encoded value must be a string")
|
||||||
|
try:
|
||||||
|
return base64.b64decode(encoded, validate=True)
|
||||||
|
except binascii.Error as ex:
|
||||||
|
raise WriteJsonValueError("Invalid $base64 encoded value") from ex
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def decode_write_json_row(row):
|
||||||
|
return {key: decode_write_json_cell(value) for key, value in row.items()}
|
||||||
|
|
||||||
|
|
||||||
|
def decode_write_json_rows(rows):
|
||||||
|
return [decode_write_json_row(row) for row in rows]
|
||||||
|
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def sqlite_timelimit(conn, ms):
|
def sqlite_timelimit(conn, ms):
|
||||||
deadline = time.perf_counter() + (ms / 1000)
|
deadline = time.perf_counter() + (ms / 1000)
|
||||||
|
|
@ -312,7 +472,7 @@ disallawed_sql_res = [
|
||||||
(
|
(
|
||||||
re.compile(f"pragma(?!_({'|'.join(allowed_pragmas)}))"),
|
re.compile(f"pragma(?!_({'|'.join(allowed_pragmas)}))"),
|
||||||
"Statement contained a disallowed PRAGMA. Allowed pragma functions are {}".format(
|
"Statement contained a disallowed PRAGMA. Allowed pragma functions are {}".format(
|
||||||
", ".join("pragma_{}()".format(pragma) for pragma in allowed_pragmas)
|
", ".join(f"pragma_{pragma}()" for pragma in allowed_pragmas)
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
|
|
@ -408,14 +568,9 @@ def escape_css_string(s):
|
||||||
|
|
||||||
|
|
||||||
def escape_sqlite(s):
|
def escape_sqlite(s):
|
||||||
if _boring_keyword_re.match(s) and (s.lower() not in reserved_words):
|
if _boring_keyword_re.fullmatch(s) and (s.lower() not in reserved_words):
|
||||||
return s
|
return s
|
||||||
elif "]" in s:
|
return '"{}"'.format(s.replace('"', '""'))
|
||||||
# SQLite does not support escaping ] inside [bracket] quoting, so fall
|
|
||||||
# back to double-quote quoting (doubling any embedded ") - #2677
|
|
||||||
return '"{}"'.format(s.replace('"', '""'))
|
|
||||||
else:
|
|
||||||
return f"[{s}]"
|
|
||||||
|
|
||||||
|
|
||||||
def make_dockerfile(
|
def make_dockerfile(
|
||||||
|
|
@ -491,10 +646,7 @@ CMD {cmd}""".format(
|
||||||
else ""
|
else ""
|
||||||
),
|
),
|
||||||
environment_variables="\n".join(
|
environment_variables="\n".join(
|
||||||
[
|
[f"ENV {key} '{value}'" for key, value in environment_variables.items()]
|
||||||
"ENV {} '{}'".format(key, value)
|
|
||||||
for key, value in environment_variables.items()
|
|
||||||
]
|
|
||||||
),
|
),
|
||||||
install_from=" ".join(install),
|
install_from=" ".join(install),
|
||||||
files=" ".join(files),
|
files=" ".join(files),
|
||||||
|
|
@ -593,11 +745,11 @@ def detect_primary_keys(conn, table):
|
||||||
|
|
||||||
|
|
||||||
def get_outbound_foreign_keys(conn, table):
|
def get_outbound_foreign_keys(conn, table):
|
||||||
infos = conn.execute(f"PRAGMA foreign_key_list([{table}])").fetchall()
|
infos = conn.execute(f"PRAGMA foreign_key_list({escape_sqlite(table)})").fetchall()
|
||||||
fks = []
|
fks = []
|
||||||
for info in infos:
|
for info in infos:
|
||||||
if info is not None:
|
if info is not None:
|
||||||
id, seq, table_name, from_, to_, on_update, on_delete, match = info
|
id, seq, table_name, from_, to_, _on_update, _on_delete, _match = info
|
||||||
fks.append(
|
fks.append(
|
||||||
{
|
{
|
||||||
"column": from_,
|
"column": from_,
|
||||||
|
|
@ -668,7 +820,8 @@ def detect_spatialite(conn):
|
||||||
|
|
||||||
def detect_fts(conn, table):
|
def detect_fts(conn, table):
|
||||||
"""Detect if table has a corresponding FTS virtual table and return it"""
|
"""Detect if table has a corresponding FTS virtual table and return it"""
|
||||||
rows = conn.execute(detect_fts_sql(table)).fetchall()
|
sql, params = detect_fts_sql(table)
|
||||||
|
rows = conn.execute(sql, params).fetchall()
|
||||||
if len(rows) == 0:
|
if len(rows) == 0:
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
|
|
@ -676,18 +829,26 @@ def detect_fts(conn, table):
|
||||||
|
|
||||||
|
|
||||||
def detect_fts_sql(table):
|
def detect_fts_sql(table):
|
||||||
return r"""
|
escaped_table = table.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")
|
||||||
select name from sqlite_master
|
return (
|
||||||
where rootpage = 0
|
r"""
|
||||||
and (
|
select name from sqlite_master
|
||||||
sql like '%VIRTUAL TABLE%USING FTS%content="{table}"%'
|
where rootpage = 0
|
||||||
or sql like '%VIRTUAL TABLE%USING FTS%content=[{table}]%'
|
and (
|
||||||
or (
|
sql like :fts_double_quoted escape char(92)
|
||||||
tbl_name = "{table}"
|
or sql like :fts_bracket_quoted escape char(92)
|
||||||
and sql like '%VIRTUAL TABLE%USING FTS%'
|
or (
|
||||||
|
tbl_name = :table
|
||||||
|
and sql like '%VIRTUAL TABLE%USING FTS%'
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
""",
|
||||||
""".format(table=table.replace("'", "''"))
|
{
|
||||||
|
"fts_double_quoted": f'%VIRTUAL TABLE%USING FTS%content="{escaped_table}"%',
|
||||||
|
"fts_bracket_quoted": f"%VIRTUAL TABLE%USING FTS%content=[{escaped_table}]%",
|
||||||
|
"table": table,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def detect_json1(conn=None):
|
def detect_json1(conn=None):
|
||||||
|
|
@ -698,7 +859,7 @@ def detect_json1(conn=None):
|
||||||
try:
|
try:
|
||||||
conn.execute("SELECT json('{}')")
|
conn.execute("SELECT json('{}')")
|
||||||
return True
|
return True
|
||||||
except Exception:
|
except sqlite3.Error:
|
||||||
return False
|
return False
|
||||||
finally:
|
finally:
|
||||||
if close_conn:
|
if close_conn:
|
||||||
|
|
@ -778,9 +939,7 @@ def is_url(value):
|
||||||
if not value.startswith("http://") and not value.startswith("https://"):
|
if not value.startswith("http://") and not value.startswith("https://"):
|
||||||
return False
|
return False
|
||||||
# Any whitespace at all is invalid
|
# Any whitespace at all is invalid
|
||||||
if whitespace_re.search(value):
|
return not whitespace_re.search(value)
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
css_class_re = re.compile(r"^[a-zA-Z]+[_a-zA-Z0-9-]*$")
|
css_class_re = re.compile(r"^[a-zA-Z]+[_a-zA-Z0-9-]*$")
|
||||||
|
|
@ -833,7 +992,9 @@ def module_from_path(path, name):
|
||||||
mod.__file__ = path
|
mod.__file__ = path
|
||||||
with open(path, "r") as file:
|
with open(path, "r") as file:
|
||||||
code = compile(file.read(), path, "exec", dont_inherit=True)
|
code = compile(file.read(), path, "exec", dont_inherit=True)
|
||||||
exec(code, mod.__dict__)
|
# Executing the file is the whole point - this is how --plugins-dir loads
|
||||||
|
# plugins and how metadata/config .py files are evaluated
|
||||||
|
exec(code, mod.__dict__) # noqa: S102
|
||||||
return mod
|
return mod
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -990,9 +1151,7 @@ def escape_fts(query):
|
||||||
query += '"'
|
query += '"'
|
||||||
bits = _escape_fts_re.split(query)
|
bits = _escape_fts_re.split(query)
|
||||||
bits = [b for b in bits if b and b != '""']
|
bits = [b for b in bits if b and b != '""']
|
||||||
return " ".join(
|
return " ".join(f'"{bit}"' if not bit.startswith('"') else bit for bit in bits)
|
||||||
'"{}"'.format(bit) if not bit.startswith('"') else bit for bit in bits
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class MultiParams:
|
class MultiParams:
|
||||||
|
|
@ -1004,7 +1163,7 @@ class MultiParams:
|
||||||
data[key], (list, tuple)
|
data[key], (list, tuple)
|
||||||
), "dictionary data should be a dictionary of key => [list]"
|
), "dictionary data should be a dictionary of key => [list]"
|
||||||
self._data = data
|
self._data = data
|
||||||
elif isinstance(data, list) or isinstance(data, tuple):
|
elif isinstance(data, (list, tuple)):
|
||||||
new_data = {}
|
new_data = {}
|
||||||
for item in data:
|
for item in data:
|
||||||
assert (
|
assert (
|
||||||
|
|
@ -1094,9 +1253,7 @@ def _gather_arguments(fn, kwargs):
|
||||||
for parameter in parameters:
|
for parameter in parameters:
|
||||||
if parameter not in kwargs:
|
if parameter not in kwargs:
|
||||||
raise TypeError(
|
raise TypeError(
|
||||||
"{} requires parameters {}, missing: {}".format(
|
f"{fn} requires parameters {tuple(parameters)}, missing: {set(parameters) - set(kwargs.keys())}"
|
||||||
fn, tuple(parameters), set(parameters) - set(kwargs.keys())
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
call_with.append(kwargs[parameter])
|
call_with.append(kwargs[parameter])
|
||||||
return call_with
|
return call_with
|
||||||
|
|
@ -1165,9 +1322,9 @@ def resolve_env_secrets(config, environ):
|
||||||
"""Create copy that recursively replaces {"$env": "NAME"} with values from environ"""
|
"""Create copy that recursively replaces {"$env": "NAME"} with values from environ"""
|
||||||
if isinstance(config, dict):
|
if isinstance(config, dict):
|
||||||
if list(config.keys()) == ["$env"]:
|
if list(config.keys()) == ["$env"]:
|
||||||
return environ.get(list(config.values())[0])
|
return environ.get(next(iter(config.values())))
|
||||||
elif list(config.keys()) == ["$file"]:
|
elif list(config.keys()) == ["$file"]:
|
||||||
with open(list(config.values())[0]) as fp:
|
with open(next(iter(config.values()))) as fp:
|
||||||
return fp.read()
|
return fp.read()
|
||||||
else:
|
else:
|
||||||
return {
|
return {
|
||||||
|
|
@ -1245,29 +1402,38 @@ class StartupError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
_single_line_comment_re = re.compile(r"--.*")
|
# Comments and string literals, matched in a single pass so that whichever
|
||||||
_multi_line_comment_re = re.compile(r"/\*.*?\*/", re.DOTALL)
|
# construct starts first "wins" - this ensures a comment marker inside a string
|
||||||
_single_quote_re = re.compile(r"'(?:''|[^'])*'")
|
# literal (or a quote inside a comment) does not confuse the parameter scan.
|
||||||
_double_quote_re = re.compile(r'"(?:\"\"|[^"])*"')
|
_comments_and_strings_re = re.compile(
|
||||||
|
r"""
|
||||||
|
--[^\n]* # single line comment
|
||||||
|
| /\*.*?(?:\*/|\Z) # multi line comment, possibly to end-of-input
|
||||||
|
| '(?:''|[^'])*' # single quoted string ('' escapes a quote)
|
||||||
|
| "(?:""|[^"])*" # double quoted identifier ("" escapes a quote)
|
||||||
|
| \[(?:[^\]])*\] # square-bracket quoted identifier
|
||||||
|
| `(?:``|[^`])*` # backtick quoted identifier
|
||||||
|
""",
|
||||||
|
re.DOTALL | re.VERBOSE,
|
||||||
|
)
|
||||||
_named_param_re = re.compile(r":(\w+)")
|
_named_param_re = re.compile(r":(\w+)")
|
||||||
|
|
||||||
|
|
||||||
@documented
|
@documented
|
||||||
def named_parameters(sql: str) -> List[str]:
|
def named_parameters(sql: str) -> list[str]:
|
||||||
"""
|
"""
|
||||||
Given a SQL statement, return a list of named parameters that are used in the statement
|
Given a SQL statement, return a list of named parameters that are used in the statement
|
||||||
|
|
||||||
e.g. for ``select * from foo where id=:id`` this would return ``["id"]``
|
e.g. for ``select * from foo where id=:id`` this would return ``["id"]``
|
||||||
"""
|
"""
|
||||||
sql = _single_line_comment_re.sub("", sql)
|
# Strip comments and string literals first so that any ":name" sequences
|
||||||
sql = _multi_line_comment_re.sub("", sql)
|
# inside them are not mistaken for named parameters
|
||||||
sql = _single_quote_re.sub("", sql)
|
sql = _comments_and_strings_re.sub("", sql)
|
||||||
sql = _double_quote_re.sub("", sql)
|
|
||||||
# Extract parameters from what is left
|
# Extract parameters from what is left
|
||||||
return _named_param_re.findall(sql)
|
return _named_param_re.findall(sql)
|
||||||
|
|
||||||
|
|
||||||
async def derive_named_parameters(db: "Database", sql: str) -> List[str]:
|
async def derive_named_parameters(db: "Database", sql: str) -> list[str]:
|
||||||
"""
|
"""
|
||||||
This undocumented but stable method exists for backwards compatibility
|
This undocumented but stable method exists for backwards compatibility
|
||||||
with plugins that were using it before it switched to named_parameters()
|
with plugins that were using it before it switched to named_parameters()
|
||||||
|
|
@ -1275,6 +1441,54 @@ async def derive_named_parameters(db: "Database", sql: str) -> List[str]:
|
||||||
return named_parameters(sql)
|
return named_parameters(sql)
|
||||||
|
|
||||||
|
|
||||||
|
def parse_size_limit(value, default, maximum, name="_size"):
|
||||||
|
"""
|
||||||
|
Parse a page-size parameter using the same semantics as the table
|
||||||
|
view's ?_size=: blank means default, "max" means maximum, integers
|
||||||
|
must be 0 or greater and no larger than maximum. Raises ValueError
|
||||||
|
with a message suitable for a 400 response.
|
||||||
|
"""
|
||||||
|
if value in (None, ""):
|
||||||
|
return default
|
||||||
|
if value == "max":
|
||||||
|
return maximum
|
||||||
|
try:
|
||||||
|
size = int(value)
|
||||||
|
if size < 0:
|
||||||
|
raise ValueError
|
||||||
|
except ValueError:
|
||||||
|
raise ValueError(f"{name} must be a positive integer")
|
||||||
|
if size > maximum:
|
||||||
|
raise ValueError(f"{name} must be <= {maximum}")
|
||||||
|
return size
|
||||||
|
|
||||||
|
|
||||||
|
UNSTABLE_API_MESSAGE = (
|
||||||
|
"This API is not part of Datasette's stable interface and may change at any time"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def error_body(messages, status):
|
||||||
|
"""
|
||||||
|
The canonical JSON error body used by every Datasette JSON error response:
|
||||||
|
|
||||||
|
{"ok": False, "error": "...", "errors": ["...", ...], "status": 400}
|
||||||
|
|
||||||
|
"error" is all of the messages joined with "; ", "errors" is the full
|
||||||
|
list, "status" matches the HTTP status code. Callers may add extra
|
||||||
|
context keys to the returned dictionary but must not remove these four.
|
||||||
|
"""
|
||||||
|
if isinstance(messages, str):
|
||||||
|
messages = [messages]
|
||||||
|
messages = [str(message) for message in messages]
|
||||||
|
return {
|
||||||
|
"ok": False,
|
||||||
|
"error": "; ".join(messages),
|
||||||
|
"errors": messages,
|
||||||
|
"status": status,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def add_cors_headers(headers):
|
def add_cors_headers(headers):
|
||||||
headers["Access-Control-Allow-Origin"] = "*"
|
headers["Access-Control-Allow-Origin"] = "*"
|
||||||
headers["Access-Control-Allow-Headers"] = "Authorization, Content-Type"
|
headers["Access-Control-Allow-Headers"] = "Authorization, Content-Type"
|
||||||
|
|
@ -1303,7 +1517,7 @@ class TildeEncoder(dict):
|
||||||
elif b == _space:
|
elif b == _space:
|
||||||
res = "+"
|
res = "+"
|
||||||
else:
|
else:
|
||||||
res = "~{:02X}".format(b)
|
res = f"~{b:02X}"
|
||||||
self[b] = res
|
self[b] = res
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
@ -1352,7 +1566,13 @@ async def row_sql_params_pks(db, table, pk_values):
|
||||||
if use_rowid:
|
if use_rowid:
|
||||||
select = "rowid, *"
|
select = "rowid, *"
|
||||||
pks = ["rowid"]
|
pks = ["rowid"]
|
||||||
wheres = [f'"{pk}"=:p{i}' for i, pk in enumerate(pks)]
|
wheres = []
|
||||||
|
for i, pk in enumerate(pks):
|
||||||
|
escaped_pk = escape_sqlite(pk)
|
||||||
|
# Preserve the historic always-quoted SQL exposed by _extra=query
|
||||||
|
if escaped_pk == pk:
|
||||||
|
escaped_pk = f'"{pk}"'
|
||||||
|
wheres.append(f"{escaped_pk}=:p{i}")
|
||||||
sql = f"select {select} from {escape_sqlite(table)} where {' AND '.join(wheres)}"
|
sql = f"select {select} from {escape_sqlite(table)} where {' AND '.join(wheres)}"
|
||||||
params = {}
|
params = {}
|
||||||
for i, pk_value in enumerate(pk_values):
|
for i, pk_value in enumerate(pk_values):
|
||||||
|
|
@ -1398,7 +1618,7 @@ def _combine(base: dict, update: dict) -> dict:
|
||||||
return base
|
return base
|
||||||
|
|
||||||
|
|
||||||
def pairs_to_nested_config(pairs: typing.List[typing.Tuple[str, typing.Any]]) -> dict:
|
def pairs_to_nested_config(pairs: list[tuple[str, typing.Any]]) -> dict:
|
||||||
"""
|
"""
|
||||||
Parse a list of key-value pairs into a nested dictionary.
|
Parse a list of key-value pairs into a nested dictionary.
|
||||||
"""
|
"""
|
||||||
|
|
@ -1413,7 +1633,7 @@ def make_slot_function(name, datasette, request, **kwargs):
|
||||||
from datasette.plugins import pm
|
from datasette.plugins import pm
|
||||||
|
|
||||||
method = getattr(pm.hook, name, None)
|
method = getattr(pm.hook, name, None)
|
||||||
assert method is not None, "No hook found for {}".format(name)
|
assert method is not None, f"No hook found for {name}"
|
||||||
|
|
||||||
async def inner():
|
async def inner():
|
||||||
html_bits = []
|
html_bits = []
|
||||||
|
|
@ -1437,7 +1657,7 @@ def prune_empty_dicts(d: dict):
|
||||||
d.pop(key, None)
|
d.pop(key, None)
|
||||||
|
|
||||||
|
|
||||||
def move_plugins_and_allow(source: dict, destination: dict) -> Tuple[dict, dict]:
|
def move_plugins_and_allow(source: dict, destination: dict) -> tuple[dict, dict]:
|
||||||
"""
|
"""
|
||||||
Move 'plugins' and 'allow' keys from source to destination dictionary. Creates
|
Move 'plugins' and 'allow' keys from source to destination dictionary. Creates
|
||||||
hierarchy in destination if needed. After moving, recursively remove any keys
|
hierarchy in destination if needed. After moving, recursively remove any keys
|
||||||
|
|
@ -1524,7 +1744,7 @@ def redact_keys(original: dict, key_patterns: Iterable) -> dict:
|
||||||
return {
|
return {
|
||||||
k: (
|
k: (
|
||||||
redact(v)
|
redact(v)
|
||||||
if not any(pattern in k for pattern in key_patterns)
|
if not any(pattern in k.casefold() for pattern in key_patterns)
|
||||||
else "***"
|
else "***"
|
||||||
)
|
)
|
||||||
for k, v in data.items()
|
for k, v in data.items()
|
||||||
|
|
@ -1548,6 +1768,17 @@ def md5_not_usedforsecurity(s):
|
||||||
_etag_cache = {}
|
_etag_cache = {}
|
||||||
|
|
||||||
|
|
||||||
|
def sha256_file(filepath, chunk_size=4096):
|
||||||
|
hasher = hashlib.sha256()
|
||||||
|
with open(filepath, "rb") as fp:
|
||||||
|
while True:
|
||||||
|
chunk = fp.read(chunk_size)
|
||||||
|
if not chunk:
|
||||||
|
break
|
||||||
|
hasher.update(chunk)
|
||||||
|
return hasher.hexdigest()
|
||||||
|
|
||||||
|
|
||||||
async def calculate_etag(filepath, chunk_size=4096):
|
async def calculate_etag(filepath, chunk_size=4096):
|
||||||
if filepath in _etag_cache:
|
if filepath in _etag_cache:
|
||||||
return _etag_cache[filepath]
|
return _etag_cache[filepath]
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,15 @@ from datasette.utils.permissions import gather_permission_sql_from_hooks
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from datasette.app import Datasette
|
from datasette.app import Datasette
|
||||||
|
from datasette.permissions import Action
|
||||||
|
|
||||||
|
|
||||||
|
def _child_collation(action: "Action") -> str:
|
||||||
|
"""Match resource identity without changing the spelling returned by SQL."""
|
||||||
|
resource_class = action.resource_class
|
||||||
|
if resource_class is not None and resource_class.case_insensitive_child:
|
||||||
|
return "NOCASE"
|
||||||
|
return "BINARY"
|
||||||
|
|
||||||
|
|
||||||
async def build_allowed_resources_sql(
|
async def build_allowed_resources_sql(
|
||||||
|
|
@ -149,6 +158,7 @@ async def _build_single_action_sql(
|
||||||
raise ValueError(f"Unknown action: {action}")
|
raise ValueError(f"Unknown action: {action}")
|
||||||
|
|
||||||
# Get base resources SQL from the resource class
|
# Get base resources SQL from the resource class
|
||||||
|
child_collation = _child_collation(action_obj)
|
||||||
base_resources_sql = await action_obj.resource_class.resources_sql(
|
base_resources_sql = await action_obj.resource_class.resources_sql(
|
||||||
datasette, actor=actor
|
datasette, actor=actor
|
||||||
)
|
)
|
||||||
|
|
@ -185,7 +195,7 @@ async def _build_single_action_sql(
|
||||||
if permission_sql.sql is None:
|
if permission_sql.sql is None:
|
||||||
continue
|
continue
|
||||||
rule_sqls.append(f"""
|
rule_sqls.append(f"""
|
||||||
SELECT parent, child, allow, reason, '{permission_sql.source}' AS source_plugin FROM (
|
SELECT parent, child COLLATE {child_collation} AS child, allow, reason, '{permission_sql.source}' AS source_plugin FROM (
|
||||||
{permission_sql.sql}
|
{permission_sql.sql}
|
||||||
)
|
)
|
||||||
""".strip())
|
""".strip())
|
||||||
|
|
@ -252,88 +262,62 @@ async def _build_single_action_sql(
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
# Continue with the cascading logic
|
# Continue with the cascading logic.
|
||||||
query_parts.extend(
|
# Aggregate the RULES by cascade level (small), rather than grouping
|
||||||
[
|
# base x rules (which scales with the number of resources).
|
||||||
"child_lvl AS (",
|
def _agg(select_key, where, group_by):
|
||||||
" SELECT b.parent, b.child,",
|
parts = [
|
||||||
" MAX(CASE WHEN ar.allow = 0 THEN 1 ELSE 0 END) AS any_deny,",
|
f" SELECT {select_key}",
|
||||||
" MAX(CASE WHEN ar.allow = 1 THEN 1 ELSE 0 END) AS any_allow,",
|
" MAX(CASE WHEN allow = 0 THEN 1 ELSE 0 END) AS any_deny,",
|
||||||
" json_group_array(CASE WHEN ar.allow = 0 THEN ar.source_plugin || ': ' || ar.reason END) AS deny_reasons,",
|
" MAX(CASE WHEN allow = 1 THEN 1 ELSE 0 END) AS any_allow,",
|
||||||
" json_group_array(CASE WHEN ar.allow = 1 THEN ar.source_plugin || ': ' || ar.reason END) AS allow_reasons",
|
" json_group_array(CASE WHEN allow = 0 THEN source_plugin || ': ' || reason END) AS deny_reasons,",
|
||||||
" FROM base b",
|
" json_group_array(CASE WHEN allow = 1 THEN source_plugin || ': ' || reason END) AS allow_reasons",
|
||||||
" LEFT JOIN all_rules ar ON ar.parent = b.parent AND ar.child = b.child",
|
f" FROM all_rules WHERE {where}",
|
||||||
" GROUP BY b.parent, b.child",
|
|
||||||
"),",
|
|
||||||
"parent_lvl AS (",
|
|
||||||
" SELECT b.parent, b.child,",
|
|
||||||
" MAX(CASE WHEN ar.allow = 0 THEN 1 ELSE 0 END) AS any_deny,",
|
|
||||||
" MAX(CASE WHEN ar.allow = 1 THEN 1 ELSE 0 END) AS any_allow,",
|
|
||||||
" json_group_array(CASE WHEN ar.allow = 0 THEN ar.source_plugin || ': ' || ar.reason END) AS deny_reasons,",
|
|
||||||
" json_group_array(CASE WHEN ar.allow = 1 THEN ar.source_plugin || ': ' || ar.reason END) AS allow_reasons",
|
|
||||||
" FROM base b",
|
|
||||||
" LEFT JOIN all_rules ar ON ar.parent = b.parent AND ar.child IS NULL",
|
|
||||||
" GROUP BY b.parent, b.child",
|
|
||||||
"),",
|
|
||||||
"global_lvl AS (",
|
|
||||||
" SELECT b.parent, b.child,",
|
|
||||||
" MAX(CASE WHEN ar.allow = 0 THEN 1 ELSE 0 END) AS any_deny,",
|
|
||||||
" MAX(CASE WHEN ar.allow = 1 THEN 1 ELSE 0 END) AS any_allow,",
|
|
||||||
" json_group_array(CASE WHEN ar.allow = 0 THEN ar.source_plugin || ': ' || ar.reason END) AS deny_reasons,",
|
|
||||||
" json_group_array(CASE WHEN ar.allow = 1 THEN ar.source_plugin || ': ' || ar.reason END) AS allow_reasons",
|
|
||||||
" FROM base b",
|
|
||||||
" LEFT JOIN all_rules ar ON ar.parent IS NULL AND ar.child IS NULL",
|
|
||||||
" GROUP BY b.parent, b.child",
|
|
||||||
"),",
|
|
||||||
]
|
]
|
||||||
|
if group_by:
|
||||||
|
parts.append(f" GROUP BY {group_by}")
|
||||||
|
return parts
|
||||||
|
|
||||||
|
query_parts.extend(
|
||||||
|
["child_agg AS ("]
|
||||||
|
+ _agg(
|
||||||
|
"parent, child,",
|
||||||
|
"parent IS NOT NULL AND child IS NOT NULL",
|
||||||
|
"parent, child",
|
||||||
|
)
|
||||||
|
+ ["),", "parent_agg AS ("]
|
||||||
|
+ _agg("parent,", "parent IS NOT NULL AND child IS NULL", "parent")
|
||||||
|
+ ["),", "global_agg AS ("]
|
||||||
|
+ _agg("", "parent IS NULL AND child IS NULL", None)
|
||||||
|
+ ["),"]
|
||||||
)
|
)
|
||||||
|
|
||||||
# Add anonymous decision logic if needed
|
# Add anonymous decision logic if needed
|
||||||
if include_is_private:
|
if include_is_private:
|
||||||
query_parts.extend(
|
|
||||||
[
|
def _anon_agg(select_key, where, group_by):
|
||||||
"anon_child_lvl AS (",
|
parts = [
|
||||||
" SELECT b.parent, b.child,",
|
f" SELECT {select_key}",
|
||||||
" MAX(CASE WHEN ar.allow = 0 THEN 1 ELSE 0 END) AS any_deny,",
|
" MAX(CASE WHEN allow = 0 THEN 1 ELSE 0 END) AS any_deny,",
|
||||||
" MAX(CASE WHEN ar.allow = 1 THEN 1 ELSE 0 END) AS any_allow",
|
" MAX(CASE WHEN allow = 1 THEN 1 ELSE 0 END) AS any_allow",
|
||||||
" FROM base b",
|
f" FROM anon_rules WHERE {where}",
|
||||||
" LEFT JOIN anon_rules ar ON ar.parent = b.parent AND ar.child = b.child",
|
|
||||||
" GROUP BY b.parent, b.child",
|
|
||||||
"),",
|
|
||||||
"anon_parent_lvl AS (",
|
|
||||||
" SELECT b.parent, b.child,",
|
|
||||||
" MAX(CASE WHEN ar.allow = 0 THEN 1 ELSE 0 END) AS any_deny,",
|
|
||||||
" MAX(CASE WHEN ar.allow = 1 THEN 1 ELSE 0 END) AS any_allow",
|
|
||||||
" FROM base b",
|
|
||||||
" LEFT JOIN anon_rules ar ON ar.parent = b.parent AND ar.child IS NULL",
|
|
||||||
" GROUP BY b.parent, b.child",
|
|
||||||
"),",
|
|
||||||
"anon_global_lvl AS (",
|
|
||||||
" SELECT b.parent, b.child,",
|
|
||||||
" MAX(CASE WHEN ar.allow = 0 THEN 1 ELSE 0 END) AS any_deny,",
|
|
||||||
" MAX(CASE WHEN ar.allow = 1 THEN 1 ELSE 0 END) AS any_allow",
|
|
||||||
" FROM base b",
|
|
||||||
" LEFT JOIN anon_rules ar ON ar.parent IS NULL AND ar.child IS NULL",
|
|
||||||
" GROUP BY b.parent, b.child",
|
|
||||||
"),",
|
|
||||||
"anon_decisions AS (",
|
|
||||||
" SELECT",
|
|
||||||
" b.parent, b.child,",
|
|
||||||
" CASE",
|
|
||||||
" WHEN acl.any_deny = 1 THEN 0",
|
|
||||||
" WHEN acl.any_allow = 1 THEN 1",
|
|
||||||
" WHEN apl.any_deny = 1 THEN 0",
|
|
||||||
" WHEN apl.any_allow = 1 THEN 1",
|
|
||||||
" WHEN agl.any_deny = 1 THEN 0",
|
|
||||||
" WHEN agl.any_allow = 1 THEN 1",
|
|
||||||
" ELSE 0",
|
|
||||||
" END AS anon_is_allowed",
|
|
||||||
" FROM base b",
|
|
||||||
" JOIN anon_child_lvl acl ON b.parent = acl.parent AND (b.child = acl.child OR (b.child IS NULL AND acl.child IS NULL))",
|
|
||||||
" JOIN anon_parent_lvl apl ON b.parent = apl.parent AND (b.child = apl.child OR (b.child IS NULL AND apl.child IS NULL))",
|
|
||||||
" JOIN anon_global_lvl agl ON b.parent = agl.parent AND (b.child = agl.child OR (b.child IS NULL AND agl.child IS NULL))",
|
|
||||||
"),",
|
|
||||||
]
|
]
|
||||||
|
if group_by:
|
||||||
|
parts.append(f" GROUP BY {group_by}")
|
||||||
|
return parts
|
||||||
|
|
||||||
|
query_parts.extend(
|
||||||
|
["anon_child_agg AS ("]
|
||||||
|
+ _anon_agg(
|
||||||
|
f"parent, child COLLATE {child_collation} AS child,",
|
||||||
|
"parent IS NOT NULL AND child IS NOT NULL",
|
||||||
|
f"parent, child COLLATE {child_collation}",
|
||||||
|
)
|
||||||
|
+ ["),", "anon_parent_agg AS ("]
|
||||||
|
+ _anon_agg("parent,", "parent IS NOT NULL AND child IS NULL", "parent")
|
||||||
|
+ ["),", "anon_global_agg AS ("]
|
||||||
|
+ _anon_agg("", "parent IS NULL AND child IS NULL", None)
|
||||||
|
+ ["),"]
|
||||||
)
|
)
|
||||||
|
|
||||||
# Final decisions
|
# Final decisions
|
||||||
|
|
@ -342,31 +326,28 @@ async def _build_single_action_sql(
|
||||||
"decisions AS (",
|
"decisions AS (",
|
||||||
" SELECT",
|
" SELECT",
|
||||||
" b.parent, b.child,",
|
" b.parent, b.child,",
|
||||||
" -- Cascading permission logic: child → parent → global, DENY beats ALLOW at each level",
|
" -- Cascading permission logic: child -> parent -> global, DENY beats ALLOW at each level",
|
||||||
" -- Priority order:",
|
" -- Priority order:",
|
||||||
" -- 1. Child-level deny (most specific, blocks access)",
|
" -- 1. Child-level deny 2. Child-level allow",
|
||||||
" -- 2. Child-level allow (most specific, grants access)",
|
" -- 3. Parent-level deny 4. Parent-level allow",
|
||||||
" -- 3. Parent-level deny (intermediate, blocks access)",
|
" -- 5. Global-level deny 6. Global-level allow",
|
||||||
" -- 4. Parent-level allow (intermediate, grants access)",
|
|
||||||
" -- 5. Global-level deny (least specific, blocks access)",
|
|
||||||
" -- 6. Global-level allow (least specific, grants access)",
|
|
||||||
" -- 7. Default deny (no rules match)",
|
" -- 7. Default deny (no rules match)",
|
||||||
" CASE",
|
" CASE",
|
||||||
" WHEN cl.any_deny = 1 THEN 0",
|
" WHEN ca.any_deny = 1 THEN 0",
|
||||||
" WHEN cl.any_allow = 1 THEN 1",
|
" WHEN ca.any_allow = 1 THEN 1",
|
||||||
" WHEN pl.any_deny = 1 THEN 0",
|
" WHEN pa.any_deny = 1 THEN 0",
|
||||||
" WHEN pl.any_allow = 1 THEN 1",
|
" WHEN pa.any_allow = 1 THEN 1",
|
||||||
" WHEN gl.any_deny = 1 THEN 0",
|
" WHEN ga.any_deny = 1 THEN 0",
|
||||||
" WHEN gl.any_allow = 1 THEN 1",
|
" WHEN ga.any_allow = 1 THEN 1",
|
||||||
" ELSE 0",
|
" ELSE 0",
|
||||||
" END AS is_allowed,",
|
" END AS is_allowed,",
|
||||||
" CASE",
|
" CASE",
|
||||||
" WHEN cl.any_deny = 1 THEN cl.deny_reasons",
|
" WHEN ca.any_deny = 1 THEN ca.deny_reasons",
|
||||||
" WHEN cl.any_allow = 1 THEN cl.allow_reasons",
|
" WHEN ca.any_allow = 1 THEN ca.allow_reasons",
|
||||||
" WHEN pl.any_deny = 1 THEN pl.deny_reasons",
|
" WHEN pa.any_deny = 1 THEN pa.deny_reasons",
|
||||||
" WHEN pl.any_allow = 1 THEN pl.allow_reasons",
|
" WHEN pa.any_allow = 1 THEN pa.allow_reasons",
|
||||||
" WHEN gl.any_deny = 1 THEN gl.deny_reasons",
|
" WHEN ga.any_deny = 1 THEN ga.deny_reasons",
|
||||||
" WHEN gl.any_allow = 1 THEN gl.allow_reasons",
|
" WHEN ga.any_allow = 1 THEN ga.allow_reasons",
|
||||||
" ELSE '[]'",
|
" ELSE '[]'",
|
||||||
" END AS reason",
|
" END AS reason",
|
||||||
]
|
]
|
||||||
|
|
@ -374,21 +355,34 @@ async def _build_single_action_sql(
|
||||||
|
|
||||||
if include_is_private:
|
if include_is_private:
|
||||||
query_parts.append(
|
query_parts.append(
|
||||||
" , CASE WHEN ad.anon_is_allowed = 0 THEN 1 ELSE 0 END AS is_private"
|
" , CASE WHEN ("
|
||||||
|
"CASE"
|
||||||
|
" WHEN aca.any_deny = 1 THEN 0"
|
||||||
|
" WHEN aca.any_allow = 1 THEN 1"
|
||||||
|
" WHEN apa.any_deny = 1 THEN 0"
|
||||||
|
" WHEN apa.any_allow = 1 THEN 1"
|
||||||
|
" WHEN aga.any_deny = 1 THEN 0"
|
||||||
|
" WHEN aga.any_allow = 1 THEN 1"
|
||||||
|
" ELSE 0 END"
|
||||||
|
") = 0 THEN 1 ELSE 0 END AS is_private"
|
||||||
)
|
)
|
||||||
|
|
||||||
query_parts.extend(
|
query_parts.extend(
|
||||||
[
|
[
|
||||||
" FROM base b",
|
" FROM base b",
|
||||||
" JOIN child_lvl cl ON b.parent = cl.parent AND (b.child = cl.child OR (b.child IS NULL AND cl.child IS NULL))",
|
" LEFT JOIN child_agg ca ON ca.parent = b.parent AND ca.child = b.child",
|
||||||
" JOIN parent_lvl pl ON b.parent = pl.parent AND (b.child = pl.child OR (b.child IS NULL AND pl.child IS NULL))",
|
" LEFT JOIN parent_agg pa ON pa.parent = b.parent",
|
||||||
" JOIN global_lvl gl ON b.parent = gl.parent AND (b.child = gl.child OR (b.child IS NULL AND gl.child IS NULL))",
|
" CROSS JOIN global_agg ga",
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
if include_is_private:
|
if include_is_private:
|
||||||
query_parts.append(
|
query_parts.extend(
|
||||||
" JOIN anon_decisions ad ON b.parent = ad.parent AND (b.child = ad.child OR (b.child IS NULL AND ad.child IS NULL))"
|
[
|
||||||
|
" LEFT JOIN anon_child_agg aca ON aca.parent = b.parent AND aca.child = b.child",
|
||||||
|
" LEFT JOIN anon_parent_agg apa ON apa.parent = b.parent",
|
||||||
|
" CROSS JOIN anon_global_agg aga",
|
||||||
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
query_parts.append(")")
|
query_parts.append(")")
|
||||||
|
|
@ -398,10 +392,31 @@ async def _build_single_action_sql(
|
||||||
# Wrap each restriction_sql in a subquery to avoid operator precedence issues
|
# Wrap each restriction_sql in a subquery to avoid operator precedence issues
|
||||||
# with UNION ALL inside the restriction SQL statements
|
# with UNION ALL inside the restriction SQL statements
|
||||||
restriction_intersect = "\nINTERSECT\n".join(
|
restriction_intersect = "\nINTERSECT\n".join(
|
||||||
f"SELECT * FROM ({sql})" for sql in restriction_sqls
|
f"SELECT parent, child COLLATE {child_collation} AS child FROM ({sql})"
|
||||||
|
for sql in restriction_sqls
|
||||||
)
|
)
|
||||||
|
# Decompose by NULL-pattern so the final filter can use pure-equality
|
||||||
|
# EXISTS lookups (satisfiable via automatic indexes) instead of a
|
||||||
|
# correlated OR-scan over the whole list.
|
||||||
query_parts.extend(
|
query_parts.extend(
|
||||||
[",", "restriction_list AS (", f" {restriction_intersect}", ")"]
|
[
|
||||||
|
",",
|
||||||
|
"restriction_list AS (",
|
||||||
|
f" {restriction_intersect}",
|
||||||
|
"),",
|
||||||
|
"restriction_exact AS (",
|
||||||
|
" SELECT parent, child FROM restriction_list WHERE parent IS NOT NULL AND child IS NOT NULL",
|
||||||
|
"),",
|
||||||
|
"restriction_parent_any AS (",
|
||||||
|
" SELECT DISTINCT parent FROM restriction_list WHERE parent IS NOT NULL AND child IS NULL",
|
||||||
|
"),",
|
||||||
|
"restriction_child_any AS (",
|
||||||
|
" SELECT DISTINCT child FROM restriction_list WHERE parent IS NULL AND child IS NOT NULL",
|
||||||
|
"),",
|
||||||
|
"restriction_all AS (",
|
||||||
|
" SELECT 1 AS matched FROM restriction_list WHERE parent IS NULL AND child IS NULL LIMIT 1",
|
||||||
|
")",
|
||||||
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
# Final SELECT
|
# Final SELECT
|
||||||
|
|
@ -416,10 +431,11 @@ async def _build_single_action_sql(
|
||||||
# Add restriction filter if there are restrictions
|
# Add restriction filter if there are restrictions
|
||||||
if restriction_sqls:
|
if restriction_sqls:
|
||||||
query_parts.append("""
|
query_parts.append("""
|
||||||
AND EXISTS (
|
AND (
|
||||||
SELECT 1 FROM restriction_list r
|
EXISTS (SELECT 1 FROM restriction_all)
|
||||||
WHERE (r.parent = decisions.parent OR r.parent IS NULL)
|
OR EXISTS (SELECT 1 FROM restriction_parent_any r WHERE r.parent = decisions.parent)
|
||||||
AND (r.child = decisions.child OR r.child IS NULL)
|
OR EXISTS (SELECT 1 FROM restriction_child_any r WHERE r.child = decisions.child)
|
||||||
|
OR EXISTS (SELECT 1 FROM restriction_exact r WHERE r.parent = decisions.parent AND r.child = decisions.child)
|
||||||
)""")
|
)""")
|
||||||
|
|
||||||
# Add parent filter if specified
|
# Add parent filter if specified
|
||||||
|
|
@ -475,6 +491,7 @@ async def build_permission_rules_sql(
|
||||||
union_parts = []
|
union_parts = []
|
||||||
all_params = {}
|
all_params = {}
|
||||||
restriction_sqls = []
|
restriction_sqls = []
|
||||||
|
child_collation = _child_collation(action_obj)
|
||||||
|
|
||||||
for permission_sql in permission_sqls:
|
for permission_sql in permission_sqls:
|
||||||
all_params.update(permission_sql.params or {})
|
all_params.update(permission_sql.params or {})
|
||||||
|
|
@ -488,7 +505,7 @@ async def build_permission_rules_sql(
|
||||||
continue
|
continue
|
||||||
|
|
||||||
union_parts.append(f"""
|
union_parts.append(f"""
|
||||||
SELECT parent, child, allow, reason, '{permission_sql.source}' AS source_plugin FROM (
|
SELECT parent, child COLLATE {child_collation} AS child, allow, reason, '{permission_sql.source}' AS source_plugin FROM (
|
||||||
{permission_sql.sql}
|
{permission_sql.sql}
|
||||||
)
|
)
|
||||||
""".strip())
|
""".strip())
|
||||||
|
|
@ -559,6 +576,7 @@ async def check_permissions_for_actions(
|
||||||
verdicts = {}
|
verdicts = {}
|
||||||
|
|
||||||
for i, (action, permission_sqls) in enumerate(zip(unique_actions, gathered)):
|
for i, (action, permission_sqls) in enumerate(zip(unique_actions, gathered)):
|
||||||
|
child_collation = _child_collation(datasette.actions[action])
|
||||||
prefix = f"a{i}_"
|
prefix = f"a{i}_"
|
||||||
rule_parts = []
|
rule_parts = []
|
||||||
restriction_parts = []
|
restriction_parts = []
|
||||||
|
|
@ -584,7 +602,7 @@ async def check_permissions_for_actions(
|
||||||
if sql is None:
|
if sql is None:
|
||||||
continue
|
continue
|
||||||
rule_parts.append(
|
rule_parts.append(
|
||||||
f"SELECT parent, child, allow, reason, '{permission_sql.source}' AS source_plugin FROM (\n{sql}\n)"
|
f"SELECT parent, child COLLATE {child_collation} AS child, allow, reason, '{permission_sql.source}' AS source_plugin FROM (\n{sql}\n)"
|
||||||
)
|
)
|
||||||
|
|
||||||
if not rule_parts:
|
if not rule_parts:
|
||||||
|
|
@ -618,7 +636,8 @@ async def check_permissions_for_actions(
|
||||||
if restriction_parts:
|
if restriction_parts:
|
||||||
# Database-level restrictions (parent, NULL) match all children
|
# Database-level restrictions (parent, NULL) match all children
|
||||||
restriction_intersect = "\nINTERSECT\n".join(
|
restriction_intersect = "\nINTERSECT\n".join(
|
||||||
f"SELECT * FROM ({sql})" for sql in restriction_parts
|
f"SELECT parent, child COLLATE {child_collation} AS child FROM ({sql})"
|
||||||
|
for sql in restriction_parts
|
||||||
)
|
)
|
||||||
ctes.append(f"a{i}_restriction AS (\n{restriction_intersect}\n)")
|
ctes.append(f"a{i}_restriction AS (\n{restriction_intersect}\n)")
|
||||||
verdict_sql = f"""({verdict_sql}) AND EXISTS (
|
verdict_sql = f"""({verdict_sql}) AND EXISTS (
|
||||||
|
|
@ -673,3 +692,240 @@ async def check_permission_for_resource(
|
||||||
child=child,
|
child=child,
|
||||||
)
|
)
|
||||||
return results[action]
|
return results[action]
|
||||||
|
|
||||||
|
|
||||||
|
async def explain_permission_for_resource(
|
||||||
|
*,
|
||||||
|
datasette: "Datasette",
|
||||||
|
actor: dict | None,
|
||||||
|
action: str,
|
||||||
|
parent: str | None,
|
||||||
|
child: str | None,
|
||||||
|
) -> dict:
|
||||||
|
"""Explain a permission decision for one action and resource.
|
||||||
|
|
||||||
|
This is intended for Datasette's permission debugging tools. It uses the
|
||||||
|
same ``permission_resources_sql`` hook results and the same resolution
|
||||||
|
rules as :func:`check_permissions_for_actions`, but also returns the
|
||||||
|
matching rules, actor restriction results and ``also_requires`` chain.
|
||||||
|
|
||||||
|
The returned dictionary is part of Datasette's unstable debugging API.
|
||||||
|
"""
|
||||||
|
|
||||||
|
action_obj = datasette.actions.get(action)
|
||||||
|
if action_obj is None:
|
||||||
|
raise ValueError(f"Unknown action: {action}")
|
||||||
|
|
||||||
|
explanation = await _explain_single_action(
|
||||||
|
datasette=datasette,
|
||||||
|
actor=actor,
|
||||||
|
action=action,
|
||||||
|
parent=parent,
|
||||||
|
child=child,
|
||||||
|
)
|
||||||
|
|
||||||
|
required_actions = []
|
||||||
|
if action_obj.also_requires:
|
||||||
|
required = await explain_permission_for_resource(
|
||||||
|
datasette=datasette,
|
||||||
|
actor=actor,
|
||||||
|
action=action_obj.also_requires,
|
||||||
|
parent=parent,
|
||||||
|
child=child,
|
||||||
|
)
|
||||||
|
required_actions.append(required)
|
||||||
|
|
||||||
|
explanation["required_actions"] = required_actions
|
||||||
|
explanation["allowed"] = bool(
|
||||||
|
explanation["rule_allowed"]
|
||||||
|
and explanation["restriction_allowed"]
|
||||||
|
and all(required["allowed"] for required in required_actions)
|
||||||
|
)
|
||||||
|
explanation["summary"] = _permission_explanation_summary(explanation)
|
||||||
|
return explanation
|
||||||
|
|
||||||
|
|
||||||
|
async def _explain_single_action(
|
||||||
|
*,
|
||||||
|
datasette: "Datasette",
|
||||||
|
actor: dict | None,
|
||||||
|
action: str,
|
||||||
|
parent: str | None,
|
||||||
|
child: str | None,
|
||||||
|
) -> dict:
|
||||||
|
"""Return matching rules and restrictions for a single action."""
|
||||||
|
from datasette.utils.permissions import SKIP_PERMISSION_CHECKS
|
||||||
|
|
||||||
|
permission_sqls = await gather_permission_sql_from_hooks(
|
||||||
|
datasette=datasette,
|
||||||
|
actor=actor,
|
||||||
|
action=action,
|
||||||
|
)
|
||||||
|
|
||||||
|
if permission_sqls is SKIP_PERMISSION_CHECKS:
|
||||||
|
return {
|
||||||
|
"action": action,
|
||||||
|
"rule_allowed": True,
|
||||||
|
"restriction_allowed": True,
|
||||||
|
"winning_scope": "global",
|
||||||
|
"matched_rules": [
|
||||||
|
{
|
||||||
|
"scope": "global",
|
||||||
|
"effect": "allow",
|
||||||
|
"source": "skip_permission_checks",
|
||||||
|
"reason": "Permission checks were explicitly skipped",
|
||||||
|
"decisive": True,
|
||||||
|
"ignored_because": None,
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"restrictions": [],
|
||||||
|
}
|
||||||
|
|
||||||
|
db = datasette.get_internal_database()
|
||||||
|
matched_rules = []
|
||||||
|
restrictions = []
|
||||||
|
child_collation = _child_collation(datasette.actions[action])
|
||||||
|
|
||||||
|
for permission_sql in permission_sqls:
|
||||||
|
params = dict(permission_sql.params or {})
|
||||||
|
parent_param = _unused_parameter_name(params, "_explain_parent")
|
||||||
|
params[parent_param] = parent
|
||||||
|
child_param = _unused_parameter_name(params, "_explain_child")
|
||||||
|
params[child_param] = child
|
||||||
|
|
||||||
|
if permission_sql.sql:
|
||||||
|
rows = await db.execute(
|
||||||
|
f"""
|
||||||
|
SELECT parent, child, allow, reason
|
||||||
|
FROM ({permission_sql.sql}) AS permission_rules
|
||||||
|
WHERE (parent IS NULL OR parent = :{parent_param})
|
||||||
|
AND (child IS NULL OR child COLLATE {child_collation} = :{child_param})
|
||||||
|
""",
|
||||||
|
params,
|
||||||
|
)
|
||||||
|
for row in rows:
|
||||||
|
specificity = (
|
||||||
|
2
|
||||||
|
if row["child"] is not None
|
||||||
|
else 1 if row["parent"] is not None else 0
|
||||||
|
)
|
||||||
|
matched_rules.append(
|
||||||
|
{
|
||||||
|
"scope": ("resource", "parent", "global")[2 - specificity],
|
||||||
|
"effect": "allow" if row["allow"] else "deny",
|
||||||
|
"source": permission_sql.source,
|
||||||
|
"reason": row["reason"],
|
||||||
|
"_specificity": specificity,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
if permission_sql.restriction_sql:
|
||||||
|
restriction_row = (
|
||||||
|
await db.execute(
|
||||||
|
f"""
|
||||||
|
SELECT EXISTS(
|
||||||
|
SELECT 1 FROM ({permission_sql.restriction_sql}) AS restriction_rules
|
||||||
|
WHERE (parent IS NULL OR parent = :{parent_param})
|
||||||
|
AND (child IS NULL OR child COLLATE {child_collation} = :{child_param})
|
||||||
|
) AS resource_is_in_allowlist
|
||||||
|
""",
|
||||||
|
params,
|
||||||
|
)
|
||||||
|
).first()
|
||||||
|
restriction_allowed = bool(restriction_row[0])
|
||||||
|
restrictions.append(
|
||||||
|
{
|
||||||
|
"source": permission_sql.source,
|
||||||
|
"allowed": restriction_allowed,
|
||||||
|
"reason": params.get("deny")
|
||||||
|
or (
|
||||||
|
"Resource is included in this restriction allowlist"
|
||||||
|
if restriction_allowed
|
||||||
|
else "Resource is not included in this restriction allowlist"
|
||||||
|
),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
matched_rules.sort(
|
||||||
|
key=lambda rule: (
|
||||||
|
-rule["_specificity"],
|
||||||
|
0 if rule["effect"] == "deny" else 1,
|
||||||
|
rule["source"] or "",
|
||||||
|
rule["reason"] or "",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
if matched_rules:
|
||||||
|
winning_specificity = matched_rules[0]["_specificity"]
|
||||||
|
winning_rules = [
|
||||||
|
rule
|
||||||
|
for rule in matched_rules
|
||||||
|
if rule["_specificity"] == winning_specificity
|
||||||
|
]
|
||||||
|
rule_allowed = not any(rule["effect"] == "deny" for rule in winning_rules)
|
||||||
|
winning_scope = winning_rules[0]["scope"]
|
||||||
|
else:
|
||||||
|
winning_specificity = None
|
||||||
|
rule_allowed = False
|
||||||
|
winning_scope = None
|
||||||
|
|
||||||
|
for rule in matched_rules:
|
||||||
|
specificity = rule.pop("_specificity")
|
||||||
|
if specificity != winning_specificity:
|
||||||
|
rule["decisive"] = False
|
||||||
|
rule["ignored_because"] = "A more specific rule matched"
|
||||||
|
elif not rule_allowed and rule["effect"] == "allow":
|
||||||
|
rule["decisive"] = False
|
||||||
|
rule["ignored_because"] = "A deny rule matched at the same scope"
|
||||||
|
else:
|
||||||
|
rule["decisive"] = True
|
||||||
|
rule["ignored_because"] = None
|
||||||
|
|
||||||
|
return {
|
||||||
|
"action": action,
|
||||||
|
"rule_allowed": rule_allowed,
|
||||||
|
"restriction_allowed": all(
|
||||||
|
restriction["allowed"] for restriction in restrictions
|
||||||
|
),
|
||||||
|
"winning_scope": winning_scope,
|
||||||
|
"matched_rules": matched_rules,
|
||||||
|
"restrictions": restrictions,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _unused_parameter_name(params: dict, preferred: str) -> str:
|
||||||
|
"""Return a SQL parameter name that is not already in ``params``."""
|
||||||
|
candidate = preferred
|
||||||
|
suffix = 2
|
||||||
|
while candidate in params:
|
||||||
|
candidate = f"{preferred}_{suffix}"
|
||||||
|
suffix += 1
|
||||||
|
return candidate
|
||||||
|
|
||||||
|
|
||||||
|
def _permission_explanation_summary(explanation: dict) -> str:
|
||||||
|
denied_requirement = next(
|
||||||
|
(
|
||||||
|
required
|
||||||
|
for required in explanation["required_actions"]
|
||||||
|
if not required["allowed"]
|
||||||
|
),
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
if denied_requirement:
|
||||||
|
return (
|
||||||
|
f"Denied because {explanation['action']} also requires "
|
||||||
|
f"{denied_requirement['action']}, which was denied."
|
||||||
|
)
|
||||||
|
if not explanation["matched_rules"]:
|
||||||
|
return "Denied because no permission rule matched this actor and resource."
|
||||||
|
if not explanation["rule_allowed"]:
|
||||||
|
return (
|
||||||
|
f"Denied by a {explanation['winning_scope']}-level rule. "
|
||||||
|
"Deny rules take precedence over allow rules at the same scope."
|
||||||
|
)
|
||||||
|
if not explanation["restriction_allowed"]:
|
||||||
|
return (
|
||||||
|
"Denied because the resource is not included in the actor's restrictions."
|
||||||
|
)
|
||||||
|
return f"Allowed by the matching {explanation['winning_scope']}-level rule."
|
||||||
|
|
|
||||||
|
|
@ -1,28 +1,30 @@
|
||||||
|
import asyncio
|
||||||
import json
|
import json
|
||||||
from typing import Optional
|
import re
|
||||||
from datasette.utils import MultiParams, calculate_etag
|
from http.cookies import Morsel, SimpleCookie
|
||||||
|
from mimetypes import guess_type
|
||||||
|
from pathlib import Path
|
||||||
|
from urllib.parse import parse_qs, parse_qsl, urlunparse
|
||||||
|
|
||||||
|
import aiofiles
|
||||||
|
import aiofiles.os
|
||||||
|
|
||||||
|
from datasette.utils import MultiParams, calculate_etag, error_body, sha256_file
|
||||||
from datasette.utils.multipart import (
|
from datasette.utils.multipart import (
|
||||||
parse_form_data,
|
|
||||||
MultipartParseError,
|
|
||||||
FormData,
|
|
||||||
DEFAULT_MAX_FILE_SIZE,
|
|
||||||
DEFAULT_MAX_REQUEST_SIZE,
|
|
||||||
DEFAULT_MAX_FIELDS,
|
|
||||||
DEFAULT_MAX_FILES,
|
|
||||||
DEFAULT_MAX_PARTS,
|
|
||||||
DEFAULT_MAX_FIELD_SIZE,
|
DEFAULT_MAX_FIELD_SIZE,
|
||||||
|
DEFAULT_MAX_FIELDS,
|
||||||
|
DEFAULT_MAX_FILE_SIZE,
|
||||||
|
DEFAULT_MAX_FILES,
|
||||||
DEFAULT_MAX_MEMORY_FILE_SIZE,
|
DEFAULT_MAX_MEMORY_FILE_SIZE,
|
||||||
DEFAULT_MAX_PART_HEADER_BYTES,
|
DEFAULT_MAX_PART_HEADER_BYTES,
|
||||||
DEFAULT_MAX_PART_HEADER_LINES,
|
DEFAULT_MAX_PART_HEADER_LINES,
|
||||||
|
DEFAULT_MAX_PARTS,
|
||||||
|
DEFAULT_MAX_REQUEST_SIZE,
|
||||||
DEFAULT_MIN_FREE_DISK_BYTES,
|
DEFAULT_MIN_FREE_DISK_BYTES,
|
||||||
|
FormData,
|
||||||
|
MultipartParseError,
|
||||||
|
parse_form_data,
|
||||||
)
|
)
|
||||||
from mimetypes import guess_type
|
|
||||||
from urllib.parse import parse_qs, urlunparse, parse_qsl
|
|
||||||
from pathlib import Path
|
|
||||||
from http.cookies import SimpleCookie, Morsel
|
|
||||||
import aiofiles
|
|
||||||
import aiofiles.os
|
|
||||||
import re
|
|
||||||
|
|
||||||
# Workaround for adding samesite support to pre 3.8 python
|
# Workaround for adding samesite support to pre 3.8 python
|
||||||
Morsel._reserved["samesite"] = "SameSite"
|
Morsel._reserved["samesite"] = "SameSite"
|
||||||
|
|
@ -67,16 +69,41 @@ class BadRequest(Base400):
|
||||||
status = 400
|
status = 400
|
||||||
|
|
||||||
|
|
||||||
|
class PayloadTooLarge(Base400):
|
||||||
|
status = 413
|
||||||
|
|
||||||
|
|
||||||
SAMESITE_VALUES = ("strict", "lax", "none")
|
SAMESITE_VALUES = ("strict", "lax", "none")
|
||||||
|
|
||||||
|
# Bodies read fully into memory (post_body/post_vars/json) are capped at this
|
||||||
|
# size unless the max_post_body_bytes setting says otherwise. Kept deliberately
|
||||||
|
# far below multipart's DEFAULT_MAX_REQUEST_SIZE: that parser streams to disk,
|
||||||
|
# while these bodies are held in RAM and json.loads() can multiply their
|
||||||
|
# footprint several times over.
|
||||||
|
DEFAULT_MAX_POST_BODY_BYTES = 2 * 1024 * 1024 # 2MB
|
||||||
|
|
||||||
|
|
||||||
|
class _RequestHeaders(dict):
|
||||||
|
"""Incoming headers with lowercase keys and case-insensitive lookups."""
|
||||||
|
|
||||||
|
def __getitem__(self, key):
|
||||||
|
return super().__getitem__(key.lower())
|
||||||
|
|
||||||
|
def get(self, key, default=None):
|
||||||
|
return super().get(key.lower(), default)
|
||||||
|
|
||||||
|
def __contains__(self, key):
|
||||||
|
return super().__contains__(key.lower())
|
||||||
|
|
||||||
|
|
||||||
class Request:
|
class Request:
|
||||||
def __init__(self, scope, receive):
|
def __init__(self, scope, receive, max_post_body_bytes=DEFAULT_MAX_POST_BODY_BYTES):
|
||||||
self.scope = scope
|
self.scope = scope
|
||||||
self.receive = receive
|
self.receive = receive
|
||||||
|
self.max_post_body_bytes = max_post_body_bytes
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<asgi.Request method="{}" url="{}">'.format(self.method, self.url)
|
return f'<asgi.Request method="{self.method}" url="{self.url}">'
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def method(self):
|
def method(self):
|
||||||
|
|
@ -98,10 +125,10 @@ class Request:
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def headers(self):
|
def headers(self):
|
||||||
return {
|
return _RequestHeaders(
|
||||||
k.decode("latin-1").lower(): v.decode("latin-1")
|
(k.decode("latin-1").lower(), v.decode("latin-1"))
|
||||||
for k, v in self.scope.get("headers") or []
|
for k, v in self.scope.get("headers") or []
|
||||||
}
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def host(self):
|
def host(self):
|
||||||
|
|
@ -141,15 +168,43 @@ class Request:
|
||||||
def actor(self):
|
def actor(self):
|
||||||
return self.scope.get("actor", None)
|
return self.scope.get("actor", None)
|
||||||
|
|
||||||
async def post_body(self):
|
async def post_body(self, max_bytes=None):
|
||||||
body = b""
|
"""
|
||||||
|
Read the request body fully into memory.
|
||||||
|
|
||||||
|
The body is capped at max_bytes - or self.max_post_body_bytes
|
||||||
|
(default 2MB, set from the max_post_body_bytes setting for requests
|
||||||
|
created by Datasette) if max_bytes is not provided. Pass max_bytes=0
|
||||||
|
to disable the limit. Raises PayloadTooLarge (HTTP 413) if exceeded -
|
||||||
|
oversized bodies are rejected as soon as the limit is passed, without
|
||||||
|
buffering the rest.
|
||||||
|
"""
|
||||||
|
if max_bytes is None:
|
||||||
|
max_bytes = self.max_post_body_bytes
|
||||||
|
too_large = PayloadTooLarge(
|
||||||
|
f"Request body exceeded maximum size of {max_bytes} bytes"
|
||||||
|
)
|
||||||
|
if max_bytes:
|
||||||
|
# Reject early if the client declares an oversized body
|
||||||
|
try:
|
||||||
|
if int(self.headers.get("content-length", "")) > max_bytes:
|
||||||
|
raise too_large
|
||||||
|
except ValueError:
|
||||||
|
# Missing or malformed - the streaming check below still applies
|
||||||
|
pass
|
||||||
|
chunks = []
|
||||||
|
received = 0
|
||||||
more_body = True
|
more_body = True
|
||||||
while more_body:
|
while more_body:
|
||||||
message = await self.receive()
|
message = await self.receive()
|
||||||
assert message["type"] == "http.request", message
|
assert message["type"] == "http.request", message
|
||||||
body += message.get("body", b"")
|
chunk = message.get("body", b"")
|
||||||
|
received += len(chunk)
|
||||||
|
if max_bytes and received > max_bytes:
|
||||||
|
raise too_large
|
||||||
|
chunks.append(chunk)
|
||||||
more_body = message.get("more_body", False)
|
more_body = message.get("more_body", False)
|
||||||
return body
|
return b"".join(chunks)
|
||||||
|
|
||||||
async def post_vars(self):
|
async def post_vars(self):
|
||||||
body = await self.post_body()
|
body = await self.post_body()
|
||||||
|
|
@ -166,7 +221,7 @@ class Request:
|
||||||
max_request_size: int = DEFAULT_MAX_REQUEST_SIZE,
|
max_request_size: int = DEFAULT_MAX_REQUEST_SIZE,
|
||||||
max_fields: int = DEFAULT_MAX_FIELDS,
|
max_fields: int = DEFAULT_MAX_FIELDS,
|
||||||
max_files: int = DEFAULT_MAX_FILES,
|
max_files: int = DEFAULT_MAX_FILES,
|
||||||
max_parts: Optional[int] = DEFAULT_MAX_PARTS,
|
max_parts: int | None = DEFAULT_MAX_PARTS,
|
||||||
max_field_size: int = DEFAULT_MAX_FIELD_SIZE,
|
max_field_size: int = DEFAULT_MAX_FIELD_SIZE,
|
||||||
max_memory_file_size: int = DEFAULT_MAX_MEMORY_FILE_SIZE,
|
max_memory_file_size: int = DEFAULT_MAX_MEMORY_FILE_SIZE,
|
||||||
max_part_header_bytes: int = DEFAULT_MAX_PART_HEADER_BYTES,
|
max_part_header_bytes: int = DEFAULT_MAX_PART_HEADER_BYTES,
|
||||||
|
|
@ -259,12 +314,24 @@ class AsgiLifespan:
|
||||||
while True:
|
while True:
|
||||||
message = await receive()
|
message = await receive()
|
||||||
if message["type"] == "lifespan.startup":
|
if message["type"] == "lifespan.startup":
|
||||||
for fn in self.on_startup:
|
try:
|
||||||
await fn()
|
for fn in self.on_startup:
|
||||||
|
await fn()
|
||||||
|
except Exception as e: # noqa: BLE001
|
||||||
|
await send(
|
||||||
|
{"type": "lifespan.startup.failed", "message": str(e)}
|
||||||
|
)
|
||||||
|
return
|
||||||
await send({"type": "lifespan.startup.complete"})
|
await send({"type": "lifespan.startup.complete"})
|
||||||
elif message["type"] == "lifespan.shutdown":
|
elif message["type"] == "lifespan.shutdown":
|
||||||
for fn in self.on_shutdown:
|
try:
|
||||||
await fn()
|
for fn in self.on_shutdown:
|
||||||
|
await fn()
|
||||||
|
except Exception as e: # noqa: BLE001
|
||||||
|
await send(
|
||||||
|
{"type": "lifespan.shutdown.failed", "message": str(e)}
|
||||||
|
)
|
||||||
|
return
|
||||||
await send({"type": "lifespan.shutdown.complete"})
|
await send({"type": "lifespan.shutdown.complete"})
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
|
|
@ -397,6 +464,9 @@ async def asgi_send_file(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
HASHED_STATIC_CACHE_CONTROL = "max-age=31536000, immutable, public"
|
||||||
|
|
||||||
|
|
||||||
def asgi_static(root_path, chunk_size=4096, headers=None, content_type=None):
|
def asgi_static(root_path, chunk_size=4096, headers=None, content_type=None):
|
||||||
root_path = Path(root_path)
|
root_path = Path(root_path)
|
||||||
static_headers = {}
|
static_headers = {}
|
||||||
|
|
@ -423,11 +493,17 @@ def asgi_static(root_path, chunk_size=4096, headers=None, content_type=None):
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
# Calculate ETag for filepath
|
# Calculate ETag for filepath
|
||||||
|
hash_value = request.args.get("_hash")
|
||||||
|
if (
|
||||||
|
hash_value
|
||||||
|
and hash_value == sha256_file(full_path, chunk_size=chunk_size)[:12]
|
||||||
|
):
|
||||||
|
headers["Cache-Control"] = HASHED_STATIC_CACHE_CONTROL
|
||||||
etag = await calculate_etag(full_path, chunk_size=chunk_size)
|
etag = await calculate_etag(full_path, chunk_size=chunk_size)
|
||||||
headers["ETag"] = etag
|
headers["ETag"] = etag
|
||||||
if_none_match = request.headers.get("if-none-match")
|
if_none_match = request.headers.get("if-none-match")
|
||||||
if if_none_match and if_none_match == etag:
|
if if_none_match and if_none_match == etag:
|
||||||
return await asgi_send(send, "", 304)
|
return await asgi_send(send, "", 304, headers=headers)
|
||||||
await asgi_send_file(
|
await asgi_send_file(
|
||||||
send, full_path, chunk_size=chunk_size, headers=headers
|
send, full_path, chunk_size=chunk_size, headers=headers
|
||||||
)
|
)
|
||||||
|
|
@ -435,6 +511,8 @@ def asgi_static(root_path, chunk_size=4096, headers=None, content_type=None):
|
||||||
await asgi_send_html(send, "404: File not found", 404)
|
await asgi_send_html(send, "404: File not found", 404)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# Only the actual static-file handler can bypass dynamic response privacy.
|
||||||
|
inner_static._datasette_static = True
|
||||||
return inner_static
|
return inner_static
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -480,9 +558,9 @@ class Response:
|
||||||
httponly=False,
|
httponly=False,
|
||||||
samesite="lax",
|
samesite="lax",
|
||||||
):
|
):
|
||||||
assert samesite in SAMESITE_VALUES, "samesite should be one of {}".format(
|
assert (
|
||||||
SAMESITE_VALUES
|
samesite in SAMESITE_VALUES
|
||||||
)
|
), f"samesite should be one of {SAMESITE_VALUES}"
|
||||||
cookie = SimpleCookie()
|
cookie = SimpleCookie()
|
||||||
cookie[key] = value
|
cookie[key] = value
|
||||||
for prop_name, prop_value in (
|
for prop_name, prop_value in (
|
||||||
|
|
@ -526,6 +604,18 @@ class Response:
|
||||||
content_type="application/json; charset=utf-8",
|
content_type="application/json; charset=utf-8",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def error(cls, messages, status=400, headers=None):
|
||||||
|
"""
|
||||||
|
A JSON error response using Datasette's standard error format.
|
||||||
|
|
||||||
|
messages can be a single string or a list of strings. For errors
|
||||||
|
that should content-negotiate between JSON and HTML, raise
|
||||||
|
Forbidden, NotFound, BadRequest or DatasetteError instead and let
|
||||||
|
Datasette's error handling hooks build the response.
|
||||||
|
"""
|
||||||
|
return cls.json(error_body(messages, status), status=status, headers=headers)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def redirect(cls, path, status=302, headers=None):
|
def redirect(cls, path, status=302, headers=None):
|
||||||
headers = headers or {}
|
headers = headers or {}
|
||||||
|
|
@ -562,10 +652,23 @@ class AsgiRunOnFirstRequest:
|
||||||
self.asgi = asgi
|
self.asgi = asgi
|
||||||
self.on_startup = on_startup
|
self.on_startup = on_startup
|
||||||
self._started = False
|
self._started = False
|
||||||
|
# Guards against concurrent early requests interleaving with startup:
|
||||||
|
# without this, several requests could all observe `_started is
|
||||||
|
# False` and proceed before any of them finish running the hooks.
|
||||||
|
self._lock = asyncio.Lock()
|
||||||
|
|
||||||
async def __call__(self, scope, receive, send):
|
async def __call__(self, scope, receive, send):
|
||||||
if not self._started:
|
# Leave "lifespan" scope events alone - this shim only exists as a
|
||||||
self._started = True
|
# fallback for hosts that never send them. It wraps AsgiLifespan, so
|
||||||
for hook in self.on_startup:
|
# if it ran on_startup here too, a startup exception would escape
|
||||||
await hook()
|
# before AsgiLifespan's own try/except got a chance to turn it into
|
||||||
|
# a lifespan.startup.failed message.
|
||||||
|
if scope["type"] != "lifespan" and not self._started:
|
||||||
|
async with self._lock:
|
||||||
|
# Re-check: another request may have finished startup while
|
||||||
|
# we were waiting for the lock.
|
||||||
|
if not self._started:
|
||||||
|
for hook in self.on_startup:
|
||||||
|
await hook()
|
||||||
|
self._started = True
|
||||||
return await self.asgi(scope, receive, send)
|
return await self.asgi(scope, receive, send)
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ Originally shared here: https://www.djangosnippets.org/snippets/1431/
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class BaseConverter(object):
|
class BaseConverter:
|
||||||
decimal_digits = "0123456789"
|
decimal_digits = "0123456789"
|
||||||
|
|
||||||
def __init__(self, digits):
|
def __init__(self, digits):
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import inspect
|
import inspect
|
||||||
import types
|
import types
|
||||||
from typing import NamedTuple, Any
|
from typing import Any, NamedTuple
|
||||||
|
|
||||||
|
|
||||||
class CallableStatus(NamedTuple):
|
class CallableStatus(NamedTuple):
|
||||||
|
|
@ -19,7 +19,7 @@ def check_callable(obj: Any) -> CallableStatus:
|
||||||
if isinstance(obj, types.FunctionType):
|
if isinstance(obj, types.FunctionType):
|
||||||
return CallableStatus(True, inspect.iscoroutinefunction(obj))
|
return CallableStatus(True, inspect.iscoroutinefunction(obj))
|
||||||
|
|
||||||
if hasattr(obj, "__call__"):
|
if callable(obj):
|
||||||
return CallableStatus(True, inspect.iscoroutinefunction(obj.__call__))
|
return CallableStatus(True, inspect.iscoroutinefunction(obj.__call__))
|
||||||
|
|
||||||
assert False, "obj {} is somehow callable with no __call__ method".format(repr(obj))
|
assert False, f"obj {obj!r} is somehow callable with no __call__ method"
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,30 @@
|
||||||
import textwrap
|
import textwrap
|
||||||
from datasette.utils import table_column_details
|
|
||||||
|
|
||||||
|
from sqlite_utils import Database as SQLiteUtilsDatabase
|
||||||
|
from sqlite_utils import Migrations
|
||||||
|
|
||||||
async def init_internal_db(db):
|
from datasette.utils import escape_sqlite, table_column_details
|
||||||
create_tables_sql = textwrap.dedent("""
|
|
||||||
|
INTERNAL_DB_SCHEMA_TABLES = {
|
||||||
|
"catalog_databases",
|
||||||
|
"catalog_tables",
|
||||||
|
"catalog_views",
|
||||||
|
"catalog_columns",
|
||||||
|
"catalog_indexes",
|
||||||
|
"catalog_foreign_keys",
|
||||||
|
"metadata_instance",
|
||||||
|
"metadata_databases",
|
||||||
|
"metadata_resources",
|
||||||
|
"metadata_columns",
|
||||||
|
"column_types",
|
||||||
|
"queries",
|
||||||
|
}
|
||||||
|
|
||||||
|
INTERNAL_DB_SCHEMA_INDEXES = {
|
||||||
|
"queries_owner_idx",
|
||||||
|
}
|
||||||
|
|
||||||
|
INTERNAL_DB_SCHEMA_SQL = textwrap.dedent("""
|
||||||
CREATE TABLE IF NOT EXISTS catalog_databases (
|
CREATE TABLE IF NOT EXISTS catalog_databases (
|
||||||
database_name TEXT PRIMARY KEY,
|
database_name TEXT PRIMARY KEY,
|
||||||
path TEXT,
|
path TEXT,
|
||||||
|
|
@ -67,99 +88,101 @@ async def init_internal_db(db):
|
||||||
FOREIGN KEY (database_name) REFERENCES catalog_databases(database_name),
|
FOREIGN KEY (database_name) REFERENCES catalog_databases(database_name),
|
||||||
FOREIGN KEY (database_name, table_name) REFERENCES catalog_tables(database_name, table_name)
|
FOREIGN KEY (database_name, table_name) REFERENCES catalog_tables(database_name, table_name)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS metadata_instance (
|
||||||
|
key text,
|
||||||
|
value text,
|
||||||
|
unique(key)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS metadata_databases (
|
||||||
|
database_name text,
|
||||||
|
key text,
|
||||||
|
value text,
|
||||||
|
unique(database_name, key)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS metadata_resources (
|
||||||
|
database_name text,
|
||||||
|
resource_name text,
|
||||||
|
key text,
|
||||||
|
value text,
|
||||||
|
unique(database_name, resource_name, key)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS metadata_columns (
|
||||||
|
database_name text,
|
||||||
|
resource_name text,
|
||||||
|
column_name text,
|
||||||
|
key text,
|
||||||
|
value text,
|
||||||
|
unique(database_name, resource_name, column_name, key)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS column_types (
|
||||||
|
database_name TEXT NOT NULL,
|
||||||
|
resource_name TEXT NOT NULL,
|
||||||
|
column_name TEXT NOT NULL,
|
||||||
|
column_type TEXT NOT NULL,
|
||||||
|
config TEXT,
|
||||||
|
PRIMARY KEY (database_name, resource_name, column_name)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS queries (
|
||||||
|
database_name TEXT NOT NULL,
|
||||||
|
name TEXT NOT NULL,
|
||||||
|
sql TEXT NOT NULL,
|
||||||
|
title TEXT,
|
||||||
|
description TEXT,
|
||||||
|
description_html TEXT,
|
||||||
|
options TEXT NOT NULL DEFAULT '{}',
|
||||||
|
parameters TEXT NOT NULL DEFAULT '[]',
|
||||||
|
is_write INTEGER NOT NULL DEFAULT 0 CHECK (is_write IN (0, 1)),
|
||||||
|
is_private INTEGER NOT NULL DEFAULT 0 CHECK (is_private IN (0, 1)),
|
||||||
|
is_trusted INTEGER NOT NULL DEFAULT 0 CHECK (is_trusted IN (0, 1)),
|
||||||
|
source TEXT NOT NULL DEFAULT 'user',
|
||||||
|
owner_id TEXT,
|
||||||
|
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
PRIMARY KEY (database_name, name)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS queries_owner_idx
|
||||||
|
ON queries(owner_id);
|
||||||
""").strip()
|
""").strip()
|
||||||
await db.execute_write_script(create_tables_sql)
|
|
||||||
await initialize_metadata_tables(db)
|
|
||||||
|
|
||||||
|
|
||||||
async def initialize_metadata_tables(db):
|
internal_migrations = Migrations("datasette_internal")
|
||||||
await db.execute_write_script(textwrap.dedent("""
|
|
||||||
CREATE TABLE IF NOT EXISTS metadata_instance (
|
|
||||||
key text,
|
|
||||||
value text,
|
|
||||||
unique(key)
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS metadata_databases (
|
|
||||||
database_name text,
|
|
||||||
key text,
|
|
||||||
value text,
|
|
||||||
unique(database_name, key)
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS metadata_resources (
|
|
||||||
database_name text,
|
|
||||||
resource_name text,
|
|
||||||
key text,
|
|
||||||
value text,
|
|
||||||
unique(database_name, resource_name, key)
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS metadata_columns (
|
|
||||||
database_name text,
|
|
||||||
resource_name text,
|
|
||||||
column_name text,
|
|
||||||
key text,
|
|
||||||
value text,
|
|
||||||
unique(database_name, resource_name, column_name, key)
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS column_types (
|
|
||||||
database_name TEXT NOT NULL,
|
|
||||||
resource_name TEXT NOT NULL,
|
|
||||||
column_name TEXT NOT NULL,
|
|
||||||
column_type TEXT NOT NULL,
|
|
||||||
config TEXT,
|
|
||||||
PRIMARY KEY (database_name, resource_name, column_name)
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS queries (
|
|
||||||
database_name TEXT NOT NULL,
|
|
||||||
name TEXT NOT NULL,
|
|
||||||
sql TEXT NOT NULL,
|
|
||||||
title TEXT,
|
|
||||||
description TEXT,
|
|
||||||
description_html TEXT,
|
|
||||||
options TEXT NOT NULL DEFAULT '{}',
|
|
||||||
parameters TEXT NOT NULL DEFAULT '[]',
|
|
||||||
is_write INTEGER NOT NULL DEFAULT 0 CHECK (is_write IN (0, 1)),
|
|
||||||
is_private INTEGER NOT NULL DEFAULT 0 CHECK (is_private IN (0, 1)),
|
|
||||||
is_trusted INTEGER NOT NULL DEFAULT 0 CHECK (is_trusted IN (0, 1)),
|
|
||||||
source TEXT NOT NULL DEFAULT 'user',
|
|
||||||
owner_id TEXT,
|
|
||||||
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
||||||
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
||||||
PRIMARY KEY (database_name, name)
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE INDEX IF NOT EXISTS queries_owner_idx
|
|
||||||
ON queries(owner_id);
|
|
||||||
"""))
|
|
||||||
|
|
||||||
|
|
||||||
async def populate_schema_tables(internal_db, db):
|
def _internal_schema_exists(db):
|
||||||
|
table_names = set(db.table_names())
|
||||||
|
if not INTERNAL_DB_SCHEMA_TABLES.issubset(table_names):
|
||||||
|
return False
|
||||||
|
index_names = {
|
||||||
|
row[0]
|
||||||
|
for row in db.execute("select name from sqlite_master where type = 'index'")
|
||||||
|
}
|
||||||
|
return INTERNAL_DB_SCHEMA_INDEXES.issubset(index_names)
|
||||||
|
|
||||||
|
|
||||||
|
@internal_migrations(name="0001_initial")
|
||||||
|
def initial_internal_schema(db):
|
||||||
|
if _internal_schema_exists(db):
|
||||||
|
return
|
||||||
|
db.executescript(INTERNAL_DB_SCHEMA_SQL)
|
||||||
|
|
||||||
|
|
||||||
|
async def init_internal_db(db):
|
||||||
|
def apply_migrations(conn):
|
||||||
|
internal_migrations.apply(SQLiteUtilsDatabase(conn, execute_plugins=False))
|
||||||
|
|
||||||
|
await db.execute_write_fn(apply_migrations, transaction=False)
|
||||||
|
|
||||||
|
|
||||||
|
async def populate_schema_tables(internal_db, db, schema_version):
|
||||||
database_name = db.name
|
database_name = db.name
|
||||||
|
|
||||||
def delete_everything(conn):
|
|
||||||
conn.execute(
|
|
||||||
"DELETE FROM catalog_tables WHERE database_name = ?", [database_name]
|
|
||||||
)
|
|
||||||
conn.execute(
|
|
||||||
"DELETE FROM catalog_views WHERE database_name = ?", [database_name]
|
|
||||||
)
|
|
||||||
conn.execute(
|
|
||||||
"DELETE FROM catalog_columns WHERE database_name = ?", [database_name]
|
|
||||||
)
|
|
||||||
conn.execute(
|
|
||||||
"DELETE FROM catalog_foreign_keys WHERE database_name = ?",
|
|
||||||
[database_name],
|
|
||||||
)
|
|
||||||
conn.execute(
|
|
||||||
"DELETE FROM catalog_indexes WHERE database_name = ?", [database_name]
|
|
||||||
)
|
|
||||||
|
|
||||||
await internal_db.execute_write_fn(delete_everything)
|
|
||||||
|
|
||||||
tables = (await db.execute("select * from sqlite_master WHERE type = 'table'")).rows
|
tables = (await db.execute("select * from sqlite_master WHERE type = 'table'")).rows
|
||||||
views = (await db.execute("select * from sqlite_master WHERE type = 'view'")).rows
|
views = (await db.execute("select * from sqlite_master WHERE type = 'view'")).rows
|
||||||
|
|
||||||
|
|
@ -184,25 +207,30 @@ async def populate_schema_tables(internal_db, db):
|
||||||
columns = table_column_details(conn, table_name)
|
columns = table_column_details(conn, table_name)
|
||||||
columns_to_insert.extend(
|
columns_to_insert.extend(
|
||||||
{
|
{
|
||||||
**{"database_name": database_name, "table_name": table_name},
|
"database_name": database_name,
|
||||||
|
"table_name": table_name,
|
||||||
**column._asdict(),
|
**column._asdict(),
|
||||||
}
|
}
|
||||||
for column in columns
|
for column in columns
|
||||||
)
|
)
|
||||||
foreign_keys = conn.execute(
|
foreign_keys = conn.execute(
|
||||||
f"PRAGMA foreign_key_list([{table_name}])"
|
f"PRAGMA foreign_key_list({escape_sqlite(table_name)})"
|
||||||
).fetchall()
|
).fetchall()
|
||||||
foreign_keys_to_insert.extend(
|
foreign_keys_to_insert.extend(
|
||||||
{
|
{
|
||||||
**{"database_name": database_name, "table_name": table_name},
|
"database_name": database_name,
|
||||||
|
"table_name": table_name,
|
||||||
**dict(foreign_key),
|
**dict(foreign_key),
|
||||||
}
|
}
|
||||||
for foreign_key in foreign_keys
|
for foreign_key in foreign_keys
|
||||||
)
|
)
|
||||||
indexes = conn.execute(f"PRAGMA index_list([{table_name}])").fetchall()
|
indexes = conn.execute(
|
||||||
|
f"PRAGMA index_list({escape_sqlite(table_name)})"
|
||||||
|
).fetchall()
|
||||||
indexes_to_insert.extend(
|
indexes_to_insert.extend(
|
||||||
{
|
{
|
||||||
**{"database_name": database_name, "table_name": table_name},
|
"database_name": database_name,
|
||||||
|
"table_name": table_name,
|
||||||
**dict(index),
|
**dict(index),
|
||||||
}
|
}
|
||||||
for index in indexes
|
for index in indexes
|
||||||
|
|
@ -223,47 +251,76 @@ async def populate_schema_tables(internal_db, db):
|
||||||
indexes_to_insert,
|
indexes_to_insert,
|
||||||
) = await db.execute_fn(collect_info)
|
) = await db.execute_fn(collect_info)
|
||||||
|
|
||||||
await internal_db.execute_write_many(
|
def replace_catalog(conn):
|
||||||
"""
|
# Delete child rows before their catalog_tables parents so this also
|
||||||
INSERT INTO catalog_tables (database_name, table_name, rootpage, sql)
|
# works if a prepare_connection plugin enables foreign key enforcement.
|
||||||
values (?, ?, ?, ?)
|
for table in (
|
||||||
""",
|
"catalog_columns",
|
||||||
tables_to_insert,
|
"catalog_foreign_keys",
|
||||||
)
|
"catalog_indexes",
|
||||||
await internal_db.execute_write_many(
|
"catalog_views",
|
||||||
"""
|
"catalog_tables",
|
||||||
INSERT INTO catalog_views (database_name, view_name, rootpage, sql)
|
):
|
||||||
values (?, ?, ?, ?)
|
conn.execute(
|
||||||
""",
|
f"DELETE FROM {table} WHERE database_name = ?",
|
||||||
views_to_insert,
|
[database_name],
|
||||||
)
|
)
|
||||||
await internal_db.execute_write_many(
|
conn.execute(
|
||||||
"""
|
"""
|
||||||
INSERT INTO catalog_columns (
|
INSERT OR REPLACE INTO catalog_databases (
|
||||||
database_name, table_name, cid, name, type, "notnull", default_value, is_pk, hidden
|
database_name, path, is_memory, schema_version
|
||||||
) VALUES (
|
) VALUES (?, ?, ?, ?)
|
||||||
:database_name, :table_name, :cid, :name, :type, :notnull, :default_value, :is_pk, :hidden
|
""",
|
||||||
|
[
|
||||||
|
database_name,
|
||||||
|
str(db.path) if db.path is not None else None,
|
||||||
|
db.is_memory,
|
||||||
|
schema_version,
|
||||||
|
],
|
||||||
)
|
)
|
||||||
""",
|
conn.executemany(
|
||||||
columns_to_insert,
|
"""
|
||||||
)
|
INSERT INTO catalog_tables (database_name, table_name, rootpage, sql)
|
||||||
await internal_db.execute_write_many(
|
values (?, ?, ?, ?)
|
||||||
"""
|
""",
|
||||||
INSERT INTO catalog_foreign_keys (
|
tables_to_insert,
|
||||||
database_name, table_name, "id", seq, "table", "from", "to", on_update, on_delete, match
|
|
||||||
) VALUES (
|
|
||||||
:database_name, :table_name, :id, :seq, :table, :from, :to, :on_update, :on_delete, :match
|
|
||||||
)
|
)
|
||||||
""",
|
conn.executemany(
|
||||||
foreign_keys_to_insert,
|
"""
|
||||||
)
|
INSERT INTO catalog_views (database_name, view_name, rootpage, sql)
|
||||||
await internal_db.execute_write_many(
|
values (?, ?, ?, ?)
|
||||||
"""
|
""",
|
||||||
INSERT INTO catalog_indexes (
|
views_to_insert,
|
||||||
database_name, table_name, seq, name, "unique", origin, partial
|
|
||||||
) VALUES (
|
|
||||||
:database_name, :table_name, :seq, :name, :unique, :origin, :partial
|
|
||||||
)
|
)
|
||||||
""",
|
conn.executemany(
|
||||||
indexes_to_insert,
|
"""
|
||||||
)
|
INSERT INTO catalog_columns (
|
||||||
|
database_name, table_name, cid, name, type, "notnull", default_value, is_pk, hidden
|
||||||
|
) VALUES (
|
||||||
|
:database_name, :table_name, :cid, :name, :type, :notnull, :default_value, :is_pk, :hidden
|
||||||
|
)
|
||||||
|
""",
|
||||||
|
columns_to_insert,
|
||||||
|
)
|
||||||
|
conn.executemany(
|
||||||
|
"""
|
||||||
|
INSERT INTO catalog_foreign_keys (
|
||||||
|
database_name, table_name, "id", seq, "table", "from", "to", on_update, on_delete, match
|
||||||
|
) VALUES (
|
||||||
|
:database_name, :table_name, :id, :seq, :table, :from, :to, :on_update, :on_delete, :match
|
||||||
|
)
|
||||||
|
""",
|
||||||
|
foreign_keys_to_insert,
|
||||||
|
)
|
||||||
|
conn.executemany(
|
||||||
|
"""
|
||||||
|
INSERT INTO catalog_indexes (
|
||||||
|
database_name, table_name, seq, name, "unique", origin, partial
|
||||||
|
) VALUES (
|
||||||
|
:database_name, :table_name, :seq, :name, :unique, :origin, :partial
|
||||||
|
)
|
||||||
|
""",
|
||||||
|
indexes_to_insert,
|
||||||
|
)
|
||||||
|
|
||||||
|
await internal_db.execute_write_fn(replace_catalog)
|
||||||
|
|
|
||||||
|
|
@ -11,15 +11,10 @@ Supports:
|
||||||
import asyncio
|
import asyncio
|
||||||
import shutil
|
import shutil
|
||||||
import tempfile
|
import tempfile
|
||||||
|
from collections.abc import Callable
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
from typing import (
|
from typing import (
|
||||||
Any,
|
Any,
|
||||||
Callable,
|
|
||||||
Dict,
|
|
||||||
List,
|
|
||||||
Optional,
|
|
||||||
Tuple,
|
|
||||||
Union,
|
|
||||||
)
|
)
|
||||||
from urllib.parse import parse_qsl
|
from urllib.parse import parse_qsl
|
||||||
|
|
||||||
|
|
@ -29,7 +24,7 @@ DEFAULT_MAX_REQUEST_SIZE = 100 * 1024 * 1024 # 100MB
|
||||||
DEFAULT_MAX_FIELDS = 1000
|
DEFAULT_MAX_FIELDS = 1000
|
||||||
DEFAULT_MAX_FILES = 100
|
DEFAULT_MAX_FILES = 100
|
||||||
# If max_parts is not specified, it defaults to max_fields + max_files
|
# If max_parts is not specified, it defaults to max_fields + max_files
|
||||||
DEFAULT_MAX_PARTS: Optional[int] = None
|
DEFAULT_MAX_PARTS: int | None = None
|
||||||
DEFAULT_MAX_FIELD_SIZE = 100 * 1024 # 100KB
|
DEFAULT_MAX_FIELD_SIZE = 100 * 1024 # 100KB
|
||||||
DEFAULT_MAX_MEMORY_FILE_SIZE = 1024 * 1024 # 1MB
|
DEFAULT_MAX_MEMORY_FILE_SIZE = 1024 * 1024 # 1MB
|
||||||
DEFAULT_MAX_PART_HEADER_BYTES = 16 * 1024 # 16KB
|
DEFAULT_MAX_PART_HEADER_BYTES = 16 * 1024 # 16KB
|
||||||
|
|
@ -40,8 +35,6 @@ DEFAULT_MIN_FREE_DISK_BYTES = 50 * 1024 * 1024 # 50MB
|
||||||
class MultipartParseError(Exception):
|
class MultipartParseError(Exception):
|
||||||
"""Raised when multipart parsing fails."""
|
"""Raised when multipart parsing fails."""
|
||||||
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class UploadedFile:
|
class UploadedFile:
|
||||||
|
|
@ -57,7 +50,7 @@ class UploadedFile:
|
||||||
|
|
||||||
name: str
|
name: str
|
||||||
filename: str
|
filename: str
|
||||||
content_type: Optional[str]
|
content_type: str | None
|
||||||
size: int
|
size: int
|
||||||
_file: tempfile.SpooledTemporaryFile = field(repr=False)
|
_file: tempfile.SpooledTemporaryFile = field(repr=False)
|
||||||
|
|
||||||
|
|
@ -86,7 +79,8 @@ class UploadedFile:
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
try:
|
try:
|
||||||
self._file.close()
|
self._file.close()
|
||||||
except Exception:
|
except Exception: # noqa: BLE001, S110
|
||||||
|
# __del__ must never raise
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -98,27 +92,27 @@ class FormData:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._data: List[Tuple[str, Union[str, UploadedFile]]] = []
|
self._data: list[tuple[str, str | UploadedFile]] = []
|
||||||
|
|
||||||
def append(self, key: str, value: Union[str, UploadedFile]) -> None:
|
def append(self, key: str, value: str | UploadedFile) -> None:
|
||||||
"""Add a key-value pair."""
|
"""Add a key-value pair."""
|
||||||
self._data.append((key, value))
|
self._data.append((key, value))
|
||||||
|
|
||||||
def __getitem__(self, key: str) -> Union[str, UploadedFile]:
|
def __getitem__(self, key: str) -> str | UploadedFile:
|
||||||
"""Get the first value for a key."""
|
"""Get the first value for a key."""
|
||||||
for k, v in self._data:
|
for k, v in self._data:
|
||||||
if k == key:
|
if k == key:
|
||||||
return v
|
return v
|
||||||
raise KeyError(key)
|
raise KeyError(key)
|
||||||
|
|
||||||
def get(self, key: str, default: Any = None) -> Optional[Union[str, UploadedFile]]:
|
def get(self, key: str, default: Any = None) -> str | UploadedFile | None:
|
||||||
"""Get the first value for a key, or default if not found."""
|
"""Get the first value for a key, or default if not found."""
|
||||||
try:
|
try:
|
||||||
return self[key]
|
return self[key]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return default
|
return default
|
||||||
|
|
||||||
def getlist(self, key: str) -> List[Union[str, UploadedFile]]:
|
def getlist(self, key: str) -> list[str | UploadedFile]:
|
||||||
"""Get all values for a key."""
|
"""Get all values for a key."""
|
||||||
return [v for k, v in self._data if k == key]
|
return [v for k, v in self._data if k == key]
|
||||||
|
|
||||||
|
|
@ -142,15 +136,15 @@ class FormData:
|
||||||
"""Return unique keys."""
|
"""Return unique keys."""
|
||||||
return list(self)
|
return list(self)
|
||||||
|
|
||||||
def items(self) -> List[Tuple[str, Union[str, UploadedFile]]]:
|
def items(self) -> list[tuple[str, str | UploadedFile]]:
|
||||||
"""Return all key-value pairs."""
|
"""Return all key-value pairs."""
|
||||||
return list(self._data)
|
return list(self._data)
|
||||||
|
|
||||||
def values(self) -> List[Union[str, UploadedFile]]:
|
def values(self) -> list[str | UploadedFile]:
|
||||||
"""Return all values."""
|
"""Return all values."""
|
||||||
return [v for _, v in self._data]
|
return [v for _, v in self._data]
|
||||||
|
|
||||||
def _uploaded_files(self) -> List[UploadedFile]:
|
def _uploaded_files(self) -> list[UploadedFile]:
|
||||||
"""Return UploadedFile instances contained in this form."""
|
"""Return UploadedFile instances contained in this form."""
|
||||||
return [v for _, v in self._data if isinstance(v, UploadedFile)]
|
return [v for _, v in self._data if isinstance(v, UploadedFile)]
|
||||||
|
|
||||||
|
|
@ -163,7 +157,7 @@ class FormData:
|
||||||
for uploaded in self._uploaded_files():
|
for uploaded in self._uploaded_files():
|
||||||
try:
|
try:
|
||||||
uploaded.close_sync()
|
uploaded.close_sync()
|
||||||
except Exception:
|
except Exception: # noqa: BLE001, S110
|
||||||
# Best-effort cleanup; ignore close errors
|
# Best-effort cleanup; ignore close errors
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
@ -172,7 +166,7 @@ class FormData:
|
||||||
for uploaded in self._uploaded_files():
|
for uploaded in self._uploaded_files():
|
||||||
try:
|
try:
|
||||||
await uploaded.close()
|
await uploaded.close()
|
||||||
except Exception:
|
except Exception: # noqa: BLE001, S110
|
||||||
# Best-effort cleanup; ignore close errors
|
# Best-effort cleanup; ignore close errors
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
@ -189,13 +183,13 @@ class FormData:
|
||||||
await self.aclose()
|
await self.aclose()
|
||||||
|
|
||||||
|
|
||||||
def parse_content_disposition(header: str) -> Dict[str, Optional[str]]:
|
def parse_content_disposition(header: str) -> dict[str, str | None]:
|
||||||
"""
|
"""
|
||||||
Parse Content-Disposition header value.
|
Parse Content-Disposition header value.
|
||||||
|
|
||||||
Returns dict with 'name', 'filename' keys (filename may be None).
|
Returns dict with 'name', 'filename' keys (filename may be None).
|
||||||
"""
|
"""
|
||||||
result: Dict[str, Optional[str]] = {"name": None, "filename": None}
|
result: dict[str, str | None] = {"name": None, "filename": None}
|
||||||
|
|
||||||
# Split on semicolons, handling quoted strings
|
# Split on semicolons, handling quoted strings
|
||||||
parts = []
|
parts = []
|
||||||
|
|
@ -238,7 +232,8 @@ def parse_content_disposition(header: str) -> Dict[str, Optional[str]]:
|
||||||
from urllib.parse import unquote
|
from urllib.parse import unquote
|
||||||
|
|
||||||
result["filename"] = unquote(encoded, encoding="utf-8")
|
result["filename"] = unquote(encoded, encoding="utf-8")
|
||||||
except Exception:
|
except Exception: # noqa: BLE001, S110
|
||||||
|
# Malformed RFC 5987 filename* - fall back to the plain filename
|
||||||
pass
|
pass
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
|
@ -250,20 +245,19 @@ def parse_content_disposition(header: str) -> Dict[str, Optional[str]]:
|
||||||
|
|
||||||
if key == "name":
|
if key == "name":
|
||||||
result["name"] = value
|
result["name"] = value
|
||||||
elif key == "filename":
|
# Only set filename if filename* hasn't already set it
|
||||||
# Only set if filename* hasn't already set it
|
elif key == "filename" and result["filename"] is None:
|
||||||
if result["filename"] is None:
|
# Strip path components (security)
|
||||||
# Strip path components (security)
|
# Handle both Unix and Windows paths
|
||||||
# Handle both Unix and Windows paths
|
value = value.replace("\\", "/")
|
||||||
value = value.replace("\\", "/")
|
if "/" in value:
|
||||||
if "/" in value:
|
value = value.rsplit("/", 1)[-1]
|
||||||
value = value.rsplit("/", 1)[-1]
|
result["filename"] = value
|
||||||
result["filename"] = value
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def parse_content_type(header: str) -> Tuple[str, Dict[str, str]]:
|
def parse_content_type(header: str) -> tuple[str, dict[str, str]]:
|
||||||
"""
|
"""
|
||||||
Parse Content-Type header value.
|
Parse Content-Type header value.
|
||||||
|
|
||||||
|
|
@ -307,7 +301,7 @@ class MultipartParser:
|
||||||
max_request_size: int = DEFAULT_MAX_REQUEST_SIZE,
|
max_request_size: int = DEFAULT_MAX_REQUEST_SIZE,
|
||||||
max_fields: int = DEFAULT_MAX_FIELDS,
|
max_fields: int = DEFAULT_MAX_FIELDS,
|
||||||
max_files: int = DEFAULT_MAX_FILES,
|
max_files: int = DEFAULT_MAX_FILES,
|
||||||
max_parts: Optional[int] = DEFAULT_MAX_PARTS,
|
max_parts: int | None = DEFAULT_MAX_PARTS,
|
||||||
max_field_size: int = DEFAULT_MAX_FIELD_SIZE,
|
max_field_size: int = DEFAULT_MAX_FIELD_SIZE,
|
||||||
max_memory_file_size: int = DEFAULT_MAX_MEMORY_FILE_SIZE,
|
max_memory_file_size: int = DEFAULT_MAX_MEMORY_FILE_SIZE,
|
||||||
max_part_header_bytes: int = DEFAULT_MAX_PART_HEADER_BYTES,
|
max_part_header_bytes: int = DEFAULT_MAX_PART_HEADER_BYTES,
|
||||||
|
|
@ -348,12 +342,12 @@ class MultipartParser:
|
||||||
self._tempdir = tempfile.gettempdir()
|
self._tempdir = tempfile.gettempdir()
|
||||||
|
|
||||||
# Current part state
|
# Current part state
|
||||||
self.current_headers: Dict[str, str] = {}
|
self.current_headers: dict[str, str] = {}
|
||||||
self.current_file: Optional[tempfile.SpooledTemporaryFile] = None
|
self.current_file: tempfile.SpooledTemporaryFile | None = None
|
||||||
self.current_body = bytearray()
|
self.current_body = bytearray()
|
||||||
self.current_name: Optional[str] = None
|
self.current_name: str | None = None
|
||||||
self.current_filename: Optional[str] = None
|
self.current_filename: str | None = None
|
||||||
self.current_content_type: Optional[str] = None
|
self.current_content_type: str | None = None
|
||||||
|
|
||||||
def feed(self, chunk: bytes) -> None:
|
def feed(self, chunk: bytes) -> None:
|
||||||
"""Feed a chunk of data to the parser."""
|
"""Feed a chunk of data to the parser."""
|
||||||
|
|
@ -364,6 +358,13 @@ class MultipartParser:
|
||||||
self.buffer.extend(chunk)
|
self.buffer.extend(chunk)
|
||||||
self._process()
|
self._process()
|
||||||
|
|
||||||
|
def close(self) -> None:
|
||||||
|
"""Discard completed uploads and any file still being received."""
|
||||||
|
if self.current_file is not None:
|
||||||
|
self.current_file.close()
|
||||||
|
self.current_file = None
|
||||||
|
self.form_data.close()
|
||||||
|
|
||||||
def _process(self) -> None:
|
def _process(self) -> None:
|
||||||
"""Process buffered data."""
|
"""Process buffered data."""
|
||||||
while True:
|
while True:
|
||||||
|
|
@ -454,7 +455,7 @@ class MultipartParser:
|
||||||
# Parse header
|
# Parse header
|
||||||
try:
|
try:
|
||||||
line_str = line.decode("utf-8", errors="replace")
|
line_str = line.decode("utf-8", errors="replace")
|
||||||
except Exception:
|
except UnicodeDecodeError:
|
||||||
line_str = line.decode("latin-1")
|
line_str = line.decode("latin-1")
|
||||||
|
|
||||||
if ":" in line_str:
|
if ":" in line_str:
|
||||||
|
|
@ -481,7 +482,9 @@ class MultipartParser:
|
||||||
if self.file_count > self.max_files:
|
if self.file_count > self.max_files:
|
||||||
raise MultipartParseError("Too many files")
|
raise MultipartParseError("Too many files")
|
||||||
if self.handle_files:
|
if self.handle_files:
|
||||||
self.current_file = tempfile.SpooledTemporaryFile(
|
# Outlives this method - it is filled in across parser callbacks
|
||||||
|
# and then handed to the UploadedFile the caller consumes
|
||||||
|
self.current_file = tempfile.SpooledTemporaryFile( # noqa: SIM115
|
||||||
max_size=self.max_memory_file_size
|
max_size=self.max_memory_file_size
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
|
|
@ -581,6 +584,9 @@ class MultipartParser:
|
||||||
def _finish_part(self) -> None:
|
def _finish_part(self) -> None:
|
||||||
"""Finalize current part and add to form data."""
|
"""Finalize current part and add to form data."""
|
||||||
if self.current_name is None:
|
if self.current_name is None:
|
||||||
|
if self.current_file is not None:
|
||||||
|
self.current_file.close()
|
||||||
|
self.current_file = None
|
||||||
return
|
return
|
||||||
|
|
||||||
if self.current_filename is not None:
|
if self.current_filename is not None:
|
||||||
|
|
@ -644,7 +650,7 @@ async def parse_form_data(
|
||||||
max_request_size: int = DEFAULT_MAX_REQUEST_SIZE,
|
max_request_size: int = DEFAULT_MAX_REQUEST_SIZE,
|
||||||
max_fields: int = DEFAULT_MAX_FIELDS,
|
max_fields: int = DEFAULT_MAX_FIELDS,
|
||||||
max_files: int = DEFAULT_MAX_FILES,
|
max_files: int = DEFAULT_MAX_FILES,
|
||||||
max_parts: Optional[int] = DEFAULT_MAX_PARTS,
|
max_parts: int | None = DEFAULT_MAX_PARTS,
|
||||||
max_field_size: int = DEFAULT_MAX_FIELD_SIZE,
|
max_field_size: int = DEFAULT_MAX_FIELD_SIZE,
|
||||||
max_memory_file_size: int = DEFAULT_MAX_MEMORY_FILE_SIZE,
|
max_memory_file_size: int = DEFAULT_MAX_MEMORY_FILE_SIZE,
|
||||||
max_part_header_bytes: int = DEFAULT_MAX_PART_HEADER_BYTES,
|
max_part_header_bytes: int = DEFAULT_MAX_PART_HEADER_BYTES,
|
||||||
|
|
@ -726,29 +732,50 @@ async def parse_form_data(
|
||||||
batch_target = 64 * 1024
|
batch_target = 64 * 1024
|
||||||
batch = bytearray()
|
batch = bytearray()
|
||||||
|
|
||||||
|
async def run_parser(fn, *args):
|
||||||
|
# Cancellation must not close files while a worker is using them.
|
||||||
|
task = asyncio.create_task(asyncio.to_thread(fn, *args))
|
||||||
|
try:
|
||||||
|
return await asyncio.shield(task)
|
||||||
|
except asyncio.CancelledError as cancelled:
|
||||||
|
try:
|
||||||
|
while not task.done():
|
||||||
|
try:
|
||||||
|
await asyncio.shield(task)
|
||||||
|
except asyncio.CancelledError:
|
||||||
|
continue
|
||||||
|
task.result()
|
||||||
|
finally:
|
||||||
|
raise cancelled
|
||||||
|
|
||||||
async def flush_batch() -> None:
|
async def flush_batch() -> None:
|
||||||
if batch:
|
if batch:
|
||||||
data = bytes(batch)
|
data = bytes(batch)
|
||||||
batch.clear()
|
batch.clear()
|
||||||
await asyncio.to_thread(parser.feed, data)
|
await run_parser(parser.feed, data)
|
||||||
|
|
||||||
while True:
|
try:
|
||||||
message = await receive()
|
while True:
|
||||||
message_type = message.get("type")
|
message = await receive()
|
||||||
if message_type == "http.disconnect":
|
message_type = message.get("type")
|
||||||
raise MultipartParseError("Client disconnected during request body")
|
if message_type == "http.disconnect":
|
||||||
if message_type is not None and message_type != "http.request":
|
raise MultipartParseError("Client disconnected during request body")
|
||||||
continue
|
if message_type is not None and message_type != "http.request":
|
||||||
chunk = message.get("body", b"")
|
continue
|
||||||
if chunk:
|
chunk = message.get("body", b"")
|
||||||
batch.extend(chunk)
|
if chunk:
|
||||||
if len(batch) >= batch_target:
|
batch.extend(chunk)
|
||||||
await flush_batch()
|
if len(batch) >= batch_target:
|
||||||
if not message.get("more_body", False):
|
await flush_batch()
|
||||||
break
|
if not message.get("more_body", False):
|
||||||
|
break
|
||||||
|
|
||||||
await flush_batch()
|
await flush_batch()
|
||||||
return await asyncio.to_thread(parser.finalize)
|
return await run_parser(parser.finalize)
|
||||||
|
except BaseException:
|
||||||
|
# No FormData is returned to the caller to take ownership on failure.
|
||||||
|
await asyncio.to_thread(parser.close)
|
||||||
|
raise
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise MultipartParseError(
|
raise MultipartParseError(
|
||||||
|
|
|
||||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue