-
Notifications
You must be signed in to change notification settings - Fork 0
/
upgist.py
320 lines (284 loc) · 12.8 KB
/
upgist.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
import os,sys
import json
import click
import requests
from rich import box
from rich.table import Table
from rich.syntax import Syntax
from rich.console import Console
console = Console()
gistAPI = "https://api.github.com/gists"
configFolderPath = os.path.expanduser('~') + os.sep +".config" + os.sep + "upgist"
configFileName = "config.txt"
configFilePath = configFolderPath + os.sep + configFileName
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
class GistFile():
def __init__(self, description, public, fileName, content, fileID):
self.fileID = fileID
self.postFile = {
"description" : description,
"public" : public,
"files" : {
fileName : {
"content" : content if content != None else self.__GetContent(fileName),
}
}
}
def __GetContent(self,fileName):
file = open(fileName,"r")
content = file.read()
file.close()
return content
class Gist:
def __init__(self):
self.headers = {
"Authorization" : "token " + self.__GetToken()
}
def GetGistRaw(self, gistID):
fileList = []
result = requests.get(url=gistAPI + "/{gistID}".format(gistID=gistID),headers=self.headers)
if self.__isError(result.status_code):
console.print("[bold red][ERROR][/bold red] {errMsg}".format(errMsg=result.json()['message']))
return
gistInfo = result.json()
for file in gistInfo['files']:
gistFile = open(file,"w+")
gistFile.write(gistInfo['files'][file]['content'])
gistFile.close()
def ViewGist(self, gistID):
fileList = []
result = requests.get(url=gistAPI + "/{gistID}".format(gistID=gistID),headers=self.headers)
if self.__isError(result.status_code):
console.print("[bold red][ERROR][/bold red] {errMsg}".format(errMsg=result.json()['message']))
return
gistInfo = result.json()
table = Table(box=box.ROUNDED,show_lines=True)
table.add_column(":open_file_folder:files",style="cyan")
table.add_column(":memo:content",style="purple")
for file in gistInfo['files']:
table.add_row(gistInfo['files'][file]['filename'],Syntax(gistInfo['files'][file]['content'], gistInfo['files'][file]['language'], theme="vim", line_numbers=True))
return table
def List(self,isShowID):
result = requests.get(url=gistAPI,headers=self.headers)
if self.__isError(result.status_code):
console.print("[bold red][ERROR][/bold red] {errMsg}".format(errMsg=result.json()['message']))
return
if not isShowID:
for gistInfo in result.json():
table = self.ViewGist(gistInfo['id'])
console.print(":link:[bold blue][link={url}]Gist ID[/link][/bold blue]: {gistID}".format(gistID=gistInfo.get('id'),url=gistInfo.get('html_url')))
console.print(table)
else:
table = Table(box=box.ROUNDED,show_lines=True)
table.add_column(":id:ID",style="blue")
table.add_column(":open_file_folder:files",style="cyan")
for gistInfo in result.json():
fileNameList = []
for file in gistInfo['files']:
fileNameList.append(gistInfo['files'][file]['filename'])
table.add_row("[link={url}]{gistID}[/link]".format(gistID=gistInfo.get('id'),url=gistInfo.get('html_url')),"\n".join(fileNameList))
console.print(table)
def Creat(self, file):
result = requests.post(url=gistAPI,data=json.dumps(file.postFile),headers=self.headers)
if self.__isError(result.status_code):
console.print("[bold red][ERROR][/bold red] {errMsg}".format(errMsg=result.json()['message']))
return
console.print(":tada:[bold green]Successfully created[/bold green]: {url}".format(url=result.json()['html_url']))
def Modify(self, file):
result = requests.post(url=gistAPI + "/{id}".format(id=file.fileID), data=json.dumps(file.postFile), headers=self.headers)
if self.__isError(result.status_code):
console.print("[bold red][ERROR][/bold red] {errMsg}".format(errMsg=result.json()['message']))
return
console.print(":tada:[bold green]Successfully modify[/bold green] : {url}".format(url=result.json()['html_url']))
def Delete(self, gistID):
result = requests.delete(url=gistAPI + "/{gistID}".format(gistID=gistID),headers=self.headers)
if self.__isError(result.status_code):
console.print("[bold red][ERROR][/bold red] {errMsg}".format(errMsg=result.json()['message']))
return
def __isError(self, code):
successCode = [200,201,204]
return code not in successCode
def __GetToken(self):
file = os.path.exists(configFilePath)
if not file:
console.print("Pleace use the following command to set your token:\ngist config -tk YOUR_TOKEN")
sys.exit(1)
else:
configFile = open(configFilePath,"r")
token = configFile.read()
return token
def SaveToken(token):
file = os.path.exists(configFolderPath)
if not file:
os.makedirs(configFolderPath)
with open(configFilePath,"w") as configFile:
configFile.write(token)
configFile.close()
def PrintVersion(ctx,param,value):
if not value or ctx.resilient_parsing:
return
print('Version 0.0.2')
ctx.exit()
def Main_Options(f):
version = click.option('--version',
'-v',
is_flag=True,
callback=PrintVersion,
expose_value=False,
is_eager=True)
return version(f)
def Config_Options(f):
token = click.option('--token',
'-tk',
'token',
nargs=1,
required=True,
multiple=False,
help="Set your token to client gist")
return token(f)
def ViewGist_Options(f):
gistid = click.option('--gistid',
'-id',
'gistID',
nargs=1,
required=True,
multiple=False,
help="View a gist by gist ID")
return gistid(f)
def GetGistRaw_Options(f):
gistid = click.option('--gistid',
'-id',
'gistID',
nargs=1,
required=True,
multiple=False,
help="Get a gist and save by gist ID")
return gistid(f)
def DeleteGist_Options(f):
gistid = click.option('--gistid',
'-id',
'gistID',
nargs=1,
required=True,
multiple=False,
help="Delete a gist by gist ID")
return gistid(f)
def CreatGist_Options(f):
file = click.option('--file',
'-f',
'fileName',
nargs=1,
required=True,
multiple=False,
help="File name of gist")
public = click.option('--public',
'-p','public',
nargs=1,
default="true",
multiple=False,
type=click.Choice(["true", "false"]),
show_default=True,
help="Public of gist")
content = click.option('--content',
'-c',
'content',
nargs=1,
multiple=False,
help="Content of gist")
description = click.option('--description',
'-d',
'description',
nargs=1,
default="From upgist by Fatpandac",
show_default=True,
multiple=False,
help="Description of gist")
return file(public(content(description(f))))
def ModifyGist_Options(f):
file = click.option('--file',
'-f',
'fileName',
nargs=1,
required=True,
multiple=False,
help="Modify filename of gist")
public = click.option('--public',
'-p','public',
nargs=1,
default="true",
multiple=False,
type=click.Choice(["true", "false"]),
show_default=True,
help="Modify public of gist")
content = click.option('--content',
'-c',
'content',
nargs=1,
multiple=False,
help="Modify content of gist")
description = click.option('--description',
'-d',
'description',
nargs=1,
default="From upgist by Fatpandac",
show_default=True,
multiple=False,
help="Modify description of gist")
gistid = click.option('--gistid',
'-id',
'gistID',
nargs=1,
required=True,
multiple=False,
help="Modify a gist by gist ID")
return file(public(content(description(gistid(f)))))
def ListGist_Options(f):
isShowID = click.option('--showid/--no-showid',
'-id/-nid',
'isShowID',
default=False,
show_default=True,
help="Just show gist id and filename")
return isShowID(f)
@click.group(context_settings=CONTEXT_SETTINGS)
@Main_Options
def main():
"""Upgist is a gist cli tool."""
@main.command(name="config",help="Config upgist")
@Config_Options
def Config(token):
SaveToken(token)
@main.command(name="get",help="Get a gist and save to the current directory")
@GetGistRaw_Options
def GetGistRaw(gistID):
gist = Gist()
gist.GetGistRaw(gistID)
@main.command(name="view",help="Just view a gist is not saved")
@ViewGist_Options
def ViewGist(gistID):
gist = Gist()
console.print(gist.ViewGist(gistID))
@main.command(name="modify",help="Modify a gist")
@ModifyGist_Options
def ModifyGist(gistID,fileName,content,description,public):
gist = Gist()
file = GistFile(description=description, public=public, fileName=fileName, content=content,fileID=gistID)
gist.Modify(file)
@main.command(name="delete",help="Delete a gist")
@DeleteGist_Options
def DeleteGist(gistID):
gist = Gist()
gist.Delete(gistID)
@main.command(name="list",help="Show all your gist")
@ListGist_Options
def GistList(isShowID):
gist = Gist()
gist.List(isShowID)
@main.command(name="create",help="Create a gist")
@CreatGist_Options
def CreatGist(fileName,content,description,public):
gist = Gist()
file = GistFile(description=description, public=public, fileName=fileName, content=content,fileID=None)
gist.Creat(file)
if __name__ == "__main__":
main()