Skip to content

Commit

Permalink
fix: CairoSVG OSError handling in social plugin
Browse files Browse the repository at this point in the history
Related issue: #6817
  • Loading branch information
kamilkrzyskow committed Feb 26, 2024
1 parent 23f1886 commit 3016a47
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 14 deletions.
35 changes: 28 additions & 7 deletions material/plugins/social/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,24 @@
from shutil import copyfile
from tempfile import TemporaryFile
from zipfile import ZipFile

from .config import SocialConfig

try:
from cairosvg import svg2png
from PIL import Image, ImageDraw, ImageFont
except ImportError:
pass
except ImportError as e:
DEP_IMPORT_ERRORS = {str(e)}
else:
DEP_IMPORT_ERRORS = set()

from .config import SocialConfig
CAIROSVG_ERROR: str = ""

try:
from cairosvg import svg2png
except ImportError as e:
DEP_IMPORT_ERRORS.add(str(e))
except OSError as e:
CAIROSVG_ERROR = str(e)


# -----------------------------------------------------------------------------
Expand All @@ -76,10 +87,20 @@ def on_config(self, config):
return

# Check dependencies
if "Image" not in globals():
if DEP_IMPORT_ERRORS:
raise PluginError(
"Required dependencies of \"social\" plugin not found.\n"
"Install with: pip install \"mkdocs-material[imaging]\"\n"
+ str("\n".join(DEP_IMPORT_ERRORS))
)

if CAIROSVG_ERROR:
raise PluginError(
"Required dependencies of \"social\" plugin not found. "
"Install with: pip install \"mkdocs-material[imaging]\""
"\"cairosvg\" Python module is installed, but it crashed with:\n"
+ CAIROSVG_ERROR
+ "\n---\nDid you install the external CairoSVG library?\n"
"Did you add the binaries path to your system PATH?\n"
"Did you restart the Terminal to assure the PATH was reloaded?"
)

# Move color options
Expand Down
35 changes: 28 additions & 7 deletions src/plugins/social/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,24 @@
from shutil import copyfile
from tempfile import TemporaryFile
from zipfile import ZipFile

from .config import SocialConfig

try:
from cairosvg import svg2png
from PIL import Image, ImageDraw, ImageFont
except ImportError:
pass
except ImportError as e:
DEP_IMPORT_ERRORS = {str(e)}
else:
DEP_IMPORT_ERRORS = set()

from .config import SocialConfig
CAIROSVG_ERROR: str = ""

try:
from cairosvg import svg2png
except ImportError as e:
DEP_IMPORT_ERRORS.add(str(e))
except OSError as e:
CAIROSVG_ERROR = str(e)


# -----------------------------------------------------------------------------
Expand All @@ -76,10 +87,20 @@ def on_config(self, config):
return

# Check dependencies
if "Image" not in globals():
if DEP_IMPORT_ERRORS:
raise PluginError(
"Required dependencies of \"social\" plugin not found.\n"
"Install with: pip install \"mkdocs-material[imaging]\"\n"
+ str("\n".join(DEP_IMPORT_ERRORS))
)

if CAIROSVG_ERROR:
raise PluginError(
"Required dependencies of \"social\" plugin not found. "
"Install with: pip install \"mkdocs-material[imaging]\""
"\"cairosvg\" Python module is installed, but it crashed with:\n"
+ CAIROSVG_ERROR
+ "\n---\nDid you install the external CairoSVG library?\n"
"Did you add the binaries path to your system PATH?\n"
"Did you restart the Terminal to assure the PATH was reloaded?"
)

# Move color options
Expand Down

0 comments on commit 3016a47

Please sign in to comment.