From b947fe1923587ae16d461e957fe09b59dc9571dc Mon Sep 17 00:00:00 2001 From: Adrien Brignon Date: Tue, 30 May 2023 22:16:25 +0200 Subject: [PATCH] fix: improved event loop It should now support all plugins that use `asyncio` too! :party: --- mkdocs_exporter/plugins/pdf/plugin.py | 25 +++++---- poetry.lock | 73 +++++---------------------- pyproject.toml | 3 +- 3 files changed, 27 insertions(+), 74 deletions(-) diff --git a/mkdocs_exporter/plugins/pdf/plugin.py b/mkdocs_exporter/plugins/pdf/plugin.py index b14b63b..470ed90 100644 --- a/mkdocs_exporter/plugins/pdf/plugin.py +++ b/mkdocs_exporter/plugins/pdf/plugin.py @@ -1,6 +1,7 @@ import os import types import asyncio +import nest_asyncio from mkdocs.plugins import BasePlugin from mkdocs_exporter.page import Page @@ -22,7 +23,14 @@ def __init__(self): self.watch: list[str] = [] self.renderer: Optional[Renderer] = None self.tasks: list[types.CoroutineType] = [] - self.loop = asyncio.AbstractEventLoop = None + self.loop: asyncio.AbstractEventLoopPolicy = asyncio.new_event_loop() + + + def on_startup(self, **kwargs) -> None: + """Invoked when the plugin is starting...""" + + nest_asyncio.apply(self.loop) + asyncio.set_event_loop(self.loop) def on_config(self, config: dict) -> None: @@ -69,18 +77,12 @@ def on_page_markdown(self, markdown: str, page: Page, config: Config, **kwargs) def on_pre_build(self, **kwargs) -> None: """Invoked before the build process starts.""" - if not self._enabled(): - return - self.tasks.clear() - if self.loop and self.loop.is_running(): - self.loop.close() + if not self._enabled(): + return self.renderer = Renderer() - self.loop = asyncio.new_event_loop() - - asyncio.set_event_loop(self.loop) for stylesheet in self.config.stylesheets: self.renderer.add_stylesheet(stylesheet) @@ -149,13 +151,10 @@ async def limit(coroutine: Coroutine) -> Coroutine: self.loop.run_until_complete(asyncio.gather(*concurrently(self.tasks, max(1, self.config.concurrency or 1)))) self.loop.run_until_complete(self.renderer.dispose()) - self.loop.close() + self.tasks.clear() - self.loop = None self.renderer = None - self.tasks.clear() - def _enabled(self, page: Page = None) -> bool: """Is the plugin enabled for this page?""" diff --git a/poetry.lock b/poetry.lock index 2fb67cd..e61fb48 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,10 +1,9 @@ -# This file is automatically @generated by Poetry 1.4.2 and should not be changed by hand. +# This file is automatically @generated by Poetry 1.5.0 and should not be changed by hand. [[package]] name = "babel" version = "2.12.1" description = "Internationalization utilities" -category = "dev" optional = false python-versions = ">=3.7" files = [ @@ -19,7 +18,6 @@ pytz = {version = ">=2015.7", markers = "python_version < \"3.9\""} name = "beautifulsoup4" version = "4.12.2" description = "Screen-scraping library" -category = "main" optional = false python-versions = ">=3.6.0" files = [ @@ -38,7 +36,6 @@ lxml = ["lxml"] name = "bracex" version = "2.3.post1" description = "Bash style brace expander." -category = "dev" optional = false python-versions = ">=3.7" files = [ @@ -50,7 +47,6 @@ files = [ name = "certifi" version = "2023.5.7" description = "Python package for providing Mozilla's CA Bundle." -category = "dev" optional = false python-versions = ">=3.6" files = [ @@ -62,7 +58,6 @@ files = [ name = "charset-normalizer" version = "3.1.0" description = "The Real First Universal Charset Detector. Open, modern and actively maintained alternative to Chardet." -category = "dev" optional = false python-versions = ">=3.7.0" files = [ @@ -147,7 +142,6 @@ files = [ name = "click" version = "8.1.3" description = "Composable command line interface toolkit" -category = "main" optional = false python-versions = ">=3.7" files = [ @@ -163,7 +157,6 @@ importlib-metadata = {version = "*", markers = "python_version < \"3.8\""} name = "colorama" version = "0.4.6" description = "Cross-platform colored terminal text." -category = "main" optional = false python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7" files = [ @@ -175,7 +168,6 @@ files = [ name = "csscompressor" version = "0.9.5" description = "A python port of YUI CSS Compressor" -category = "dev" optional = false python-versions = "*" files = [ @@ -186,7 +178,6 @@ files = [ name = "flake8" version = "3.9.2" description = "the modular source code checker: pep8 pyflakes and co" -category = "dev" optional = false python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,>=2.7" files = [ @@ -204,7 +195,6 @@ pyflakes = ">=2.3.0,<2.4.0" name = "ghp-import" version = "2.1.0" description = "Copy your docs directly to the gh-pages branch." -category = "main" optional = false python-versions = "*" files = [ @@ -222,7 +212,6 @@ dev = ["flake8", "markdown", "twine", "wheel"] name = "gitdb" version = "4.0.10" description = "Git Object Database" -category = "dev" optional = false python-versions = ">=3.7" files = [ @@ -237,7 +226,6 @@ smmap = ">=3.0.1,<6" name = "gitpython" version = "3.1.31" description = "GitPython is a Python library used to interact with Git repositories" -category = "dev" optional = false python-versions = ">=3.7" files = [ @@ -253,7 +241,6 @@ typing-extensions = {version = ">=3.7.4.3", markers = "python_version < \"3.8\"" name = "greenlet" version = "2.0.2" description = "Lightweight in-process concurrent programming" -category = "main" optional = false python-versions = ">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*" files = [ @@ -327,7 +314,6 @@ test = ["objgraph", "psutil"] name = "htmlmin2" version = "0.1.13" description = "An HTML Minifier" -category = "dev" optional = false python-versions = "*" files = [ @@ -338,7 +324,6 @@ files = [ name = "idna" version = "3.4" description = "Internationalized Domain Names in Applications (IDNA)" -category = "dev" optional = false python-versions = ">=3.5" files = [ @@ -350,7 +335,6 @@ files = [ name = "importlib-metadata" version = "4.13.0" description = "Read metadata from Python packages" -category = "main" optional = false python-versions = ">=3.7" files = [ @@ -371,7 +355,6 @@ testing = ["flake8 (<5)", "flufl.flake8", "importlib-resources (>=1.3)", "packag name = "importlib-resources" version = "5.12.0" description = "Read resources from Python packages" -category = "main" optional = false python-versions = ">=3.7" files = [ @@ -390,7 +373,6 @@ testing = ["flake8 (<5)", "pytest (>=6)", "pytest-black (>=0.3.7)", "pytest-chec name = "jinja2" version = "3.1.2" description = "A very fast and expressive template engine." -category = "main" optional = false python-versions = ">=3.7" files = [ @@ -408,7 +390,6 @@ i18n = ["Babel (>=2.7)"] name = "jsmin" version = "3.0.1" description = "JavaScript minifier." -category = "dev" optional = false python-versions = "*" files = [ @@ -419,7 +400,6 @@ files = [ name = "libsass" version = "0.22.0" description = "Sass for Python: A straightforward binding of libsass for Python." -category = "main" optional = false python-versions = ">=3.6" files = [ @@ -434,7 +414,6 @@ files = [ name = "lxml" version = "4.9.2" description = "Powerful and Pythonic XML processing library combining libxml2/libxslt with the ElementTree API." -category = "main" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, != 3.4.*" files = [ @@ -527,7 +506,6 @@ source = ["Cython (>=0.29.7)"] name = "markdown" version = "3.3.7" description = "Python implementation of Markdown." -category = "main" optional = false python-versions = ">=3.6" files = [ @@ -545,7 +523,6 @@ testing = ["coverage", "pyyaml"] name = "markupsafe" version = "2.1.2" description = "Safely add untrusted strings to HTML/XML markup." -category = "main" optional = false python-versions = ">=3.7" files = [ @@ -605,7 +582,6 @@ files = [ name = "mccabe" version = "0.6.1" description = "McCabe checker, plugin for flake8" -category = "dev" optional = false python-versions = "*" files = [ @@ -617,7 +593,6 @@ files = [ name = "mdx-truly-sane-lists" version = "1.3" description = "Extension for Python-Markdown that makes lists truly sane. Custom indents for nested lists and fix for messy linebreaks." -category = "dev" optional = false python-versions = "*" files = [ @@ -632,7 +607,6 @@ Markdown = ">=2.6" name = "mergedeep" version = "1.3.4" description = "A deep merge function for 🐍." -category = "main" optional = false python-versions = ">=3.6" files = [ @@ -644,7 +618,6 @@ files = [ name = "mkdocs" version = "1.4.3" description = "Project documentation with Markdown." -category = "main" optional = false python-versions = ">=3.7" files = [ @@ -674,7 +647,6 @@ min-versions = ["babel (==2.9.0)", "click (==7.0)", "colorama (==0.4)", "ghp-imp name = "mkdocs-awesome-pages-plugin" version = "2.9.1" description = "An MkDocs plugin that simplifies configuring page titles and their order" -category = "dev" optional = false python-versions = ">=3.7" files = [ @@ -691,7 +663,6 @@ wcmatch = ">=7" name = "mkdocs-git-authors-plugin" version = "0.7.1" description = "Mkdocs plugin to display git authors of a page" -category = "dev" optional = false python-versions = ">=3.7" files = [ @@ -706,7 +677,6 @@ mkdocs = ">=1.0" name = "mkdocs-git-revision-date-localized-plugin" version = "1.2.0" description = "Mkdocs plugin that enables displaying the localized date of the last git modification of a markdown file." -category = "dev" optional = false python-versions = ">=3.6" files = [ @@ -724,7 +694,6 @@ pytz = "*" name = "mkdocs-macros-plugin" version = "0.7.0" description = "Unleash the power of MkDocs with macros and variables" -category = "dev" optional = false python-versions = ">=3.5" files = [ @@ -746,7 +715,6 @@ test = ["mkdocs-include-markdown-plugin", "mkdocs-macros-test", "mkdocs-material name = "mkdocs-material" version = "9.1.15" description = "Documentation that simply works" -category = "dev" optional = false python-versions = ">=3.7" files = [ @@ -769,7 +737,6 @@ requests = ">=2.26" name = "mkdocs-material-extensions" version = "1.1.1" description = "Extension pack for Python Markdown and MkDocs Material." -category = "dev" optional = false python-versions = ">=3.7" files = [ @@ -781,7 +748,6 @@ files = [ name = "mkdocs-minify-plugin" version = "0.6.4" description = "An MkDocs plugin to minify HTML, JS or CSS files prior to being written to disk" -category = "dev" optional = false python-versions = ">=3.7" files = [ @@ -799,7 +765,6 @@ mkdocs = ">=1.4.1" name = "mkdocs-redirects" version = "1.2.0" description = "A MkDocs plugin for dynamic page redirects to prevent broken links." -category = "dev" optional = false python-versions = ">=3.6" files = [ @@ -818,7 +783,6 @@ test = ["autoflake", "black", "isort", "pytest"] name = "natsort" version = "8.3.1" description = "Simple yet flexible natural sorting in Python." -category = "dev" optional = false python-versions = ">=3.7" files = [ @@ -830,11 +794,21 @@ files = [ fast = ["fastnumbers (>=2.0.0)"] icu = ["PyICU (>=1.0.0)"] +[[package]] +name = "nest-asyncio" +version = "1.5.6" +description = "Patch asyncio to allow nested event loops" +optional = false +python-versions = ">=3.5" +files = [ + {file = "nest_asyncio-1.5.6-py3-none-any.whl", hash = "sha256:b9a953fb40dceaa587d109609098db21900182b16440652454a146cffb06e8b8"}, + {file = "nest_asyncio-1.5.6.tar.gz", hash = "sha256:d267cc1ff794403f7df692964d1d2a3fa9418ffea2a3f6859a439ff482fef290"}, +] + [[package]] name = "packaging" version = "23.1" description = "Core utilities for Python packages" -category = "main" optional = false python-versions = ">=3.7" files = [ @@ -846,7 +820,6 @@ files = [ name = "playwright" version = "1.34.0" description = "A high-level API to automate web browsers" -category = "main" optional = false python-versions = ">=3.7" files = [ @@ -868,7 +841,6 @@ typing-extensions = {version = "*", markers = "python_version <= \"3.8\""} name = "pycodestyle" version = "2.7.0" description = "Python style guide checker" -category = "dev" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" files = [ @@ -880,7 +852,6 @@ files = [ name = "pyee" version = "9.0.4" description = "A port of node.js's EventEmitter to python." -category = "main" optional = false python-versions = "*" files = [ @@ -895,7 +866,6 @@ typing-extensions = "*" name = "pyflakes" version = "2.3.1" description = "passive checker of Python programs" -category = "dev" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" files = [ @@ -907,7 +877,6 @@ files = [ name = "pygments" version = "2.15.1" description = "Pygments is a syntax highlighting package written in Python." -category = "dev" optional = false python-versions = ">=3.7" files = [ @@ -922,7 +891,6 @@ plugins = ["importlib-metadata"] name = "pymdown-extensions" version = "10.0.1" description = "Extension pack for Python Markdown." -category = "dev" optional = false python-versions = ">=3.7" files = [ @@ -938,7 +906,6 @@ pyyaml = "*" name = "python-dateutil" version = "2.8.2" description = "Extensions to the standard Python datetime module" -category = "main" optional = false python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,>=2.7" files = [ @@ -953,7 +920,6 @@ six = ">=1.5" name = "pytz" version = "2023.3" description = "World timezone definitions, modern and historical" -category = "dev" optional = false python-versions = "*" files = [ @@ -965,7 +931,6 @@ files = [ name = "pyyaml" version = "6.0" description = "YAML parser and emitter for Python" -category = "main" optional = false python-versions = ">=3.6" files = [ @@ -1015,7 +980,6 @@ files = [ name = "pyyaml-env-tag" version = "0.1" description = "A custom YAML tag for referencing environment variables in YAML files. " -category = "main" optional = false python-versions = ">=3.6" files = [ @@ -1030,7 +994,6 @@ pyyaml = "*" name = "regex" version = "2023.5.5" description = "Alternative regular expression module, to replace re." -category = "dev" optional = false python-versions = ">=3.6" files = [ @@ -1128,7 +1091,6 @@ files = [ name = "requests" version = "2.31.0" description = "Python HTTP for Humans." -category = "dev" optional = false python-versions = ">=3.7" files = [ @@ -1150,7 +1112,6 @@ use-chardet-on-py3 = ["chardet (>=3.0.2,<6)"] name = "six" version = "1.16.0" description = "Python 2 and 3 compatibility utilities" -category = "main" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*" files = [ @@ -1162,7 +1123,6 @@ files = [ name = "smmap" version = "5.0.0" description = "A pure Python implementation of a sliding window memory map manager" -category = "dev" optional = false python-versions = ">=3.6" files = [ @@ -1174,7 +1134,6 @@ files = [ name = "soupsieve" version = "2.4.1" description = "A modern CSS selector implementation for Beautiful Soup." -category = "main" optional = false python-versions = ">=3.7" files = [ @@ -1186,7 +1145,6 @@ files = [ name = "termcolor" version = "2.3.0" description = "ANSI color formatting for output in terminal" -category = "dev" optional = false python-versions = ">=3.7" files = [ @@ -1201,7 +1159,6 @@ tests = ["pytest", "pytest-cov"] name = "typing-extensions" version = "4.6.2" description = "Backported and Experimental Type Hints for Python 3.7+" -category = "main" optional = false python-versions = ">=3.7" files = [ @@ -1213,7 +1170,6 @@ files = [ name = "urllib3" version = "2.0.2" description = "HTTP library with thread-safe connection pooling, file post, and more." -category = "dev" optional = false python-versions = ">=3.7" files = [ @@ -1231,7 +1187,6 @@ zstd = ["zstandard (>=0.18.0)"] name = "watchdog" version = "3.0.0" description = "Filesystem events monitoring" -category = "main" optional = false python-versions = ">=3.7" files = [ @@ -1271,7 +1226,6 @@ watchmedo = ["PyYAML (>=3.10)"] name = "wcmatch" version = "8.4.1" description = "Wildcard/glob file name matcher." -category = "dev" optional = false python-versions = ">=3.7" files = [ @@ -1286,7 +1240,6 @@ bracex = ">=2.1.1" name = "zipp" version = "3.15.0" description = "Backport of pathlib-compatible object wrapper for zip files" -category = "main" optional = false python-versions = ">=3.7" files = [ @@ -1301,4 +1254,4 @@ testing = ["big-O", "flake8 (<5)", "jaraco.functools", "jaraco.itertools", "more [metadata] lock-version = "2.0" python-versions = ">=3.7" -content-hash = "9572dca2cd47d51d698817f8046d9f1ec31bda48008635ad4c44fd1749f8f8f4" +content-hash = "8e181fbbb84d65d5abf422a9456be48a32f3e4fbc0f253979f0dc5b1ef5b70cf" diff --git a/pyproject.toml b/pyproject.toml index e795d58..9401869 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "mkdocs-exporter" -version = "3.0.1" +version = "3.0.2" repository = "https://github.com/adrienbrignon/mkdocs-exporter" keywords = ["mkdocs", "pdf", "exporter"] description = "A highly-configurable plugin for MkDocs that exports your pages to PDF files." @@ -28,6 +28,7 @@ lxml = ">=4.9" libsass = ">=0.22.0" importlib-resources = ">=5.0" importlib-metadata = "<5.0" +nest-asyncio = ">=1.5.6" [tool.poetry.plugins."mkdocs.plugins"] "mkdocs/exporter" = "mkdocs_exporter.plugin:Plugin"