From 982708c64b9e44aa0880d9ab3c19c4a42d2516b3 Mon Sep 17 00:00:00 2001 From: Liam Bigelow <40188355+bglw@users.noreply.github.com> Date: Mon, 30 Sep 2024 11:18:55 +1300 Subject: [PATCH] Allow deleting a Pagefind index in a context, update docs to match --- docs/content/docs/py-api.md | 25 ++++++++---- ...y-close-the-pagefind-backend.toolproof.yml | 38 +++++++++++++++++++ .../python/src/pagefind/index/__init__.py | 6 ++- 3 files changed, 60 insertions(+), 9 deletions(-) create mode 100644 pagefind/integration_tests/python_api/py-close-the-pagefind-backend.toolproof.yml diff --git a/docs/content/docs/py-api.md b/docs/content/docs/py-api.md index ef19afb0..c12758bd 100644 --- a/docs/content/docs/py-api.md +++ b/docs/content/docs/py-api.md @@ -234,17 +234,28 @@ for file in (await index.get_files()): ## index.write_files -Closing the `PagefindIndex`'s context automatically calls `index.write_files`. +Calling `index.write_files()` writes the index files to disk, as they would be written when running the standard Pagefind binary directly. -If you aren't using `PagefindIndex` as a context manager, calling `index.write_files()` writes the index files to disk, as they would be written when running the standard Pagefind binary directly. +Closing the `PagefindIndex`'s context automatically calls `index.write_files`, so calling this function is not necessary in normal operation. + +Calling this function won't prevent files being written when the context closes, which may cause duplicate files to be written. +If calling this function manually, you probably want to also call `index.delete_index()`. ```py -await index = PagefindIndex( - IndexConfig( - output_path="./public/pagefind", - ), +config = IndexConfig( + output_path="./public/pagefind", ) -await index.write_files() +async with PagefindIndex(config=config) as index: + # ... add content to index + + # write files to the configured output path for the index: + await index.write_files() + + # write files to a different output path: + await index.write_files(output_path="./custom/pagefind") + + # prevent also writing files when closing the `PagefindIndex`: + await index.delete_index() ``` The `output_path` option should contain the path to the desired Pagefind bundle directory. If relative, is relative to the current working directory of your Python process. diff --git a/pagefind/integration_tests/python_api/py-close-the-pagefind-backend.toolproof.yml b/pagefind/integration_tests/python_api/py-close-the-pagefind-backend.toolproof.yml new file mode 100644 index 00000000..8eba935f --- /dev/null +++ b/pagefind/integration_tests/python_api/py-close-the-pagefind-backend.toolproof.yml @@ -0,0 +1,38 @@ +name: Python API > Close the Pagefind backend +platforms: + - linux + - mac + +steps: + - ref: ./background.toolproof.yml + - step: I have a "public/run.py" file with the content {python} + python: |2- + import sys + sys.path.append('%repo_wd%/wrappers/python/src') + + import asyncio + import json + import logging + import os + from pagefind.index import PagefindIndex, IndexConfig + + async def main(): + async with PagefindIndex() as index: + files = await index.get_files() + + for file in files: + print(file["path"]) + + await index.delete_index() + + try: + files = await index.get_files() + except AssertionError: + print("errored getting files after close") + + if __name__ == "__main__": + asyncio.run(main()) + - step: I run "cd public && PAGEFIND_BINARY_PATH=%pagefind_exec_path% python3 run.py" + - step: stdout should contain "pagefind.js" + - step: stdout should contain "pagefind-ui.js" + - step: stdout should contain "errored getting files after close" diff --git a/wrappers/python/src/pagefind/index/__init__.py b/wrappers/python/src/pagefind/index/__init__.py index ff86adc4..3791c097 100644 --- a/wrappers/python/src/pagefind/index/__init__.py +++ b/wrappers/python/src/pagefind/index/__init__.py @@ -265,8 +265,10 @@ async def __aexit__( exc_value: Optional[Any], traceback: Optional[Any], ) -> None: - assert self._service is not None - assert self._index_id is not None + if self._service is None: + return + if self._index_id is None: + return if exc_type is None: await self.write_files() await self._service.close()