Skip to content

Commit 92b2667

Browse files
committed
Fix blacken-docs errors and warnings, refs #1718
1 parent 3657363 commit 92b2667

File tree

4 files changed

+289
-141
lines changed

4 files changed

+289
-141
lines changed

docs/authentication.rst

+14-11
Original file line numberDiff line numberDiff line change
@@ -381,11 +381,10 @@ Authentication plugins can set signed ``ds_actor`` cookies themselves like so:
381381
.. code-block:: python
382382
383383
response = Response.redirect("/")
384-
response.set_cookie("ds_actor", datasette.sign({
385-
"a": {
386-
"id": "cleopaws"
387-
}
388-
}, "actor"))
384+
response.set_cookie(
385+
"ds_actor",
386+
datasette.sign({"a": {"id": "cleopaws"}}, "actor"),
387+
)
389388
390389
Note that you need to pass ``"actor"`` as the namespace to :ref:`datasette_sign`.
391390

@@ -412,12 +411,16 @@ To include an expiry, add a ``"e"`` key to the cookie value containing a `base62
412411
expires_at = int(time.time()) + (24 * 60 * 60)
413412
414413
response = Response.redirect("/")
415-
response.set_cookie("ds_actor", datasette.sign({
416-
"a": {
417-
"id": "cleopaws"
418-
},
419-
"e": baseconv.base62.encode(expires_at),
420-
}, "actor"))
414+
response.set_cookie(
415+
"ds_actor",
416+
datasette.sign(
417+
{
418+
"a": {"id": "cleopaws"},
419+
"e": baseconv.base62.encode(expires_at),
420+
},
421+
"actor",
422+
),
423+
)
421424
422425
The resulting cookie will encode data that looks something like this:
423426

docs/internals.rst

+65-33
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ And a class method that can be used to create fake request objects for use in te
7070
from datasette import Request
7171
from pprint import pprint
7272
73-
request = Request.fake("/fixtures/facetable/", url_vars={
74-
"database": "fixtures",
75-
"table": "facetable"
76-
})
73+
request = Request.fake(
74+
"/fixtures/facetable/",
75+
url_vars={"database": "fixtures", "table": "facetable"},
76+
)
7777
pprint(request.scope)
7878
7979
This outputs::
@@ -146,7 +146,7 @@ For example:
146146
147147
response = Response(
148148
"<xml>This is XML</xml>",
149-
content_type="application/xml; charset=utf-8"
149+
content_type="application/xml; charset=utf-8",
150150
)
151151
152152
The quickest way to create responses is using the ``Response.text(...)``, ``Response.html(...)``, ``Response.json(...)`` or ``Response.redirect(...)`` helper methods:
@@ -157,9 +157,13 @@ The quickest way to create responses is using the ``Response.text(...)``, ``Resp
157157
158158
html_response = Response.html("This is HTML")
159159
json_response = Response.json({"this_is": "json"})
160-
text_response = Response.text("This will become utf-8 encoded text")
160+
text_response = Response.text(
161+
"This will become utf-8 encoded text"
162+
)
161163
# Redirects are served as 302, unless you pass status=301:
162-
redirect_response = Response.redirect("https://latest.datasette.io/")
164+
redirect_response = Response.redirect(
165+
"https://latest.datasette.io/"
166+
)
163167
164168
Each of these responses will use the correct corresponding content-type - ``text/html; charset=utf-8``, ``application/json; charset=utf-8`` or ``text/plain; charset=utf-8`` respectively.
165169

@@ -207,13 +211,17 @@ To set cookies on the response, use the ``response.set_cookie(...)`` method. The
207211
httponly=False,
208212
samesite="lax",
209213
):
214+
...
210215
211216
You can use this with :ref:`datasette.sign() <datasette_sign>` to set signed cookies. Here's how you would set the :ref:`ds_actor cookie <authentication_ds_actor>` for use with Datasette :ref:`authentication <authentication>`:
212217

213218
.. code-block:: python
214219
215220
response = Response.redirect("/")
216-
response.set_cookie("ds_actor", datasette.sign({"a": {"id": "cleopaws"}}, "actor"))
221+
response.set_cookie(
222+
"ds_actor",
223+
datasette.sign({"a": {"id": "cleopaws"}}, "actor"),
224+
)
217225
return response
218226
219227
.. _internals_datasette:
@@ -236,13 +244,16 @@ You can create your own instance of this - for example to help write tests for a
236244
datasette = Datasette(files=["/path/to/my-database.db"])
237245
238246
# Pass metadata as a JSON dictionary like this
239-
datasette = Datasette(files=["/path/to/my-database.db"], metadata={
240-
"databases": {
241-
"my-database": {
242-
"description": "This is my database"
247+
datasette = Datasette(
248+
files=["/path/to/my-database.db"],
249+
metadata={
250+
"databases": {
251+
"my-database": {
252+
"description": "This is my database"
253+
}
243254
}
244-
}
245-
})
255+
},
256+
)
246257
247258
Constructor parameters include:
248259

@@ -345,7 +356,7 @@ This is useful when you need to check multiple permissions at once. For example,
345356
("view-table", (database, table)),
346357
("view-database", database),
347358
"view-instance",
348-
]
359+
],
349360
)
350361
351362
.. _datasette_check_visibilty:
@@ -406,20 +417,26 @@ The ``db`` parameter should be an instance of the ``datasette.database.Database`
406417
407418
from datasette.database import Database
408419
409-
datasette.add_database(Database(
410-
datasette,
411-
path="path/to/my-new-database.db",
412-
is_mutable=True
413-
))
420+
datasette.add_database(
421+
Database(
422+
datasette,
423+
path="path/to/my-new-database.db",
424+
is_mutable=True,
425+
)
426+
)
414427
415428
This will add a mutable database and serve it at ``/my-new-database``.
416429

