Repository navigation
Expand file tree
/
Copy pathworkflow3.py
More file actions
365 lines (267 loc) · 9.98 KB
/
Copy pathworkflow3.py
File metadata and controls
365 lines (267 loc) · 9.98 KB
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# encoding: utf-8
#
# Copyright (c) 2016 Dean Jackson <deanishe@deanishe.net>
#
# MIT Licence. See http://opensource.org/licenses/MIT
#
# Created on 2016-06-25
#
"""
:class:`Workflow3` supports Alfred 3's new features.
It is an Alfred 3-only version of :class:`~workflow.workflow.Workflow`.
It supports setting :ref:`workflow-variables` and
:class:`the more advanced modifiers <Modifier>` supported by Alfred 3.
In order for the feedback mechanism to work correctly, it's important
to create :class:`Item3` and :class:`Modifier` objects via the
:meth:`Workflow3.add_item()` and :meth:`Item3.add_modifier()` methods
respectively.
"""
from __future__ import print_function, unicode_literals, absolute_import
import json
import os
import sys
from .workflow import Workflow
class Modifier(object):
"""Modify ``Item3`` values for when specified modifier keys are pressed.
Valid modifiers (i.e. values for ``key``) are:
* cmd
* alt
* shift
* ctrl
* fn
Attributes:
arg (unicode): Arg to pass to following action.
key (unicode): Modifier key (see above).
subtitle (unicode): Override item subtitle.
valid (bool): Override item validity.
variables (dict): Workflow variables set by this modifier.
"""
def __init__(self, key, subtitle=None, arg=None, valid=None):
"""Create a new :class:`Modifier`.
You probably don't want to use this class directly, but rather
use :meth:`Item3.add_modifier()` to add modifiers to results.
Args:
key (unicode): Modifier key, e.g. ``"cmd"``, ``"alt"`` etc.
subtitle (unicode, optional): Override default subtitle.
arg (unicode, optional): Argument to pass for this modifier.
valid (bool, optional): Override item's validity.
"""
self.key = key
self.subtitle = subtitle
self.arg = arg
self.valid = valid
self.config = {}
self.variables = {}
def setvar(self, name, value):
"""Set a workflow variable for this Item.
Args:
name (unicode): Name of variable.
value (unicode): Value of variable.
"""
self.variables[name] = value
def getvar(self, name, default=None):
"""Return value of workflow variable for ``name`` or ``default``.
Args:
name (unicode): Variable name.
default (None, optional): Value to return if variable is unset.
Returns:
unicode or ``default``: Value of variable if set or ``default``.
"""
return self.variables.get(name, default)
@property
def obj(self):
"""Return modifier for Alfred 3 feedback JSON.
Returns:
dict: Modifier for serializing to JSON.
"""
o = {}
if self.subtitle is not None:
o['subtitle'] = self.subtitle
if self.arg is not None:
o['arg'] = self.arg
if self.valid is not None:
o['valid'] = self.valid
# Variables and config
if self.variables or self.config:
d = {}
if self.variables:
d['variables'] = self.variables
if self.config:
d['config'] = self.config
if self.arg is not None:
d['arg'] = self.arg
o['arg'] = json.dumps({'alfredworkflow': d})
return o
class Item3(object):
"""Represents a feedback item for Alfred 3.
Generates Alfred-compliant XML for a single item.
You probably shouldn't use this class directly, but via
:meth:`Workflow3.add_item`. See :meth:`~Workflow3.add_item`
for details of arguments.
"""
def __init__(self, title, subtitle='', arg=None, autocomplete=None,
valid=False, uid=None, icon=None, icontype=None,
type=None, largetext=None, copytext=None):
"""Use same arguments as for :meth:`Workflow.add_item`.
Argument ``subtitle_modifiers`` is not supported.
"""
self.title = title
self.subtitle = subtitle
self.arg = arg
self.autocomplete = autocomplete
self.valid = valid
self.uid = uid
self.icon = icon
self.icontype = icontype
self.type = type
self.largetext = largetext
self.copytext = copytext
self.modifiers = {}
self.config = {}
self.variables = {}
def setvar(self, name, value):
"""Set a workflow variable for this Item.
Args:
name (unicode): Name of variable.
value (unicode): Value of variable.
"""
self.variables[name] = value
def getvar(self, name, default=None):
"""Return value of workflow variable for ``name`` or ``default``.
Args:
name (unicode): Variable name.
default (None, optional): Value to return if variable is unset.
Returns:
unicode or ``default``: Value of variable if set or ``default``.
"""
return self.variables.get(name, default)
def add_modifier(self, key, subtitle=None, arg=None, valid=None):
"""Add alternative values for a modifier key.
Args:
key (unicode): Modifier key, e.g. ``"cmd"`` or ``"alt"``
subtitle (unicode, optional): Override item subtitle.
arg (unicode, optional): Input for following action.
valid (bool, optional): Override item validity.
Returns:
Modifier: Configured :class:`Modifier`.
"""
mod = Modifier(key, subtitle, arg, valid)
for k in self.variables:
mod.setvar(k, self.variables[k])
self.modifiers[key] = mod
return mod
@property
def obj(self):
"""Return Modifier formatted for JSON serialization.
Returns:
dict: Data suitable for Alfred 3 feedback.
"""
o = {'title': self.title,
'subtitle': self.subtitle,
'valid': self.valid}
text = {}
icon = {}
if self.arg is not None:
o['arg'] = self.arg
if self.autocomplete is not None:
o['autocomplete'] = self.autocomplete
if self.uid is not None:
o['uid'] = self.uid
if self.type is not None:
o['type'] = self.type
# Largetype and copytext
if self.largetext is not None:
text['largetype'] = self.largetext
if self.copytext is not None:
text['copy'] = self.copytext
if text:
o['text'] = text
# Icon
if self.icon is not None:
icon['path'] = self.icon
if self.icontype is not None:
icon['type'] = self.icontype
if icon:
o['icon'] = icon
# Variables and config
if self.variables or self.config:
d = {}
if self.variables:
d['variables'] = self.variables
if self.config:
d['config'] = self.config
if self.arg is not None:
d['arg'] = self.arg
o['arg'] = json.dumps({'alfredworkflow': d})
# Modifiers
if self.modifiers:
mods = {}
for k, mod in self.modifiers.items():
mods[k] = mod.obj
o['mods'] = mods
return o
class Workflow3(Workflow):
"""Workflow class that generates Alfred 3 feedback."""
item_class = Item3
def __init__(self, **kwargs):
"""Create a new :class:`Workflow3` object.
See :class:`~workflow.workflow.Workflow` for documentation.
"""
Workflow.__init__(self, **kwargs)
self.variables = {}
@property
def _default_cachedir(self):
"""Alfred 3's default cache directory."""
return os.path.join(
os.path.expanduser(
'~/Library/Caches/com.runningwithcrayons.Alfred-3/'
'Workflow Data/'),
self.bundleid)
@property
def _default_datadir(self):
"""Alfred 3's default data directory."""
return os.path.join(os.path.expanduser(
'~/Library/Application Support/Alfred 3/Workflow Data/'),
self.bundleid)
def setvar(self, name, value):
"""Set a workflow variable that will be inherited by all new items.
Args:
name (unicode): Name of variable.
value (unicode): Value of variable.
"""
self.variables[name] = value
def getvar(self, name, default=None):
"""Return value of workflow variable for ``name`` or ``default``.
Args:
name (unicode): Variable name.
default (None, optional): Value to return if variable is unset.
Returns:
unicode or ``default``: Value of variable if set or ``default``.
"""
return self.variables.get(name, default)
def add_item(self, title, subtitle='', arg=None, autocomplete=None,
valid=False, uid=None, icon=None, icontype=None,
type=None, largetext=None, copytext=None):
"""Add an item to be output to Alfred.
See :meth:`~workflow.workflow.Workflow.add_item` for the main
documentation.
The key difference is that this method does not support the
``modifier_subtitles`` argument. Use the :meth:`~Item3.add_modifier()`
method instead on the returned item instead.
Returns:
Item3: Alfred feedback item.
"""
item = self.item_class(title, subtitle, arg,
autocomplete, valid, uid, icon, icontype, type,
largetext, copytext)
for k in self.variables:
item.setvar(k, self.variables[k])
self._items.append(item)
return item
def send_feedback(self):
"""Print stored items to console/Alfred as JSON."""
items = []
for item in self._items:
items.append(item.obj)
json.dump({'items': items}, sys.stdout)
sys.stdout.flush()