/
image_path_manager.py
65 lines (53 loc) · 2.13 KB
/
image_path_manager.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
#
# Copyright © Spyder Project Contributors
# Licensed under the terms of the MIT License
# (see spyder/__init__.py for details)
# Standard library imports
import os
import os.path as osp
import warnings
# Local imports
from spyder.config.base import get_module_data_path
from spyder.config.gui import is_dark_interface
# =============================================================================
# Image path list
# =============================================================================
class ImagePathManager():
"""Manager of the image path in the project."""
def __init__(self):
"""Initialize main path with all the images."""
self.IMG_PATH = {}
self.add_image_path(get_module_data_path('spyder', relpath='images'))
self.default = 'not_found'
def add_image_path(self, path):
"""Add path to the image path list."""
if not osp.isdir(path):
return
for dirpath, __, _filenames in os.walk(path):
if is_dark_interface() and osp.basename(dirpath) == 'light':
continue
elif not is_dark_interface() and osp.basename(dirpath) == 'dark':
continue
for filename in _filenames:
if filename.startswith('.'):
continue
name, __ = osp.splitext(osp.basename(filename))
complete_path = osp.join(dirpath, filename)
if name in self.IMG_PATH:
warnings.warn(
f'The icon located in {complete_path} is overriding '
f'the existing {name}')
self.IMG_PATH[name] = complete_path
def get_image_path(self, name):
"""Get path of the image given its name."""
try:
act_image = self.IMG_PATH[name]
if osp.isfile(act_image):
return osp.abspath(act_image)
except KeyError:
return osp.abspath(self.IMG_PATH[self.default])
IMAGE_PATH_MANAGER = ImagePathManager()
def get_image_path(name):
"""Return absolute image path."""
return IMAGE_PATH_MANAGER.get_image_path(name)