-
Notifications
You must be signed in to change notification settings - Fork 7
/
trello.py
135 lines (104 loc) · 5.63 KB
/
trello.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
import sublime, sublime_plugin, re
import sys, os.path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "lib"))
from progress_notifier import ProgressNotifier
from open_views import OpenViews
import trollop
# A base for each command
class TrelloCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.setup_data_from_settings()
if not self.token:
self.help_text()
return
if not self.use_cache:
self.view.run_command("trello_delete_cache")
trello_connection = trollop.TrelloConnection(self.key, self.token)
self.defer(lambda: self.work(trello_connection))
def setup_data_from_settings(self):
default_settings = sublime.load_settings("Default_app.sublime-settings")
user_settings = sublime.load_settings("Trello.sublime-settings")
self.key = user_settings.get("key") or default_settings.get("key")
self.secret = user_settings.get("secret") or default_settings.get("secret")
self.token = user_settings.get("token")
self.use_cache = user_settings.get("use_cache", True)
self.renavigate = user_settings.get("keep_navigate_open_after_action", True)
self.results_in_new_tab = user_settings.get("results_in_new_tab", True)
self.card_delimiter = user_settings.get("card_delimiter", "<end>")
self.syntax_file = user_settings.get("syntax_file")
def help_text(self):
first = "Sorry for the interruption, in order to use the package please go to:\n%s\nand paste the token in the settings (Preferences -> Package Settings -> Trello -> Settings - User). You can check Settings - Default to see the settings structure." % self.token_url()
second = "If you don't want to use the default app, you can change the key and the secret too, just go to:\n%s\nand copy paste both to the settings :)" % self.key_secret_generator_url()
self.show_output_panel_composing(first, second)
def show_token_expired_help(self, e):
self.show_output_panel_composing("It seems your token is invalid or has expired, try adding it again.\nToken URL: %s" % self.token_url(), "The error encountered was: '%s'" % e)
def show_output_panel_composing(self, *args):
help_text = self.compose_help_text(*args)
self.show_output_panel(help_text)
def compose_help_text(self, first="", second="", last="For more info, you can go to: https://github.com/NicoSantangelo/sublime-text-trello"):
return "%s\n\n%s\n\n%s" % (first, second, last)
def token_url(self):
return "https://trello.com/1/connect?key=%s&name=sublime_app&response_type=token&scope=read,write" % self.key
def key_secret_generator_url(self):
return "https://trello.com/1/appKey/generate"
# Main method, override
def work(self, connection):
pass
# Panels and message
def display_message(self, text):
sublime.status_message("trello: %s" % text)
def show_quick_panel(self, items, on_done = None, on_highlighted = None, selected_index = -1):
sublime.set_timeout(lambda: self.view.window().show_quick_panel(items, on_done, sublime.MONOSPACE_FONT, selected_index, on_highlighted), 0)
def show_input_panel(self, caption, initial_text = "", on_done = None, on_change = None, on_cancel = None):
self.view.window().show_input_panel(caption, initial_text, on_done, on_change, on_cancel)
# Output view
def show_in_editable_tab(self, text, extra = None):
view = self.show_in_tab(text)
view.set_scratch(True)
OpenViews.set(view, extra)
def insert_text(self, text):
view = self.view.window().active_view()
view.run_command("view_insert", { "size" : self.view.sel()[0].begin(), "content": text });
def show_in_tab(self, text):
view = self.view.window().new_file()
view.set_name("Trello")
view.run_command("view_insert", { "size" : view.size(), "content": text });
self.set_new_view_attributes(view)
return view
def show_output_panel(self, text):
self.output_view = self.view.window().get_output_panel("textarea")
self.append_to_output_view(text)
self.view.window().run_command("show_panel", { "panel": "output.textarea" })
self.set_new_view_attributes(self.output_view)
def append_to_output_view(self, text):
self.output_view.set_read_only(False)
self.output_view.run_command("append", { "characters": text })
self.output_view.set_read_only(True)
def set_new_view_attributes(self, view):
view.set_syntax_file(self.syntax_file)
view.set_viewport_position((0, 0), True)
# Helpers
def defer(self, fn):
self.async(fn, 0)
def async(self, fn, delay):
progress = ProgressNotifier('Trello: Working')
sublime.set_timeout_async(lambda: self.call(fn, progress), delay)
sublime.set_timeout_async(lambda: progress.stop(), 5000)
def call(self, fn, progress):
fn()
progress.stop()
class ViewInsertCommand(sublime_plugin.TextCommand):
def run(self, edit, size, content):
self.view.insert(edit, size, content)
class TrelloOnClose(sublime_plugin.EventListener):
def on_close(self, view):
view_dict = OpenViews.get(view)
if view_dict:
OpenViews.remove(view)
operation = view_dict['extra']
content = view.substr(sublime.Region(0, view.size()))
if content:
operation.command.defer(lambda: operation.full_add(content))
else:
operation.reexecute()