Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4aa3e41

Browse files
committed
Fix semantics of MEP22 image names.
See writeup in changelog note.
1 parent07a6f66 commit4aa3e41

File tree

3 files changed

+63
-23
lines changed

3 files changed

+63
-23
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
MEP22 Tool image path semantics
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
Previously, MEP22 Tools would try to load their icon (``tool.image``) relative
4+
to the current working directory, or, as a fallback, from Matplotlib's own
5+
image directory. Because both approaches are problematic for third-party
6+
tools (the end-user may change the current working directory at any time, and
7+
third-parties cannot add new icons in Matplotlib's image directory), this
8+
behavior is deprecated; instead, ``tool.image`` is now interpreted relative
9+
to the directory containing the source file where the ``Tool.image`` class
10+
attribute is defined. (Defining ``tool.image`` as an absolute path also works
11+
and is compatible with both the old and the new semantics.)

‎lib/matplotlib/backend_bases.py

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
importitertools
3636
importlogging
3737
importos
38+
importpathlib
3839
importsignal
3940
importsocket
4041
importsys
@@ -3284,7 +3285,7 @@ def add_tool(self, tool, group, position=-1):
32843285
The position within the group to place this tool.
32853286
"""
32863287
tool=self.toolmanager.get_tool(tool)
3287-
image=self._get_image_filename(tool.image)
3288+
image=self._get_image_filename(tool)
32883289
toggle=getattr(tool,'toggled',None)isnotNone
32893290
self.add_toolitem(tool.name,group,position,
32903291
image,tool.description,toggle)
@@ -3295,19 +3296,43 @@ def add_tool(self, tool, group, position=-1):
32953296
iftool.toggled:
32963297
self.toggle_toolitem(tool.name,True)
32973298

3298-
def_get_image_filename(self,image):
3299-
"""Find the image based on its name."""
3300-
ifnotimage:
3299+
def_get_image_filename(self,tool):
3300+
"""Resolve a tool icon's filename."""
3301+
ifnottool.image:
33013302
returnNone
3302-
3303-
basedir=cbook._get_data_path("images")
3304-
forfnamein [
3305-
image,
3306-
image+self._icon_extension,
3307-
str(basedir/image),
3308-
str(basedir/ (image+self._icon_extension)),
3303+
ifos.path.isabs(tool.image):
3304+
filename=tool.image
3305+
else:
3306+
if"image"ingetattr(tool,"__dict__", {}):
3307+
raiseValueError("If 'tool.image' is an instance variable, "
3308+
"it must be an absolute path")
3309+
forclsintype(tool).__mro__:
3310+
if"image"invars(cls):
3311+
try:
3312+
src=inspect.getfile(cls)
3313+
break
3314+
except (OSError,TypeError):
3315+
raiseValueError("Failed to locate source file "
3316+
"where 'tool.image' is defined")fromNone
3317+
else:
3318+
raiseValueError("Failed to find parent class defining 'tool.image'")
3319+
filename=str(pathlib.Path(src).parent/tool.image)
3320+
forfilenamein [filename,filename+self._icon_extension]:
3321+
ifos.path.isfile(filename):
3322+
returnfilename
3323+
forfnamein [# Fallback; once deprecation elapses.
3324+
tool.image,
3325+
tool.image+self._icon_extension,
3326+
cbook._get_data_path("images",tool.image),
3327+
cbook._get_data_path("images",tool.image+self._icon_extension),
33093328
]:
33103329
ifos.path.isfile(fname):
3330+
_api.warn_deprecated(
3331+
"3.9",message=f"Loading icon{tool.image!r} from the current "
3332+
"directory or from Matplotlib's image directory. This behavior "
3333+
"is deprecated since %(since)s and will be removed %(removal)s; "
3334+
"Tool.image should be set to a path relative to the Tool's source "
3335+
"file, or to an absolute path.")
33113336
returnfname
33123337

33133338
deftrigger_tool(self,name):

‎lib/matplotlib/backend_tools.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,14 @@ class ToolBase:
9797

9898
image=None
9999
"""
100-
Filename of the image.
101-
102-
`str`: Filename of the image to use in a Toolbar. If None, the *name* is
103-
used as a label in the toolbar button.
100+
Icon filename.
101+
102+
``str | None``: Filename of the Toolbar icon; either absolute, or
103+
relative to the directory containing the Python source file where the
104+
``Tool.image``class attribute is defined (in the latter case, this cannot
105+
be defined as an instance attribute). In either case, the extension is
106+
optional; leaving it off lets individual backends select the icon format
107+
they prefer. If None, the *name* is used as a label in the toolbar button.
104108
"""
105109

106110
def__init__(self,toolmanager,name):
@@ -601,7 +605,7 @@ class ToolHome(ViewsPositionsBase):
601605
"""Restore the original view limits."""
602606

603607
description='Reset original view'
604-
image='home'
608+
image='mpl-data/images/home'
605609
default_keymap=property(lambdaself:mpl.rcParams['keymap.home'])
606610
_on_trigger='home'
607611

@@ -610,7 +614,7 @@ class ToolBack(ViewsPositionsBase):
610614
"""Move back up the view limits stack."""
611615

612616
description='Back to previous view'
613-
image='back'
617+
image='mpl-data/images/back'
614618
default_keymap=property(lambdaself:mpl.rcParams['keymap.back'])
615619
_on_trigger='back'
616620

@@ -619,7 +623,7 @@ class ToolForward(ViewsPositionsBase):
619623
"""Move forward in the view lim stack."""
620624

621625
description='Forward to next view'
622-
image='forward'
626+
image='mpl-data/images/forward'
623627
default_keymap=property(lambdaself:mpl.rcParams['keymap.forward'])
624628
_on_trigger='forward'
625629

@@ -628,14 +632,14 @@ class ConfigureSubplotsBase(ToolBase):
628632
"""Base tool for the configuration of subplots."""
629633

630634
description='Configure subplots'
631-
image='subplots'
635+
image='mpl-data/images/subplots'
632636

633637

634638
classSaveFigureBase(ToolBase):
635639
"""Base tool for figure saving."""
636640

637641
description='Save the figure'
638-
image='filesave'
642+
image='mpl-data/images/filesave'
639643
default_keymap=property(lambdaself:mpl.rcParams['keymap.save'])
640644

641645

@@ -710,7 +714,7 @@ class ToolZoom(ZoomPanBase):
710714
"""A Tool for zooming using a rectangle selector."""
711715

712716
description='Zoom to rectangle'
713-
image='zoom_to_rect'
717+
image='mpl-data/images/zoom_to_rect'
714718
default_keymap=property(lambdaself:mpl.rcParams['keymap.zoom'])
715719
cursor=cursors.SELECT_REGION
716720
radio_group='default'
@@ -832,7 +836,7 @@ class ToolPan(ZoomPanBase):
832836

833837
default_keymap=property(lambdaself:mpl.rcParams['keymap.pan'])
834838
description='Pan axes with left mouse, zoom with right'
835-
image='move'
839+
image='mpl-data/images/move'
836840
cursor=cursors.MOVE
837841
radio_group='default'
838842

@@ -896,7 +900,7 @@ def _mouse_move(self, event):
896900
classToolHelpBase(ToolBase):
897901
description='Print tool list, shortcuts and description'
898902
default_keymap=property(lambdaself:mpl.rcParams['keymap.help'])
899-
image='help'
903+
image='mpl-data/images/help'
900904

901905
@staticmethod
902906
defformat_shortcut(key_sequence):

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp