File: setup_mplcursors_pth.py

package info (click to toggle)
mplcursors 0.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 400 kB
  • sloc: python: 1,915; makefile: 14; sh: 9
file content (49 lines) | stat: -rw-r--r-- 2,227 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import os


if os.environ.get("MPLCURSORS"):
    # We cannot directly import matplotlib if `MPLCURSORS` is set because
    # `sys.path` is not correctly set yet.
    # The loading of `matplotlib.figure` does not go through the path entry
    # finder because it is a submodule, so we use a metapath finder instead.

    from importlib.machinery import PathFinder
    import sys

    class MplcursorsMetaPathFinder(PathFinder):
        def find_spec(self, fullname, path=None, target=None):
            spec = super().find_spec(fullname, path, target)
            if fullname == "matplotlib.figure":
                def exec_module(module):
                    type(spec.loader).exec_module(spec.loader, module)
                    # The pth file does not get properly uninstalled from a
                    # develop install.  See pypa/pip#4176.
                    try:
                        import mplcursors
                    except ImportError:
                        return
                    import functools
                    import json
                    import weakref
                    # Ensure that when the cursor is removed(), or gets GC'd
                    # because its referents artists are GC'd, the entry also
                    # disappears.
                    cursors = weakref.WeakValueDictionary()
                    options = json.loads(os.environ["MPLCURSORS"])
                    @functools.wraps(module.Figure.draw)
                    def wrapper(self, *args, **kwargs):
                        rv = wrapper.__wrapped__(self, *args, **kwargs)
                        if self not in cursors:
                            cursor = mplcursors.cursor(self, **options)
                            if cursor.artists:
                                cursors[self] = cursor
                            else:
                                # No artist yet; skip possible init code.
                                cursor.remove()
                        return rv
                    module.Figure.draw = wrapper
                spec.loader.exec_module = exec_module
                sys.meta_path.remove(self)
            return spec

    sys.meta_path.insert(0, MplcursorsMetaPathFinder())