Skip to content

Commit

Permalink
Upgrade to Black 20.8b1, closes #958
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Sep 2, 2020
1 parent 26b2922 commit a648bb8
Show file tree
Hide file tree
Showing 22 changed files with 203 additions and 58 deletions.
34 changes: 25 additions & 9 deletions datasette/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,9 @@ def app_css_hash(self):
async def get_canned_queries(self, database_name, actor):
queries = self.metadata("queries", database=database_name, fallback=False) or {}
for more_queries in pm.hook.canned_queries(
datasette=self, database=database_name, actor=actor,
datasette=self,
database=database_name,
actor=actor,
):
more_queries = await await_me_maybe(more_queries)
queries.update(more_queries or {})
Expand Down Expand Up @@ -468,7 +470,10 @@ async def permission_allowed(self, actor, action, resource=None, default=False):
"Check permissions using the permissions_allowed plugin hook"
result = None
for check in pm.hook.permission_allowed(
datasette=self, actor=actor, action=action, resource=resource,
datasette=self,
actor=actor,
action=action,
resource=resource,
):
check = await await_me_maybe(check)
if check is not None:
Expand Down Expand Up @@ -861,22 +866,28 @@ def add_route(view, regex):
r"/-/actor(?P<as_format>(\.json)?)$",
)
add_route(
AuthTokenView.as_view(self), r"/-/auth-token$",
AuthTokenView.as_view(self),
r"/-/auth-token$",
)
add_route(
LogoutView.as_view(self), r"/-/logout$",
LogoutView.as_view(self),
r"/-/logout$",
)
add_route(
PermissionsDebugView.as_view(self), r"/-/permissions$",
PermissionsDebugView.as_view(self),
r"/-/permissions$",
)
add_route(
MessagesDebugView.as_view(self), r"/-/messages$",
MessagesDebugView.as_view(self),
r"/-/messages$",
)
add_route(
AllowDebugView.as_view(self), r"/-/allow-debug$",
AllowDebugView.as_view(self),
r"/-/allow-debug$",
)
add_route(
PatternPortfolioView.as_view(self), r"/-/patterns$",
PatternPortfolioView.as_view(self),
r"/-/patterns$",
)
add_route(
DatabaseDownload.as_view(self), r"/(?P<db_name>[^/]+?)(?P<as_db>\.db)$"
Expand Down Expand Up @@ -1079,7 +1090,12 @@ async def handle_500(self, request, send, exception):
if status != 500:
templates = ["{}.html".format(status)] + templates
info.update(
{"ok": False, "error": message, "status": status, "title": title,}
{
"ok": False,
"error": message,
"status": status,
"title": title,
}
)
headers = {}
if self.ds.cors:
Expand Down
8 changes: 6 additions & 2 deletions datasette/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,10 @@ def plugins(all, plugins_dir):
default=lambda: os.urandom(32).hex(),
)
@click.option(
"-p", "--port", default=8001, help="Port to run the server on, defaults to 8001",
"-p",
"--port",
default=8001,
help="Port to run the server on, defaults to 8001",
)
@click.option("--title", help="Title for metadata")
@click.option("--license", help="License label for metadata")
Expand Down Expand Up @@ -344,7 +347,8 @@ def uninstall(packages, yes):
is_flag=True,
)
@click.option(
"--get", help="Run an HTTP GET request against this path, print results and exit",
"--get",
help="Run an HTTP GET request against this path, print results and exit",
)
@click.option("--version-note", help="Additional note to show on /-/versions")
@click.option("--help-config", is_flag=True, help="Show available config options")
Expand Down
18 changes: 13 additions & 5 deletions datasette/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ def urlsafe_components(token):


def path_from_row_pks(row, pks, use_rowid, quote=True):
""" Generate an optionally URL-quoted unique identifier
for a row from its primary keys."""
"""Generate an optionally URL-quoted unique identifier
for a row from its primary keys."""
if use_rowid:
bits = [row["rowid"]]
else:
Expand Down Expand Up @@ -839,7 +839,9 @@ def check_connection(conn):
]
for table in tables:
try:
conn.execute("PRAGMA table_info({});".format(escape_sqlite(table)),)
conn.execute(
"PRAGMA table_info({});".format(escape_sqlite(table)),
)
except sqlite3.OperationalError as e:
if e.args[0] == "no such module: VirtualSpatialIndex":
raise SpatialiteConnectionProblem(e)
Expand Down Expand Up @@ -915,12 +917,18 @@ def actor_matches_allow(actor, allow):
async def check_visibility(datasette, actor, action, resource, default=True):
"Returns (visible, private) - visible = can you see it, private = can others see it too"
visible = await datasette.permission_allowed(
actor, action, resource=resource, default=default,
actor,
action,
resource=resource,
default=default,
)
if not visible:
return (False, False)
private = not await datasette.permission_allowed(
None, action, resource=resource, default=default,
None,
action,
resource=resource,
default=default,
)
return visible, private

Expand Down
16 changes: 11 additions & 5 deletions datasette/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ async def head(self, *args, **kwargs):

async def check_permission(self, request, action, resource=None):
ok = await self.ds.permission_allowed(
request.actor, action, resource=resource, default=True,
request.actor,
action,
resource=resource,
default=True,
)
if not ok:
raise Forbidden(action)
Expand All @@ -85,7 +88,10 @@ async def check_permissions(self, request, permissions):
repr(permission)
)
ok = await self.ds.permission_allowed(
request.actor, action, resource=resource, default=None,
request.actor,
action,
resource=resource,
default=None,
)
if ok is not None:
if ok:
Expand Down Expand Up @@ -343,10 +349,10 @@ async def stream_fn(r):
return AsgiStream(stream_fn, headers=headers, content_type=content_type)

