forked from dreadatour/Flake8Lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor_theme.py
203 lines (174 loc) · 6.39 KB
/
color_theme.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# -*- coding: utf-8 -*-
"""
Override Sublime Text color theme.
Add lint highlight colors and set gutter marks foreground color
for better visibility.
Based on https://github.com/JulianEberius/SublimePythonIDE
"""
import codecs
import os
import sys
from xml.etree import ElementTree
try:
from xml.parsers import expat # noqa
except ImportError:
# Add 'contrib' to sys.path to simulate installation
# of package 'elementtree_contrib'
CONTRIB_PATH = os.path.join(os.path.dirname(__file__), 'contrib')
if CONTRIB_PATH not in sys.path:
sys.path.insert(0, CONTRIB_PATH)
# this is fallback for systems without python-expat module installed
from elementtree_contrib import SimpleXMLTreeBuilder
ElementTree.XMLTreeBuilder = SimpleXMLTreeBuilder.TreeBuilder
import sublime
DEFAULT_MARK_COLORS = {
'critical': '#981600',
'error': '#DA2000',
'warning': '#EDBA00',
'gutter': '#FFFFFF',
}
COLOR_SCHEME_PREAMBLE = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">"""
COLOR_SCHEME_STYLES = {
'critical': """
<dict>
<key>name</key>
<string>Python Flake8 Lint Critical</string>
<key>scope</key>
<string>flake8lint.mark.critical</string>
<key>settings</key>
<dict>
<key>foreground</key>
<string>{0}</string>
</dict>
</dict>
""",
'error': """
<dict>
<key>name</key>
<string>Python Flake8 Lint Error</string>
<key>scope</key>
<string>flake8lint.mark.error</string>
<key>settings</key>
<dict>
<key>foreground</key>
<string>{0}</string>
</dict>
</dict>
""",
'warning': """
<dict>
<key>name</key>
<string>Python Flake8 Lint Warning</string>
<key>scope</key>
<string>flake8lint.mark.warning</string>
<key>settings</key>
<dict>
<key>foreground</key>
<string>{0}</string>
</dict>
</dict>
""",
'gutter': """
<dict>
<key>name</key>
<string>Python Flake8 Lint Gutter Mark</string>
<key>scope</key>
<string>flake8lint.mark.gutter</string>
<key>settings</key>
<dict>
<key>foreground</key>
<string>#FFFFFF</string>
</dict>
</dict>
"""
}
STYLE_MAP = {
'flake8lint.mark.critical': 'critical',
'flake8lint.mark.error': 'error',
'flake8lint.mark.warning': 'warning',
'flake8lint.mark.gutter': 'gutter',
}
def update_color_scheme(settings):
"""Modify the current color scheme to contain Flake8Lint color entries.
Asynchronously call generate_color_scheme_async.
"""
colors = {
'critical': settings.highlight_color_critical,
'error': settings.highlight_color_error,
'warning': settings.highlight_color_warning,
}
sublime3 = int(sublime.version()) >= 3000
def generate_color_scheme_async():
"""Modify current color scheme asynchronously."""
# find and parse current theme
prefs = sublime.load_settings('Preferences.sublime-settings')
scheme = prefs.get('color_scheme')
if scheme is None:
return
if sublime3:
scheme_text = sublime.load_resource(scheme)
else:
scheme = scheme[9:]
with open(os.path.join(sublime.packages_path(), scheme)) as f:
scheme_text = f.read()
try:
plist = ElementTree.XML(scheme_text)
except ImportError:
return
dicts = plist.find('./dict/array')
# find all style infos in the theme and update if necessary
theme_was_changed = False
unknown_styles = set(('critical', 'error', 'warning', 'gutter'))
for d in dicts.findall('./dict'):
for c in d.getchildren():
if c.text and 'flake8lint' in c.text:
style = STYLE_MAP.get(c.text)
if style not in DEFAULT_MARK_COLORS:
continue
color_elem = d.find('./dict/string')
found_color = color_elem.text.upper().lstrip('#')
our_color = colors.get(style) or DEFAULT_MARK_COLORS[style]
target_color = our_color.upper().lstrip('#')
if found_color != target_color:
theme_was_changed = True
color_elem.text = '#' + target_color
unknown_styles.discard(style)
break
# add defaults for all styles that were not found
for style in unknown_styles:
if style not in DEFAULT_MARK_COLORS:
continue
color = colors.get(style) or DEFAULT_MARK_COLORS[style]
if not color:
continue
dicts.append(ElementTree.XML(
COLOR_SCHEME_STYLES[style].format('#' + color.lstrip('#'))
))
theme_was_changed = True
# only write new theme if necessary
if not theme_was_changed:
return
# write new theme
original_name = os.path.splitext(os.path.basename(scheme))[0]
new_name = original_name + ' (Flake8Lint).tmTheme'
scheme_path = os.path.join(sublime.packages_path(), 'User', new_name)
if sublime3:
with open(scheme_path, 'w', encoding='utf-8') as f:
f.write(COLOR_SCHEME_PREAMBLE)
f.write(ElementTree.tostring(plist, encoding='unicode'))
else:
with codecs.open(scheme_path, 'w', encoding='utf-8') as f:
f.write(COLOR_SCHEME_PREAMBLE)
f.write(ElementTree.tostring(plist, encoding='utf-8'))
# ST does not expect platform specific paths here, but only
# forward-slash separated paths relative to "Packages"
new_theme_setting = '/'.join(['Packages', 'User', new_name])
prefs.set('color_scheme', new_theme_setting)
sublime.save_settings('Preferences.sublime-settings')
# run async
if sublime3:
sublime.set_timeout_async(generate_color_scheme_async, 0)
else:
sublime.set_timeout(generate_color_scheme_async, 100)