-
Notifications
You must be signed in to change notification settings - Fork 1
/
ITDCHelperAsana.py
330 lines (262 loc) · 11.9 KB
/
ITDCHelperAsana.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
# @package ITDCHelper
# @author Avtandil Kikabidze aka LONGMAN
# @copyright Copyright (c) 2008-2015, Avtandil Kikabidze (akalongman@gmail.com)
# @link http://long.ge
# @license http://opensource.org/licenses/mit-license.php The MIT License (MIT)
import os
import sys
import sublime
import sublime_plugin
import threading
import subprocess
import functools
import os.path
directory = os.path.dirname(os.path.realpath(__file__))
libs_path = os.path.join(directory, "itdchelper")
if libs_path not in sys.path:
sys.path.append(libs_path)
from asanalib.asana import asana
from pprint import pprint
AsanaProjects = sublime.load_settings('ITDCHelper.sublime-settings')
class GetAsanaTasksCommand(sublime_plugin.TextCommand):
window = None
def run(self,edit,archive = False):
self.window = self.view.window()
try:
self.path = self.window.folders()[0]
except:
sublime.status_message('Not project mode')
return
self.archive = archive
if AsanaProjects.has(self.path):
project_id = AsanaProjects.get(self.path).get('id')
project_name = AsanaProjects.get(self.path).get('name')
self.task_ids = [project_id]
self.task_names = ['### '+project_name+' ###']
thread = AsanaApiCall('get_project_tasks', int(project_id), self.show_quick_panel_task)
thread.start()
else:
self.window.run_command('set_asana_project')
def show_quick_panel_task(self,tasks):
if tasks != 'cache':
for task in tasks:
if (self.archive or task[u'completed'] == False) and task[u'name'][-1:] != ':':
self.task_ids.append(task[u'id'])
self.task_names.append(task[u'name'])
if len(self.task_ids) > 0 :
pprint (self.task_names)
self.window.show_quick_panel(self.task_names, self.show_quick_panel_select)
else:
sublime.status_message('Not exist tasks')
def show_quick_panel_select(self,index):
if index == -1:
return
elif index == 0:
self.window.run_command('get_current_project')
return
self.current_task_id = self.task_ids[index]
self.current_task_name = self.task_names[index]
command = ['0: Back','1: Done','2: Done & Commit','3: Update','4: Cancel']
self.window.show_quick_panel(command, self.command_task)
def command_task(self,index):
if index == 0 :
self.show_quick_panel_task('cache')
elif index == 1 or index == 2:
del self.task_ids[index]
del self.task_names[index]
thread = AsanaApiCall('done_task', int(self.current_task_id), self.on_done)
thread.start()
if index == 2:
self.window.show_input_panel('Commit -am: ', self.current_task_name+' #'+str(self.current_task_id), self.git_commit, None, None)
elif index == 3 :
self.window.show_input_panel('Change name: ', self.current_task_name, self.update_task, None, None)
elif index == 4 :
self.on_done()
def update_task(self,name):
thread = AsanaApiCall('update_task', [int(self.current_task_id),name], self.on_done)
thread.start()
def git_commit(self,message):
thread = CommandThread(['git', 'commit', '-am',message], self.repo_name)
thread.start()
def repo_name(self,message):
self.story = message
thread = CommandThread(['git', 'remote', '-v'], self.git_log)
thread.start()
return
def git_log(self,repo):
repo = repo.split(':')
repo = repo[1].split('.git')
format = '%an, %ad \n https://github.com/'+repo[0]+'/commit/%H'
thread = CommandThread(['git', 'log', '--pretty=format:'+format,'-1'], self.add_story)
thread.start()
def add_story(self,message):
self.story = '.\n'+ self.story + '\n'+ message
thread = AsanaApiCall('add_story', [int(self.current_task_id),self.story], self.on_done)
thread.start()
def on_done(self,name=False):
if name :
sublime.status_message('Done '+ name)
self.show_quick_panel_task('cache')
class getCurrentProjectCommand(sublime_plugin.TextCommand):
window = None
def run(self,edit):
self.window = self.view.window()
command = ['0: Create New Task','1: Show Tasks','2: Show Completed Tasks','3: Change Project','4: Cancel']
# command = ['0: Create New Task','1: Show Tasks','2: Show Archive Tasks','3: Change Project','4: Update Project','5: Cancel']
self.window.show_quick_panel(command, self.command_task)
def command_task(self,index):
if index == 0 :
self.window.run_command('add_asana_task')
elif index == 1:
self.window.run_command('get_asana_tasks')
elif index == 2:
self.window.run_command('get_asana_tasks',{"archive": True})
elif index == 3 :
self.window.run_command('set_asana_project')
# elif index == 4 :
# sublime.message_dialog('Now under development')
elif index == 4 :
self.on_done()
def on_done(self,name=False):
if name :
sublime.status_message('Done '+ name)
class SetAsanaProjectCommand(sublime_plugin.TextCommand):
window = None
def run(self,edit):
self.window = self.view.window()
thread = AsanaApiCall('get_project_id', None, self.show_quick_panel)
thread.start()
def show_quick_panel(self,projects):
self.project_ids = []
self.project_names = []
self.project_workspaces = []
for project in projects:
if project[u'archived'] == False:
self.project_names.append(project[u'name'])
self.project_ids.append(project[u'id'])
self.project_workspaces.append(project[u'workspace'][u'id'])
self.window.show_quick_panel(self.project_names, self.save_project_id)
def save_project_id(self,index):
if index == -1 :
return
self.path = self.window.folders()[0]
AsanaProjects.set(self.path, {
'id':str(self.project_ids[index]),
'name':str(self.project_names[index]),
'workspace':str(self.project_workspaces[index]),
})
sublime.save_settings('AsanaProjects.sublime-settings')
self.window.run_command('get_asana_tasks')
class AddAsanaTaskCommand(sublime_plugin.TextCommand):
window = None
def run(self,edit):
self.window = self.view.window()
self.path = self.view.window().folders()[0]
self.window.show_input_panel('New Task: ', '', self.create_task, None, None)
def create_task(self,name):
project_id = AsanaProjects.get(self.path).get('id')
project_workspace = AsanaProjects.get(self.path).get('workspace')
thread = AsanaApiCall('create_task', [name,project_id,project_workspace], self.show_quick_panel)
thread.start()
def show_quick_panel(self,name):
sublime.status_message('Created: '+ name)
self.window.run_command('get_asana_tasks')
def main_thread(callback, *args, **kwargs):
# sublime.set_timeout gets used to send things onto the main thread
# most sublime.[something] calls need to be on the main thread
sublime.set_timeout(functools.partial(callback, *args, **kwargs), 0)
def _make_text_safeish(text, fallback_encoding, method='decode'):
# The unicode decode here is because sublime converts to unicode inside
# insert in such a way that unknown characters will cause errors, which is
# distinctly non-ideal... and there's no way to tell what's coming out of
# git in output. So...
try:
unitext = getattr(text, method)('utf-8')
except (UnicodeEncodeError, UnicodeDecodeError):
unitext = getattr(text, method)(fallback_encoding)
return unitext
class AsanaApiCall(threading.Thread):
def __init__(self,command,args,callback):
asana_api_key = sublime.load_settings('ITDCHelper.sublime-settings').get('asana_api_key')
if not asana_api_key :
sublime.error_message('You have to set asana_api_key in the Preferences.sublime-settings. folllowing { "asana_api_key" : "YOUR_API KEY" }')
self.AsanaApi = None
else :
self.AsanaApi = asana.AsanaAPI( asana_api_key , debug=True)
self.command = command
self.args = args
self.callback = callback
threading.Thread.__init__(self)
def run(self):
if not self.AsanaApi:
return
try:
if self.command == 'get_project_tasks':
tasks = self.AsanaApi.get_project_tasks(self.args)
main_thread(self.callback, tasks)
elif self.command == 'get_task':
projects = self.AsanaApi.get_task(self.args)
main_thread(self.callback,projects)
elif self.command == 'get_project_id':
projects = self.AsanaApi.list_projects()
main_thread(self.callback,projects)
elif self.command == 'create_task':
task = self.AsanaApi.create_task(self.args[0], self.args[2])
self.AsanaApi.add_project_task(task[u'id'], self.args[1])
main_thread(self.callback,self.args[0])
elif self.command == 'update_task':
task = self.AsanaApi.update_task(self.args[0], self.args[1])
main_thread(self.callback,task[u'name'])
elif self.command == 'add_story':
task = self.AsanaApi.add_story(self.args[0], self.args[1])
main_thread(self.callback,self.args[1])
elif self.command == 'done_task':
task = self.AsanaApi.update_task(self.args, None, None, None, True)
main_thread(self.callback,task[u'name'])
return
except:
err = 'error'
sublime.error_message(err)
self.result = False
# self.view.window().run_command('exec', {'cmd': ['sh', 'script.sh'], 'quiet': False})
class CommandThread(threading.Thread):
def __init__(self, command, on_done, working_dir="", fallback_encoding="", **kwargs):
threading.Thread.__init__(self)
self.command = command
self.on_done = on_done
self.working_dir = working_dir
if "stdin" in kwargs:
self.stdin = kwargs["stdin"]
else:
self.stdin = None
if "stdout" in kwargs:
self.stdout = kwargs["stdout"]
else:
self.stdout = subprocess.PIPE
self.fallback_encoding = fallback_encoding
self.kwargs = kwargs
def run(self):
try:
# Per http://bugs.python.org/issue8557 shell=True is required to
# get $PATH on Windows. Yay portable code.
shell = os.name == 'nt'
if self.working_dir != "":
os.chdir(self.working_dir)
proc = subprocess.Popen(self.command,
stdout=self.stdout, stderr=subprocess.STDOUT,
stdin=subprocess.PIPE,
shell=shell, universal_newlines=True)
output = proc.communicate(self.stdin)[0]
if not output:
output = ''
# if sublime's python gets bumped to 2.7 we can just do:
# output = subprocess.check_output(self.command)
main_thread(self.on_done,
_make_text_safeish(output, self.fallback_encoding), **self.kwargs)
except subprocess.CalledProcessError as e:
main_thread(self.on_done, e.returncode)
except OSError as e:
if e.errno == 2:
main_thread(sublime.error_message, "Git binary could not be found in PATH\n\nConsider using the git_command setting for the Git plugin\n\nPATH is: %s" % os.environ['PATH'])
else:
raise e