-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathpath.py
164 lines (124 loc) · 4.36 KB
/
path.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"""A module with tools for paths."""
import os
try:
from . import st_helper
from .settings import COLOR_HIGHLIGHTER_SETTINGS_NAME
except ValueError:
from settings import COLOR_HIGHLIGHTER_SETTINGS_NAME
import st_helper
if st_helper.running_in_st():
import sublime # pylint: disable=import-error
else:
from . import sublime
def normalize_path_for_st(path):
"""
Normalize path for ST.
On Linux, does nothing. On windows, maps windows directory separators to linux ones, as ST wants linux
directory separators.
Arguments:
- path - the path to normalize.
"""
if sublime.platform() == "windows":
return path.replace("\\", "/")
return path
RELATIVE = True
ABSOLUTE = False
def plugin_name():
"""
Determine if the plugin is installed via a Package Control package or manually.
Returns the plugin directory or package name.
"""
manual_install_plugin_name = "ColorHighlighter"
package_install_plugin_name = "Color Highlighter"
if (os.path.exists(os.path.join(
packages_path(ABSOLUTE), manual_install_plugin_name, COLOR_HIGHLIGHTER_SETTINGS_NAME))):
return manual_install_plugin_name
return package_install_plugin_name
def packages_path(relative):
"""
Get packages path.
Arguments:
- relative - whether to get an absolute path or a relative to sublime packages directory.
"""
path = sublime.packages_path()
if relative:
path = os.path.basename(path)
return path
def data_path(relative):
"""
Get Color Highlighter path.
Arguments:
- relative - whether to get an absolute path or a relative to sublime packages directory.
"""
return os.path.join(packages_path(relative), "User", plugin_name())
def icons_path(relative):
"""
Get Color Highlighter icons path.
Arguments:
- relative - whether to get an absolute path or a relative to sublime packages directory.
"""
return os.path.join(data_path(relative), "icons")
def themes_path(relative):
"""
Get Color Highlighter themes path.
Arguments:
- relative - whether to get an absolute path or a relative to sublime packages directory.
"""
return os.path.join(data_path(relative), "themes")
def color_picker_path(relative):
"""
Get color picker directory path.
Arguments:
- relative - whether to get an absolute path or a relative to sublime packages directory.
"""
return os.path.join(data_path(relative), "ColorPicker")
def _color_picker_file():
executable_suffix = None
platform = sublime.platform()
if platform == "windows":
executable_suffix = "win.exe"
else:
executable_suffix = "%s_%s" % (platform, sublime.arch())
return "ColorPicker_" + executable_suffix
def color_picker_file(relative):
"""
Get color picker file.
Arguments:
- relative - whether to get an absolute path or a relative to sublime packages directory.
"""
return os.path.join(color_picker_path(relative), _color_picker_file())
def color_picker_binary(relative):
"""
Get color picker file.
Arguments:
- relative - whether to get an absolute path or a relative to sublime packages directory.
"""
path = os.path.join(packages_path(relative), plugin_name(), "ColorPicker", _color_picker_file())
if relative:
path = normalize_path_for_st(path)
return path
def fake_color_scheme_path(color_scheme, relative):
"""
Given a color scheme, get a fake color scheme path.
Arguments:
- color_scheme - color scheme sublime relative path.
- relative - whether to get an absolute path or a relative to sublime packages directory.
Returns a path to the fake color scheme for this color scheme.
"""
file_name = os.path.basename(color_scheme)
path = os.path.join(themes_path(relative), file_name)
if relative:
path = normalize_path_for_st(path)
return path
def cached_scheme_path(color_scheme):
"""
Get the .cache file path for a color scheme file.
Arguments:
- color_scheme - the absolute color scheme file path.
"""
cache_suffix = ".cache"
if st_helper.is_st3():
packages = packages_path(ABSOLUTE)
cache_dir = os.path.join(os.path.dirname(packages), "Cache")
return os.path.join(cache_dir, color_scheme[len(packages) + 1:] + cache_suffix)
return color_scheme + cache_suffix