-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsub_notify.py
More file actions
executable file
·234 lines (179 loc) · 6.04 KB
/
sub_notify.py
File metadata and controls
executable file
·234 lines (179 loc) · 6.04 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
"""
SubNotify.
Copyright (c) 2013 - 2016 Isaac Muse <isaacmuse@gmail.com>
License: MIT
"""
import os
import sublime
import sublime_plugin
from .lib import notify
from .lib.notify.util import which
LINUX_PLAYERS = ["paplay", "aplay", "play"]
NOTIFY_EXT = {
"afplay": ['.wav', '.mp3', '.aiff'],
"windows": ['.wav'],
"paplay": ['.wav', '.mp3', '.ogg'],
"aplay": ['.wav', '.mp3'],
"play": ['.wav', '.mp3'],
}
PLUGIN_SETTINGS = "sub_notify.sublime-settings"
SUB_NOTIFY_READY = False
######################
# Settings
######################
def get_settings():
"""Get plugin settings."""
return sublime.load_settings(PLUGIN_SETTINGS)
######################
# Logging
######################
def log(msg, status=False):
"""Log messages."""
string = str(msg)
print("SubNotify: %s" % string)
if status:
sublime.status_message(string)
def debug_log(s):
"""Debug log messages."""
if get_settings().get("debug", False):
log(s)
######################
# Commands
######################
class SubNotifyCommand(sublime_plugin.ApplicationCommand):
"""SubNotify message command."""
def run(self, title, msg, sound=False, level="info"):
"""Run the command."""
if SubNotifyIsReadyCommand.is_ready():
if level == "error":
notify.error(title, msg, sound)
elif level == "warning":
notify.warning(title, msg, sound)
else:
notify.info(title, msg, sound)
else:
log("Not ready for messages yet!")
class SubNotifyTestCommand(sublime_plugin.ApplicationCommand):
"""SubNotify test command."""
def run(self):
"""Run the command."""
sublime.run_command("sub_notify", {"title": "SubNotify", "msg": "Debug test popup!", "sound": True})
class SubNotifyIsReadyCommand(sublime_plugin.ApplicationCommand):
"""Command to check if command is ready."""
@classmethod
def is_ready(cls):
"""Check if the command is ready."""
return SUB_NOTIFY_READY
def run(self):
"""Run the command."""
ready = SubNotifyIsReadyCommand.is_ready()
if ready:
log("Ready for messages!")
######################
# Setup
######################
def enable_notifications(notice=False):
"""Enable notifications."""
global SUB_NOTIFY_READY
SUB_NOTIFY_READY = False
notify.destroy_notifications()
# Create icon folder for systems that need a icon from path
png, icon, icns = get_icon_files()
if notify._PLATFORM == "windows":
img = icon
elif notify._PLATFORM == "macos":
img = icns
else:
img = png
player, audio = get_sound_settings()
# Set up notifications
notifier = get_settings().get(
"terminal_notifier_path",
"/usr/local/bin/terminal-notifier"
)
if (
os.path.isdir(notifier) and
notifier.endswith('.app') and
os.path.exists(os.path.join(notifier, 'Contents/MacOS/terminal-notifier'))
):
notifier = os.path.join(notifier, 'Contents/MacOS/terminal-notifier')
notify.setup_notifications(
"Sublime Text (SubNotify)",
img,
term_notify=notifier,
sender=None,
sound=audio,
sound_player=player
)
# Announce that SubNotify is ready
SUB_NOTIFY_READY = True
sublime.run_command("sub_notify_is_ready")
settings = get_settings()
# Setup reload
settings.clear_on_change('reload')
settings.add_on_change('reload', lambda: enable_notifications(True))
# Show notice of reload and show message to confirm change took place
if notice:
sublime.run_command(
"sub_notify",
{
"title": "SubNotify",
"msg": "Settings reloaded."
}
)
def get_icon_files():
"""Get icon files."""
settings = get_settings()
png_name = 'SublimeText@2x.png' if settings.get('large_icons', True) else 'SublimeText.png'
graphics = os.path.join(sublime.packages_path(), "SubNotify", "graphics")
png = os.path.join(graphics, png_name)
icon = os.path.join(graphics, "SublimeText.ico")
icns = None
if notify._PLATFORM == "macos":
icns = os.path.join(
os.path.dirname(sublime.executable_path()), "..", "Resources", "Sublime Text.icns"
)
if not os.path.exists(icns):
icns = None
if not os.path.exists(png):
png = None
if not os.path.exists(icon):
icon = None
return png, icon, icns
def get_sound_settings():
"""Get sound settings."""
if notify._PLATFORM == "windows":
player = 'windows'
audio = get_settings().get('windows_audio', 'C:/Windows/Media/notify.wav')
if not audio or not os.path.exists(audio) or os.path.splitext(audio)[1].lower() not in NOTIFY_EXT[player]:
audio = None
elif notify._PLATFORM == "macos":
player = 'afplay'
audio = get_settings().get('macos_audio', '/System/Library/Sounds/Glass.aiff')
if not audio or not os.path.exists(audio) or os.path.splitext(audio)[1].lower() not in NOTIFY_EXT[player]:
audio = None
else:
player = get_settings().get('linux_audio_player', 'paplay')
if not player or not which(player) or player not in LINUX_PLAYERS:
player = None
player_key = os.path.basename(player) if player else None
audio = get_settings().get('linux_audio', None)
if (
not player_key or
not audio or
not os.path.exists(audio) or
os.path.splitext(audio)[1].lower() not in NOTIFY_EXT[player_key]
):
audio = None
return player, audio
def plugin_loaded():
"""Setup plugin."""
# Try to enable notification systems
enable_notifications()
if get_settings().get("debug", False):
sublime.set_timeout(lambda: sublime.run_command("sub_notify_test"), 3000)
def plugin_unloaded():
"""Tear down plugin."""
global SUB_NOTIFY_READY
SUB_NOTIFY_READY = False
notify.destroy_notifications()