forked from torhve/Weechat-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrtm.py
260 lines (212 loc) · 7.9 KB
/
wrtm.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
#
# -*- coding: utf-8 -*-
#
# Copyright (c) 2009 by xt <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
# (this script requires WeeChat 0.3.0 or newer)
#
# History:
# 2009-06-22, xt <[email protected]>
# version 0.1: initial release
import weechat as w
from rtm import createRTM
import datetime
import time
SCRIPT_NAME = "wrtm"
SCRIPT_AUTHOR = "xt <[email protected]>"
SCRIPT_VERSION = "0.1"
SCRIPT_LICENSE = "GPL3"
SCRIPT_DESC = "TODO"
SCRIPT_COMMAND = 'rtm'
settings = {
"token" : '',
"update_interval" : '5', #In minutes, must be int
}
rtm = False
#def lists_update(*kwargs):
# ''' update the bar with the lists '''
#
# w.bar_item_update('rtm_lists')
# return w.WEECHAT_RC_OK
#def toggle_rtm_bar(data, signal, signaldata):
# ''' cb for signal buffer_switch '''
#
# global rtm_buffer
#
# if w.current_buffer() == rtm_buffer:
# w.command('', '/bar show rtm')
# else:
# w.command('', '/bar hide rtm')
#
# w.unhook('buffer_switch')
#
# return w.WEECHAT_RC_OK
def buffer_input(data, buffer, string):
''' Callback on rtm buffer input '''
for list in rtm.list_names():
if string.lower() == list.lower():
rtm.active_list_name = list
rtm.redraw()
break
return w.WEECHAT_RC_OK
def buffer_close(*kwargs):
rtm.buffer = None
return w.WEECHAT_RC_OK
def rtm_cmd(data, buffer, args):
''' Callback for /rtm command '''
if not args:
#lists_update()
#w.command('', '/bar show %s' %SCRIPT_COMMAND)
#w.hook_signal('buffer_switch', 'toggle_imap_bar', '')
rtm.redraw()
w.command('', '/buffer %s' %SCRIPT_COMMAND)
elif args == 'list_left' or args == 'list_right':
new_name = ''
for i, name in enumerate(rtm.list_names()):
if name == rtm.active_list_name:
if args.endswith('left'):
new_name = rtm.list_names()[i-1]
else:
new_name = rtm.list_names()[i+1]
rtm.active_list_name = new_name
rtm.redraw()
return w.WEECHAT_RC_OK
class RTM(object):
def __init__(self, token):
self.token = token
apik = 'e282189b97ed465ba34b5bc000543065'
secret = 'b68702a7f207244c'
if not token:
raise ValueError('Error: Need token configured')
else:
self.rtm = createRTM(apik, secret, token)
self._active_list_name = False
self.active_task_id = 0
self.lists = {}
self._list_names = []
self.buffer_create()
def list_names(self):
''' Return cached list of list names '''
if not self.lists:
rspLists = self.rtm.lists.getList()
for list in rspLists.lists.list:
self.lists[list.name] = list.id
self._list_names.append(list.name)
return self._list_names
@property
def active_list_name(self):
if not self._active_list_name:
default = self._default_list_name()
self._active_list_name = default
return self._active_list_name
@active_list_name.setter
def active_list_name(self, value):
if value in self.list_names():
self._active_list_name = value
else:
raise ValueError('Wrong list name')
def _default_list_name(self):
default_id = self.rtm.settings.getList().settings.defaultlist
for name, id in self.lists.iteritems():
if int(id) == int(default_id):
return name
def buffer_create(self):
''' create rtm buffer '''
rtm_buffer = w.buffer_search('python', SCRIPT_COMMAND)
if not rtm_buffer:
rtm_buffer = w.buffer_new(SCRIPT_COMMAND, "buffer_input", "", "buffer_close", "")
w.buffer_set(rtm_buffer, "time_for_each_line", "0")
w.buffer_set(rtm_buffer, "nicklist", "0")
w.buffer_set(rtm_buffer, "title", "Remember The Milk !")
#w.buffer_set(rtm_buffer, "type", "free")
w.buffer_set(rtm_buffer, "localvar_set_no_log", "1")
w.buffer_set(rtm_buffer, 'key_bind_meta2-A', '/%s task_up' %SCRIPT_COMMAND)
w.buffer_set(rtm_buffer, 'key_bind_meta2-B', '/%s task_down' %SCRIPT_COMMAND)
w.buffer_set(rtm_buffer, 'key_bind_meta2-D', '/%s list_left' %SCRIPT_COMMAND)
w.buffer_set(rtm_buffer, 'key_bind_meta2-C', '/%s list_right' %SCRIPT_COMMAND)
self.buffer = rtm_buffer
def update_title(self):
result = ''
for rlist in self.list_names():
fgcolor = 'green'
bgcolor = 'default'
if rlist == self.active_list_name:
bgcolor = 'red'
result += '%s%s%s ' % (w.color('%s,%s' %(fgcolor, bgcolor)), rlist, w.color('reset'))
w.buffer_set(self.buffer, "title", result)
return str(result)
def print_tasks(self):
w.buffer_clear(self.buffer)
list_id = self.lists[self.active_list_name]
rspTasks = self.rtm.tasks.getList(list_id=list_id, filter='status:incomplete')
for task in reversed(rspTasks.tasks.list.taskseries):
if not self.active_task_id:
self.active_task_id = task.id
priority = task.task.priority
fgcolor = 'default'
bgcolor = 'default'
if priority == '3':
fgcolor = 'green'
elif priority == '2':
fgcolor = 'cyan'
elif priority == '1':
fgcolor = 'yellow'
elif priority == 'N':
fgcolor = 'default'
if int(task.id) == int(self.active_task_id):
bgcolor = 'red'
try:
ttime = datetime.datetime.fromtimestamp(time.mktime(time.strptime(task.created, "%Y-%m-%dT%H:%M:%SZ")))
except Exception, e:
ttime = ''
print task.created, e
task_name = task.name.encode('UTF-8')
task_name = '%s%s%s' %(w.color('%s,%s' %(fgcolor,bgcolor)), task_name, w.color('reset'))
taskstr = '%s\t%s %s' %(ttime, '', task_name)
w.prnt(self.buffer, taskstr)
def redraw(self):
self.update_title()
self.print_tasks()
def update_interval_cb(*kwargs):
global rtm
rtm.redraw()
return w.WEECHAT_RC_OK
if w.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, "", ""):
for option, default_value in settings.iteritems():
if not w.config_is_set_plugin(option):
w.config_set_plugin(option, default_value)
token = w.config_get_plugin('token')
rtm = RTM(token)
w.hook_command(SCRIPT_COMMAND,
"weechat RTM interface",
"[]",
"\n",
"",
"rtm_cmd", "")
w.command('', '/' + SCRIPT_COMMAND)
update_interval = int(w.config_get_plugin('update_interval'))
if update_interval > 0:
w.hook_timer(\
update_interval*1000*60,
0,
0,
'update_interval_cb',
'')
#w.bar_item_new("rtm_lists", "rtm_lists_cb", "");
#w.bar_new("rtm", "on", "2000", "window", "", "top", "horizontal",
# "horizontal", "0", "0", "default", "default", "default", "1",
# "rtm_lists")