Skip to content

Commit 2b81c12

Browse files
committed
partly implement the deprecation mechanism
1 parent 10aa22e commit 2b81c12

File tree

2 files changed

+59
-13
lines changed

2 files changed

+59
-13
lines changed

doc/api/next_api_changes/2018-10-09-plot-directive.rst

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,18 @@ Documents that were using this feature may need to adjust the path argument
1010
given to the plot directive. Two options are available:
1111

1212
1. Use absolute paths to find the file relative the ``plot_basedir`` (which
13-
defaults to the directory where conf.py is).
13+
defaults to the source directory, where conf.py is).
1414
2. Use relative paths and the file is found relative to the directory of the
1515
file containing the directive.
1616

17-
Before this change, relative paths were resolved relative to the source
18-
directory (where conf.py is) and absolute paths were pointing to files in the
19-
host system.
17+
Before this change, relative paths were resolved relative to ``plot_basedir``
18+
(which defaulted to the source directory) and absolute paths were pointing to
19+
files in the host system.
20+
21+
Since this will break documentations that were depending on the old behavior,
22+
there is a deprecation period and a new configuration option is introduced to
23+
get the old behavior. To get the old behavior specify
24+
``plot_dir_resolve_method='absolute'`` in ``conf.py`` and specify
25+
``plot_dir_resolve_method='relative'`` to get the new behavior. The old
26+
behavior might be removed in future. The users are advised to switch to the new
27+
behavior and fix the plot directives accordingly.

lib/matplotlib/sphinxext/plot_directive.py

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,26 @@
100100
import numpy as np
101101
from matplotlib import pyplot as plt
102102
103+
plot_dir_resolve_method
104+
The method to use for resolving file names that come after the
105+
``plot::`` directive. Two options are available: ``relative`` and
106+
``absolute``. Defaults to ``absolute`` but the default value will
107+
change to ``relative`` in future versions. The ``absolute`` method
108+
is deprecated and might be removed in future. See the ``plot_basedir``
109+
for explanation of methods.
110+
103111
plot_basedir
104-
Base directory, to which ``plot::`` file names are relative
105-
to. Defaults to the source directory. Only absolute paths are treated
106-
relative to this option. Relative paths are found relative to the
107-
directory of the file containing the directive.
112+
Base directory, to which ``plot::`` file names are relative.
113+
Defaults to the source directory.
114+
115+
If ``plot_dir_resolve_method`` is ``relative``, relative file names are
116+
found relative to the directory of the file containing the directive.
117+
Absolute file names (paths starting with ``/``) are found relative to
118+
``plot_basedir``.
119+
120+
If ``plot_dir_resolve_method`` is ``absolute``, all files are found
121+
relative to ``plot_basedir`` whether the paths start with ``/`` or not.
122+
This method is deprecated and may be removed in future.
108123
109124
plot_formats
110125
File formats to generate. List of tuples or strings::
@@ -671,13 +686,36 @@ def run(arguments, content, options, state_machine, state, lineno):
671686

672687
if len(arguments):
673688

674-
if arguments[0].startswith('/'):
675-
arguments[0] = arguments[0][1:]
676-
src_dir = config.plot_basedir or setup.app.builder.srcdir
689+
# if the new method is selected for resolving paths
690+
if config.plot_dir_resolve_method.lower() == 'relative':
691+
692+
if arguments[0].startswith('/'):
693+
arguments[0] = arguments[0][1:]
694+
src_dir = config.plot_basedir or setup.app.builder.srcdir
695+
else:
696+
src_dir = rst_dir
697+
698+
source_file_name = os.path.join(src_dir, directives.uri(arguments[0]))
699+
677700
else:
678-
src_dir = rst_dir
679701

680-
source_file_name = os.path.join(src_dir, directives.uri(arguments[0]))
702+
cbook.warn_deprecated(
703+
'3.2', message='You are using an old method '
704+
'to resolve filenames of the ``.. ::plot`` directive. Please '
705+
'switch to the new method by specifying '
706+
'``plot_dir_resolve_method=\'relative\'`` in your conf.py '
707+
'and fix the filenames accordingly. Please see '
708+
'matplotlib/doc/api/next_api_changes/2018-10-09-plot-directive.rst '
709+
'for more information. The old method will be removed in '
710+
'%(removal)s.', removal='4.0')
711+
712+
if not config.plot_basedir:
713+
source_file_name = os.path.join(
714+
setup.app.builder.srcdir, directives.uri(arguments[0]))
715+
716+
else:
717+
source_file_name = os.path.join(
718+
setup.confdir, config.plot_basedir, directives.uri(arguments[0]))
681719

682720
# If there is content, it will be passed as a caption.
683721
caption = '\n'.join(content)

0 commit comments

Comments
 (0)