-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
558 lines (459 loc) · 18.7 KB
/
main.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
import sublime, sublime_plugin
import os
import json
import threading
from .gist.api import GistApi
from .gist.api import GIST_BASE_URL as base_url
from .gist.lib import util
from .gist.lib import callback
from .gist.lib.progress import ThreadProgress
from .gist.lib.panel import Printer
class BaseGistView(object):
"""Base Class for Command with view"""
def is_enabled(self):
if not self.view or not self.view.file_name():
return False
# Rad file_full_name and filename
self.fileFullName = self.view.file_name()
self.settings = util.get_settings()
# Get options settings of this view
self.options = self.view.settings().get("options")
if not self.options: return False
self.fileName = self.options["fileName"]
self.fileProperty = self.options["fileProperty"]
self.gist = self.options["gist"]
self.gist_id = self.gist["id"]
self.html_url = self.gist["html_url"]
self.gist_url = self.gist["url"]
self.raw_url = self.fileProperty["raw_url"]
return True
class ChooseGist(sublime_plugin.WindowCommand):
def __init__(self, *args, **kwargs):
super(ChooseGist, self).__init__(*args, **kwargs)
def run(self, read_cache=True, include_files=True, callback_command=None):
self.include_files = include_files
self.callback_command = callback_command
self.settings = util.get_settings()
if not self.settings["token"]:
message = "Your own token is empty, please set it by " +\
"HaoGist > Settings > User Settings in the context menu"
return Printer.get("gist_error").write(message)
# Read gist from cache if read_cache flag is true
if read_cache:
_gists = util.get_gists_cache(self.settings)
if _gists: return self.choose_gist(_gists)
api = GistApi(self.settings["token"])
thread = threading.Thread(target=api.list)
thread.start()
ThreadProgress(api, thread, 'List All Gist',
self.choose_gist, _callback_options={}
)
def choose_gist(self, res, options={}):
# Keep the gists to cache
if not isinstance(res, list):
_gists = res.json()
else:
_gists = res
_gists = sorted(_gists, key=lambda k: k['updated_at'], reverse=True)
util.add_gists_to_cache(_gists)
# If no callback command, it will just refresh cache
if self.callback_command:
# Show the filenames in the quick panel
self.items = []
self.items_property = {}
for _gist in _gists:
if not self.include_files:
description = _gist["description"]
if not description:
description = "gist:%s" % _gist["id"]
self.items.append(description)
self.items_property[description] = {
"gist": _gist
}
else:
# Get gist description
description = _gist["description"]
if not description:
description = "gist:%s" % _gist["id"]
files_number = len(_gist["files"])
if files_number > 1:
self.items.append(description)
# Add gist files to items
gist_items_property = []
for key, value in _gist["files"].items():
filename = key
if files_number > 1:
key = "%s%s" % (" " * 4, key)
else:
key = _gist["description"] or key
if key in self.items:
self._update_item_title(key)
key = "{key} ({filename})".format(
key=key,
filename=value["filename"],
)
self.items.append(key)
self.items_property[key] = [{
"fileName": filename,
"fileProperty": value,
"gist": _gist
}]
gist_items_property.extend(self.items_property[key])
# Populate items_property
self.items_property[description] = gist_items_property
self.window.show_quick_panel(self.items, self.on_done,
sublime.MONOSPACE_FONT)
def _update_item_title(self, key):
""" This method finds the item, removes and puts it back in with the new
name by appending the filename in the title.
"""
gist = self.items_property[key]
gist_filename = gist[0]["fileProperty"]["filename"]
new_key = "{key} ({filename})".format(key=key, filename=gist_filename)
self.items.remove(key)
self.items.append(new_key)
del self.items_property[key]
self.items_property[new_key] = gist
def on_done(self, index):
if index == -1: return
item_property = self.items_property[self.items[index]]
if isinstance(item_property, list):
for item in item_property:
self.window.run_command(self.callback_command, {
"options": item
})
else:
self.window.run_command(self.callback_command, {
"options": item_property
})
class OpenGist(sublime_plugin.WindowCommand):
def __init__(self, *args, **kwargs):
super(OpenGist, self).__init__(*args, **kwargs)
def run(self, options={}):
if "fileName" not in options:
return
filename = options["fileName"]
fileProperty = options["fileProperty"]
_gist = options["gist"]
settings = util.get_settings()
api = GistApi(settings["token"])
thread = threading.Thread(target=api.retrieve, args=(fileProperty["raw_url"], ))
thread.start()
ThreadProgress(api, thread, 'Opening Gist %s' % filename.strip(),
callback.open_gist, _callback_options=options
)
class DeleteExistGist(sublime_plugin.WindowCommand):
def __init__(self, *args, **kwargs):
super(DeleteExistGist, self).__init__(*args, **kwargs)
def run(self, options={}):
if "gist" not in options:
return
filename = options["fileName"]
_gist = options["gist"]
settings = util.get_settings()
api = GistApi(settings["token"])
thread = threading.Thread(target=api.delete, args=(_gist["url"], ))
thread.start()
ThreadProgress(api, thread, 'Deleting Gist %s' % filename,
callback.delete_gist, _callback_options={
"fileName": filename
}
)
class OpenGistInBrowser(sublime_plugin.WindowCommand):
def __init__(self, *args, **kwargs):
super(OpenGistInBrowser, self).__init__(*args, **kwargs)
def run(self, options={}):
if "gist" not in options:
return
_gist = options["gist"]
util.open_with_browser(_gist["html_url"])
class UpdateContentToGist(sublime_plugin.TextCommand):
def run(self, edit, options):
if "gist" not in options:
return
# Get filename and gist from chosen options
filename = options["fileName"]
_gist = options["gist"]
# Prepare data for update
data = {
"files": {
filename: {
"content": self.selection
}
}
}
settings = util.get_settings()
api = GistApi(settings["token"])
thread = threading.Thread(target=api.patch, args=(_gist["url"], data, ))
thread.start()
ThreadProgress(api, thread, 'Updating Gist %s' % filename,
callback.update_to_gist, _callback_options={
"fileName": filename
}
)
def is_enabled(self):
self.selection = self.view.substr(self.view.sel()[0])
if not self.selection:
self.selection = self.view.substr(sublime.Region(0, self.view.size()))
if not self.selection:
return False
return True
class AddFileToGist(sublime_plugin.TextCommand):
def run(self, edit, options={}):
self.gist = options["gist"]
# Get gist description, if no description,
# use "gist:<gist_id>" instead
gist_description = self.gist["description"]
if not gist_description:
gist_description = "gist:%s" % self.gist["id"]
if self.view.file_name():
# Get the current file's base name
basename = os.path.basename(self.view.file_name())
else:
basename = ''
message = "Input fileName to be added into %s" % gist_description
sublime.active_window().show_input_panel(message,
basename, self.on_input_name, None, None)
def on_input_name(self, filename):
if not filename:
message = "FileName is required, do you want to try again?"
if not sublime.ok_cancel_dialog(message, "Yes?"): return
sublime.active_window().show_input_panel("Gist File Name: (Required)",
'', self.on_input_name, None, None)
return
data = {
"files": {
filename: {
"content": self.selection
}
}
}
settings = util.get_settings()
api = GistApi(settings["token"])
thread = threading.Thread(target=api.patch, args=(self.gist["url"], data, ))
thread.start()
description = self.gist["description"]
if not description:
description = "gist:%s" % self.gist["id"]
ThreadProgress(api, thread, 'Adding %s to Gist %s' % (filename, description),
callback.add_file_to_gist, _callback_options={
"fileName": filename,
"gistName": description
}
)
def is_enabled(self):
self.selection = self.view.substr(self.view.sel()[0])
if not self.selection:
self.selection = self.view.substr(sublime.Region(0, self.view.size()))
if not self.selection:
return False
return True
class DeleteFileFromGist(sublime_plugin.WindowCommand):
def __init__(self, *args, **kwargs):
super(DeleteFileFromGist, self).__init__(*args, **kwargs)
def run(self, options={}):
if "gist" not in options:
return
self.options = options
self.gist = options["gist"]
self.fileNames = self.gist["files"]
self.items = []
for key, value in self.fileNames.items():
self.items.append(key)
self.window.show_quick_panel(self.items, self.on_done)
def on_done(self, index):
if index == -1: return
self.fileName = self.items[index]
data = {
"files": {
self.fileName: None
}
}
settings = util.get_settings()
api = GistApi(settings["token"])
thread = threading.Thread(target=api.patch, args=(self.gist["url"], data, ))
thread.start()
description = self.gist["description"]
if not description:
description = "gist:%s" % self.gist["id"]
ThreadProgress(api, thread, 'Delete %s from Gist %s' % (self.fileName, description),
callback.delete_file_from_gist, _callback_options={
"fileName": self.fileName,
"gistName": description
}
)
class CreateGist(sublime_plugin.TextCommand):
def run(self, edit, public=False):
self.public = public
self.settings = util.get_settings()
if self.view.file_name():
# Get the current file's base name
basename = os.path.basename(self.view.file_name())
else:
basename = ''
sublime.active_window().show_input_panel("Gist File Name: (Required)",
basename, self.on_input_name, None, None)
def on_input_name(self, filename):
if not filename:
message = "FileName is required, do you want to try again?"
if not sublime.ok_cancel_dialog(message, "Yes?"): return
sublime.active_window().show_input_panel("Gist File Name: (Required)",
'', self.on_input_name, None, None)
return
self.fileName = filename
sublime.active_window().show_input_panel('Gist Description: (optional):',
filename.split(".")[0], self.on_input_descrition, None, None)
def on_input_descrition(self, desc):
post_url = base_url % "gists"
data = {
"description": desc,
"public": self.public,
"files": {
self.fileName : {
"content": self.selection
}
}
}
api = GistApi(self.settings["token"])
thread = threading.Thread(target=api.post, args=(post_url, data, ))
thread.start()
ThreadProgress(api, thread, 'Creating Gist %s' % self.fileName,
callback.create_gist, _callback_options={
"fileName": self.fileName,
"content": self.selection
}
)
def is_enabled(self):
self.selection = self.view.substr(self.view.sel()[0])
if not self.selection:
self.selection = self.view.substr(sublime.Region(0, self.view.size()))
if not self.selection:
return False
return True
class RenameGist(BaseGistView, sublime_plugin.TextCommand):
def run(self, edit):
self.old_filename = self.fileName
sublime.active_window().show_input_panel('Input New Name:',
self.old_filename, self.on_input_name, None, None)
def on_input_name(self, input):
if not input:
sublime.error_message("File name can't be empty")
return
self.new_filename = input
body = open(self.view.file_name(), encoding="utf-8").read()
data = {
"files": {
self.old_filename: {
"filename": self.new_filename,
"content": body
}
}
}
api = GistApi(self.settings["token"])
thread = threading.Thread(target=api.patch, args=(self.gist_url, data, ))
thread.start()
ThreadProgress(api, thread, 'Renaming Gist from %s to %s' % (
self.old_filename,
self.new_filename
),
callback.rename_gist, _callback_options={
"old_filename": self.old_filename,
"new_filename": self.new_filename,
"fileFullName": self.fileFullName,
"options": self.options
}
)
class UpdateGistDescription(BaseGistView, sublime_plugin.TextCommand):
def run(self, edit):
sublime.active_window().show_input_panel('Input New Description:',
"", self.on_input_name, None, None)
def on_input_name(self, input):
if not input:
sublime.error_message("File Description can't be empty")
return
self.desc = input
body = open(self.view.file_name(), encoding="utf-8").read()
data = {
"description": self.desc,
"files": {
self.fileName: {
"content": body
}
}
}
api = GistApi(self.settings["token"])
thread = threading.Thread(target=api.patch, args=(self.gist_url, data, ))
thread.start()
ThreadProgress(api, thread, 'Update Gist Description',
callback.update_description, _callback_options={
"fileFullName": self.fileFullName,
"desc": self.desc
}
)
class UpdateGist(BaseGistView, sublime_plugin.TextCommand):
def run(self, edit):
if self.fileName in globals():
thread = globals()[self.fileName]
if thread and thread.is_alive():
return
body = open(self.view.file_name(), encoding="utf-8").read()
data = {
"files": {
self.fileName: {
"content": body
}
}
}
api = GistApi(self.settings["token"])
thread = threading.Thread(target=api.patch, args=(self.gist_url, data, ))
thread.start()
ThreadProgress(api, thread, 'Updating Gist %s' % self.fileName,
callback.update_gist, _callback_options={
"fileFullName": self.fileFullName
}
)
globals()[self.fileName] = thread
class RefreshGist(BaseGistView, sublime_plugin.TextCommand):
def run(self, edit):
api = GistApi(self.settings["token"])
thread = threading.Thread(target=api.retrieve, args=(self.raw_url, ))
thread.start()
ThreadProgress(api, thread, 'Refreshing Gist %s' % self.fileName,
callback.refresh_gist, _callback_options={
"fileFullName": self.fileFullName
}
)
class DeleteGist(BaseGistView, sublime_plugin.TextCommand):
def run(self, edit):
api = GistApi(self.settings["token"])
thread = threading.Thread(target=api.delete, args=(self.gist_url, ))
thread.start()
ThreadProgress(api, thread, 'Deleting Gist %s' % self.fileName,
callback.delete_gist, _callback_options={
"fileFullName": self.fileFullName
}
)
class ClearGistCache(sublime_plugin.WindowCommand):
def __init__(self, *args, **kwargs):
super(ClearGistCache, self).__init__(*args, **kwargs)
def run(self):
settings = util.get_settings()
cachedir = os.path.join(settings["workspace"], ".cache", "gists.json")
try:
os.remove(cachedir)
Printer.get("gist_log").write("Gist cache is cleared")
except:
Printer.get("gist_error").write("Gist cache clear failed")
class OpenCurrentGistInBrowser(BaseGistView, sublime_plugin.TextCommand):
def run(self, edit):
util.open_with_browser(self.html_url)
class ReleaseNote(sublime_plugin.WindowCommand):
def __init__(self, *args, **kwargs):
super(ReleaseNote, self).__init__(*args, **kwargs)
def run(self):
util.open_with_browser("https://github.com/xjsender/HaoGist/blob/master/HISTORY.rst")
class AboutHaoGist(sublime_plugin.WindowCommand):
def __init__(self, *args, **kwargs):
super(AboutHaoGist, self).__init__(*args, **kwargs)
def run(self):
util.open_with_browser("https://github.com/xjsender/HaoGist")