Skip to content

Commit 78c484e

Browse files
authored
Merge pull request #10439 from anntzer/mkdir
mkdir is in the stdlib in Py3.
2 parents 7673002 + 97c27dd commit 78c484e

File tree

6 files changed

+24
-26
lines changed

6 files changed

+24
-26
lines changed

lib/matplotlib/__init__.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
import locale
118118
import logging
119119
import os
120+
from pathlib import Path
120121
import re
121122
import shutil
122123
import stat
@@ -651,14 +652,10 @@ def _get_xdg_cache_dir():
651652

652653

653654
def _get_config_or_cache_dir(xdg_base):
654-
from matplotlib.cbook import mkdirs
655-
656655
configdir = os.environ.get('MPLCONFIGDIR')
657656
if configdir is not None:
658657
configdir = os.path.abspath(configdir)
659-
if not os.path.exists(configdir):
660-
mkdirs(configdir)
661-
658+
Path(configdir).mkdir(parents=True, exist_ok=True)
662659
if not _is_writable_dir(configdir):
663660
return _create_tmp_config_dir()
664661
return configdir
@@ -678,7 +675,7 @@ def _get_config_or_cache_dir(xdg_base):
678675
return p
679676
else:
680677
try:
681-
mkdirs(p)
678+
Path(p).mkdir(parents=True, exist_ok=True)
682679
except OSError:
683680
pass
684681
else:

lib/matplotlib/cbook/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,7 @@ def __delattr__(self, name):
850850
return self
851851

852852

853+
@deprecated("3.0")
853854
def mkdirs(newdir, mode=0o777):
854855
"""
855856
make directory *newdir* recursively, and set *mode*. Equivalent to ::

lib/matplotlib/sphinxext/plot_directive.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142

143143
import sys, os, shutil, io, re, textwrap
144144
from os.path import relpath
145+
from pathlib import Path
145146
import traceback
146147
import warnings
147148

@@ -846,8 +847,7 @@ def run(arguments, content, options, state_machine, state, lineno):
846847
state_machine.insert_input(total_lines, source=source_file_name)
847848

848849
# copy image files to builder's output directory, if necessary
849-
if not os.path.exists(dest_dir):
850-
cbook.mkdirs(dest_dir)
850+
Path(dest_dir).mkdir(parents=True, exist_ok=True)
851851

852852
for code_piece, images in results:
853853
for img in images:

lib/matplotlib/testing/compare.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import hashlib
1212
import itertools
1313
import os
14+
from pathlib import Path
1415
import re
1516
import shutil
1617
import sys
@@ -85,11 +86,10 @@ def get_cache_dir():
8586
if cachedir is None:
8687
raise RuntimeError('Could not find a suitable configuration directory')
8788
cache_dir = os.path.join(cachedir, 'test_cache')
88-
if not os.path.exists(cache_dir):
89-
try:
90-
cbook.mkdirs(cache_dir)
91-
except IOError:
92-
return None
89+
try:
90+
Path(cache_dir).mkdir(parents=True, exist_ok=True)
91+
except IOError:
92+
return None
9393
if not os.access(cache_dir, os.W_OK):
9494
return None
9595
return cache_dir

lib/matplotlib/testing/decorators.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
import functools
66
import inspect
77
import os
8-
import sys
8+
from pathlib import Path
99
import shutil
10-
import warnings
10+
import sys
1111
import unittest
12+
import warnings
1213

1314
# Note - don't import nose up here - import it only as needed in functions.
1415
# This allows other functions here to be used by pytest-based testing suites
@@ -532,9 +533,7 @@ def find_dotted_module(module_name, path=None):
532533

533534
baseline_dir = os.path.join(basedir, 'baseline_images', subdir)
534535
result_dir = os.path.abspath(os.path.join('result_images', subdir))
535-
536-
if not os.path.exists(result_dir):
537-
cbook.mkdirs(result_dir)
536+
Path(result_dir).mkdir(parents=True, exist_ok=True)
538537

539538
return baseline_dir, result_dir
540539

lib/matplotlib/texmanager.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,20 @@
3939

4040
import copy
4141
import glob
42+
import hashlib
43+
import logging
4244
import os
45+
from pathlib import Path
4346
import shutil
4447
import sys
4548
import warnings
46-
import logging
47-
48-
from hashlib import md5
4949

5050
import distutils.version
5151
import numpy as np
5252
import matplotlib as mpl
5353
from matplotlib import rcParams
5454
from matplotlib._png import read_png
55-
from matplotlib.cbook import mkdirs, Locked
55+
from matplotlib.cbook import Locked
5656
from matplotlib.compat.subprocess import subprocess, Popen, PIPE, STDOUT
5757
import matplotlib.dviread as dviread
5858
import re
@@ -88,7 +88,7 @@ class TexManager(object):
8888
cachedir = mpl.get_cachedir()
8989
if cachedir is not None:
9090
texcache = os.path.join(cachedir, 'tex.cache')
91-
mkdirs(texcache)
91+
Path(texcache).mkdir(parents=True, exist_ok=True)
9292
else:
9393
# Should only happen in a restricted environment (such as Google App
9494
# Engine). Deal with this gracefully by not creating a cache directory.
@@ -136,7 +136,7 @@ def __init__(self):
136136
raise RuntimeError('Cannot create TexManager, as there is no '
137137
'cache directory available')
138138

139-
mkdirs(self.texcache)
139+
Path(self.texcache).mkdir(parents=True, exist_ok=True)
140140
ff = rcParams['font.family']
141141
if len(ff) == 1 and ff[0].lower() in self.font_families:
142142
self.font_family = ff[0].lower()
@@ -171,7 +171,7 @@ def __init__(self):
171171
# correct png is selected for strings rendered with same font and dpi
172172
# even if the latex preamble changes within the session
173173
preamble_bytes = self.get_custom_preamble().encode('utf-8')
174-
fontconfig.append(md5(preamble_bytes).hexdigest())
174+
fontconfig.append(hashlib.md5(preamble_bytes).hexdigest())
175175
self._fontconfig = ''.join(fontconfig)
176176

177177
# The following packages and commands need to be included in the latex
@@ -188,7 +188,8 @@ def get_basefile(self, tex, fontsize, dpi=None):
188188
"""
189189
s = ''.join([tex, self.get_font_config(), '%f' % fontsize,
190190
self.get_custom_preamble(), str(dpi or '')])
191-
return os.path.join(self.texcache, md5(s.encode('utf-8')).hexdigest())
191+
return os.path.join(
192+
self.texcache, hashlib.md5(s.encode('utf-8')).hexdigest())
192193

193194
def get_font_config(self):
194195
"""Reinitializes self if relevant rcParams on have changed."""

0 commit comments

Comments
 (0)