-
Notifications
You must be signed in to change notification settings - Fork 248
/
Copy pathgistcheckWestacular.py
257 lines (240 loc) · 7.61 KB
/
gistcheckWestacular.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
from __future__ import print_function
# https://forum.omz-software.com/topic/795/sharing-code-on-github/29
# Source: https://gist.github.com/4145515
#
# All-purpose gist tool for Pythonista.
#
# When run directly, this script sets up four other scripts that call various
# functions within this file. Each of these sub-scripts are meant for use as
# action menu items. They are:
#
# Set Gist ID.py - Set the gist id that the current file should be
# associated with.
#
# Download Gist.py - Download the gist from the URL on the clipboard, and
# automatically set the association for it (if possible).
#
# Commit Gist.py - Commits the currently open file to the associated gist.
#
# Pull Gist.py - Replaces the current file with the latest version from
# the associated gist.
#
# Download Gist.py can also be run as a bookmarklet from Mobile Safari (to
# jump from browsing a gist directly to downloading it in Pythonista) by
# creating a bookmark to:
#
# javascript:(function()%7Bif(document.location.href.indexOf('http')===0)document.location.href='pythonista://Download%20Gist?action=run&argv='+document.location.href;%7D)();
#
#
# Credits:
#
# This combines the gist pull/commit/set id scripts from
# https://gist.github.com/4043334
#
# with the bookmarklet-compatible, multi-gist download script
# https://gist.github.com/5f3f2035d8aa46de42ad
#
# (which in turn is based on https://gist.github.com/4036200,
# which is based on https://gist.github.com/b0644f5ed1d94bd32805)
import console
import editor
import json
import keychain
import os.path
import pickle
import re
import urllib2
class InvalidGistURLError (Exception): pass
class NoFilesInGistError (Exception): pass
class GistDownloadError (Exception): pass
class InvalidGistIDError (Exception): pass
#Perform authorization
def auth(username, password):
data='{"scopes":["gist"],"note":"gistcheck"}'
request = urllib2.Request("https://api.github.com/authorizations",data)
import base64
enc = base64.standard_b64encode('%s:%s' % (username, password))
request.add_header("Authorization", "Basic %s" % enc)
result = urllib2.urlopen(request)
rdata = result.read()
result.close()
return rdata
def edit(gist, files, token, message=None):
reqdict = {"files":{}}
if message is not None: reqdict['description']=message
for f, c in files.items():
reqdict['files'][f] = {"content":c}
request = urllib2.Request("https://api.github.com/gists/%s" % gist,json.dumps(reqdict))
request.add_header("Authorization", "token %s" % token)
request.add_header('Content-Type','application/json')
try:
result = urllib2.urlopen(request)
rdata = result.read()
result.close()
return rdata
except Exception as e:
print(e)
return None
def get_gist_id(fname):
if os.path.exists('gistcheck.db'):
dbfile = open('gistcheck.db','r')
db = pickle.load(dbfile)
dbfile.close()
return db.get(fname,None)
return None
def set_gist_id(fname, gist):
gist = extract_gist_id(gist)
db = {}
if os.path.exists('gistcheck.db'):
dbfile = open('gistcheck.db','r')
db = pickle.load(dbfile)
dbfile.close()
db[fname]=gist
dbfile=open('gistcheck.db','w')
pickle.dump(db,dbfile)
dbfile.close()
def extract_gist_id(gist):
if re.match(r'^([0-9a-f]+)$', gist):
return gist
m = re.match(r'^http(s?)://gist.github.com/([0-9a-f]*)$', gist)
if m:
return m.group(2)
else:
raise InvalidGistIDError()
#load a file from a gist
def load(gist, fname):
request = urllib2.Request("https://api.github.com/gists/%s" % gist)
try:
result = urllib2.urlopen(request)
rdata = json.loads(result.read())
result.close()
return rdata['files'][fname]['content']
# url = rdata['files'][fname]['raw_url']
# result = urllib2.urlopen(url)
# rdata = result.read()
# result.close()
# return rdata
except Exception as e:
print(e)
return None
def pull():
gist = get_gist_id(editor.get_path())
if gist is None:
console.alert('Error', 'There is no gist id set for this file')
else:
fname = os.path.basename(editor.get_path())
newtxt = load(gist,fname)
if newtxt is not None:
editor.replace_text(0,len(editor.get_text()),newtxt)
def commit():
gist = get_gist_id(editor.get_path())
if gist is not None:
token = keychain.get_password('gistcheck','gistcheck')
if token is None:
u, p = console.login_alert('GitHub Login')
r = json.loads(auth(u, p))
print(r)
token = r['token']
keychain.set_password('gistcheck','gistcheck',token)
fname = os.path.basename(editor.get_path())
m = console.input_alert('Edit Description','Enter a new description:','')
if m == '': m = None
return edit(gist,{fname:editor.get_text()},token,m)
def set():
gist = get_gist_id(editor.get_path())
if gist == None: gist = ''
gist = console.input_alert('Assign Gist ID','Enter the gist id for this file',gist)
try:
set_gist_id(editor.get_path(),gist)
except InvalidGistIDError:
console.alert('Invalid Gist ID', 'That does not appear to be a valid gist id')
def download_gist(gist_url):
# Returns a 2-tuple of filename and content
# console.show_activity()
raw_match = re.match('http(s?)://raw.github.com/gist/', gist_url)
if raw_match:
import requests
from urlparse import urlparse
filename = os.path.split(urlparse(gist_url).path)[1]
try:
r = requests.get(gist_url)
content = r.text
yield filename, content
except:
raise GistDownloadError()
else:
gist_id_match = re.match('http(s?)://gist.github.com/([0-9a-f]*)', gist_url)
if gist_id_match:
import requests
gist_id = gist_id_match.group(2)
json_url = 'https://api.github.com/gists/' + gist_id
try:
import json
gist_json = requests.get(json_url).text
gist_info = json.loads(gist_json)
files = gist_info['files']
except:
raise GistDownloadError()
for file_info in files.values():
lang = file_info.get('language', None)
if lang != 'Python':
continue
filename = file_info['filename']
content = file_info['content']
yield filename, content
else:
raise InvalidGistURLError()
def download(gist_url):
num = 0
try:
for num, (filename, content) in enumerate(download_gist(gist_url), start=1):
if os.path.isfile(filename):
i = console.alert('File exists', 'A file with the name ' + filename +
' already exists in your library.',
'Auto Rename', 'Skip')
if i == 1:
editor.make_new_file(filename, content)
else:
editor.make_new_file(filename, content)
try:
set_gist_id(editor.get_path(), gist_url)
except InvalidGistIDError:
# console.alert('Gist ID not set', filename + '\n' + gist_url)
pass
except InvalidGistURLError:
console.alert('No Gist URL',
'Invalid Gist URL.',
'OK')
except GistDownloadError:
console.alert('Error', 'The Gist could not be downloaded.')
if not num:
console.alert('No Python Files', 'This Gist contains no Python files.')
def setup():
if not os.path.exists('Commit Gist.py'):
f = open('Commit Gist.py','w')
f.writelines(['import gistcheck\n','gistcheck.commit()\n'])
f.close()
if not os.path.exists('Pull Gist.py'):
f = open('Pull Gist.py','w')
f.writelines(['import gistcheck\n','gistcheck.pull()\n'])
f.close()
if not os.path.exists('Set Gist ID.py'):
f = open('Set Gist ID.py','w')
f.writelines(['import gistcheck\n','gistcheck.set()\n'])
f.close()
if not os.path.exists('Download Gist.py'):
f = open('Download Gist.py','w')
from textwrap import dedent
f.write(dedent('''\
import gistcheck
import clipboard
if __name__ == '__main__':
if len(sys.argv) == 2:
url = sys.argv[1]
else:
url = clipboard.get()
gistcheck.download(url)
'''))
f.close()
if __name__ == '__main__':
setup()