-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstickies_search.py
82 lines (72 loc) · 2.35 KB
/
stickies_search.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
#!/usr/bin/env python
# -*- coding=utf-8 -*-
from sys import path, argv, exit
import datetime
import unicodedata
from os.path import expanduser, getmtime, exists
from subprocess import Popen, PIPE
import json
import re
home = expanduser('~')
from pystickies.pystickies import parseStickies
from workflow import Workflow
OSX_NINE_DATABASE_FILE = home + '/Library/StickiesDatabase'
def get_db_path():
return OSX_NINE_DATABASE_FILE
def main(wf):
filename = u'database_cache.txt'
update_flag = False
stickies = []
if exists(filename):
mtime = getmtime(filename)
lastupdate = datetime.datetime.fromtimestamp(mtime)
if lastupdate < datetime.datetime.now() - datetime.timedelta(minutes=1):
update_flag = True
else:
update_flag = True
if update_flag:
f = open(filename, 'w')
db_path = get_db_path()
rtfs = parseStickies(db_path)
for rtf in rtfs:
pr = Popen(["textutil", "-convert", "txt", "-stdin","-stdout"],
stdout=PIPE,
stderr=PIPE,
stdin=PIPE)
(out, error) = pr.communicate(rtf)
out = unicodedata.normalize('NFC', out.decode('utf-8'))
stickies.append(out)
json.dump(stickies, f)
else:
f = open(filename)
stickies = json.load(f)
query = unicodedata.normalize('NFC', argv[1].decode('utf-8'))
item_isset = False
if query == u"clear":
wf.add_item(
u'CLEAR CACHED DATABASE',
arg=u'clear',
valid=True)
item_isset = True
query = re.compile(query, re.IGNORECASE)
for stickie in stickies:
m = query.search(stickie)
if m:
index = m.start(0)
title = stickie[:50]
arg = stickie[:26]
start_offset = 23
end_offset = 65
start = index - start_offset if index > start_offset else 0
end = start + end_offset if len(stickie) > start + end_offset else len(stickie)
subtitle = stickie[start:end].replace('\n','')
wf.add_item(title, subtitle, arg=arg, valid=True)
item_isset = True
if item_isset is False:
wf.add_item(u'No Item')
wf.send_feedback()
if __name__ == '__main__':
if len(argv) != 2:
exit()
wf = Workflow()
exit(wf.run(main))