-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault.py
executable file
·87 lines (75 loc) · 2.25 KB
/
default.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
#!/usr/bin/python3
#
# LaunchBar Action Script
#
import sys
import json
import os
from devonthink import DEVONthink
from logger import logger
from config import UserConfig
from cache import DB_PATH as DB_PATH_CACHE
from frequency import DB_PATH as DB_PATH_FREQUENCY
EXCLUDED_TAG = UserConfig.excluded_tag
QUERY_TEMPLATE = 'name:({}) tags!={}'
items = []
class LaunchBarError(Exception):
def __init__(self, launchbar_item, message=None):
self.message = message
self.launchbar_item = launchbar_item
def clean_all_db():
os.remove(DB_PATH_CACHE)
os.remove(DB_PATH_FREQUENCY)
def parse_query(arg):
def prepend_tilde(word):
if word.startswith('~'):
return word
else:
return '~' + word
if arg.startswith('>'):
arg = arg[1:].strip()
if arg == 'clean':
clean_all_db()
else:
raise LaunchBarError(dict(title='Invalid arguments',icon='character:🚫'))
elif arg.startswith('`'):
return arg[1:]
elif len(arg.split()) == 1:
return QUERY_TEMPLATE.format(prepend_tilde(arg), EXCLUDED_TAG)
else:
parts = arg.split(' ')
parts = [prepend_tilde(p) for p in parts]
return QUERY_TEMPLATE.format(' '.join(parts), EXCLUDED_TAG)
def main():
dt = DEVONthink()
assert len(sys.argv) == 2
arg = sys.argv[1]
try:
if arg:
logger.debug('======================')
logger.debug('before search')
query = parse_query(arg)
logger.debug('query: ' + query)
items.extend(dt.search(query))
logger.debug('after search')
if not items:
raise LaunchBarError({
'title': 'No record found',
'icon': 'character:☹️'
})
else:
raise LaunchBarError({
'title': 'Please inpu the query',
'icon': 'character:⌨️'
})
except LaunchBarError as e:
lb_item = e.launchbar_item
if lb_item:
items.append(lb_item)
else:
raise ValueError()
else:
logger.debug(f'Record amounts: {len(items)}')
print(json.dumps(items))
if __name__ == "__main__":
main()