async def get_format(self, request, database, args):
""" Determine the format of the response from the request, from URL
parameters or from a file extension.
"""Determine the format of the response from the request, from URL
parameters or from a file extension.
`args` is a dict of the path components parsed from the URL by the router.
`args` is a dict of the path components parsed from the URL by the router.
"""
# If ?_format= is provided, use that as the format
_format = request.args.get("_format", None)
Expand Down
26 changes: 21 additions & 5 deletions datasette/views/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ class DatabaseView(DataView):

async def data(self, request, database, hash, default_labels=False, _size=None):
await self.check_permissions(
request, [("view-database", database), "view-instance",],
request,
[
("view-database", database),
"view-instance",
],
)
metadata = (self.ds.metadata("databases") or {}).get(database, {})
self.ds.update_with_inherited_metadata(metadata)
Expand All @@ -42,17 +46,26 @@ async def data(self, request, database, hash, default_labels=False, _size=None):
views = []
for view_name in await db.view_names():
visible, private = await check_visibility(
self.ds, request.actor, "view-table", (database, view_name),
self.ds,
request.actor,
"view-table",
(database, view_name),
)
if visible:
views.append(
{"name": view_name, "private": private,}
{
"name": view_name,
"private": private,
}
)

tables = []
for table in table_counts:
visible, private = await check_visibility(
self.ds, request.actor, "view-table", (database, table),
self.ds,
request.actor,
"view-table",
(database, table),
)
if not visible:
continue
Expand All @@ -76,7 +89,10 @@ async def data(self, request, database, hash, default_labels=False, _size=None):
await self.ds.get_canned_queries(database, request.actor)
).values():
visible, private = await check_visibility(
self.ds, request.actor, "view-query", (database, query["name"]),
self.ds,
request.actor,
"view-query",
(database, query["name"]),
)
if visible:
canned_queries.append(dict(query, private=private))
Expand Down
15 changes: 12 additions & 3 deletions datasette/views/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ async def get(self, request, as_format):
databases = []
for name, db in self.ds.databases.items():
visible, database_private = await check_visibility(
self.ds, request.actor, "view-database", name,
self.ds,
request.actor,
"view-database",
name,
)
if not visible:
continue
Expand All @@ -36,7 +39,10 @@ async def get(self, request, as_format):
views = []
for view_name in await db.view_names():
visible, private = await check_visibility(
self.ds, request.actor, "view-table", (name, view_name),
self.ds,
request.actor,
"view-table",
(name, view_name),
)
if visible:
views.append({"name": view_name, "private": private})
Expand All @@ -52,7 +58,10 @@ async def get(self, request, as_format):
tables = {}
for table in table_names:
visible, private = await check_visibility(
self.ds, request.actor, "view-table", (name, table),
self.ds,
request.actor,
"view-table",
(name, table),
)
if not visible:
continue
Expand Down
6 changes: 5 additions & 1 deletion datasette/views/special.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ def __init__(self, datasette):
async def get(self, request):
if not request.actor:
return Response.redirect("/")
return await self.render(["logout.html"], request, {"actor": request.actor},)
return await self.render(
["logout.html"],
request,
{"actor": request.actor},
)

async def post(self, request):
response = Response.redirect("/")
Expand Down
5 changes: 4 additions & 1 deletion datasette/views/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,10 @@ async def data(
# Add _where= from querystring
if "_where" in request.args:
if not await self.ds.permission_allowed(
request.actor, "execute-sql", resource=database, default=True,
request.actor,
"execute-sql",
resource=database,
default=True,
):
raise DatasetteError("_where= is not allowed", status=403)
else:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def get_version():
"pytest>=5.2.2,<6.1.0",
"pytest-asyncio>=0.10,<0.15",
"beautifulsoup4>=4.8.1,<4.10.0",
"black~=19.10b0",
"black==20.8b1",
"pytest-timeout>=1.4.2,<1.5",
],
},
Expand Down
4 changes: 3 additions & 1 deletion tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,9 @@ def assert_permissions_checked(datasette, actions):
], """Missing expected permission check: action={}, resource={}
Permission checks seen: {}
""".format(
action, resource, json.dumps(list(datasette._permission_checks), indent=4),
action,
resource,
json.dumps(list(datasette._permission_checks), indent=4),
)


Expand Down
13 changes: 10 additions & 3 deletions tests/plugins/my_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ async def inner():
@hookimpl
def extra_js_urls():
return [
{"url": "https://plugin-example.com/jquery.js", "sri": "SRIHASH",},
{
"url": "https://plugin-example.com/jquery.js",
"sri": "SRIHASH",
},
"https://plugin-example.com/plugin1.js",
]

Expand All @@ -73,7 +76,9 @@ async def inner():
"database": database,
"table": table,
"config": datasette.plugin_config(
"name-of-plugin", database=database, table=table,
"name-of-plugin",
database=database,
table=table,
),
"view_name": view_name,
"request_path": request.path if request is not None else None,
Expand All @@ -99,7 +104,9 @@ def render_cell(value, column, table, database, datasette):
"table": table,
"database": database,
"config": datasette.plugin_config(
"name-of-plugin", database=database, table=table,
"name-of-plugin",
database=database,
table=table,
),
}
)
Expand Down
5 changes: 4 additions & 1 deletion tests/plugins/my_plugin_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
@hookimpl
def extra_js_urls():
return [
{"url": "https://plugin-example.com/jquery.js", "sri": "SRIHASH",},
{
"url": "https://plugin-example.com/jquery.js",
"sri": "SRIHASH",
},
"https://plugin-example.com/plugin2.js",
]

Expand Down
6 changes: 5 additions & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1786,7 +1786,11 @@ def test_null_foreign_keys_are_not_expanded(app_client):
"foreign_key_with_label": {"value": "1", "label": "hello"},
"foreign_key_with_no_label": {"value": "1", "label": "1"},
},
{"pk": "2", "foreign_key_with_label": None, "foreign_key_with_no_label": None,},
{
"pk": "2",
"foreign_key_with_label": None,
"foreign_key_with_no_label": None,
},
] == response.json


Expand Down
Loading

0 comments on commit a648bb8

Please sign in to comment.