Skip to content

Commit

Permalink
vendor ensure_dir_exists
Browse files Browse the repository at this point in the history
  • Loading branch information
Carreau committed Oct 1, 2023
1 parent adf9590 commit 9f7703c
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions qtconsole/rich_jupyter_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

from qtpy import QtCore, QtGui, QtWidgets

from ipython_genutils.path import ensure_dir_exists
from traitlets import Bool
from pygments.util import ClassNotFound

Expand All @@ -22,6 +21,23 @@
latex_to_png = None


def _ensure_dir_exists(path, mode=0o755):
"""ensure that a directory exists
If it doesn't exist, try to create it and protect against a race condition
if another process is doing the same.
The default permissions are 755, which differ from os.makedirs default of 777.
"""
if not os.path.exists(path):
try:
os.makedirs(path, mode=mode)
except OSError as e:
if e.errno != errno.EEXIST:
raise
elif not os.path.isdir(path):
raise IOError("%r exists but is not a directory" % path)

class LatexError(Exception):
"""Exception for Latex errors"""

Expand Down Expand Up @@ -310,7 +326,7 @@ def _get_image_tag(self, match, path = None, format = "png"):
return "<b>Couldn't find image %s</b>" % match.group("name")

if path is not None:
ensure_dir_exists(path)
_ensure_dir_exists(path)
relpath = os.path.basename(path)
if image.save("%s/qt_img%s.%s" % (path, match.group("name"), format),
"PNG"):
Expand Down

0 comments on commit 9f7703c

Please sign in to comment.