-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap-sample-env.py
More file actions
91 lines (79 loc) · 2.5 KB
/
Copy pathbootstrap-sample-env.py
File metadata and controls
91 lines (79 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""Bootstrap the Notion objects referenced by the sample documentation.
The sample documentation links to and mentions a Notion page, user and
database. These only exist in a particular workspace. This script creates the
page and database in a workspace of your choice, picks a user to mention, and
writes their IDs to an environment file (``sample.env`` by default) that
``sample/conf.py`` reads when building the sample.
Run it with the integration token in the environment::
export NOTION_TOKEN="your_integration_token_here"
uv run --all-extras python bootstrap-sample-env.py \
--parent-page-id parent_page_id
"""
from pathlib import Path
import click
import cloup
from beartype import beartype
from ultimate_notion import Session
@beartype
def _env_file_contents(
*,
page_id: str,
user_id: str,
database_id: str,
) -> str:
"""Render the environment file contents for the given IDs."""
lines = [
"# Generated by bootstrap-sample-env.py.",
f"NOTION_SAMPLE_PAGE_ID={page_id}",
f"NOTION_SAMPLE_USER_ID={user_id}",
f"NOTION_SAMPLE_DATABASE_ID={database_id}",
]
return "\n".join(lines) + "\n"
@cloup.command()
@cloup.option(
"--parent-page-id",
help="ID of a page (shared with the integration) to create objects under",
required=True,
)
@cloup.option(
"--output",
help="Path to the environment file to write",
default="sample.env",
show_default=True,
type=cloup.Path(path_type=Path, file_okay=True, dir_okay=False),
)
@beartype
def main(
*,
parent_page_id: str,
output: Path,
) -> None:
"""Create the sample's Notion objects and write their IDs to a
file.
"""
session = Session()
try:
parent = session.get_page(page_ref=parent_page_id)
page = session.create_page(
parent=parent,
title="Sphinx-Notionbuilder sample linked page",
)
database = session.create_ds(
parent=parent,
title="Sphinx-Notionbuilder sample database",
inline=True,
)
people = [user for user in session.all_users() if user.is_person]
user = people[0] if people else session.whoami()
user_id = str(object=user.id)
finally:
session.close()
contents = _env_file_contents(
page_id=page.id.hex,
user_id=user_id,
database_id=database.id.hex,
)
output.write_text(data=contents, encoding="utf-8")
click.echo(message=f"Wrote {output}")
if __name__ == "__main__":
main()