-
Notifications
You must be signed in to change notification settings - Fork 136
/
gitio.py
47 lines (38 loc) · 1.36 KB
/
gitio.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
import contextlib
from urllib.error import URLError, HTTPError
from urllib.parse import urlencode
from urllib.request import urlopen
try:
import sublime
import sublime_plugin
except ImportError:
from test.stubs import sublime
from test.stubs import sublime_plugin
gitio_url = "https://git.io/create"
caption = "GitHub URL:"
def gitio(req_url):
data = urlencode({"url": req_url}).encode()
try:
with contextlib.closing(urlopen(gitio_url, data)) as response:
body = response.read().decode()
if response.status == 200:
return None, "https://git.io/{}".format(body)
return body, None
except HTTPError as e:
with contextlib.closing(e):
return e.read().decode(), None
except URLError:
return "Gist: Error contacting git.io", None
class GitioCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.window().show_input_panel(caption, "", self.on_done, None, None)
def on_done(self, req_url):
err, short_url = gitio(req_url)
if err:
sublime.error_message(err)
self.view.window().show_input_panel(
caption, req_url, self.on_done, None, None
)
else:
sublime.set_clipboard(short_url)
sublime.status_message("Gist: Copied to Clipboard! " + short_url)