-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtmom_net.py
150 lines (126 loc) · 4.96 KB
/
rtmom_net.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
"""
RTM Service access (updates from / to Internet)
Elementary based client for "Remember the Milk" (http://www.rememberthemilk.com/) written in Python.
Copyright (C) 2010 Michael Pilgermann <kichkasch@gmx.de>
http://github.com/kichkasch/rtmom
rtmom 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.
rtmom 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 rtmom. If not, see <http://www.gnu.org/licenses/>.
"""
import config
import rtm
import datetime
import rtmom
import os
import os.path
internetConnector = None
"""Singleton"""
def getInternetConnector():
"""
Singleton
"""
global internetConnector
if not internetConnector:
internetConnector = InternetConnector()
return internetConnector
class InternetConnector():
"""
Implementation for RTM Internet Connector
"""
def __init__(self):
"""
Initialize 'empty' connection; YOU have to connect manually!
"""
self._connection = None
def isConnected(self):
"""
Check, whether connection has been established
"""
return self._connection != None
def loadCategories(self):
"""
Loads categories (task lists) from the net
Returns a dictionary containing the names (key) and IDs (value) for the categories.
"""
if not self.isConnected():
raise ValueError('Not connected with RTM Net Service; cannot proceed. Please connect first.')
categories = {}
rspLists = self._connection.lists.getList()
for cat in rspLists.lists.list:
try:
config.getSettings().getValue("hidden_groups", list).index(cat.name)
except:
categories[cat.name] = cat.id
return categories
def _assembleFilter(self, filter):
"""
Add components to an existing filter for additionally filtering completed tasks
Depends on setting in config file.
"""
if not config.getSettings().getValue("show_completed", bool):
if filter:
filter = "(" + filter + ") AND " + "status:incomplete"
else:
filter = "status:incomplete"
return filter
def loadFullTasks(self, catid = None, filter = ""):
"""
Loads all tasks for the given category from the net
The entire task instance will be returned. Extract the required information yourself!
"""
if not self.isConnected():
raise ValueError('Not connected with RTM Net Service; cannot proceed. Please connect first.')
filter = self._assembleFilter(filter)
if catid:
rspTasks = self._connection.tasks.getList(list_id = catid, filter = filter)
else:
rspTasks = self._connection.tasks.getList(filter = filter)
return rtmom.getExtractor().extractTasksFromDottedDict(rspTasks.tasks)
def markTaskCompleted(self, catId, task):
"""
Mark a single task completed
"""
if not self.isConnected():
raise ValueError('Not connected with RTM Net Service; cannot proceed. Please connect first.')
tl = self._connection.timelines.create().timeline
rspTasks = self._connection.tasks.complete(list_id =catId, task_id = task.task.id, timeline = tl, taskseries_id = task.id)
def connect(self, tokenPath = None, interactive = True):
"""
Estabilshes connection with Remember The Milk backend
Checks, whether token is known already; otherwise user interaction; finally connection
"""
if not tokenPath:
tokenPath = config.TOKEN_PATH
token = None
try:
f = open(tokenPath, 'r')
token = f.read().strip()
f.close()
except:
token = None
conn = rtm.RTM(config.api_key, config.shared_secret, token)
if not token:
print 'No token found'
if not interactive:
return conn, tokenPath
print 'Give me access here:', conn.getAuthURL()
raw_input('Press enter once you gave access')
self.connectAuthorizedClicked(conn, tokenPath)
else:
self._connection = conn
return None, None
def connectAuthorizedClicked(self, conn, tokenPath):
folder, fileName = os.path.split(tokenPath)
if not os.path.isdir(folder):
os.makedirs(folder)
f = open(tokenPath, "w")
f.write(conn.getToken())
f.close()
self._connection = conn