2020-09-02 22:21:12 +00:00
|
|
|
import asyncio
|
2017-11-10 19:25:54 +00:00
|
|
|
from contextlib import contextmanager
|
2020-10-31 20:35:47 +00:00
|
|
|
import click
|
2020-11-29 19:30:17 +00:00
|
|
|
from collections import OrderedDict, namedtuple, Counter
|
2017-11-10 19:25:54 +00:00
|
|
|
import base64
|
2017-11-30 07:09:54 +00:00
|
|
|
import hashlib
|
2020-05-27 19:25:52 +00:00
|
|
|
import inspect
|
2017-11-10 19:25:54 +00:00
|
|
|
import json
|
2021-06-01 15:49:50 +00:00
|
|
|
import markupsafe
|
2020-04-10 18:34:09 +00:00
|
|
|
import mergedeep
|
2017-11-13 16:13:38 +00:00
|
|
|
import os
|
2017-11-11 05:55:50 +00:00
|
|
|
import re
|
2017-11-21 18:51:58 +00:00
|
|
|
import shlex
|
2017-11-13 16:13:38 +00:00
|
|
|
import tempfile
|
2022-02-07 06:30:00 +00:00
|
|
|
import typing
|
2017-11-10 19:25:54 +00:00
|
|
|
import time
|
2019-07-03 04:32:55 +00:00
|
|
|
import types
|
2022-03-15 18:01:57 +00:00
|
|
|
import secrets
|
2017-12-08 16:06:24 +00:00
|
|
|
import shutil
|
2017-11-10 19:25:54 +00:00
|
|
|
import urllib
|
2020-04-02 19:30:53 +00:00
|
|
|
import yaml
|
2020-05-27 18:17:43 +00:00
|
|
|
from .shutil_backport import copytree
|
2021-07-16 06:26:06 +00:00
|
|
|
from .sqlite import sqlite3, supports_table_xinfo
|
2017-11-10 19:25:54 +00:00
|
|
|
|
2020-09-15 21:59:17 +00:00
|
|
|
|
2018-04-03 13:39:50 +00:00
|
|
|
# From https://www.sqlite.org/lang_keywords.html
|
2019-05-04 02:15:14 +00:00
|
|
|
reserved_words = set(
|
|
|
|
(
|
|
|
|
"abort action add after all alter analyze and as asc attach autoincrement "
|
|
|
|
"before 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"
|
|
|
|
).split()
|
|
|
|
)
|
|
|
|
|
2020-11-25 03:05:35 +00:00
|
|
|
APT_GET_DOCKERFILE_EXTRAS = r"""
|
2018-05-31 14:16:50 +00:00
|
|
|
RUN apt-get update && \
|
2020-11-25 03:05:35 +00:00
|
|
|
apt-get install -y {} && \
|
2018-05-31 14:16:50 +00:00
|
|
|
rm -rf /var/lib/apt/lists/*
|
2019-05-04 02:15:14 +00:00
|
|
|
"""
|
2018-05-31 14:16:50 +00:00
|
|
|
|
2020-10-19 22:37:31 +00:00
|
|
|
# Can replace with sqlite-utils when I add that dependency
|
|
|
|
SPATIALITE_PATHS = (
|
|
|
|
"/usr/lib/x86_64-linux-gnu/mod_spatialite.so",
|
|
|
|
"/usr/local/lib/mod_spatialite.dylib",
|
2020-11-29 17:37:43 +00:00
|
|
|
"/usr/local/lib/mod_spatialite.so",
|
2022-03-06 19:39:15 +00:00
|
|
|
"/opt/homebrew/lib/mod_spatialite.dylib",
|
2020-10-19 22:37:31 +00:00
|
|
|
)
|
2022-02-09 06:32:19 +00:00
|
|
|
# Used to display /-/versions.json SpatiaLite information
|
|
|
|
SPATIALITE_FUNCTIONS = (
|
|
|
|
"spatialite_version",
|
|
|
|
"spatialite_target_cpu",
|
|
|
|
"check_strict_sql_quoting",
|
|
|
|
"freexl_version",
|
|
|
|
"proj_version",
|
|
|
|
"geos_version",
|
|
|
|
"rttopo_version",
|
|
|
|
"libxml2_version",
|
|
|
|
"HasIconv",
|
|
|
|
"HasMathSQL",
|
|
|
|
"HasGeoCallbacks",
|
|
|
|
"HasProj",
|
|
|
|
"HasProj6",
|
|
|
|
"HasGeos",
|
|
|
|
"HasGeosAdvanced",
|
|
|
|
"HasGeosTrunk",
|
|
|
|
"HasGeosReentrant",
|
|
|
|
"HasGeosOnlyReentrant",
|
|
|
|
"HasMiniZip",
|
|
|
|
"HasRtTopo",
|
|
|
|
"HasLibXML2",
|
|
|
|
"HasEpsg",
|
|
|
|
"HasFreeXL",
|
|
|
|
"HasGeoPackage",
|
|
|
|
"HasGCP",
|
|
|
|
"HasTopology",
|
|
|
|
"HasKNN",
|
|
|
|
"HasRouting",
|
|
|
|
)
|
2020-10-20 00:33:59 +00:00
|
|
|
# Length of hash subset used in hashed URLs:
|
|
|
|
HASH_LENGTH = 7
|
2020-10-19 22:37:31 +00:00
|
|
|
|
2022-02-09 06:32:19 +00:00
|
|
|
|
2020-10-06 00:32:10 +00:00
|
|
|
# Can replace this with Column from sqlite_utils when I add that dependency
|
|
|
|
Column = namedtuple(
|
2020-11-30 21:29:57 +00:00
|
|
|
"Column", ("cid", "name", "type", "notnull", "default_value", "is_pk", "hidden")
|
2020-10-06 00:32:10 +00:00
|
|
|
)
|
|
|
|
|
2022-02-07 06:30:00 +00:00
|
|
|
functions_marked_as_documented = []
|
2018-04-03 13:39:50 +00:00
|
|
|
|
2022-02-07 06:30:00 +00:00
|
|
|
|
|
|
|
def documented(fn):
|
|
|
|
functions_marked_as_documented.append(fn)
|
|
|
|
return fn
|
|
|
|
|
|
|
|
|
|
|
|
@documented
|
|
|
|
async def await_me_maybe(value: typing.Any) -> typing.Any:
|
|
|
|
"If value is callable, call it. If awaitable, await it. Otherwise return it."
|
2020-09-02 22:21:12 +00:00
|
|
|
if callable(value):
|
|
|
|
value = value()
|
|
|
|
if asyncio.iscoroutine(value):
|
|
|
|
value = await value
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
2018-04-09 00:06:10 +00:00
|
|
|
def urlsafe_components(token):
|
2022-03-15 18:01:57 +00:00
|
|
|
"""Splits token on commas and tilde-decodes each component"""
|
|
|
|
return [tilde_decode(b) for b in token.split(",")]
|
2017-11-10 19:25:54 +00:00
|
|
|
|
|
|
|
|
2018-04-15 21:48:30 +00:00
|
|
|
def path_from_row_pks(row, pks, use_rowid, quote=True):
|
2022-03-15 18:01:57 +00:00
|
|
|
"""Generate an optionally tilde-encoded unique identifier
|
2020-09-02 22:24:55 +00:00
|
|
|
for a row from its primary keys."""
|
2017-11-10 19:25:54 +00:00
|
|
|
if use_rowid:
|
2019-05-04 02:15:14 +00:00
|
|
|
bits = [row["rowid"]]
|
2018-04-15 21:48:30 +00:00
|
|
|
else:
|
2018-06-21 14:56:28 +00:00
|
|
|
bits = [
|
2019-05-04 02:15:14 +00:00
|
|
|
row[pk]["value"] if isinstance(row[pk], dict) else row[pk] for pk in pks
|
2018-06-21 14:56:28 +00:00
|
|
|
]
|
2018-04-15 21:48:30 +00:00
|
|
|
if quote:
|
2022-03-15 18:01:57 +00:00
|
|
|
bits = [tilde_encode(str(bit)) for bit in bits]
|
2018-04-15 21:48:30 +00:00
|
|
|
else:
|
|
|
|
bits = [str(bit) for bit in bits]
|
|
|
|
|
2019-05-04 02:15:14 +00:00
|
|
|
return ",".join(bits)
|
2017-11-10 19:25:54 +00:00
|
|
|
|
|
|
|
|
2018-03-30 06:26:22 +00:00
|
|
|
def compound_keys_after_sql(pks, start_index=0):
|
2018-03-30 05:10:09 +00:00
|
|
|
# Implementation of keyset pagination
|
|
|
|
# See https://github.com/simonw/datasette/issues/190
|
|
|
|
# For pk1/pk2/pk3 returns:
|
|
|
|
#
|
|
|
|
# ([pk1] > :p0)
|
|
|
|
# or
|
|
|
|
# ([pk1] = :p0 and [pk2] > :p1)
|
|
|
|
# or
|
|
|
|
# ([pk1] = :p0 and [pk2] = :p1 and [pk3] > :p2)
|
|
|
|
or_clauses = []
|
|
|
|
pks_left = pks[:]
|
|
|
|
while pks_left:
|
|
|
|
and_clauses = []
|
|
|
|
last = pks_left[-1]
|
|
|
|
rest = pks_left[:-1]
|
2019-05-04 02:15:14 +00:00
|
|
|
and_clauses = [
|
2020-11-15 23:24:22 +00:00
|
|
|
f"{escape_sqlite(pk)} = :p{i + start_index}" for i, pk in enumerate(rest)
|
2019-05-04 02:15:14 +00:00
|
|
|
]
|
2020-11-15 23:24:22 +00:00
|
|
|
and_clauses.append(f"{escape_sqlite(last)} > :p{len(rest) + start_index}")
|
|
|
|
or_clauses.append(f"({' and '.join(and_clauses)})")
|
2018-03-30 05:10:09 +00:00
|
|
|
pks_left.pop()
|
|
|
|
or_clauses.reverse()
|
2019-05-04 02:15:14 +00:00
|
|
|
return "({})".format("\n or\n".join(or_clauses))
|
2018-03-30 05:10:09 +00:00
|
|
|
|
|
|
|
|
2017-11-10 19:25:54 +00:00
|
|
|
class CustomJSONEncoder(json.JSONEncoder):
|
|
|
|
def default(self, obj):
|
|
|
|
if isinstance(obj, sqlite3.Row):
|
|
|
|
return tuple(obj)
|
|
|
|
if isinstance(obj, sqlite3.Cursor):
|
|
|
|
return list(obj)
|
|
|
|
if isinstance(obj, bytes):
|
|
|
|
# Does it encode to utf8?
|
|
|
|
try:
|
2019-05-04 02:15:14 +00:00
|
|
|
return obj.decode("utf8")
|
2017-11-10 19:25:54 +00:00
|
|
|
except UnicodeDecodeError:
|
|
|
|
return {
|
2019-05-04 02:15:14 +00:00
|
|
|
"$base64": True,
|
|
|
|
"encoded": base64.b64encode(obj).decode("latin1"),
|
2017-11-10 19:25:54 +00:00
|
|
|
}
|
|
|
|
return json.JSONEncoder.default(self, obj)
|
|
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def sqlite_timelimit(conn, ms):
|
2020-12-21 21:49:14 +00:00
|
|
|
deadline = time.perf_counter() + (ms / 1000)
|
2017-11-15 02:41:03 +00:00
|
|
|
# n is the number of SQLite virtual machine instructions that will be
|
2022-03-21 21:55:50 +00:00
|
|
|
# executed between each check. It takes about 0.08ms to execute 1000.
|
|
|
|
# https://github.com/simonw/datasette/issues/1679
|
2017-11-15 02:41:03 +00:00
|
|
|
n = 1000
|
2022-03-21 21:55:50 +00:00
|
|
|
if ms <= 20:
|
|
|
|
# This mainly happens while executing our test suite
|
2017-11-15 02:41:03 +00:00
|
|
|
n = 1
|
2017-11-10 19:25:54 +00:00
|
|
|
|
|
|
|
def handler():
|
2020-12-21 21:49:14 +00:00
|
|
|
if time.perf_counter() >= deadline:
|
2022-03-21 21:55:50 +00:00
|
|
|
# Returning 1 terminates the query with an error
|
2017-11-10 19:25:54 +00:00
|
|
|
return 1
|
2017-11-15 02:41:03 +00:00
|
|
|
|
|
|
|
conn.set_progress_handler(handler, n)
|
2019-04-21 19:02:24 +00:00
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
conn.set_progress_handler(None, n)
|
2017-11-10 19:25:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
class InvalidSql(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-12-04 04:51:31 +00:00
|
|
|
allowed_sql_res = [
|
2019-05-04 02:15:14 +00:00
|
|
|
re.compile(r"^select\b"),
|
2022-01-13 20:34:55 +00:00
|
|
|
re.compile(r"^explain\s+select\b"),
|
|
|
|
re.compile(r"^explain\s+query\s+plan\s+select\b"),
|
2019-05-04 02:15:14 +00:00
|
|
|
re.compile(r"^with\b"),
|
2022-01-13 20:34:55 +00:00
|
|
|
re.compile(r"^explain\s+with\b"),
|
|
|
|
re.compile(r"^explain\s+query\s+plan\s+with\b"),
|
2017-12-04 04:51:31 +00:00
|
|
|
]
|
2020-05-06 17:18:31 +00:00
|
|
|
allowed_pragmas = (
|
|
|
|
"database_list",
|
|
|
|
"foreign_key_list",
|
|
|
|
"function_list",
|
|
|
|
"index_info",
|
|
|
|
"index_list",
|
|
|
|
"index_xinfo",
|
|
|
|
"page_count",
|
|
|
|
"max_page_count",
|
|
|
|
"page_size",
|
|
|
|
"schema_version",
|
|
|
|
"table_info",
|
|
|
|
"table_xinfo",
|
|
|
|
)
|
|
|
|
disallawed_sql_res = [
|
|
|
|
(
|
2020-11-15 23:24:22 +00:00
|
|
|
re.compile(f"pragma(?!_({'|'.join(allowed_pragmas)}))"),
|
2021-01-12 22:26:19 +00:00
|
|
|
"Statement contained a disallowed PRAGMA. Allowed pragma functions are {}".format(
|
|
|
|
", ".join("pragma_{}()".format(pragma) for pragma in allowed_pragmas)
|
|
|
|
),
|
2020-05-06 17:18:31 +00:00
|
|
|
)
|
|
|
|
]
|
2017-12-04 04:51:31 +00:00
|
|
|
|
|
|
|
|
2017-11-10 19:25:54 +00:00
|
|
|
def validate_sql_select(sql):
|
2020-02-13 06:05:46 +00:00
|
|
|
sql = "\n".join(
|
|
|
|
line for line in sql.split("\n") if not line.strip().startswith("--")
|
|
|
|
)
|
2017-11-10 19:25:54 +00:00
|
|
|
sql = sql.strip().lower()
|
2017-12-04 04:51:31 +00:00
|
|
|
if not any(r.match(sql) for r in allowed_sql_res):
|
2019-05-04 02:15:14 +00:00
|
|
|
raise InvalidSql("Statement must be a SELECT")
|
2017-12-04 04:51:31 +00:00
|
|
|
for r, msg in disallawed_sql_res:
|
|
|
|
if r.search(sql):
|
|
|
|
raise InvalidSql(msg)
|
2017-11-10 20:41:14 +00:00
|
|
|
|
|
|
|
|
2018-06-18 06:03:22 +00:00
|
|
|
def append_querystring(url, querystring):
|
|
|
|
op = "&" if ("?" in url) else "?"
|
2020-11-15 23:24:22 +00:00
|
|
|
return f"{url}{op}{querystring}"
|
2018-06-18 06:03:22 +00:00
|
|
|
|
|
|
|
|
2018-04-03 14:52:54 +00:00
|
|
|
def path_with_added_args(request, args, path=None):
|
|
|
|
path = path or request.path
|
2017-11-23 04:03:46 +00:00
|
|
|
if isinstance(args, dict):
|
|
|
|
args = args.items()
|
2018-05-14 22:09:09 +00:00
|
|
|
args_to_remove = {k for k, v in args if v is None}
|
2018-05-12 21:35:25 +00:00
|
|
|
current = []
|
2018-05-14 03:02:07 +00:00
|
|
|
for key, value in urllib.parse.parse_qsl(request.query_string):
|
2018-05-14 22:09:09 +00:00
|
|
|
if key not in args_to_remove:
|
2018-05-14 03:02:07 +00:00
|
|
|
current.append((key, value))
|
2019-05-04 02:15:14 +00:00
|
|
|
current.extend([(key, value) for key, value in args if value is not None])
|
2018-05-12 21:35:25 +00:00
|
|
|
query_string = urllib.parse.urlencode(current)
|
2017-11-23 20:32:54 +00:00
|
|
|
if query_string:
|
2020-11-15 23:24:22 +00:00
|
|
|
query_string = f"?{query_string}"
|
2018-04-03 14:52:54 +00:00
|
|
|
return path + query_string
|
2017-11-11 05:55:50 +00:00
|
|
|
|
|
|
|
|
2018-05-14 20:42:10 +00:00
|
|
|
def path_with_removed_args(request, args, path=None):
|
2019-03-17 22:55:04 +00:00
|
|
|
query_string = request.query_string
|
|
|
|
if path is None:
|
|
|
|
path = request.path
|
|
|
|
else:
|
|
|
|
if "?" in path:
|
|
|
|
bits = path.split("?", 1)
|
|
|
|
path, query_string = bits
|
2018-05-15 10:11:52 +00:00
|
|
|
# args can be a dict or a set
|
2018-05-14 20:42:10 +00:00
|
|
|
current = []
|
2018-05-15 10:11:52 +00:00
|
|
|
if isinstance(args, set):
|
2019-05-04 02:15:14 +00:00
|
|
|
|
2018-05-15 10:11:52 +00:00
|
|
|
def should_remove(key, value):
|
|
|
|
return key in args
|
2019-05-04 02:15:14 +00:00
|
|
|
|
2018-05-15 10:11:52 +00:00
|
|
|
elif isinstance(args, dict):
|
|
|
|
# Must match key AND value
|
|
|
|
def should_remove(key, value):
|
|
|
|
return args.get(key) == value
|
2019-05-04 02:15:14 +00:00
|
|
|
|
2019-03-17 22:55:04 +00:00
|
|
|
for key, value in urllib.parse.parse_qsl(query_string):
|
2018-05-15 10:11:52 +00:00
|
|
|
if not should_remove(key, value):
|
2018-05-14 20:42:10 +00:00
|
|
|
current.append((key, value))
|
|
|
|
query_string = urllib.parse.urlencode(current)
|
|
|
|
if query_string:
|
2020-11-15 23:24:22 +00:00
|
|
|
query_string = f"?{query_string}"
|
2018-05-14 20:42:10 +00:00
|
|
|
return path + query_string
|
|
|
|
|
|
|
|
|
2018-05-15 09:34:45 +00:00
|
|
|
def path_with_replaced_args(request, args, path=None):
|
|
|
|
path = path or request.path
|
|
|
|
if isinstance(args, dict):
|
|
|
|
args = args.items()
|
|
|
|
keys_to_replace = {p[0] for p in args}
|
|
|
|
current = []
|
|
|
|
for key, value in urllib.parse.parse_qsl(request.query_string):
|
|
|
|
if key not in keys_to_replace:
|
|
|
|
current.append((key, value))
|
|
|
|
current.extend([p for p in args if p[1] is not None])
|
|
|
|
query_string = urllib.parse.urlencode(current)
|
|
|
|
if query_string:
|
2020-11-15 23:24:22 +00:00
|
|
|
query_string = f"?{query_string}"
|
2018-05-15 09:34:45 +00:00
|
|
|
return path + query_string
|
|
|
|
|
|
|
|
|
2019-05-04 02:15:14 +00:00
|
|
|
_css_re = re.compile(r"""['"\n\\]""")
|
|
|
|
_boring_keyword_re = re.compile(r"^[a-zA-Z_][a-zA-Z0-9_]*$")
|
2017-11-11 05:55:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
def escape_css_string(s):
|
2020-09-29 19:16:30 +00:00
|
|
|
return _css_re.sub(
|
2020-11-15 23:24:22 +00:00
|
|
|
lambda m: "\\" + (f"{ord(m.group()):X}".zfill(6)),
|
2020-09-29 19:16:30 +00:00
|
|
|
s.replace("\r\n", "\n"),
|
|
|
|
)
|
2017-11-11 07:25:22 +00:00
|
|
|
|
|
|
|
|
2018-04-03 13:39:50 +00:00
|
|
|
def escape_sqlite(s):
|
|
|
|
if _boring_keyword_re.match(s) and (s.lower() not in reserved_words):
|
2017-11-12 23:17:00 +00:00
|
|
|
return s
|
|
|
|
else:
|
2020-11-15 23:24:22 +00:00
|
|
|
return f"[{s}]"
|
2019-05-04 02:15:14 +00:00
|
|
|
|
2017-11-12 23:17:00 +00:00
|
|
|
|
2019-05-04 02:15:14 +00:00
|
|
|
def make_dockerfile(
|
|
|
|
files,
|
|
|
|
metadata_file,
|
|
|
|
extra_options,
|
|
|
|
branch,
|
|
|
|
template_dir,
|
|
|
|
plugins_dir,
|
|
|
|
static,
|
|
|
|
install,
|
|
|
|
spatialite,
|
|
|
|
version_note,
|
2020-06-11 16:02:03 +00:00
|
|
|
secret,
|
2019-07-08 02:06:31 +00:00
|
|
|
environment_variables=None,
|
2020-01-29 22:46:43 +00:00
|
|
|
port=8001,
|
2020-11-25 03:05:35 +00:00
|
|
|
apt_get_extras=None,
|
2019-05-04 02:15:14 +00:00
|
|
|
):
|
|
|
|
cmd = ["datasette", "serve", "--host", "0.0.0.0"]
|
2020-06-11 16:02:03 +00:00
|
|
|
environment_variables = environment_variables or {}
|
|
|
|
environment_variables["DATASETTE_SECRET"] = secret
|
2020-11-25 03:05:35 +00:00
|
|
|
apt_get_extras = apt_get_extras or []
|
2019-05-19 22:53:34 +00:00
|
|
|
for filename in files:
|
|
|
|
cmd.extend(["-i", filename])
|
2019-05-04 02:15:14 +00:00
|
|
|
cmd.extend(["--cors", "--inspect-file", "inspect-data.json"])
|
2017-11-13 21:58:34 +00:00
|
|
|
if metadata_file:
|
2020-11-15 23:24:22 +00:00
|
|
|
cmd.extend(["--metadata", f"{metadata_file}"])
|
2017-12-09 18:19:39 +00:00
|
|
|
if template_dir:
|
2019-05-04 02:15:14 +00:00
|
|
|
cmd.extend(["--template-dir", "templates/"])
|
2018-04-16 05:22:01 +00:00
|
|
|
if plugins_dir:
|
2019-05-04 02:15:14 +00:00
|
|
|
cmd.extend(["--plugins-dir", "plugins/"])
|
2018-06-17 20:14:55 +00:00
|
|
|
if version_note:
|
2020-11-15 23:24:22 +00:00
|
|
|
cmd.extend(["--version-note", f"{version_note}"])
|
2017-12-09 18:19:39 +00:00
|
|
|
if static:
|
|
|
|
for mount_point, _ in static:
|
2020-11-15 23:24:22 +00:00
|
|
|
cmd.extend(["--static", f"{mount_point}:{mount_point}"])
|
2017-11-13 21:58:34 +00:00
|
|
|
if extra_options:
|
|
|
|
for opt in extra_options.split():
|
2020-11-15 23:24:22 +00:00
|
|
|
cmd.append(f"{opt}")
|
2019-05-03 13:59:01 +00:00
|
|
|
cmd = [shlex.quote(part) for part in cmd]
|
|
|
|
# port attribute is a (fixed) env variable and should not be quoted
|
2019-05-04 02:15:14 +00:00
|
|
|
cmd.extend(["--port", "$PORT"])
|
|
|
|
cmd = " ".join(cmd)
|
2017-11-19 18:20:17 +00:00
|
|
|
if branch:
|
2020-11-15 23:24:22 +00:00
|
|
|
install = [f"https://github.com/simonw/datasette/archive/{branch}.zip"] + list(
|
|
|
|
install
|
|
|
|
)
|
2018-04-18 14:48:34 +00:00
|
|
|
else:
|
2019-05-04 02:15:14 +00:00
|
|
|
install = ["datasette"] + list(install)
|
2018-04-18 14:48:34 +00:00
|
|
|
|
2020-11-25 03:05:35 +00:00
|
|
|
apt_get_extras_ = []
|
|
|
|
apt_get_extras_.extend(apt_get_extras)
|
|
|
|
apt_get_extras = apt_get_extras_
|
|
|
|
if spatialite:
|
|
|
|
apt_get_extras.extend(["python3-dev", "gcc", "libsqlite3-mod-spatialite"])
|
|
|
|
environment_variables[
|
|
|
|
"SQLITE_EXTENSIONS"
|
|
|
|
] = "/usr/lib/x86_64-linux-gnu/mod_spatialite.so"
|
2019-05-04 02:15:14 +00:00
|
|
|
return """
|
2022-08-14 15:49:14 +00:00
|
|
|
FROM python:3.10.6-slim-bullseye
|
2017-11-11 07:25:22 +00:00
|
|
|
COPY . /app
|
|
|
|
WORKDIR /app
|
2020-11-25 03:05:35 +00:00
|
|
|
{apt_get_extras}
|
2019-07-08 02:06:31 +00:00
|
|
|
{environment_variables}
|
2018-07-24 01:51:43 +00:00
|
|
|
RUN pip install -U {install_from}
|
2017-12-07 16:57:31 +00:00
|
|
|
RUN datasette inspect {files} --inspect-file inspect-data.json
|
2020-01-29 22:46:43 +00:00
|
|
|
ENV PORT {port}
|
|
|
|
EXPOSE {port}
|
2019-05-04 02:15:14 +00:00
|
|
|
CMD {cmd}""".format(
|
2020-11-25 03:05:35 +00:00
|
|
|
apt_get_extras=APT_GET_DOCKERFILE_EXTRAS.format(" ".join(apt_get_extras))
|
|
|
|
if apt_get_extras
|
|
|
|
else "",
|
2019-07-08 02:06:31 +00:00
|
|
|
environment_variables="\n".join(
|
|
|
|
[
|
|
|
|
"ENV {} '{}'".format(key, value)
|
2020-06-11 16:02:03 +00:00
|
|
|
for key, value in environment_variables.items()
|
2019-07-08 02:06:31 +00:00
|
|
|
]
|
|
|
|
),
|
2019-05-04 02:15:14 +00:00
|
|
|
install_from=" ".join(install),
|
2020-11-25 03:05:35 +00:00
|
|
|
files=" ".join(files),
|
2020-01-29 22:46:43 +00:00
|
|
|
port=port,
|
2020-11-25 03:05:35 +00:00
|
|
|
cmd=cmd,
|
2017-11-11 07:25:22 +00:00
|
|
|
).strip()
|
2017-11-13 16:13:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
2018-04-18 14:48:34 +00:00
|
|
|
def temporary_docker_directory(
|
|
|
|
files,
|
|
|
|
name,
|
|
|
|
metadata,
|
|
|
|
extra_options,
|
|
|
|
branch,
|
|
|
|
template_dir,
|
|
|
|
plugins_dir,
|
|
|
|
static,
|
|
|
|
install,
|
2018-05-31 14:16:50 +00:00
|
|
|
spatialite,
|
2018-06-17 20:14:55 +00:00
|
|
|
version_note,
|
2020-06-11 16:02:03 +00:00
|
|
|
secret,
|
2019-05-04 02:15:14 +00:00
|
|
|
extra_metadata=None,
|
2019-07-08 02:06:31 +00:00
|
|
|
environment_variables=None,
|
2020-01-29 22:46:43 +00:00
|
|
|
port=8001,
|
2020-11-25 03:05:35 +00:00
|
|
|
apt_get_extras=None,
|
2018-04-18 14:48:34 +00:00
|
|
|
):
|
2017-11-15 05:02:11 +00:00
|
|
|
extra_metadata = extra_metadata or {}
|
2017-11-13 16:13:38 +00:00
|
|
|
tmp = tempfile.TemporaryDirectory()
|
|
|
|
# We create a datasette folder in there to get a nicer now deploy name
|
|
|
|
datasette_dir = os.path.join(tmp.name, name)
|
|
|
|
os.mkdir(datasette_dir)
|
|
|
|
saved_cwd = os.getcwd()
|
2019-05-04 02:15:14 +00:00
|
|
|
file_paths = [os.path.join(saved_cwd, file_path) for file_path in files]
|
2017-11-13 16:13:38 +00:00
|
|
|
file_names = [os.path.split(f)[-1] for f in files]
|
2017-11-15 05:02:11 +00:00
|
|
|
if metadata:
|
2020-04-02 19:30:53 +00:00
|
|
|
metadata_content = parse_metadata(metadata.read())
|
2017-11-15 05:02:11 +00:00
|
|
|
else:
|
|
|
|
metadata_content = {}
|
2020-04-10 18:34:09 +00:00
|
|
|
# Merge in the non-null values in extra_metadata
|
|
|
|
mergedeep.merge(
|
|
|
|
metadata_content,
|
|
|
|
{key: value for key, value in extra_metadata.items() if value is not None},
|
|
|
|
)
|
2017-11-13 16:13:38 +00:00
|
|
|
try:
|
2017-12-09 18:19:39 +00:00
|
|
|
dockerfile = make_dockerfile(
|
|
|
|
file_names,
|
2019-05-04 02:15:14 +00:00
|
|
|
metadata_content and "metadata.json",
|
2017-12-09 18:19:39 +00:00
|
|
|
extra_options,
|
|
|
|
branch,
|
|
|
|
template_dir,
|
2018-04-16 05:22:01 +00:00
|
|
|
plugins_dir,
|
2017-12-09 18:19:39 +00:00
|
|
|
static,
|
2018-04-18 14:48:34 +00:00
|
|
|
install,
|
2018-05-31 14:16:50 +00:00
|
|
|
spatialite,
|
2018-06-17 20:14:55 +00:00
|
|
|
version_note,
|
2020-06-11 16:02:03 +00:00
|
|
|
secret,
|
2019-07-08 02:06:31 +00:00
|
|
|
environment_variables,
|
2020-01-29 22:46:43 +00:00
|
|
|
port=port,
|
2020-11-25 03:05:35 +00:00
|
|
|
apt_get_extras=apt_get_extras,
|
2017-12-09 18:19:39 +00:00
|
|
|
)
|
2017-11-13 16:13:38 +00:00
|
|
|
os.chdir(datasette_dir)
|
2017-11-15 05:02:11 +00:00
|
|
|
if metadata_content:
|
2021-03-11 16:15:49 +00:00
|
|
|
with open("metadata.json", "w") as fp:
|
|
|
|
fp.write(json.dumps(metadata_content, indent=2))
|
|
|
|
with open("Dockerfile", "w") as fp:
|
|
|
|
fp.write(dockerfile)
|
2017-11-13 16:13:38 +00:00
|
|
|
for path, filename in zip(file_paths, file_names):
|
2017-12-08 16:06:24 +00:00
|
|
|
link_or_copy(path, os.path.join(datasette_dir, filename))
|
2017-12-09 18:19:39 +00:00
|
|
|
if template_dir:
|
|
|
|
link_or_copy_directory(
|
|
|
|
os.path.join(saved_cwd, template_dir),
|
2019-05-04 02:15:14 +00:00
|
|
|
os.path.join(datasette_dir, "templates"),
|
2017-12-09 18:19:39 +00:00
|
|
|
)
|
2018-04-16 05:22:01 +00:00
|
|
|
if plugins_dir:
|
|
|
|
link_or_copy_directory(
|
|
|
|
os.path.join(saved_cwd, plugins_dir),
|
2019-05-04 02:15:14 +00:00
|
|
|
os.path.join(datasette_dir, "plugins"),
|
2018-04-16 05:22:01 +00:00
|
|
|
)
|
2017-12-09 18:19:39 +00:00
|
|
|
for mount_point, path in static:
|
|
|
|
link_or_copy_directory(
|
2019-05-04 02:15:14 +00:00
|
|
|
os.path.join(saved_cwd, path), os.path.join(datasette_dir, mount_point)
|
2017-12-09 18:19:39 +00:00
|
|
|
)
|
2017-12-08 16:06:24 +00:00
|
|
|
yield datasette_dir
|
2017-11-13 16:13:38 +00:00
|
|
|
finally:
|
|
|
|
tmp.cleanup()
|
|
|
|
os.chdir(saved_cwd)
|
2017-11-15 19:53:00 +00:00
|
|
|
|
2017-11-16 01:34:32 +00:00
|
|
|
|
2019-04-07 03:11:08 +00:00
|
|
|
def detect_primary_keys(conn, table):
|
2020-12-23 17:04:32 +00:00
|
|
|
"""Figure out primary keys for a table."""
|
2020-11-30 21:29:57 +00:00
|
|
|
columns = table_column_details(conn, table)
|
|
|
|
pks = [column for column in columns if column.is_pk]
|
|
|
|
pks.sort(key=lambda column: column.is_pk)
|
|
|
|
return [column.name for column in pks]
|
2019-04-07 03:11:08 +00:00
|
|
|
|
|
|
|
|
2019-04-07 02:56:07 +00:00
|
|
|
def get_outbound_foreign_keys(conn, table):
|
2020-11-15 23:24:22 +00:00
|
|
|
infos = conn.execute(f"PRAGMA foreign_key_list([{table}])").fetchall()
|
2019-04-07 02:56:07 +00:00
|
|
|
fks = []
|
|
|
|
for info in infos:
|
|
|
|
if info is not None:
|
|
|
|
id, seq, table_name, from_, to_, on_update, on_delete, match = info
|
2019-05-04 02:15:14 +00:00
|
|
|
fks.append(
|
2020-11-29 19:30:17 +00:00
|
|
|
{
|
|
|
|
"column": from_,
|
|
|
|
"other_table": table_name,
|
|
|
|
"other_column": to_,
|
|
|
|
"id": id,
|
|
|
|
"seq": seq,
|
|
|
|
}
|
2019-05-04 02:15:14 +00:00
|
|
|
)
|
2020-11-29 19:30:17 +00:00
|
|
|
# Filter out compound foreign keys by removing any where "id" is not unique
|
|
|
|
id_counts = Counter(fk["id"] for fk in fks)
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
"column": fk["column"],
|
|
|
|
"other_table": fk["other_table"],
|
|
|
|
"other_column": fk["other_column"],
|
|
|
|
}
|
|
|
|
for fk in fks
|
|
|
|
if id_counts[fk["id"]] == 1
|
|
|
|
]
|
2019-04-07 02:56:07 +00:00
|
|
|
|
|
|
|
|
2017-11-16 01:34:32 +00:00
|
|
|
def get_all_foreign_keys(conn):
|
2019-05-04 02:15:14 +00:00
|
|
|
tables = [
|
|
|
|
r[0] for r in conn.execute('select name from sqlite_master where type="table"')
|
|
|
|
]
|
2017-11-16 01:34:32 +00:00
|
|
|
table_to_foreign_keys = {}
|
|
|
|
for table in tables:
|
2019-05-04 02:15:14 +00:00
|
|
|
table_to_foreign_keys[table] = {"incoming": [], "outgoing": []}
|
2017-11-16 01:34:32 +00:00
|
|
|
for table in tables:
|
2020-11-29 19:30:17 +00:00
|
|
|
fks = get_outbound_foreign_keys(conn, table)
|
|
|
|
for fk in fks:
|
|
|
|
table_name = fk["other_table"]
|
|
|
|
from_ = fk["column"]
|
|
|
|
to_ = fk["other_column"]
|
|
|
|
if table_name not in table_to_foreign_keys:
|
|
|
|
# Weird edge case where something refers to a table that does
|
|
|
|
# not actually exist
|
|
|
|
continue
|
|
|
|
table_to_foreign_keys[table_name]["incoming"].append(
|
|
|
|
{"other_table": table, "column": to_, "other_column": from_}
|
|
|
|
)
|
|
|
|
table_to_foreign_keys[table]["outgoing"].append(
|
|
|
|
{"other_table": table_name, "column": from_, "other_column": to_}
|
|
|
|
)
|
2017-11-16 01:34:32 +00:00
|
|
|
|
|
|
|
return table_to_foreign_keys
|
2017-11-19 16:59:26 +00:00
|
|
|
|
|
|
|
|
2018-04-11 21:20:25 +00:00
|
|
|
def detect_spatialite(conn):
|
2019-05-04 02:15:14 +00:00
|
|
|
rows = conn.execute(
|
|
|
|
'select 1 from sqlite_master where tbl_name = "geometry_columns"'
|
|
|
|
).fetchall()
|
2018-04-11 21:20:25 +00:00
|
|
|
return len(rows) > 0
|
|
|
|
|
|
|
|
|
2018-04-29 00:04:32 +00:00
|
|
|
def detect_fts(conn, table):
|
2020-12-23 17:04:32 +00:00
|
|
|
"""Detect if table has a corresponding FTS virtual table and return it"""
|
2017-11-19 16:59:26 +00:00
|
|
|
rows = conn.execute(detect_fts_sql(table)).fetchall()
|
|
|
|
if len(rows) == 0:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return rows[0][0]
|
|
|
|
|
|
|
|
|
|
|
|
def detect_fts_sql(table):
|
2019-05-04 02:15:14 +00:00
|
|
|
return r"""
|
2017-11-19 16:59:26 +00:00
|
|
|
select name from sqlite_master
|
|
|
|
where rootpage = 0
|
2017-11-20 06:18:07 +00:00
|
|
|
and (
|
|
|
|
sql like '%VIRTUAL TABLE%USING FTS%content="{table}"%'
|
2019-09-03 00:32:27 +00:00
|
|
|
or sql like '%VIRTUAL TABLE%USING FTS%content=[{table}]%'
|
2017-11-24 22:51:00 +00:00
|
|
|
or (
|
|
|
|
tbl_name = "{table}"
|
2017-12-07 04:54:25 +00:00
|
|
|
and sql like '%VIRTUAL TABLE%USING FTS%'
|
2017-11-24 22:51:00 +00:00
|
|
|
)
|
2017-11-20 06:18:07 +00:00
|
|
|
)
|
2019-05-04 02:15:14 +00:00
|
|
|
""".format(
|
2021-06-02 03:27:04 +00:00
|
|
|
table=table.replace("'", "''")
|
2019-05-04 02:15:14 +00:00
|
|
|
)
|
2017-11-20 06:03:24 +00:00
|
|
|
|
|
|
|
|
2019-04-10 15:17:19 +00:00
|
|
|
def detect_json1(conn=None):
|
|
|
|
if conn is None:
|
|
|
|
conn = sqlite3.connect(":memory:")
|
|
|
|
try:
|
|
|
|
conn.execute("SELECT json('{}')")
|
|
|
|
return True
|
|
|
|
except Exception:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2019-04-07 01:58:51 +00:00
|
|
|
def table_columns(conn, table):
|
2020-10-06 00:32:10 +00:00
|
|
|
return [column.name for column in table_column_details(conn, table)]
|
|
|
|
|
|
|
|
|
|
|
|
def table_column_details(conn, table):
|
2020-12-01 00:28:02 +00:00
|
|
|
if supports_table_xinfo():
|
2020-11-30 21:29:57 +00:00
|
|
|
# table_xinfo was added in 3.26.0
|
|
|
|
return [
|
|
|
|
Column(*r)
|
|
|
|
for r in conn.execute(
|
|
|
|
f"PRAGMA table_xinfo({escape_sqlite(table)});"
|
|
|
|
).fetchall()
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
# Treat hidden as 0 for all columns
|
|
|
|
return [
|
|
|
|
Column(*(list(r) + [0]))
|
|
|
|
for r in conn.execute(
|
|
|
|
f"PRAGMA table_info({escape_sqlite(table)});"
|
|
|
|
).fetchall()
|
|
|
|
]
|
2019-04-07 01:58:51 +00:00
|
|
|
|
|
|
|
|
2019-05-04 02:15:14 +00:00
|
|
|
filter_column_re = re.compile(r"^_filter_column_\d+$")
|
2017-11-23 04:03:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
def filters_should_redirect(special_args):
|
|
|
|
redirect_params = []
|
2017-11-23 20:32:54 +00:00
|
|
|
# Handle _filter_column=foo&_filter_op=exact&_filter_value=...
|
2019-05-04 02:15:14 +00:00
|
|
|
filter_column = special_args.get("_filter_column")
|
|
|
|
filter_op = special_args.get("_filter_op") or ""
|
|
|
|
filter_value = special_args.get("_filter_value") or ""
|
|
|
|
if "__" in filter_op:
|
|
|
|
filter_op, filter_value = filter_op.split("__", 1)
|
2017-11-23 20:32:54 +00:00
|
|
|
if filter_column:
|
2020-11-15 23:24:22 +00:00
|
|
|
redirect_params.append((f"{filter_column}__{filter_op}", filter_value))
|
2019-05-04 02:15:14 +00:00
|
|
|
for key in ("_filter_column", "_filter_op", "_filter_value"):
|
2017-11-23 20:32:54 +00:00
|
|
|
if key in special_args:
|
|
|
|
redirect_params.append((key, None))
|
2017-11-23 04:03:46 +00:00
|
|
|
# Now handle _filter_column_1=name&_filter_op_1=contains&_filter_value_1=hello
|
|
|
|
column_keys = [k for k in special_args if filter_column_re.match(k)]
|
|
|
|
for column_key in column_keys:
|
2019-05-04 02:15:14 +00:00
|
|
|
number = column_key.split("_")[-1]
|
2017-11-23 04:03:46 +00:00
|
|
|
column = special_args[column_key]
|
2020-11-15 23:24:22 +00:00
|
|
|
op = special_args.get(f"_filter_op_{number}") or "exact"
|
|
|
|
value = special_args.get(f"_filter_value_{number}") or ""
|
2019-05-04 02:15:14 +00:00
|
|
|
if "__" in op:
|
|
|
|
op, value = op.split("__", 1)
|
2017-11-23 20:32:54 +00:00
|
|
|
if column:
|
2020-11-15 23:24:22 +00:00
|
|
|
redirect_params.append((f"{column}__{op}", value))
|
2019-05-04 02:15:14 +00:00
|
|
|
redirect_params.extend(
|
|
|
|
[
|
2020-11-15 23:24:22 +00:00
|
|
|
(f"_filter_column_{number}", None),
|
|
|
|
(f"_filter_op_{number}", None),
|
|
|
|
(f"_filter_value_{number}", None),
|
2019-05-04 02:15:14 +00:00
|
|
|
]
|
|
|
|
)
|
2017-11-23 04:03:46 +00:00
|
|
|
return redirect_params
|
2017-11-29 17:05:24 +00:00
|
|
|
|
|
|
|
|
2019-05-04 02:15:14 +00:00
|
|
|
whitespace_re = re.compile(r"\s")
|
2017-11-29 17:05:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
def is_url(value):
|
2020-12-23 17:04:32 +00:00
|
|
|
"""Must start with http:// or https:// and contain JUST a URL"""
|
2017-11-29 17:05:24 +00:00
|
|
|
if not isinstance(value, str):
|
|
|
|
return False
|
2019-05-04 02:15:14 +00:00
|
|
|
if not value.startswith("http://") and not value.startswith("https://"):
|
2017-11-29 17:05:24 +00:00
|
|
|
return False
|
|
|
|
# Any whitespace at all is invalid
|
|
|
|
if whitespace_re.search(value):
|
|
|
|
return False
|
|
|
|
return True
|
2017-11-30 07:09:54 +00:00
|
|
|
|
|
|
|
|
2019-05-04 02:15:14 +00:00
|
|
|
css_class_re = re.compile(r"^[a-zA-Z]+[_a-zA-Z0-9-]*$")
|
|
|
|
css_invalid_chars_re = re.compile(r"[^a-zA-Z0-9_\-]")
|
2017-11-30 07:09:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
def to_css_class(s):
|
|
|
|
"""
|
|
|
|
Given a string (e.g. a table name) returns a valid unique CSS class.
|
|
|
|
For simple cases, just returns the string again. If the string is not a
|
|
|
|
valid CSS class (we disallow - and _ prefixes even though they are valid
|
|
|
|
as they may be confused with browser prefixes) we strip invalid characters
|
|
|
|
and add a 6 char md5 sum suffix, to make sure two tables with identical
|
|
|
|
names after stripping characters don't end up with the same CSS class.
|
|
|
|
"""
|
|
|
|
if css_class_re.match(s):
|
|
|
|
return s
|
2019-05-04 02:15:14 +00:00
|
|
|
md5_suffix = hashlib.md5(s.encode("utf8")).hexdigest()[:6]
|
2017-11-30 07:09:54 +00:00
|
|
|
# Strip leading _, -
|
2019-05-04 02:15:14 +00:00
|
|
|
s = s.lstrip("_").lstrip("-")
|
2017-11-30 07:09:54 +00:00
|
|
|
# Replace any whitespace with hyphens
|
2019-05-04 02:15:14 +00:00
|
|
|
s = "-".join(s.split())
|
2017-11-30 07:09:54 +00:00
|
|
|
# Remove any remaining invalid characters
|
2019-05-04 02:15:14 +00:00
|
|
|
s = css_invalid_chars_re.sub("", s)
|
2017-11-30 07:09:54 +00:00
|
|
|
# Attach the md5 suffix
|
|
|
|
bits = [b for b in (s, md5_suffix) if b]
|
2019-05-04 02:15:14 +00:00
|
|
|
return "-".join(bits)
|
2017-12-08 16:06:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
def link_or_copy(src, dst):
|
|
|
|
# Intended for use in populating a temp directory. We link if possible,
|
|
|
|
# but fall back to copying if the temp directory is on a different device
|
|
|
|
# https://github.com/simonw/datasette/issues/141
|
|
|
|
try:
|
|
|
|
os.link(src, dst)
|
2017-12-09 18:19:39 +00:00
|
|
|
except OSError:
|
2017-12-08 16:06:24 +00:00
|
|
|
shutil.copyfile(src, dst)
|
2017-12-09 18:19:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
def link_or_copy_directory(src, dst):
|
|
|
|
try:
|
2020-05-27 18:17:43 +00:00
|
|
|
copytree(src, dst, copy_function=os.link, dirs_exist_ok=True)
|
2017-12-09 18:19:39 +00:00
|
|
|
except OSError:
|
2020-05-27 18:17:43 +00:00
|
|
|
copytree(src, dst, dirs_exist_ok=True)
|
2018-04-16 05:22:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def module_from_path(path, name):
|
|
|
|
# Adapted from http://sayspy.blogspot.com/2011/07/how-to-import-module-from-just-file.html
|
2019-07-03 04:32:55 +00:00
|
|
|
mod = types.ModuleType(name)
|
2018-04-16 05:22:01 +00:00
|
|
|
mod.__file__ = path
|
2019-05-04 02:15:14 +00:00
|
|
|
with open(path, "r") as file:
|
|
|
|
code = compile(file.read(), path, "exec", dont_inherit=True)
|
2018-04-16 05:22:01 +00:00
|
|
|
exec(code, mod.__dict__)
|
|
|
|
return mod
|
2018-04-19 05:24:48 +00:00
|
|
|
|
|
|
|
|
2020-10-31 18:16:28 +00:00
|
|
|
def path_with_format(
|
|
|
|
*, request=None, path=None, format=None, extra_qs=None, replace_format=None
|
|
|
|
):
|
2018-06-15 06:51:23 +00:00
|
|
|
qs = extra_qs or {}
|
2020-10-31 18:16:28 +00:00
|
|
|
path = request.path if request else path
|
2020-11-15 23:24:22 +00:00
|
|
|
if replace_format and path.endswith(f".{replace_format}"):
|
2020-10-29 22:47:32 +00:00
|
|
|
path = path[: -(1 + len(replace_format))]
|
|
|
|
if "." in path:
|
2018-06-15 06:51:23 +00:00
|
|
|
qs["_format"] = format
|
|
|
|
else:
|
2020-11-15 23:24:22 +00:00
|
|
|
path = f"{path}.{format}"
|
2018-06-15 06:51:23 +00:00
|
|
|
if qs:
|
|
|
|
extra = urllib.parse.urlencode(sorted(qs.items()))
|
2020-10-31 18:16:28 +00:00
|
|
|
if request and request.query_string:
|
2020-11-15 23:24:22 +00:00
|
|
|
path = f"{path}?{request.query_string}&{extra}"
|
2018-06-15 06:51:23 +00:00
|
|
|
else:
|
2020-11-15 23:24:22 +00:00
|
|
|
path = f"{path}?{extra}"
|
2020-10-31 18:16:28 +00:00
|
|
|
elif request and request.query_string:
|
2020-11-15 23:24:22 +00:00
|
|
|
path = f"{path}?{request.query_string}"
|
2018-06-15 06:51:23 +00:00
|
|
|
return path
|
2018-06-16 16:44:31 +00:00
|
|
|
|
|
|
|
|
?_labels= and ?_label=COL to expand foreign keys in JSON/CSV
These new querystring arguments can be used to request expanded foreign keys
in both JSON and CSV formats.
?_labels=on turns on expansions for ALL foreign key columns
?_label=COLUMN1&_label=COLUMN2 can be used to pick specific columns to expand
e.g. `Street_Tree_List.json?_label=qSpecies&_label=qLegalStatus`
{
"rowid": 233,
"TreeID": 121240,
"qLegalStatus": {
"value" 2,
"label": "Private"
}
"qSpecies": {
"value": 16,
"label": "Sycamore"
}
"qAddress": "91 Commonwealth Ave",
...
}
The labels option also works for the HTML and CSV views.
HTML defaults to `?_labels=on`, so if you pass `?_labels=off` you can disable
foreign key expansion entirely - or you can use `?_label=COLUMN` to request
just specific columns.
If you expand labels on CSV you get additional columns in the output:
`/Street_Tree_List.csv?_label=qLegalStatus`
rowid,TreeID,qLegalStatus,qLegalStatus_label...
1,141565,1,Permitted Site...
2,232565,2,Undocumented...
I also refactored the existing foreign key expansion code.
Closes #233. Refs #266.
2018-06-16 22:18:57 +00:00
|
|
|
class CustomRow(OrderedDict):
|
|
|
|
# Loose imitation of sqlite3.Row which offers
|
|
|
|
# both index-based AND key-based lookups
|
2018-06-21 14:56:28 +00:00
|
|
|
def __init__(self, columns, values=None):
|
?_labels= and ?_label=COL to expand foreign keys in JSON/CSV
These new querystring arguments can be used to request expanded foreign keys
in both JSON and CSV formats.
?_labels=on turns on expansions for ALL foreign key columns
?_label=COLUMN1&_label=COLUMN2 can be used to pick specific columns to expand
e.g. `Street_Tree_List.json?_label=qSpecies&_label=qLegalStatus`
{
"rowid": 233,
"TreeID": 121240,
"qLegalStatus": {
"value" 2,
"label": "Private"
}
"qSpecies": {
"value": 16,
"label": "Sycamore"
}
"qAddress": "91 Commonwealth Ave",
...
}
The labels option also works for the HTML and CSV views.
HTML defaults to `?_labels=on`, so if you pass `?_labels=off` you can disable
foreign key expansion entirely - or you can use `?_label=COLUMN` to request
just specific columns.
If you expand labels on CSV you get additional columns in the output:
`/Street_Tree_List.csv?_label=qLegalStatus`
rowid,TreeID,qLegalStatus,qLegalStatus_label...
1,141565,1,Permitted Site...
2,232565,2,Undocumented...
I also refactored the existing foreign key expansion code.
Closes #233. Refs #266.
2018-06-16 22:18:57 +00:00
|
|
|
self.columns = columns
|
2018-06-21 14:56:28 +00:00
|
|
|
if values:
|
|
|
|
self.update(values)
|
?_labels= and ?_label=COL to expand foreign keys in JSON/CSV
These new querystring arguments can be used to request expanded foreign keys
in both JSON and CSV formats.
?_labels=on turns on expansions for ALL foreign key columns
?_label=COLUMN1&_label=COLUMN2 can be used to pick specific columns to expand
e.g. `Street_Tree_List.json?_label=qSpecies&_label=qLegalStatus`
{
"rowid": 233,
"TreeID": 121240,
"qLegalStatus": {
"value" 2,
"label": "Private"
}
"qSpecies": {
"value": 16,
"label": "Sycamore"
}
"qAddress": "91 Commonwealth Ave",
...
}
The labels option also works for the HTML and CSV views.
HTML defaults to `?_labels=on`, so if you pass `?_labels=off` you can disable
foreign key expansion entirely - or you can use `?_label=COLUMN` to request
just specific columns.
If you expand labels on CSV you get additional columns in the output:
`/Street_Tree_List.csv?_label=qLegalStatus`
rowid,TreeID,qLegalStatus,qLegalStatus_label...
1,141565,1,Permitted Site...
2,232565,2,Undocumented...
I also refactored the existing foreign key expansion code.
Closes #233. Refs #266.
2018-06-16 22:18:57 +00:00
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
if isinstance(key, int):
|
|
|
|
return super().__getitem__(self.columns[key])
|
|
|
|
else:
|
|
|
|
return super().__getitem__(key)
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
for column in self.columns:
|
|
|
|
yield self[column]
|
|
|
|
|
|
|
|
|
2018-06-16 16:44:31 +00:00
|
|
|
def value_as_boolean(value):
|
2019-05-04 02:15:14 +00:00
|
|
|
if value.lower() not in ("on", "off", "true", "false", "1", "0"):
|
2018-06-16 16:44:31 +00:00
|
|
|
raise ValueAsBooleanError
|
2019-05-04 02:15:14 +00:00
|
|
|
return value.lower() in ("on", "true", "1")
|
2018-06-16 16:44:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ValueAsBooleanError(ValueError):
|
|
|
|
pass
|
2018-06-18 03:21:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WriteLimitExceeded(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class LimitedWriter:
|
|
|
|
def __init__(self, writer, limit_mb):
|
|
|
|
self.writer = writer
|
|
|
|
self.limit_bytes = limit_mb * 1024 * 1024
|
|
|
|
self.bytes_count = 0
|
|
|
|
|
2019-06-24 03:13:09 +00:00
|
|
|
async def write(self, bytes):
|
2018-06-18 03:21:02 +00:00
|
|
|
self.bytes_count += len(bytes)
|
|
|
|
if self.limit_bytes and (self.bytes_count > self.limit_bytes):
|
2020-11-15 23:24:22 +00:00
|
|
|
raise WriteLimitExceeded(f"CSV contains more than {self.limit_bytes} bytes")
|
2019-06-24 03:13:09 +00:00
|
|
|
await self.writer.write(bytes)
|
2018-07-24 03:07:57 +00:00
|
|
|
|
|
|
|
|
2021-06-01 15:49:50 +00:00
|
|
|
class EscapeHtmlWriter:
|
|
|
|
def __init__(self, writer):
|
|
|
|
self.writer = writer
|
|
|
|
|
|
|
|
async def write(self, content):
|
|
|
|
await self.writer.write(markupsafe.escape(content))
|
|
|
|
|
|
|
|
|
2018-07-24 03:07:57 +00:00
|
|
|
_infinities = {float("inf"), float("-inf")}
|
|
|
|
|
|
|
|
|
|
|
|
def remove_infinites(row):
|
|
|
|
if any((c in _infinities) if isinstance(c, float) else 0 for c in row):
|
2019-05-04 02:15:14 +00:00
|
|
|
return [None if (isinstance(c, float) and c in _infinities) else c for c in row]
|
2018-07-24 03:07:57 +00:00
|
|
|
return row
|
publish_subcommand hook + default plugins mechanism, used for publish heroku/now (#349)
This change introduces a new plugin hook, publish_subcommand, which can be
used to implement new subcommands for the "datasette publish" command family.
I've used this new hook to refactor out the "publish now" and "publish heroku"
implementations into separate modules. I've also added unit tests for these
two publishers, mocking the subprocess.call and subprocess.check_output
functions.
As part of this, I introduced a mechanism for loading default plugins. These
are defined in the new "default_plugins" list inside datasette/app.py
Closes #217 (Plugin support for datasette publish)
Closes #348 (Unit tests for "datasette publish")
Refs #14, #59, #102, #103, #146, #236, #347
2018-07-26 05:15:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
class StaticMount(click.ParamType):
|
2019-11-26 02:31:42 +00:00
|
|
|
name = "mount:directory"
|
publish_subcommand hook + default plugins mechanism, used for publish heroku/now (#349)
This change introduces a new plugin hook, publish_subcommand, which can be
used to implement new subcommands for the "datasette publish" command family.
I've used this new hook to refactor out the "publish now" and "publish heroku"
implementations into separate modules. I've also added unit tests for these
two publishers, mocking the subprocess.call and subprocess.check_output
functions.
As part of this, I introduced a mechanism for loading default plugins. These
are defined in the new "default_plugins" list inside datasette/app.py
Closes #217 (Plugin support for datasette publish)
Closes #348 (Unit tests for "datasette publish")
Refs #14, #59, #102, #103, #146, #236, #347
2018-07-26 05:15:59 +00:00
|
|
|
|
|
|
|
def convert(self, value, param, ctx):
|
|
|
|
if ":" not in value:
|
|
|
|
self.fail(
|
2020-11-15 23:24:22 +00:00
|
|
|
f'"{value}" should be of format mountpoint:directory',
|
2019-05-04 02:15:14 +00:00
|
|
|
param,
|
|
|
|
ctx,
|
publish_subcommand hook + default plugins mechanism, used for publish heroku/now (#349)
This change introduces a new plugin hook, publish_subcommand, which can be
used to implement new subcommands for the "datasette publish" command family.
I've used this new hook to refactor out the "publish now" and "publish heroku"
implementations into separate modules. I've also added unit tests for these
two publishers, mocking the subprocess.call and subprocess.check_output
functions.
As part of this, I introduced a mechanism for loading default plugins. These
are defined in the new "default_plugins" list inside datasette/app.py
Closes #217 (Plugin support for datasette publish)
Closes #348 (Unit tests for "datasette publish")
Refs #14, #59, #102, #103, #146, #236, #347
2018-07-26 05:15:59 +00:00
|
|
|
)
|
2019-07-11 16:13:19 +00:00
|
|
|
path, dirpath = value.split(":", 1)
|
|
|
|
dirpath = os.path.abspath(dirpath)
|
publish_subcommand hook + default plugins mechanism, used for publish heroku/now (#349)
This change introduces a new plugin hook, publish_subcommand, which can be
used to implement new subcommands for the "datasette publish" command family.
I've used this new hook to refactor out the "publish now" and "publish heroku"
implementations into separate modules. I've also added unit tests for these
two publishers, mocking the subprocess.call and subprocess.check_output
functions.
As part of this, I introduced a mechanism for loading default plugins. These
are defined in the new "default_plugins" list inside datasette/app.py
Closes #217 (Plugin support for datasette publish)
Closes #348 (Unit tests for "datasette publish")
Refs #14, #59, #102, #103, #146, #236, #347
2018-07-26 05:15:59 +00:00
|
|
|
if not os.path.exists(dirpath) or not os.path.isdir(dirpath):
|
2020-11-15 23:24:22 +00:00
|
|
|
self.fail(f"{value} is not a valid directory path", param, ctx)
|
publish_subcommand hook + default plugins mechanism, used for publish heroku/now (#349)
This change introduces a new plugin hook, publish_subcommand, which can be
used to implement new subcommands for the "datasette publish" command family.
I've used this new hook to refactor out the "publish now" and "publish heroku"
implementations into separate modules. I've also added unit tests for these
two publishers, mocking the subprocess.call and subprocess.check_output
functions.
As part of this, I introduced a mechanism for loading default plugins. These
are defined in the new "default_plugins" list inside datasette/app.py
Closes #217 (Plugin support for datasette publish)
Closes #348 (Unit tests for "datasette publish")
Refs #14, #59, #102, #103, #146, #236, #347
2018-07-26 05:15:59 +00:00
|
|
|
return path, dirpath
|
2019-02-06 04:53:44 +00:00
|
|
|
|
2022-08-23 18:34:30 +00:00
|
|
|
# The --load-extension parameter can optionally include a specific entrypoint.
|
|
|
|
# This is done by appending ":entrypoint_name" after supplying the path to the extension
|
|
|
|
class LoadExtension(click.ParamType):
|
|
|
|
name = "path:entrypoint?"
|
|
|
|
|
|
|
|
def convert(self, value, param, ctx):
|
|
|
|
if ":" not in value:
|
|
|
|
return value
|
|
|
|
path, entrypoint = value.split(":", 1)
|
|
|
|
return path, entrypoint
|
|
|
|
|
2019-02-06 04:53:44 +00:00
|
|
|
|
|
|
|
def format_bytes(bytes):
|
|
|
|
current = float(bytes)
|
|
|
|
for unit in ("bytes", "KB", "MB", "GB", "TB"):
|
|
|
|
if current < 1024:
|
|
|
|
break
|
|
|
|
current = current / 1024
|
|
|
|
if unit == "bytes":
|
2020-11-15 23:24:22 +00:00
|
|
|
return f"{int(current)} {unit}"
|
2019-02-06 04:53:44 +00:00
|
|
|
else:
|
2020-11-15 23:24:22 +00:00
|
|
|
return f"{current:.1f} {unit}"
|
2019-06-24 03:13:09 +00:00
|
|
|
|
|
|
|
|
2019-12-29 18:48:13 +00:00
|
|
|
_escape_fts_re = re.compile(r'\s+|(".*?")')
|
|
|
|
|
|
|
|
|
|
|
|
def escape_fts(query):
|
|
|
|
# If query has unbalanced ", add one at end
|
|
|
|
if query.count('"') % 2:
|
|
|
|
query += '"'
|
|
|
|
bits = _escape_fts_re.split(query)
|
|
|
|
bits = [b for b in bits if b and b != '""']
|
|
|
|
return " ".join(
|
|
|
|
'"{}"'.format(bit) if not bit.startswith('"') else bit for bit in bits
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-06-05 17:52:50 +00:00
|
|
|
class MultiParams:
|
2020-05-29 23:18:01 +00:00
|
|
|
def __init__(self, data):
|
2020-06-05 17:52:50 +00:00
|
|
|
# data is a dictionary of key => [list, of, values] or a list of [["key", "value"]] pairs
|
|
|
|
if isinstance(data, dict):
|
|
|
|
for key in data:
|
|
|
|
assert isinstance(
|
2020-06-05 18:01:06 +00:00
|
|
|
data[key], (list, tuple)
|
2020-06-05 17:52:50 +00:00
|
|
|
), "dictionary data should be a dictionary of key => [list]"
|
|
|
|
self._data = data
|
2020-06-05 18:01:06 +00:00
|
|
|
elif isinstance(data, list) or isinstance(data, tuple):
|
2020-06-05 17:52:50 +00:00
|
|
|
new_data = {}
|
|
|
|
for item in data:
|
|
|
|
assert (
|
2020-06-05 18:01:06 +00:00
|
|
|
isinstance(item, (list, tuple)) and len(item) == 2
|
2020-06-05 17:52:50 +00:00
|
|
|
), "list data should be a list of [key, value] pairs"
|
|
|
|
key, value = item
|
|
|
|
new_data.setdefault(key, []).append(value)
|
|
|
|
self._data = new_data
|
2020-05-29 23:18:01 +00:00
|
|
|
|
2020-06-05 19:05:57 +00:00
|
|
|
def __repr__(self):
|
2020-11-15 23:24:22 +00:00
|
|
|
return f"<MultiParams: {self._data}>"
|
2020-06-05 19:05:57 +00:00
|
|
|
|
2020-05-29 23:18:01 +00:00
|
|
|
def __contains__(self, key):
|
|
|
|
return key in self._data
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
return self._data[key][0]
|
|
|
|
|
|
|
|
def keys(self):
|
|
|
|
return self._data.keys()
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
yield from self._data.keys()
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self._data)
|
|
|
|
|
2019-06-24 03:13:09 +00:00
|
|
|
def get(self, name, default=None):
|
2020-12-23 17:04:32 +00:00
|
|
|
"""Return first value in the list, if available"""
|
2019-06-24 03:13:09 +00:00
|
|
|
try:
|
2020-05-29 23:18:01 +00:00
|
|
|
return self._data.get(name)[0]
|
2019-06-24 03:13:09 +00:00
|
|
|
except (KeyError, TypeError):
|
|
|
|
return default
|
|
|
|
|
2020-05-29 22:51:30 +00:00
|
|
|
def getlist(self, name):
|
2020-12-23 17:04:32 +00:00
|
|
|
"""Return full list"""
|
2020-05-29 23:18:01 +00:00
|
|
|
return self._data.get(name) or []
|
2020-02-15 17:56:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ConnectionProblem(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class SpatialiteConnectionProblem(ConnectionProblem):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def check_connection(conn):
|
|
|
|
tables = [
|
|
|
|
r[0]
|
|
|
|
for r in conn.execute(
|
|
|
|
"select name from sqlite_master where type='table'"
|
|
|
|
).fetchall()
|
|
|
|
]
|
|
|
|
for table in tables:
|
|
|
|
try:
|
2020-09-02 22:24:55 +00:00
|
|
|
conn.execute(
|
2020-11-15 23:24:22 +00:00
|
|
|
f"PRAGMA table_info({escape_sqlite(table)});",
|
2020-09-02 22:24:55 +00:00
|
|
|
)
|
2020-02-15 17:56:48 +00:00
|
|
|
except sqlite3.OperationalError as e:
|
|
|
|
if e.args[0] == "no such module: VirtualSpatialIndex":
|
|
|
|
raise SpatialiteConnectionProblem(e)
|
|
|
|
else:
|
|
|
|
raise ConnectionProblem(e)
|
2020-04-02 19:30:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BadMetadataError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2022-02-07 06:30:00 +00:00
|
|
|
@documented
|
|
|
|
def parse_metadata(content: str) -> dict:
|
|
|
|
"Detects if content is JSON or YAML and parses it appropriately."
|
2020-04-02 19:30:53 +00:00
|
|
|
# content can be JSON or YAML
|
|
|
|
try:
|
|
|
|
return json.loads(content)
|
|
|
|
except json.JSONDecodeError:
|
|
|
|
try:
|
|
|
|
return yaml.safe_load(content)
|
|
|
|
except yaml.YAMLError:
|
|
|
|
raise BadMetadataError("Metadata is not valid JSON or YAML")
|
2020-05-27 19:25:52 +00:00
|
|
|
|
|
|
|
|
2020-06-09 03:12:06 +00:00
|
|
|
def _gather_arguments(fn, kwargs):
|
2020-05-27 19:25:52 +00:00
|
|
|
parameters = inspect.signature(fn).parameters.keys()
|
|
|
|
call_with = []
|
|
|
|
for parameter in parameters:
|
|
|
|
if parameter not in kwargs:
|
2020-05-28 05:57:05 +00:00
|
|
|
raise TypeError(
|
|
|
|
"{} requires parameters {}, missing: {}".format(
|
|
|
|
fn, tuple(parameters), set(parameters) - set(kwargs.keys())
|
|
|
|
)
|
|
|
|
)
|
2020-05-27 19:25:52 +00:00
|
|
|
call_with.append(kwargs[parameter])
|
2020-06-09 03:12:06 +00:00
|
|
|
return call_with
|
|
|
|
|
|
|
|
|
|
|
|
def call_with_supported_arguments(fn, **kwargs):
|
|
|
|
call_with = _gather_arguments(fn, kwargs)
|
2020-05-27 19:25:52 +00:00
|
|
|
return fn(*call_with)
|
2020-06-06 18:39:11 +00:00
|
|
|
|
|
|
|
|
2020-06-09 03:12:06 +00:00
|
|
|
async def async_call_with_supported_arguments(fn, **kwargs):
|
|
|
|
call_with = _gather_arguments(fn, kwargs)
|
|
|
|
return await fn(*call_with)
|
|
|
|
|
|
|
|
|
2020-06-06 18:39:11 +00:00
|
|
|
def actor_matches_allow(actor, allow):
|
2020-07-25 00:04:06 +00:00
|
|
|
if allow is True:
|
|
|
|
return True
|
|
|
|
if allow is False:
|
|
|
|
return False
|
2020-06-09 17:01:03 +00:00
|
|
|
if actor is None and allow and allow.get("unauthenticated") is True:
|
|
|
|
return True
|
2020-06-06 18:39:11 +00:00
|
|
|
if allow is None:
|
|
|
|
return True
|
2020-06-09 17:01:03 +00:00
|
|
|
actor = actor or {}
|
2020-06-06 18:39:11 +00:00
|
|
|
for key, values in allow.items():
|
|
|
|
if values == "*" and key in actor:
|
|
|
|
return True
|
2020-06-09 14:01:23 +00:00
|
|
|
if not isinstance(values, list):
|
2020-06-06 18:39:11 +00:00
|
|
|
values = [values]
|
|
|
|
actor_values = actor.get(key)
|
|
|
|
if actor_values is None:
|
2020-06-11 22:47:19 +00:00
|
|
|
continue
|
2020-06-09 14:01:23 +00:00
|
|
|
if not isinstance(actor_values, list):
|
2020-06-06 18:39:11 +00:00
|
|
|
actor_values = [actor_values]
|
|
|
|
actor_values = set(actor_values)
|
|
|
|
if actor_values.intersection(values):
|
|
|
|
return True
|
|
|
|
return False
|
2020-06-08 13:49:55 +00:00
|
|
|
|
|
|
|
|
2020-06-12 00:21:48 +00:00
|
|
|
def resolve_env_secrets(config, environ):
|
2020-12-23 17:04:32 +00:00
|
|
|
"""Create copy that recursively replaces {"$env": "NAME"} with values from environ"""
|
2020-06-12 00:21:48 +00:00
|
|
|
if isinstance(config, dict):
|
|
|
|
if list(config.keys()) == ["$env"]:
|
|
|
|
return environ.get(list(config.values())[0])
|
2020-06-12 00:48:20 +00:00
|
|
|
elif list(config.keys()) == ["$file"]:
|
|
|
|
return open(list(config.values())[0]).read()
|
2020-06-12 00:21:48 +00:00
|
|
|
else:
|
|
|
|
return {
|
|
|
|
key: resolve_env_secrets(value, environ)
|
|
|
|
for key, value in config.items()
|
|
|
|
}
|
|
|
|
elif isinstance(config, list):
|
|
|
|
return [resolve_env_secrets(value, environ) for value in config]
|
|
|
|
else:
|
|
|
|
return config
|
2020-06-29 18:40:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def display_actor(actor):
|
|
|
|
for key in ("display", "name", "username", "login", "id"):
|
|
|
|
if actor.get(key):
|
|
|
|
return actor[key]
|
|
|
|
return str(actor)
|
2020-10-19 22:37:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SpatialiteNotFound(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
# Can replace with sqlite-utils when I add that dependency
|
|
|
|
def find_spatialite():
|
|
|
|
for path in SPATIALITE_PATHS:
|
|
|
|
if os.path.exists(path):
|
|
|
|
return path
|
|
|
|
raise SpatialiteNotFound
|
2020-10-26 05:06:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def initial_path_for_datasette(datasette):
|
2020-12-23 17:04:32 +00:00
|
|
|
"""Return suggested path for opening this Datasette, based on number of DBs and tables"""
|
2020-12-21 19:48:06 +00:00
|
|
|
databases = dict([p for p in datasette.databases.items() if p[0] != "_internal"])
|
2020-12-18 22:34:05 +00:00
|
|
|
if len(databases) == 1:
|
|
|
|
db_name = next(iter(databases.keys()))
|
2020-10-26 05:06:20 +00:00
|
|
|
path = datasette.urls.database(db_name)
|
|
|
|
# Does this DB only have one table?
|
2020-12-18 22:34:05 +00:00
|
|
|
db = next(iter(databases.values()))
|
2020-10-26 05:06:20 +00:00
|
|
|
tables = await db.table_names()
|
|
|
|
if len(tables) == 1:
|
|
|
|
path = datasette.urls.table(db_name, tables[0])
|
|
|
|
else:
|
|
|
|
path = datasette.urls.instance()
|
|
|
|
return path
|
2020-10-31 19:11:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PrefixedUrlString(str):
|
|
|
|
def __add__(self, other):
|
|
|
|
return type(self)(super().__add__(other))
|
|
|
|
|
2020-10-31 20:35:47 +00:00
|
|
|
def __str__(self):
|
|
|
|
return super().__str__()
|
|
|
|
|
2020-10-31 19:11:40 +00:00
|
|
|
def __getattribute__(self, name):
|
2020-10-31 20:35:47 +00:00
|
|
|
if not name.startswith("__") and name in dir(str):
|
2020-10-31 19:11:40 +00:00
|
|
|
|
|
|
|
def method(self, *args, **kwargs):
|
|
|
|
value = getattr(super(), name)(*args, **kwargs)
|
|
|
|
if isinstance(value, str):
|
|
|
|
return type(self)(value)
|
|
|
|
elif isinstance(value, list):
|
|
|
|
return [type(self)(i) for i in value]
|
|
|
|
elif isinstance(value, tuple):
|
|
|
|
return tuple(type(self)(i) for i in value)
|
|
|
|
else:
|
|
|
|
return value
|
|
|
|
|
|
|
|
return method.__get__(self)
|
|
|
|
else:
|
|
|
|
return super().__getattribute__(name)
|
2020-11-24 20:37:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
class StartupError(Exception):
|
|
|
|
pass
|
2021-08-09 03:21:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
_re_named_parameter = re.compile(":([a-zA-Z0-9_]+)")
|
|
|
|
|
2021-08-09 03:26:08 +00:00
|
|
|
|
2021-08-09 03:21:13 +00:00
|
|
|
async def derive_named_parameters(db, sql):
|
2021-08-09 03:26:08 +00:00
|
|
|
explain = "explain {}".format(sql.strip().rstrip(";"))
|
2021-08-09 03:21:13 +00:00
|
|
|
possible_params = _re_named_parameter.findall(sql)
|
|
|
|
try:
|
|
|
|
results = await db.execute(explain, {p: None for p in possible_params})
|
|
|
|
return [row["p4"].lstrip(":") for row in results if row["opcode"] == "Variable"]
|
|
|
|
except sqlite3.DatabaseError:
|
2021-08-09 03:26:08 +00:00
|
|
|
return possible_params
|
2021-10-14 19:03:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
def add_cors_headers(headers):
|
|
|
|
headers["Access-Control-Allow-Origin"] = "*"
|
|
|
|
headers["Access-Control-Allow-Headers"] = "Authorization"
|
2021-11-27 20:08:42 +00:00
|
|
|
headers["Access-Control-Expose-Headers"] = "Link"
|
2022-03-07 15:38:29 +00:00
|
|
|
|
|
|
|
|
2022-03-15 18:01:57 +00:00
|
|
|
_TILDE_ENCODING_SAFE = frozenset(
|
2022-03-07 15:38:29 +00:00
|
|
|
b"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
b"abcdefghijklmnopqrstuvwxyz"
|
2022-03-15 18:01:57 +00:00
|
|
|
b"0123456789_-"
|
2022-03-07 15:38:29 +00:00
|
|
|
# This is the same as Python percent-encoding but I removed
|
2022-03-15 18:01:57 +00:00
|
|
|
# '.' and '~'
|
2022-03-07 15:38:29 +00:00
|
|
|
)
|
|
|
|
|
2022-04-06 15:55:01 +00:00
|
|
|
_space = ord(" ")
|
|
|
|
|
2022-03-07 15:38:29 +00:00
|
|
|
|
2022-03-15 18:01:57 +00:00
|
|
|
class TildeEncoder(dict):
|
2022-03-07 15:38:29 +00:00
|
|
|
# Keeps a cache internally, via __missing__
|
|
|
|
def __missing__(self, b):
|
|
|
|
# Handle a cache miss, store encoded string in cache and return.
|
2022-04-06 15:55:01 +00:00
|
|
|
if b in _TILDE_ENCODING_SAFE:
|
|
|
|
res = chr(b)
|
|
|
|
elif b == _space:
|
|
|
|
res = "+"
|
|
|
|
else:
|
|
|
|
res = "~{:02X}".format(b)
|
2022-03-07 15:38:29 +00:00
|
|
|
self[b] = res
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
2022-03-15 18:01:57 +00:00
|
|
|
_tilde_encoder = TildeEncoder().__getitem__
|
2022-03-07 15:38:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
@documented
|
2022-03-15 18:01:57 +00:00
|
|
|
def tilde_encode(s: str) -> str:
|
|
|
|
"Returns tilde-encoded string - for example ``/foo/bar`` -> ``~2Ffoo~2Fbar``"
|
|
|
|
return "".join(_tilde_encoder(char) for char in s.encode("utf-8"))
|
2022-03-07 15:38:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
@documented
|
2022-03-15 18:01:57 +00:00
|
|
|
def tilde_decode(s: str) -> str:
|
|
|
|
"Decodes a tilde-encoded string, so ``~2Ffoo~2Fbar`` -> ``/foo/bar``"
|
|
|
|
# Avoid accidentally decoding a %2f style sequence
|
|
|
|
temp = secrets.token_hex(16)
|
|
|
|
s = s.replace("%", temp)
|
2022-04-06 15:55:01 +00:00
|
|
|
decoded = urllib.parse.unquote_plus(s.replace("~", "%"))
|
2022-03-15 18:01:57 +00:00
|
|
|
return decoded.replace(temp, "%")
|
2022-03-19 04:03:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
def resolve_routes(routes, path):
|
|
|
|
for regex, view in routes:
|
|
|
|
match = regex.match(path)
|
|
|
|
if match is not None:
|
|
|
|
return match, view
|
|
|
|
return None, None
|