Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2933c52

Browse files
committed
partly implement the deprecation mechanism
1 parent 6b14b00 commit 2933c52

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::
@@ -652,13 +667,36 @@ def run(arguments, content, options, state_machine, state, lineno):
652667

653668
if len(arguments):
654669

655-
if arguments[0].startswith('/'):
656-
arguments[0] = arguments[0][1:]
657-
src_dir = config.plot_basedir or setup.app.builder.srcdir
670+
# if the new method is selected for resolving paths
671+
if config.plot_dir_resolve_method.lower() == 'relative':
672+
673+
if arguments[0].startswith('/'):
674+
arguments[0] = arguments[0][1:]
675+
src_dir = config.plot_basedir or setup.app.builder.srcdir
676+
else:
677+
src_dir = rst_dir
678+
679+
source_file_name = os.path.join(src_dir, directives.uri(arguments[0]))
680+
658681
else:
659-
src_dir = rst_dir
660682

661-
source_file_name = os.path.join(src_dir, directives.uri(arguments[0]))
683+
cbook.warn_deprecated(
684+
'3.2', message='You are using an old method '
685+
'to resolve filenames of the ``.. ::plot`` directive. Please '
686+
'switch to the new method by specifying '
687+
'``plot_dir_resolve_method=\'relative\'`` in your conf.py '
688+
'and fix the filenames accordingly. Please see '
689+
'matplotlib/doc/api/next_api_changes/2018-10-09-plot-directive.rst '
690+
'for more information. The old method will be removed in '
691+
'%(removal)s.', removal='4.0')
692+
693+
if not config.plot_basedir:
694+
source_file_name = os.path.join(
695+
setup.app.builder.srcdir, directives.uri(arguments[0]))
696+
697+
else:
698+
source_file_name = os.path.join(
699+
setup.confdir, config.plot_basedir, directives.uri(arguments[0]))
662700

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

0 commit comments

Comments
 (0)