Skip to content

Commit 46af42a

Browse files
committed
Remove usage of distutils
distutils is scheduled to be removed in Python 3.12. It's used to create the full directory tree for storing the files, we can replace that with [pathlib.Path.mkdir](https://docs.python.org/3/library/pathlib.html#pathlib.Path.mkdir). Since we already have a `Path` object now, we can also replace calls to `os.path.join`, `os.stat` with calls on `Path`.
1 parent 9b31b0e commit 46af42a

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

src/django_mermaid/apps.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
from distutils import dir_util
2-
from os import stat
3-
from os.path import dirname
4-
from os.path import exists
5-
from os.path import join
1+
import pathlib
62
from urllib.request import urlretrieve
73

84
from django.apps import AppConfig
@@ -18,9 +14,11 @@ def ready(self):
1814
"""Download mermaid.js from CDN if not already present"""
1915
version = getattr(settings, "MERMAID_VERSION", DEFAULT_VERSION)
2016
cdn = "https://cdnjs.cloudflare.com/ajax/libs/mermaid/%s/mermaid.min.js" % version
21-
static_dir = join(dirname(__file__), "static")
22-
mermaid_dir = join(static_dir, "mermaid", version)
23-
if not exists(join(mermaid_dir, "mermaid.js")) or \
24-
stat(join(mermaid_dir, "mermaid.js")).st_size == 0:
25-
dir_util.create_tree(mermaid_dir, ["mermaid.js"])
26-
urlretrieve(cdn, join(mermaid_dir, "mermaid.js"))
17+
current_dir = pathlib.Path(__file__).parent
18+
static_dir = current_dir / "static"
19+
mermaid_dir = static_dir / "mermaid" / version
20+
mermaid_js = mermaid_dir / "mermaid.js"
21+
if not mermaid_js.exists() or \
22+
mermaid_js.stat().st_size == 0:
23+
mermaid_dir.mkdir(parents=True, exist_ok=True)
24+
urlretrieve(cdn, str(mermaid_js))

0 commit comments

Comments
 (0)