From 5afacad66c0e983535cd51cc5f75a3897162d0d8 Mon Sep 17 00:00:00 2001 From: squidfunk Date: Thu, 15 Sep 2022 18:51:35 +0200 Subject: [PATCH] Removed dependencies with native extensions --- material/plugins/social/plugin.py | 29 +++++++++++++++++++++++------ requirements.txt | 4 +--- src/plugins/social/plugin.py | 29 +++++++++++++++++++++++------ 3 files changed, 47 insertions(+), 15 deletions(-) diff --git a/material/plugins/social/plugin.py b/material/plugins/social/plugin.py index e2714f7d6..a67b1d29f 100644 --- a/material/plugins/social/plugin.py +++ b/material/plugins/social/plugin.py @@ -18,21 +18,29 @@ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # IN THE SOFTWARE. +import logging import os import re import requests +import sys -from cairosvg import svg2png from collections import defaultdict from hashlib import md5 from io import BytesIO -from mkdocs.config.config_options import Deprecated, Type +from mkdocs.commands.build import DuplicateFilter +from mkdocs.config.config_options import Type from mkdocs.plugins import BasePlugin -from PIL import Image, ImageDraw, ImageFont from shutil import copyfile from tempfile import TemporaryFile from zipfile import ZipFile +try: + from cairosvg import svg2png + from PIL import Image, ImageDraw, ImageFont + dependencies = True +except ImportError: + dependencies = False + # ----------------------------------------------------------------------------- # Class # ----------------------------------------------------------------------------- @@ -50,9 +58,6 @@ class SocialPlugin(BasePlugin): ("cards_dir", Type(str, default = "assets/images/social")), ("cards_color", Type(dict, default = {})), ("cards_font", Type(str, default = None)), - - # Deprecated options - ("cards_directory", Deprecated(moved_to = "cards_dir")), ) # Retrieve configuration @@ -61,6 +66,14 @@ class SocialPlugin(BasePlugin): if not self.config["cards"]: return + # Check if required dependencies are installed + if not dependencies: + log.error( + "Required dependencies of \"social\" plugin not found. " + "Install with: pip install cairosvg pillow" + ) + sys.exit() + # Ensure presence of cache directory self.cache = self.config["cache_dir"] if not os.path.isdir(self.cache): @@ -372,6 +385,10 @@ class SocialPlugin(BasePlugin): # Data # ----------------------------------------------------------------------------- +# Set up logging +log = logging.getLogger("mkdocs") +log.addFilter(DuplicateFilter()) + # Color palette colors = dict({ "red": { "fill": "#ef5552", "text": "#ffffff" }, diff --git a/requirements.txt b/requirements.txt index 61b7af4ba..fa067f417 100644 --- a/requirements.txt +++ b/requirements.txt @@ -26,7 +26,5 @@ mkdocs-material-extensions>=1.0.3 pygments>=2.12 pymdown-extensions>=9.4 -# Requirements for social plugin -cairosvg>=2.5 -pillow>=8.0 +# Requirements for plugins requests>=2.26 diff --git a/src/plugins/social/plugin.py b/src/plugins/social/plugin.py index e2714f7d6..a67b1d29f 100644 --- a/src/plugins/social/plugin.py +++ b/src/plugins/social/plugin.py @@ -18,21 +18,29 @@ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # IN THE SOFTWARE. +import logging import os import re import requests +import sys -from cairosvg import svg2png from collections import defaultdict from hashlib import md5 from io import BytesIO -from mkdocs.config.config_options import Deprecated, Type +from mkdocs.commands.build import DuplicateFilter +from mkdocs.config.config_options import Type from mkdocs.plugins import BasePlugin -from PIL import Image, ImageDraw, ImageFont from shutil import copyfile from tempfile import TemporaryFile from zipfile import ZipFile +try: + from cairosvg import svg2png + from PIL import Image, ImageDraw, ImageFont + dependencies = True +except ImportError: + dependencies = False + # ----------------------------------------------------------------------------- # Class # ----------------------------------------------------------------------------- @@ -50,9 +58,6 @@ class SocialPlugin(BasePlugin): ("cards_dir", Type(str, default = "assets/images/social")), ("cards_color", Type(dict, default = {})), ("cards_font", Type(str, default = None)), - - # Deprecated options - ("cards_directory", Deprecated(moved_to = "cards_dir")), ) # Retrieve configuration @@ -61,6 +66,14 @@ class SocialPlugin(BasePlugin): if not self.config["cards"]: return + # Check if required dependencies are installed + if not dependencies: + log.error( + "Required dependencies of \"social\" plugin not found. " + "Install with: pip install cairosvg pillow" + ) + sys.exit() + # Ensure presence of cache directory self.cache = self.config["cache_dir"] if not os.path.isdir(self.cache): @@ -372,6 +385,10 @@ class SocialPlugin(BasePlugin): # Data # ----------------------------------------------------------------------------- +# Set up logging +log = logging.getLogger("mkdocs") +log.addFilter(DuplicateFilter()) + # Color palette colors = dict({ "red": { "fill": "#ef5552", "text": "#ffffff" },