-
Notifications
You must be signed in to change notification settings - Fork 20
/
create.py
93 lines (71 loc) · 2.47 KB
/
create.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
#!/usr/bin/python
# encoding: utf-8
"""
Note creation script for alfred-bear workflow.
"""
import sys
from urllib import quote
from workflow import Workflow, ICON_SYNC
LOGGER = None
# Update workflow from GitHub repo
UPDATE_SETTINGS = {'github_slug': 'chrisbro/alfred-bear'}
SHOW_UPDATES = True
def main(workflow):
"""
I'm just here so I don't get fined by pylint
"""
if SHOW_UPDATES and workflow.update_available:
workflow.add_item('A new version is available',
'Action this item to install the update',
autocomplete='workflow:update',
icon=ICON_SYNC)
LOGGER.debug('Started create workflow')
query = workflow.args[0]
LOGGER.debug(query)
tags = extract_tags(query)
tags_string = ', '.join(tags)
title_string = strip_tags_from_string(tags, query)
query_string = create_query_output(query, tags)
LOGGER.debug(title_string)
LOGGER.debug(query_string)
if tags:
workflow.add_item(title='Create note with title ' + title_string,
subtitle='Tags: ' + tags_string, arg=query_string, valid=True)
else:
workflow.add_item(title='Create note with title ' + title_string,
arg=query_string, valid=True)
workflow.send_feedback()
def create_query_output(title, tags):
"""
Generates what query parameters to pass to the Alfred callback step.
"""
query_string = ''
if title:
query_string += 'title=' + quote(title.encode('utf-8'))
query_string += '&text=' + quote(title.encode('utf-8'))
if tags:
tags_string = ''
for tag in tags:
tags_string += quote(tag.encode('utf-8')) + ','
query_string = strip_tags_from_string(tags, query_string)
tags_string = tags_string[:-1]
query_string += '&tags=' + tags_string
LOGGER.debug(query_string)
return query_string
def strip_tags_from_string(tags, query):
"""
Yanks out all the hashtags from a string.
"""
for tag in tags:
query = query.replace(quote('#' + tag.encode('utf-8')), '')
query = query.replace('#' + tag, '')
return query
def extract_tags(query):
"""
Gets all the tags from the query.
"""
return set(part[1:] for part in query.split() if part.startswith('#'))
if __name__ == '__main__':
WORKFLOW = Workflow(update_settings=UPDATE_SETTINGS)
LOGGER = WORKFLOW.logger
sys.exit(WORKFLOW.run(main))