mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Renamed datasette.config() to .setting(), closes #1107
This commit is contained in:
parent
5a77f7a649
commit
f2e2bfcdd9
10 changed files with 86 additions and 60 deletions
|
|
@ -264,15 +264,15 @@ class Datasette:
|
|||
raise StartupError("config.json should be renamed to settings.json")
|
||||
if config_dir and (config_dir / "settings.json").exists() and not config:
|
||||
config = json.load((config_dir / "settings.json").open())
|
||||
self._config = dict(DEFAULT_SETTINGS, **(config or {}))
|
||||
self._settings = dict(DEFAULT_SETTINGS, **(config or {}))
|
||||
self.renderers = {} # File extension -> (renderer, can_render) functions
|
||||
self.version_note = version_note
|
||||
self.executor = futures.ThreadPoolExecutor(
|
||||
max_workers=self.config("num_sql_threads")
|
||||
max_workers=self.setting("num_sql_threads")
|
||||
)
|
||||
self.max_returned_rows = self.config("max_returned_rows")
|
||||
self.sql_time_limit_ms = self.config("sql_time_limit_ms")
|
||||
self.page_size = self.config("default_page_size")
|
||||
self.max_returned_rows = self.setting("max_returned_rows")
|
||||
self.sql_time_limit_ms = self.setting("sql_time_limit_ms")
|
||||
self.page_size = self.setting("default_page_size")
|
||||
# Execute plugins in constructor, to ensure they are available
|
||||
# when the rest of `datasette inspect` executes
|
||||
if self.plugins_dir:
|
||||
|
|
@ -347,12 +347,12 @@ class Datasette:
|
|||
def remove_database(self, name):
|
||||
self.databases.pop(name)
|
||||
|
||||
def config(self, key):
|
||||
return self._config.get(key, None)
|
||||
def setting(self, key):
|
||||
return self._settings.get(key, None)
|
||||
|
||||
def config_dict(self):
|
||||
# Returns a fully resolved config dictionary, useful for templates
|
||||
return {option.name: self.config(option.name) for option in SETTINGS}
|
||||
return {option.name: self.setting(option.name) for option in SETTINGS}
|
||||
|
||||
def metadata(self, key=None, database=None, table=None, fallback=True):
|
||||
"""
|
||||
|
|
@ -454,8 +454,8 @@ class Datasette:
|
|||
conn.enable_load_extension(True)
|
||||
for extension in self.sqlite_extensions:
|
||||
conn.execute(f"SELECT load_extension('{extension}')")
|
||||
if self.config("cache_size_kb"):
|
||||
conn.execute(f"PRAGMA cache_size=-{self.config('cache_size_kb')}")
|
||||
if self.setting("cache_size_kb"):
|
||||
conn.execute(f"PRAGMA cache_size=-{self.setting('cache_size_kb')}")
|
||||
# pylint: disable=no-member
|
||||
pm.hook.prepare_connection(conn=conn, database=database, datasette=self)
|
||||
|
||||
|
|
@ -567,7 +567,7 @@ class Datasette:
|
|||
|
||||
def absolute_url(self, request, path):
|
||||
url = urllib.parse.urljoin(request.url, path)
|
||||
if url.startswith("http://") and self.config("force_https_urls"):
|
||||
if url.startswith("http://") and self.setting("force_https_urls"):
|
||||
url = "https://" + url[len("http://") :]
|
||||
return url
|
||||
|
||||
|
|
@ -781,12 +781,12 @@ class Datasette:
|
|||
"extra_js_urls": await self._asset_urls(
|
||||
"extra_js_urls", template, context, request, view_name
|
||||
),
|
||||
"base_url": self.config("base_url"),
|
||||
"base_url": self.setting("base_url"),
|
||||
"csrftoken": request.scope["csrftoken"] if request else lambda: "",
|
||||
},
|
||||
**extra_template_vars,
|
||||
}
|
||||
if request and request.args.get("_context") and self.config("template_debug"):
|
||||
if request and request.args.get("_context") and self.setting("template_debug"):
|
||||
return "<pre>{}</pre>".format(
|
||||
jinja2.escape(json.dumps(template_context, default=repr, indent=4))
|
||||
)
|
||||
|
|
@ -882,7 +882,7 @@ class Datasette:
|
|||
r"/-/plugins(?P<as_format>(\.json)?)$",
|
||||
)
|
||||
add_route(
|
||||
JsonDataView.as_view(self, "settings.json", lambda: self._config),
|
||||
JsonDataView.as_view(self, "settings.json", lambda: self._settings),
|
||||
r"/-/settings(?P<as_format>(\.json)?)$",
|
||||
)
|
||||
add_route(
|
||||
|
|
@ -1001,7 +1001,7 @@ class DatasetteRouter:
|
|||
|
||||
async def route_path(self, scope, receive, send, path):
|
||||
# Strip off base_url if present before routing
|
||||
base_url = self.ds.config("base_url")
|
||||
base_url = self.ds.setting("base_url")
|
||||
if base_url != "/" and path.startswith(base_url):
|
||||
path = "/" + path[len(base_url) :]
|
||||
request = Request(scope, receive)
|
||||
|
|
@ -1016,7 +1016,7 @@ class DatasetteRouter:
|
|||
scope_modifications = {}
|
||||
# Apply force_https_urls, if set
|
||||
if (
|
||||
self.ds.config("force_https_urls")
|
||||
self.ds.setting("force_https_urls")
|
||||
and scope["type"] == "http"
|
||||
and scope.get("scheme") != "https"
|
||||
):
|
||||
|
|
|
|||
|
|
@ -136,7 +136,7 @@ class ColumnFacet(Facet):
|
|||
async def suggest(self):
|
||||
row_count = await self.get_row_count()
|
||||
columns = await self.get_columns(self.sql, self.params)
|
||||
facet_size = self.ds.config("default_facet_size")
|
||||
facet_size = self.ds.setting("default_facet_size")
|
||||
suggested_facets = []
|
||||
already_enabled = [c["config"]["simple"] for c in self.get_configs()]
|
||||
for column in columns:
|
||||
|
|
@ -158,7 +158,7 @@ class ColumnFacet(Facet):
|
|||
suggested_facet_sql,
|
||||
self.params,
|
||||
truncate=False,
|
||||
custom_time_limit=self.ds.config("facet_suggest_time_limit_ms"),
|
||||
custom_time_limit=self.ds.setting("facet_suggest_time_limit_ms"),
|
||||
)
|
||||
num_distinct_values = len(distinct_values)
|
||||
if (
|
||||
|
|
@ -188,7 +188,7 @@ class ColumnFacet(Facet):
|
|||
|
||||
qs_pairs = self.get_querystring_pairs()
|
||||
|
||||
facet_size = self.ds.config("default_facet_size")
|
||||
facet_size = self.ds.setting("default_facet_size")
|
||||
for source_and_config in self.get_configs():
|
||||
config = source_and_config["config"]
|
||||
source = source_and_config["source"]
|
||||
|
|
@ -208,7 +208,7 @@ class ColumnFacet(Facet):
|
|||
facet_sql,
|
||||
self.params,
|
||||
truncate=False,
|
||||
custom_time_limit=self.ds.config("facet_time_limit_ms"),
|
||||
custom_time_limit=self.ds.setting("facet_time_limit_ms"),
|
||||
)
|
||||
facet_results_values = []
|
||||
facet_results[column] = {
|
||||
|
|
@ -290,7 +290,7 @@ class ArrayFacet(Facet):
|
|||
suggested_facet_sql,
|
||||
self.params,
|
||||
truncate=False,
|
||||
custom_time_limit=self.ds.config("facet_suggest_time_limit_ms"),
|
||||
custom_time_limit=self.ds.setting("facet_suggest_time_limit_ms"),
|
||||
log_sql_errors=False,
|
||||
)
|
||||
types = tuple(r[0] for r in results.rows)
|
||||
|
|
@ -305,7 +305,7 @@ class ArrayFacet(Facet):
|
|||
),
|
||||
self.params,
|
||||
truncate=False,
|
||||
custom_time_limit=self.ds.config(
|
||||
custom_time_limit=self.ds.setting(
|
||||
"facet_suggest_time_limit_ms"
|
||||
),
|
||||
log_sql_errors=False,
|
||||
|
|
@ -335,7 +335,7 @@ class ArrayFacet(Facet):
|
|||
facet_results = {}
|
||||
facets_timed_out = []
|
||||
|
||||
facet_size = self.ds.config("default_facet_size")
|
||||
facet_size = self.ds.setting("default_facet_size")
|
||||
for source_and_config in self.get_configs():
|
||||
config = source_and_config["config"]
|
||||
source = source_and_config["source"]
|
||||
|
|
@ -354,7 +354,7 @@ class ArrayFacet(Facet):
|
|||
facet_sql,
|
||||
self.params,
|
||||
truncate=False,
|
||||
custom_time_limit=self.ds.config("facet_time_limit_ms"),
|
||||
custom_time_limit=self.ds.setting("facet_time_limit_ms"),
|
||||
)
|
||||
facet_results_values = []
|
||||
facet_results[column] = {
|
||||
|
|
@ -421,7 +421,7 @@ class DateFacet(Facet):
|
|||
suggested_facet_sql,
|
||||
self.params,
|
||||
truncate=False,
|
||||
custom_time_limit=self.ds.config("facet_suggest_time_limit_ms"),
|
||||
custom_time_limit=self.ds.setting("facet_suggest_time_limit_ms"),
|
||||
log_sql_errors=False,
|
||||
)
|
||||
values = tuple(r[0] for r in results.rows)
|
||||
|
|
@ -446,7 +446,7 @@ class DateFacet(Facet):
|
|||
facet_results = {}
|
||||
facets_timed_out = []
|
||||
args = dict(self.get_querystring_pairs())
|
||||
facet_size = self.ds.config("default_facet_size")
|
||||
facet_size = self.ds.setting("default_facet_size")
|
||||
for source_and_config in self.get_configs():
|
||||
config = source_and_config["config"]
|
||||
source = source_and_config["source"]
|
||||
|
|
@ -467,7 +467,7 @@ class DateFacet(Facet):
|
|||
facet_sql,
|
||||
self.params,
|
||||
truncate=False,
|
||||
custom_time_limit=self.ds.config("facet_time_limit_ms"),
|
||||
custom_time_limit=self.ds.setting("facet_time_limit_ms"),
|
||||
)
|
||||
facet_results_values = []
|
||||
facet_results[column] = {
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ class Urls:
|
|||
if not isinstance(path, PrefixedUrlString):
|
||||
if path.startswith("/"):
|
||||
path = path[1:]
|
||||
path = self.ds.config("base_url") + path
|
||||
path = self.ds.setting("base_url") + path
|
||||
if format is not None:
|
||||
path = path_with_format(path=path, format=format)
|
||||
return PrefixedUrlString(path)
|
||||
|
|
@ -29,7 +29,7 @@ class Urls:
|
|||
|
||||
def database(self, database, format=None):
|
||||
db = self.ds.databases[database]
|
||||
if self.ds.config("hash_urls") and db.hash:
|
||||
if self.ds.setting("hash_urls") and db.hash:
|
||||
path = self.path(f"{database}-{db.hash[:HASH_LENGTH]}", format=format)
|
||||
else:
|
||||
path = self.path(database, format=format)
|
||||
|
|
|
|||
|
|
@ -230,7 +230,7 @@ class DataView(BaseView):
|
|||
should_redirect += kwargs["as_db"]
|
||||
|
||||
if (
|
||||
(self.ds.config("hash_urls") or "_hash" in request.args)
|
||||
(self.ds.setting("hash_urls") or "_hash" in request.args)
|
||||
and
|
||||
# Redirect only if database is immutable
|
||||
not self.ds.databases[name].is_mutable
|
||||
|
|
@ -260,7 +260,7 @@ class DataView(BaseView):
|
|||
stream = request.args.get("_stream")
|
||||
if stream:
|
||||
# Some quick sanity checks
|
||||
if not self.ds.config("allow_csv_stream"):
|
||||
if not self.ds.setting("allow_csv_stream"):
|
||||
raise BadRequest("CSV streaming is disabled")
|
||||
if request.args.get("_next"):
|
||||
raise BadRequest("_next not allowed for CSV streaming")
|
||||
|
|
@ -296,7 +296,7 @@ class DataView(BaseView):
|
|||
|
||||
async def stream_fn(r):
|
||||
nonlocal data
|
||||
writer = csv.writer(LimitedWriter(r, self.ds.config("max_csv_mb")))
|
||||
writer = csv.writer(LimitedWriter(r, self.ds.setting("max_csv_mb")))
|
||||
first = True
|
||||
next = None
|
||||
while first or (next and stream):
|
||||
|
|
@ -566,9 +566,9 @@ class DataView(BaseView):
|
|||
ttl = request.args.get("_ttl", None)
|
||||
if ttl is None or not ttl.isdigit():
|
||||
if correct_hash_provided:
|
||||
ttl = self.ds.config("default_cache_ttl_hashed")
|
||||
ttl = self.ds.setting("default_cache_ttl_hashed")
|
||||
else:
|
||||
ttl = self.ds.config("default_cache_ttl")
|
||||
ttl = self.ds.setting("default_cache_ttl")
|
||||
|
||||
return self.set_response_headers(r, ttl)
|
||||
|
||||
|
|
|
|||
|
|
@ -136,7 +136,7 @@ class DatabaseView(DataView):
|
|||
"show_hidden": request.args.get("_show_hidden"),
|
||||
"editable": True,
|
||||
"metadata": metadata,
|
||||
"allow_download": self.ds.config("allow_download")
|
||||
"allow_download": self.ds.setting("allow_download")
|
||||
and not db.is_mutable
|
||||
and database != ":memory:",
|
||||
},
|
||||
|
|
@ -161,7 +161,7 @@ class DatabaseDownload(DataView):
|
|||
db = self.ds.databases[database]
|
||||
if db.is_memory:
|
||||
raise DatasetteError("Cannot download :memory: database", status=404)
|
||||
if not self.ds.config("allow_download") or db.is_mutable:
|
||||
if not self.ds.setting("allow_download") or db.is_mutable:
|
||||
raise Forbidden("Database download is forbidden")
|
||||
if not db.path:
|
||||
raise DatasetteError("Cannot download database", status=404)
|
||||
|
|
|
|||
|
|
@ -121,7 +121,7 @@ class RowTableShared(DataView):
|
|||
}
|
||||
|
||||
cell_rows = []
|
||||
base_url = self.ds.config("base_url")
|
||||
base_url = self.ds.setting("base_url")
|
||||
for row in rows:
|
||||
cells = []
|
||||
# Unless we are a view, the first column is a link - either to the rowid
|
||||
|
|
@ -654,7 +654,7 @@ class TableView(RowTableShared):
|
|||
pass
|
||||
|
||||
# facets support
|
||||
if not self.ds.config("allow_facet") and any(
|
||||
if not self.ds.setting("allow_facet") and any(
|
||||
arg.startswith("_facet") for arg in request.args
|
||||
):
|
||||
raise BadRequest("_facet= is not allowed")
|
||||
|
|
@ -772,8 +772,8 @@ class TableView(RowTableShared):
|
|||
suggested_facets = []
|
||||
|
||||
if (
|
||||
self.ds.config("suggest_facets")
|
||||
and self.ds.config("allow_facet")
|
||||
self.ds.setting("suggest_facets")
|
||||
and self.ds.setting("allow_facet")
|
||||
and not _next
|
||||
):
|
||||
for facet in facet_instances:
|
||||
|
|
@ -801,7 +801,7 @@ class TableView(RowTableShared):
|
|||
results.description,
|
||||
rows,
|
||||
link_column=not is_view,
|
||||
truncate_cells=self.ds.config("truncate_cells_html"),
|
||||
truncate_cells=self.ds.setting("truncate_cells_html"),
|
||||
)
|
||||
metadata = (
|
||||
(self.ds.metadata("databases") or {})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue