-
Notifications
You must be signed in to change notification settings - Fork 6
/
config.py
215 lines (191 loc) · 11.3 KB
/
config.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
#!/usr/bin/env python
# encoding: utf-8
#
# Copyright (c) 2020 Michael Schmidt-Korth
#
# GNU GPL v2.0 Licence. See https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
#
from __future__ import unicode_literals
import sys
from workflow import Workflow, Workflow3, ICON_WARNING, web, PasswordNotFound
confNames = {'confApi': 'apiKey', 'confDue': 'dueDate', 'confList': 'list', 'confSpace': 'space', 'confTeam': 'workspace', 'confProject': 'folder', 'confNotification': 'notification', 'confDefaultTag': 'defaultTag', 'confHierarchyLimit': 'hierarchyLimit', 'confUser': 'userId'}
def main(wf):
configuration()
def configuration():
'''Provides list items to configure the workflow.
----------
'''
if len(wf.args):
query = wf.args[0]
else:
query = None
if not query:
apiKeyValue = getConfigValue(confNames['confApi'])
dueValue = getConfigValue(confNames['confDue'])
listValue = getConfigValue(confNames['confList'])
spaceValue = getConfigValue(confNames['confSpace'])
teamValue = getConfigValue(confNames['confTeam'])
projectValue = getConfigValue(confNames['confProject'])
notificationValue = getConfigValue(confNames['confNotification'])
if notificationValue == 'true':
notificationValue = '✓'
elif notificationValue == 'false':
notificationValue = '✗'
defaultTagValue = getConfigValue(confNames['confDefaultTag'])
hierarchyLimitValue = getConfigValue(confNames['confHierarchyLimit'])
wf3.add_item(title = 'Set API key' + (' (' + apiKeyValue + ')' if apiKeyValue else ''), subtitle = 'Your personal ClickUp API key/token.', valid = False, autocomplete = confNames['confApi'] + ' ')
wf3.add_item(title = 'Set default due date' + (' (' + dueValue + ')' if dueValue else ''), subtitle = 'e.g. m30 (in 30 minutes), h2 (in two hours), d1 (in one day), w1 (in one week).', valid = False, autocomplete = confNames['confDue'] + ' ')
wf3.add_item(title = 'Set ClickUp workspace' + (' (' + teamValue + ')' if teamValue else ''), subtitle = 'Workspace that defines which tasks can be searched.', valid = False, autocomplete = confNames['confTeam'] + ' ')
wf3.add_item(title = 'Set ClickUp space' + (' (' + spaceValue + ')' if spaceValue else ''), subtitle = 'Space that defines your available labels and priorities.', valid = False, autocomplete = confNames['confSpace'] + ' ')
wf3.add_item(title = 'Set ClickUp folder' + (' (' + projectValue + ')' if projectValue else ''), subtitle = 'Folder that which tasks can be searched. The Folder must be part of the workspace.', valid = False, autocomplete = confNames['confProject'] + ' ')
wf3.add_item(title = 'Set default ClickUp list' + (' (' + listValue + ')' if listValue else ''), subtitle = 'List you want to add tasks to by default.', valid = False, autocomplete = confNames['confList'] + ' ')
wf3.add_item(title = 'Set Show Notification' + (' (' + notificationValue + ')' if notificationValue else ''), subtitle = 'Show notification after creating task?', valid = False, autocomplete = confNames['confNotification'] + ' ')
wf3.add_item(title = 'Set default Tag' + (' (' + defaultTagValue + ')' if defaultTagValue else ''), subtitle = 'Tag that is added to all new tasks.', valid = False, autocomplete = confNames['confDefaultTag'] + ' ')
wf3.add_item(title = 'Set hierarchy levels to limit search results' + (' (' + hierarchyLimitValue + ')' if hierarchyLimitValue else ''), subtitle = 'Levels to limit search results by (list, folder, space).', valid = False, autocomplete = confNames['confHierarchyLimit'] + ' ')
wf3.add_item(title = 'Validate Configuration', subtitle = 'Check if provided configuration parameters are valid.', valid = False, autocomplete = 'validate', icon = './settings.png')
clearCache = wf3.add_item(title = 'Clear Cache', subtitle = 'Clear list of available labels and lists to be retrieved again.', valid = True, arg = 'cu:config cache', icon = './settings.png')
clearCache.setvar('isSubmitted', 'true') # No secondary screen necessary
elif query.startswith(confNames['confApi'] + ' '): # Check for suffix ' ' which we add automatically so user can type immediately
userInput = query.replace(confNames['confApi'] + ' ', '')
apiItem = wf3.add_item(title = 'Enter API key: ' + userInput, subtitle = 'Confirm to save to keychain?', valid = True, arg = 'cu:config ' + query)
apiItem.setvar('isSubmitted', 'true')
elif query.startswith(confNames['confDue'] + ' '):
userInput = query.replace(confNames['confDue'] + ' ', '')
dueType = userInput[:1]
if dueType == 'm':
dueType = 'minutes'
elif dueType == 'h':
dueType = 'hours'
elif dueType == 'd':
dueType = 'days'
elif dueType == 'w':
dueType = 'weeks'
dueTime = userInput[1:]
output = dueTime + ' ' + dueType
if not dueType.isalpha() or not dueTime.isnumeric():
output = '(Invalid input).'
dueItem = wf3.add_item(title = 'Enter default due date (e.g. d2): ' + output, subtitle = 'Save?', valid = True, arg = 'cu:config ' + query)
dueItem.setvar('isSubmitted', 'true')
elif query.startswith(confNames['confList'] + ' '):
userInput = query.replace(confNames['confList'] + ' ', '')
if not userInput.isnumeric() or len(userInput) != 7:
userInput = '(Invalid input).'
listItem = wf3.add_item(title = 'Enter default list (Id): ' + userInput, subtitle = 'Save?', valid = True, arg = 'cu:config ' + query)
listItem.setvar('isSubmitted', 'true')
elif query.startswith(confNames['confSpace'] + ' '):
userInput = query.replace(confNames['confSpace'] + ' ', '')
if not userInput.isnumeric() or len(userInput) != 7:
userInput = '(Invalid input).'
spaceItem = wf3.add_item(title = 'Enter space (Id): ' + userInput, subtitle = 'Save?', valid = True, arg = 'cu:config ' + query)
spaceItem.setvar('isSubmitted', 'true')
elif query.startswith(confNames['confTeam'] + ' '):
userInput = query.replace(confNames['confTeam'] + ' ', '')
if not userInput.isnumeric() or len(userInput) != 7:
userInput = '(Invalid input).'
teamItem = wf3.add_item(title = 'Enter workspace (Id): ' + userInput, subtitle = 'Save?', valid = True, arg = 'cu:config ' + query)
teamItem.setvar('isSubmitted', 'true')
elif query.startswith(confNames['confProject'] + ' '):
userInput = query.replace(confNames['confProject'] + ' ', '')
if not userInput.isnumeric() or len(userInput) != 7:
userInput = '(Invalid input).'
projectItem = wf3.add_item(title = 'Enter folder (Id): ' + userInput, subtitle = 'Save?', valid = True, arg = 'cu:config ' + query)
projectItem.setvar('isSubmitted', 'true')
elif query.startswith(confNames['confDefaultTag'] + ' '):
userInput = query.replace(confNames['confDefaultTag'] + ' ', '')
if ',' in userInput:
userInput = '(Invalid input).'
tagItem = wf3.add_item(title = 'Enter default tag: ' + userInput.lower(), subtitle = 'Save?', valid = True, arg = 'cu:config ' + query)
tagItem.setvar('isSubmitted', 'true')
elif query.startswith(confNames['confHierarchyLimit'] + ' '):
userInput = query.replace(confNames['confHierarchyLimit'] + ' ', '')
for level in userInput.split(','):
if level.strip() not in ('list', 'folder', 'space'):
userInput = '(Invalid input).'
tagItem = wf3.add_item(title = 'Enter hierarchy level(s): ' + userInput, subtitle = 'e.g. "list" or "list, folder". Save?', valid = True, arg = 'cu:config ' + query)
tagItem.setvar('isSubmitted', 'true')
elif query.startswith(confNames['confNotification'] + ' '):
userInput = query.replace(confNames['confNotification'] + ' ', '')
if not (userInput == 'true' or userInput == 'false'):
userInput = '(Invalid input).'
elif userInput == 'true':
userInput = '✓'
elif userInput == 'false':
userInput = '✗'
notificationItem = wf3.add_item(title = 'Enable notification (true, false): ' + userInput, subtitle = 'Save?', valid = True, arg = 'cu:config ' + query)
notificationItem.setvar('isSubmitted', 'true')
elif query.startswith('validate'): # No suffix ' ' needed, as user is not expected to provide any input.
wf3.add_item(title = 'Checking API Key: ' + ('✓' if checkClickUpId('list', 'confList') else '✗'), valid = True, arg = 'cu:config ')
wf3.add_item(title = 'Checking List Id: ' + ('✓' if checkClickUpId('list', 'confList') else '✗'), valid = True, arg = 'cu:config ')
wf3.add_item(title = 'Checking Space Id: ' + ('✓' if checkClickUpId('space', 'confSpace') else '✗'), valid = True, arg = 'cu:config ')
wf3.add_item(title = 'Checking Team Id: ' + ('✓' if checkClickUpId('team', 'confTeam') else '✗'), valid = True, arg = 'cu:config ')
if getConfigValue(confNames['confProject']):
wf3.add_item(title = 'Checking Project Id: ' + ('✓' if checkClickUpId('folder', 'confProject') else '✗'), valid = True, arg = 'cu:config ')
wf3.send_feedback()
def getConfigName(query):
'''Returns the name of a configuration item from a user's query, e.g. extracts 'defaultTag' from 'cu:config defaultTag'.
----------
@param str query: The user's input.
'''
wf = Workflow()
log = wf.logger
hasValue = query.split(' ') > 1
if hasValue:
# First element is our config name, whether there is a value or not
return query.split(' ')[1]
else:
return query
def getUserInput(query, configName):
'''Returns the value for a configuration item from a user's query, e.g. extracts 'to_sort' from 'cu:config defaultTag to_sort'.
----------
@param str query: The user's input.
@param str configName: The name of a configuration item-, e.g. 'dueDate'.
'''
return query.replace('cu:config ', '').replace(configName, '').strip()
def getConfigValue(configName):
'''Returns the stored value for a configuration item Workflow settings or MacOS Keychain.
----------
@param str configName: The name of a configuration item-, e.g. 'dueDate'.
'''
wf = Workflow()
log = wf.logger
if configName == confNames['confApi']:
try:
value = wf.get_password('clickUpAPI')
except PasswordNotFound:
value = None
pass
else:
if configName in wf.settings:
value = wf.settings[configName]
else:
value = None
return value
def checkClickUpId(idType, configKey):
'''Calls ClickUp API and returns whether call was successful or not..
----------
@param str idType: The value to be used in the API URL.
@param str configKey: The name of the setting to be retrieved.
'''
url = 'https://api.clickup.com/api/v2/' + idType + '/' + getConfigValue(confNames[configKey])
headers = {}
headers['Authorization'] = getConfigValue(confNames['confApi'])
headers['Content-Type'] = 'application/json'
# Use requests instead of Workflow.web, as web does not return the response in case of failure (only 401 NOT_AUTHORIZED, which is the same for API key failure or listId etc. failure)
import requests
request = requests.get(url, headers = headers)
result = request.json()
if 'ECODE' in result and result['ECODE'] == 'OAUTH_019': # Wrong API key
return False
elif 'ECODE' in result and result['ECODE'] == 'OAUTH_023': # Wrong ListId or team not authorized
return False
elif 'ECODE' in result and result['ECODE'] == 'OAUTH_027': # Wrong SpaceId/ProjectId or team not authorized
return False
elif idType != 'team' and 'id' in result and result['id'] == getConfigValue(confNames[configKey]) and 'name' in result and result['name'] != '':
return True
elif idType == 'team' and 'team' in result and 'id' in result['team'] and result['team']['id'] == getConfigValue(confNames[configKey]) and 'name' in result['team'] and result['team']['name'] != '':
return True
if __name__ == "__main__":
wf = Workflow()
wf3 = Workflow3()
log = wf.logger
sys.exit(wf.run(main))