-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathopen_url.py
402 lines (332 loc) · 13.1 KB
/
open_url.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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
from __future__ import annotations
import os
import re
import subprocess
import threading
import webbrowser
from typing import TypedDict, cast
from urllib.parse import quote, urlparse
import sublime # type: ignore
import sublime_plugin # type: ignore
from .url import is_url
Settings = TypedDict(
"Settings",
{
"delimiters": str,
"trailing_delimiters": str,
"web_browser": str,
"web_browser_path": list,
"web_searchers": list,
"file_prefixes": list,
"file_suffixes": list,
"search_paths": list,
"aliases": dict,
"file_custom_commands": list,
"folder_custom_commands": list,
"other_custom_commands": list,
},
)
# these are necessary to convert settings object to a dict, which can then be merged with project settings
settings_keys = [
"delimiters",
"trailing_delimiters",
"web_browser",
"web_browser_path",
"web_searchers",
"file_prefixes",
"file_suffixes",
"search_paths",
"aliases",
"file_custom_commands",
"folder_custom_commands",
"other_custom_commands",
]
def prepend_scheme(s: str) -> str:
o = urlparse(s)
if not o.scheme:
s = "http://" + s
return s
def remove_trailing_delimiters(url: str, trailing_delimiters: str) -> str:
"""
Removes any and all chars in trailing_delimiters from end of url.
"""
if not trailing_delimiters:
return url
while url:
if url[-1] in trailing_delimiters:
url = url[:-1]
else:
break
return url
def match_openers(openers: list[dict], url: str) -> list[dict]:
ret: list[dict] = []
platform = sublime.platform()
for opener in openers:
pattern: str | None = opener.get("pattern")
o_s: str | None = opener.get("os")
if pattern and not re.search(pattern, url):
continue
if o_s and not o_s.lower() == platform:
continue
ret.append(opener)
return ret
def resolve_aliases(url: str, aliases: dict) -> str:
for key, val in aliases.items():
url = url.replace(key, val)
return url
def generate_urls(
url: str, search_paths: list[str], file_prefixes: list[str], file_suffixes: list[str], trailing_delimiters: str
):
urls: list[str] = []
bare_urls = [url]
clean = remove_trailing_delimiters(url, trailing_delimiters)
if clean != url:
bare_urls.append(clean)
for u in bare_urls:
for path in [""] + search_paths:
d, base = os.path.split(os.path.join(path, u))
for prefix in [""] + file_prefixes:
for suffix in [""] + file_suffixes:
urls.append(os.path.join(d, prefix + base + suffix))
return urls
def merge_settings(window, keys: list[str]) -> Settings:
settings_object = sublime.load_settings("open_url.sublime-settings")
settings = cast(Settings, {k: settings_object.get(k) for k in keys})
project = window.project_data()
if project is None:
return settings
try:
for k, v in project["settings"]["open_url"].items():
settings[k] = v
return settings
except Exception:
return settings
class OpenUrlCommand(sublime_plugin.TextCommand):
config: Settings
def run(
self,
edit=None,
url: str | None = None,
show_menu: bool = True,
show_input: bool = False,
) -> None:
self.config = merge_settings(self.view.window(), settings_keys)
if show_input:
def on_done(input_url: str):
self.handle(input_url, show_menu)
self.view.window().show_input_panel("Path:", "", on_done, None, None)
return
# Sublime Text has its own open_url command used for things like Help > Documentation
# so if a url is passed, open it instead of getting text from the view
if url is not None:
urls = [url]
else:
urls = [self.get_selection(region) for region in self.view.sel()]
if len(urls) > 1:
show_menu = False
for url in urls:
self.handle(url, show_menu)
def handle(self, url: str, show_menu: bool) -> None:
url = resolve_aliases(url, self.config["aliases"])
urls = generate_urls(
url,
self.config["search_paths"],
self.config["file_prefixes"],
self.config["file_suffixes"],
self.config["trailing_delimiters"],
)
for u in urls:
path = self.abs_path(u)
if os.path.isfile(path):
self.file_action(path, show_menu, u)
return
if self.view.file_name() and not u:
# open current file if url is empty
self.file_action(self.view.file_name(), show_menu, self.view.file_name())
return
if os.path.isdir(path):
self.folder_action(path, show_menu, u)
return
clean_path = remove_trailing_delimiters(url, self.config["trailing_delimiters"])
if is_url(clean_path) or clean_path.startswith("http://") or clean_path.startswith("https://"):
self.open_tab(prepend_scheme(clean_path))
return
openers = match_openers(self.config["other_custom_commands"], clean_path)
if openers:
self.other_action(clean_path, openers, show_menu)
return
self.modify_or_search_action(url)
def get_selection(self, region) -> str:
"""Returns selection. If selection contains no characters, expands it
until hitting delimiter chars.
"""
start: int = region.begin()
end: int = region.end()
if start != end:
sel: str = self.view.substr(sublime.Region(start, end))
return sel.strip()
# nothing is selected, so expand selection to nearest delimiters
view_size: int = self.view.size()
delimiters = list(self.config["delimiters"])
# move the selection back to the start of the url
while start > 0:
if self.view.substr(start - 1) in delimiters:
break
start -= 1
# move end of selection forward to the end of the url
while end < view_size:
if self.view.substr(end) in delimiters:
break
end += 1
sel = self.view.substr(sublime.Region(start, end))
return sel.strip()
def file_path(self):
path = self.view.file_name()
if path: # this file has been saved to disk
return os.path.dirname(path)
return None
def project_path(self):
project = self.view.window().project_data()
if project is None:
return None
try:
return os.path.expanduser(project["folders"][0]["path"])
except Exception:
return None
def abs_path(self, path: str) -> str:
"""Normalizes path, and attempts to convert path into absolute path."""
path = os.path.normcase(os.path.expandvars(os.path.expanduser(path)))
if os.path.isabs(path):
return path
file_path = self.file_path()
if file_path:
abs_path = os.path.join(file_path, path)
if os.path.exists(abs_path): # if file relative to current view exists, open it, else continue
return abs_path
project_path = self.project_path()
if project_path is None: # nothing more to try
return path
return os.path.join(project_path, path)
def prepare_args_and_run(self, opener: dict, path: str):
commands = opener.get("commands", [])
kwargs = opener.get("kwargs", {})
cwd = kwargs.get("cwd")
if cwd == "project_root":
project_path = self.project_path()
if project_path:
kwargs["cwd"] = project_path
if cwd == "current_file":
file_path = self.file_path()
if file_path:
kwargs["cwd"] = file_path
if isinstance(commands, str):
kwargs["shell"] = True
if "$url" in commands:
self.run_subprocess(commands.replace("$url", path), kwargs)
else:
self.run_subprocess(f"{commands} {path}", kwargs)
else:
has_url = any("$url" in command for command in commands)
if has_url:
self.run_subprocess([command.replace("$url", path) for command in commands], kwargs)
else:
self.run_subprocess(commands + [path], kwargs)
def run_subprocess(self, args, kwargs):
"""Runs on another thread to avoid blocking main thread."""
def sp(args, kwargs):
subprocess.check_call(args, **kwargs)
threading.Thread(target=sp, args=(args, kwargs)).start()
def open_tab(self, url: str) -> None:
browser = self.config["web_browser"]
browser_path = self.config["web_browser_path"]
def ot(url, browser, browser_path):
if browser_path:
if not webbrowser.get(browser_path).open(url):
sublime.error_message(f'Could not open tab using your "web_browser_path" setting: {browser_path}')
return
try:
controller = webbrowser.get(browser or None)
except Exception:
e = 'Python couldn\'t find the "{}" browser. Change "web_browser" in Open URL\'s settings.'
sublime.error_message(e.format(browser or "default"))
return
controller.open_new_tab(url)
threading.Thread(target=ot, args=(url, browser, browser_path)).start()
def modify_or_search_action(self, term: str):
"""Not a URL and not a local path; prompts user to modify path and looks
for it again, or searches for this term using a web searcher.
"""
searchers = self.config["web_searchers"]
opts = [f"modify path {term}"]
opts += [f'{s["label"]} ({term})' for s in searchers]
sublime.active_window().show_quick_panel(opts, lambda idx: self.modify_or_search_done(idx, searchers, term))
def modify_or_search_done(self, idx: int, searchers, term: str):
if idx < 0:
return
if idx == 0:
self.view.window().show_input_panel("URL or path:", term, self.url_search_modified, None, None)
return
idx -= 1
searcher = searchers[idx]
self.open_tab(
"{}{}".format(
searcher.get("url"),
quote(term.encode(searcher.get("encoding", "utf-8"))),
)
)
def url_search_modified(self, text: str):
"""Call open_url again on modified path."""
try:
self.view.run_command("open_url", {"url": text})
except ValueError:
pass
def other_action(self, path: str, openers: list[dict], show_menu: bool):
if openers and not show_menu:
self.other_done(0, openers, path)
return
opts = [opener.get("label") for opener in openers]
sublime.active_window().show_quick_panel(opts, lambda idx: self.other_done(idx, openers, path))
def other_done(self, idx, openers, path):
if idx < 0:
return
opener = openers[idx]
self.prepare_args_and_run(opener, path)
def folder_action(self, folder: str, show_menu: bool, raw_folder: str):
"""Choose from folder actions."""
openers = match_openers(self.config["folder_custom_commands"], folder)
if openers and not show_menu:
self.folder_done(0, openers, folder, raw_folder)
return
opts = [*[opener.get("label") for opener in openers], "search..."]
sublime.active_window().show_quick_panel(opts, lambda idx: self.folder_done(idx, openers, folder, raw_folder))
def folder_done(self, idx: int, openers: list[dict], folder: str, raw_folder: str):
if idx < 0:
return
if idx >= len(openers):
self.modify_or_search_action(raw_folder)
opener = openers[idx]
if sublime.platform() == "windows":
folder = os.path.normcase(folder)
self.prepare_args_and_run(opener, folder)
def file_action(self, path: str, show_menu: bool, raw_path: str) -> None:
"""Edit file or choose from file actions."""
openers = match_openers(self.config["file_custom_commands"], path)
if not show_menu:
self.view.window().open_file(path)
return
sublime.active_window().show_quick_panel(
["edit", *[opener.get("label") for opener in openers], "search..."],
lambda idx: self.file_done(idx, openers, path, raw_path),
)
def file_done(self, idx: int, openers: list[dict], path: str, raw_path: str):
if idx < 0:
return
if idx == 0:
self.view.window().open_file(path)
return
if idx >= len(openers) + 1:
self.modify_or_search_action(raw_path)
opener = openers[idx - 1]
if sublime.platform() == "windows":
path = os.path.normcase(path)
self.prepare_args_and_run(opener, path)