Skip to content

Commit 0ae5bc5

Browse files
committed
Remove sha mapping which isn't working well
1 parent dcef547 commit 0ae5bc5

File tree

5 files changed

+0
-98
lines changed

5 files changed

+0
-98
lines changed

.github/workflows/publish-notion.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ jobs:
3434
--parent-type "page" \
3535
--file "./build-sample/index.json" \
3636
--title "Sphinx-Notionbuilder Sample" \
37-
--sha-mapping "./sample/notion-sha-mapping.json" \
3837
--icon "🐍"
3938
env:
4039
NOTION_TOKEN: ${{ secrets.NOTION_TOKEN }}

README.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,6 @@ Arguments:
299299
- ``--parent-id``: The ID of the parent page or database in Notion (must be shared with your integration)
300300
- ``--parent-type``: "page" or "database"
301301
- ``--title``: Title for the new page in Notion
302-
- ``--sha-mapping``: Optional JSON file mapping file SHAs to Notion block IDs for efficient file re-uploads (use one file per document). The command will update this file.
303302

304303
The command will create a new page if one with the given title doesn't exist, or update the existing page if one with the given title already exists.
305304

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ dependencies = [
3535
"beartype>=0.21.0",
3636
"click>=8.0.0",
3737
"docutils>=0.21",
38-
"notion-client>=2.5.0",
3938
"sphinx>=8.2.3",
4039
"sphinx-immaterial>=0.13.7",
4140
"sphinx-simplepdf>=1.6.0",

sample/notion-sha-mapping.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/_notion_scripts/upload.py

Lines changed: 0 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import click
1818
from beartype import beartype
19-
from notion_client.errors import APIResponseError
2019
from ultimate_notion import Emoji, Session
2120
from ultimate_notion.blocks import PDF as UnoPDF # noqa: N811
2221
from ultimate_notion.blocks import Audio as UnoAudio
@@ -46,40 +45,11 @@ def _calculate_file_sha(*, file_path: Path) -> str:
4645
return sha256_hash.hexdigest()
4746

4847

49-
@beartype
50-
def _clean_deleted_blocks_from_mapping(
51-
*,
52-
sha_to_block_id: dict[str, str],
53-
session: Session,
54-
) -> dict[str, str]:
55-
"""Remove deleted blocks from ``SHA`` mapping.
56-
57-
Returns a new dictionary with only existing blocks.
58-
"""
59-
cleaned_mapping = sha_to_block_id.copy()
60-
deleted_block_shas: set[str] = set()
61-
62-
for sha, block_id_str in sha_to_block_id.items():
63-
block_id = UUID(hex=block_id_str)
64-
try:
65-
session.api.blocks.retrieve(block=block_id)
66-
except APIResponseError:
67-
deleted_block_shas.add(sha)
68-
msg = f"Block {block_id} does not exist, removing from SHA mapping"
69-
click.echo(message=msg)
70-
71-
for deleted_block_sha in deleted_block_shas:
72-
del cleaned_mapping[deleted_block_sha]
73-
74-
return cleaned_mapping
75-
76-
7748
@beartype
7849
def _find_last_matching_block_index(
7950
*,
8051
existing_blocks: list[Block] | tuple[Block, ...],
8152
local_blocks: list[Block],
82-
sha_to_block_id: dict[str, str],
8353
) -> int | None:
8454
"""Find the last index where existing blocks match local blocks.
8555
@@ -92,7 +62,6 @@ def _find_last_matching_block_index(
9262
_is_existing_equivalent(
9363
existing_page_block=existing_page_block,
9464
local_block=local_blocks[index],
95-
sha_to_block_id=sha_to_block_id,
9665
)
9766
):
9867
last_matching_index = index
@@ -215,20 +184,6 @@ class _ParentType(Enum):
215184
help="Icon of the page",
216185
required=False,
217186
)
218-
@click.option(
219-
"--sha-mapping",
220-
help=(
221-
"JSON file mapping file SHAs to Notion block IDs "
222-
"(use one file per document)"
223-
),
224-
required=False,
225-
type=click.Path(
226-
exists=True,
227-
path_type=Path,
228-
file_okay=True,
229-
dir_okay=False,
230-
),
231-
)
232187
@beartype
233188
def main(
234189
*,
@@ -237,23 +192,12 @@ def main(
237192
parent_type: _ParentType,
238193
title: str,
239194
icon: str | None = None,
240-
sha_mapping: Path | None = None,
241195
) -> None:
242196
"""
243197
Upload documentation to Notion.
244198
"""
245199
session = Session()
246200

247-
sha_mapping_content = (
248-
sha_mapping.read_text(encoding="utf-8") if sha_mapping else "{}"
249-
)
250-
sha_to_block_id: dict[str, str] = dict(json.loads(s=sha_mapping_content))
251-
252-
sha_to_block_id = _clean_deleted_blocks_from_mapping(
253-
sha_to_block_id=sha_to_block_id,
254-
session=session,
255-
)
256-
257201
blocks = json.loads(s=file.read_text(encoding="utf-8"))
258202

259203
parent: Page | Database
@@ -291,7 +235,6 @@ def main(
291235
last_matching_index = _find_last_matching_block_index(
292236
existing_blocks=page.children,
293237
local_blocks=block_objs,
294-
sha_to_block_id=sha_to_block_id,
295238
)
296239

297240
click.echo(
@@ -310,36 +253,4 @@ def main(
310253
]
311254
page.append(blocks=block_objs_to_upload)
312255

313-
if sha_mapping:
314-
for uploaded_block_index, uploaded_block in enumerate(
315-
iterable=block_objs_to_upload
316-
):
317-
if isinstance(uploaded_block, _FILE_BLOCK_TYPES):
318-
pre_uploaded_block = block_objs[
319-
delete_start_index + uploaded_block_index
320-
]
321-
assert isinstance(pre_uploaded_block, _FILE_BLOCK_TYPES)
322-
parsed = urlparse(url=pre_uploaded_block.url)
323-
if parsed.scheme == "file":
324-
# Ignore ``mypy`` error as the keyword arguments are
325-
# different across Python versions and platforms.
326-
file_path = Path(url2pathname(parsed.path)) # type: ignore[misc]
327-
file_sha = _calculate_file_sha(file_path=file_path)
328-
sha_to_block_id[file_sha] = str(object=uploaded_block.id)
329-
330-
sha_mapping.write_text(
331-
data=json.dumps(
332-
obj=sha_to_block_id, indent=2, sort_keys=True
333-
)
334-
+ "\n",
335-
encoding="utf-8",
336-
)
337-
338-
click.echo(
339-
message=(
340-
f"Updated SHA mapping for {file_path.name}:"
341-
f"{uploaded_block.id}"
342-
)
343-
)
344-
345256
click.echo(message=f"Updated existing page: '{title}' ({page.url})")

0 commit comments

Comments
 (0)