From b8d950b64f82763f3338d416b780ce22c15559a1 Mon Sep 17 00:00:00 2001 From: Martin Ledvina Date: Tue, 12 Mar 2024 14:59:25 +0100 Subject: [PATCH 1/2] Implement warning and critical in the style of the original authors. Handle exceptions raised from the use of `requests.exceptions`. --- mermaid2/plugin.py | 4 +++- mermaid2/util.py | 30 +++++++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/mermaid2/plugin.py b/mermaid2/plugin.py index 6646c99..86d6622 100755 --- a/mermaid2/plugin.py +++ b/mermaid2/plugin.py @@ -10,7 +10,7 @@ from bs4 import BeautifulSoup from . import pyjs -from .util import info, libname, url_exists +from .util import info, libname, url_exists, critical # ------------------------ @@ -140,6 +140,8 @@ def javascript(self) -> str: # make checks if not url_exists(javascript, local_base_dir=self.full_config['docs_dir']): + critical("Cannot find Mermaid library: %s" % + javascript) raise FileNotFoundError("Cannot find Mermaid library: %s" % javascript) self._javascript = javascript diff --git a/mermaid2/util.py b/mermaid2/util.py index 8ca8db0..9a4a47f 100644 --- a/mermaid2/util.py +++ b/mermaid2/util.py @@ -27,7 +27,19 @@ def info(*args) -> str: args = [MERMAID_LABEL] + [str(arg) for arg in args] msg = ' '.join(args) log.info(msg) - + +def warning(*args) -> str: + "Write information on the console, preceded by the signature label" + args = [MERMAID_LABEL] + [str(arg) for arg in args] + msg = ' '.join(args) + log.warning(msg) + +def critical(*args) -> str: + "Write information on the console, preceded by the signature label" + args = [MERMAID_LABEL] + [str(arg) for arg in args] + msg = ' '.join(args) + log.critical(msg) + # ------------------- # Paths and URLs # ------------------- @@ -45,8 +57,20 @@ def libname(lib:str) -> str: def url_exists(url:str, local_base_dir:str='') -> bool: "Checks that a url exists" if url.startswith('http'): - request = requests.get(url) - return request.status_code == 200 + # requests can fail without HTTP code, list: + # https://docs.python-requests.org/en/latest/_modules/requests/exceptions/ + try: + request = requests.get(url) + except Exception as ue: + if ue.__class__.__module__ == "requests.exceptions": + warning("Exception when making a GET request: %s" % ue) + return False + # Returning the rest + else: + raise + else: + return request.status_code == 200 + else: pathname = os.path.join(local_base_dir, url) return os.path.exists(pathname) From ca5b3804854884ab7e562134cee3a98f17d09a20 Mon Sep 17 00:00:00 2001 From: Martin Ledvina Date: Tue, 12 Mar 2024 16:23:11 +0100 Subject: [PATCH 2/2] Added optional parameter allow_inaccessible_url. Default value is False. Moved raising the exception behind the parameter. --- mermaid2/plugin.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mermaid2/plugin.py b/mermaid2/plugin.py index 86d6622..80140f6 100755 --- a/mermaid2/plugin.py +++ b/mermaid2/plugin.py @@ -42,6 +42,7 @@ class MarkdownMermaidPlugin(BasePlugin): ('version', PluginType(str, default=JAVASCRIPT_VERSION)), ('javascript', PluginType(str, default=None)), + ('allow_inaccessible_url', PluginType(bool, default=False)), ('arguments', PluginType(dict, default={})), # ('custom_loader', PluginType(bool, default=False)) ) @@ -142,8 +143,9 @@ def javascript(self) -> str: local_base_dir=self.full_config['docs_dir']): critical("Cannot find Mermaid library: %s" % javascript) - raise FileNotFoundError("Cannot find Mermaid library: %s" % - javascript) + if not self.config['allow_inaccessible_url']: + raise FileNotFoundError("Cannot find Mermaid library: %s" % + javascript) self._javascript = javascript return self._javascript