417430
``.add_database()`` returns the Database instance, with its name set as the ``database.name`` attribute. Any time you are working with a newly added database you should use the return value of ``.add_database()``, for example:
418431

419432
.. code-block:: python
420433
421-
db = datasette.add_database(Database(datasette, memory_name="statistics"))
422-
await db.execute_write("CREATE TABLE foo(id integer primary key)")
434+
db = datasette.add_database(
435+
Database(datasette, memory_name="statistics")
436+
)
437+
await db.execute_write(
438+
"CREATE TABLE foo(id integer primary key)"
439+
)
423440
424441
.. _datasette_add_memory_database:
425442

@@ -438,10 +455,9 @@ This is a shortcut for the following:
438455
439456
from datasette.database import Database
440457
441-
datasette.add_database(Database(
442-
datasette,
443-
memory_name="statistics"
444-
))
458+
datasette.add_database(
459+
Database(datasette, memory_name="statistics")
460+
)
445461
446462
Using either of these pattern will result in the in-memory database being served at ``/statistics``.
447463

@@ -516,7 +532,9 @@ Returns the absolute URL for the given path, including the protocol and host. Fo
516532

517533
.. code-block:: python
518534
519-
absolute_url = datasette.absolute_url(request, "/dbname/table.json")
535+
absolute_url = datasette.absolute_url(
536+
request, "/dbname/table.json"
537+
)
520538
# Would return "http://localhost:8001/dbname/table.json"
521539
522540
The current request object is used to determine the hostname and protocol that should be used for the returned URL. The :ref:`setting_force_https_urls` configuration setting is taken into account.
@@ -578,7 +596,9 @@ These methods can be used with :ref:`internals_datasette_urls` - for example:
578596
579597
table_json = (
580598
await datasette.client.get(
581-
datasette.urls.table("fixtures", "facetable", format="json")
599+
datasette.urls.table(
600+
"fixtures", "facetable", format="json"
601+
)
582602
)
583603
).json()
584604
@@ -754,6 +774,7 @@ Example usage:
754774
"select sqlite_version()"
755775
).fetchall()[0][0]
756776
777+
757778
version = await db.execute_fn(get_version)
758779
759780
.. _database_execute_write:
@@ -789,7 +810,7 @@ Like ``execute_write()`` but uses the ``sqlite3`` `conn.executemany() <https://d
789810
790811
await db.execute_write_many(
791812
"insert into characters (id, name) values (?, ?)",
792-
[(1, "Melanie"), (2, "Selma"), (2, "Viktor")]
813+
[(1, "Melanie"), (2, "Selma"), (2, "Viktor")],
793814
)
794815
795816
.. _database_execute_write_fn:
@@ -811,10 +832,15 @@ For example:
811832
812833
def delete_and_return_count(conn):
813834
conn.execute("delete from some_table where id > 5")
814-
return conn.execute("select count(*) from some_table").fetchone()[0]
835+
return conn.execute(
836+
"select count(*) from some_table"
837+
).fetchone()[0]
838+
815839
816840
try:
817-
num_rows_left = await database.execute_write_fn(delete_and_return_count)
841+
num_rows_left = await database.execute_write_fn(
842+
delete_and_return_count
843+
)
818844
except Exception as e:
819845
print("An error occurred:", e)
820846
@@ -1021,6 +1047,7 @@ This example uses trace to record the start, end and duration of any HTTP GET re
10211047
from datasette.tracer import trace
10221048
import httpx
10231049
1050+
10241051
async def fetch_url(url):
10251052
with trace("fetch-url", url=url):
10261053
async with httpx.AsyncClient() as client:
@@ -1051,17 +1078,22 @@ This example uses the :ref:`register_routes() <plugin_register_routes>` plugin h
10511078
from datasette import hookimpl
10521079
from datasette import tracer
10531080
1081+
10541082
@hookimpl
10551083
def register_routes():
1056-
10571084
async def parallel_queries(datasette):
10581085
db = datasette.get_database()
10591086
with tracer.trace_child_tasks():
10601087
one, two = await asyncio.gather(
10611088
db.execute("select 1"),
10621089
db.execute("select 2"),
10631090
)
1064-
return Response.json({"one": one.single_value(), "two": two.single_value()})
1091+
return Response.json(
1092+
{
1093+
"one": one.single_value(),
1094+
"two": two.single_value(),
1095+
}
1096+
)
10651097
10661098
return [
10671099
(r"/parallel-queries$", parallel_queries),

docs/json_api.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ Most of the HTML pages served by Datasette provide a mechanism for discovering t
446446

447447
You can find this near the top of the source code of those pages, looking like this:
448448

449-
.. code-block:: python
449+
.. code-block:: html
450450

451451
<link rel="alternate"
452452
type="application/json+datasette"

0 commit comments

Comments
 (0)