(xdg_base_getter)
| 526 | |
| 527 | |
| 528 | def _get_config_or_cache_dir(xdg_base_getter): |
| 529 | configdir = os.environ.get('MPLCONFIGDIR') |
| 530 | if configdir: |
| 531 | configdir = Path(configdir) |
| 532 | elif sys.platform.startswith(('linux', 'freebsd')): |
| 533 | # Only call _xdg_base_getter here so that MPLCONFIGDIR is tried first, |
| 534 | # as _xdg_base_getter can throw. |
| 535 | try: |
| 536 | configdir = Path(xdg_base_getter(), "matplotlib") |
| 537 | except RuntimeError: # raised if Path.home() is not available |
| 538 | pass |
| 539 | elif sys.platform == 'win32': |
| 540 | # On Windows, prefer %LOCALAPPDATA%\matplotlib which is the proper |
| 541 | # location for non-roaming application data (cache and config). |
| 542 | # See: https://docs.microsoft.com/en-us/windows/apps/design/app-settings/store-and-retrieve-app-data |
| 543 | # |
| 544 | # However, for backwards compatibility, if the old location |
| 545 | # (%USERPROFILE%\.matplotlib) exists, continue using it so existing |
| 546 | # users don't lose their config. |
| 547 | try: |
| 548 | old_configdir = Path.home() / ".matplotlib" |
| 549 | if old_configdir.is_dir(): |
| 550 | configdir = old_configdir |
| 551 | else: |
| 552 | localappdata = os.environ.get('LOCALAPPDATA') |
| 553 | if localappdata: |
| 554 | configdir = Path(localappdata) / "matplotlib" |
| 555 | else: |
| 556 | configdir = old_configdir |
| 557 | except RuntimeError: # raised if Path.home() is not available |
| 558 | localappdata = os.environ.get('LOCALAPPDATA') |
| 559 | if localappdata: |
| 560 | configdir = Path(localappdata) / "matplotlib" |
| 561 | else: |
| 562 | try: |
| 563 | configdir = Path.home() / ".matplotlib" |
| 564 | except RuntimeError: # raised if Path.home() is not available |
| 565 | pass |
| 566 | |
| 567 | if configdir: |
| 568 | # Resolve the path to handle potential issues with inaccessible symlinks. |
| 569 | configdir = configdir.resolve() |
| 570 | try: |
| 571 | configdir.mkdir(parents=True, exist_ok=True) |
| 572 | except OSError as exc: |
| 573 | _log.warning("mkdir -p failed for path %s: %s", configdir, exc) |
| 574 | else: |
| 575 | if os.access(str(configdir), os.W_OK) and configdir.is_dir(): |
| 576 | return str(configdir) |
| 577 | _log.warning("%s is not a writable directory", configdir) |
| 578 | issue_msg = "the default path ({configdir})" |
| 579 | else: |
| 580 | issue_msg = "resolving the home directory" |
| 581 | # If the config or cache directory cannot be created or is not a writable |
| 582 | # directory, create a temporary one. |
| 583 | try: |
| 584 | tmpdir = tempfile.mkdtemp(prefix="matplotlib-") |
| 585 | except OSError as exc: |
no test coverage detected
searching dependent graphs…