-
Notifications
You must be signed in to change notification settings - Fork 248
/
Copy pathgistcheckMichaeluhl.py
123 lines (111 loc) · 3.49 KB
/
gistcheckMichaeluhl.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
from __future__ import print_function
# https://forum.omz-software.com/topic/795/sharing-code-on-github/29
# https://gist.github.com/michaeluhl/4043334
import base64
import console
import editor
import json
import keychain
import os.path
import pickle
import re
import urllib2
#Perform authorization
def auth(username, password):
data='{"scopes":["gist"],"note":"gistcheck"}'
request = urllib2.Request("https://api.github.com/authorizations",data)
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):
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()
#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()
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 not None:
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 = 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)
set_gist_id(editor.get_path(),gist)
def setup():
if not os.path.exists('gistcheck_commit.py'):
f = open('gistcheck_commit.py','w')
f.writelines(['import gistcheck\n','gistcheck.commit()'])
f.close()
if not os.path.exists('gistcheck_pull.py'):
f = open('gistcheck_pull.py','w')
f.writelines(['import gistcheck\n','gistcheck.pull()'])
f.close()
if not os.path.exists('gistcheck_set.py'):
f = open('gistcheck_set.py','w')
f.writelines(['import gistcheck\n','gistcheck.set()'])
f.close()
if __name__ == '__main__':
setup()