-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnotable.py
114 lines (84 loc) · 2.57 KB
/
notable.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
import sys
from workflow import Workflow3
import os
import glob
# set paths
DATA_DIRECTORY = os.getenv("notes_location")
note_files = glob.glob(DATA_DIRECTORY + "/*.md")
# icons
ICON_MD = "markdown-icon.png"
ICON_WARNING = "warning-icon.png"
ICON_UPDATE = 'update-icon.png'
# notable uri
NOTABLE_URI = "notable://"
NOTABLE_NOTE = "note/"
log = None
def get_notes() -> list:
"""Generate notes list from data dictionary
provided by the user
Returns:
returns a list
"""
import frontmatter as fm
notes_data = []
for f in note_files:
path = os.path.join(DATA_DIRECTORY, f)
log.info(f"loading note at {path}")
with open(path, "r") as f:
content = fm.load(f)
title = content.get("title")
log.info(f"Extract title: {title}")
tags = content.get("tags")
if isinstance(tags, list):
tags = "/".join(tags)
if not tags:
tags = ""
log.info(f"Extracted tags: {tags}")
note = dict(title=title, tags=tags, path=path)
notes_data.append(note)
return notes_data
def search_key_notes(note: dict) -> str:
"""Creates a search key based on the title and tags
to filter results
Args:
note: A dict with title and tags
Returns:
key: A search string
"""
keys = []
keys.append(note["title"])
key = " ".join(keys)
log.info(f"Key built: {key}")
return key
def main(wf):
# Get query from Alfred
if len(wf.args):
query = wf.args[0]
else:
query = None
log.info(query)
notes = wf.cached_data("notes", get_notes, max_age=0, session=True)
# If script was passed a query, use it to filter posts
if query:
notes = wf.filter(query, notes, key=search_key_notes, min_score=20)
if not notes: # we have no data to show, so show a warning and stop
wf.add_item('No notes found sorry 😔', icon=ICON_WARNING)
wf.send_feedback()
return 0
for note in notes:
# open note using URI
open_note_notable = NOTABLE_URI + NOTABLE_NOTE + note["path"]
item = wf.add_item(
title=note["title"],
subtitle=note["tags"],
icon=ICON_MD,
arg=open_note_notable,
valid=True,
)
# let user open note in default editor instead
item.add_modifier(key="cmd", arg=note["path"], valid=True)
wf.send_feedback()
if __name__ == "__main__":
wf = Workflow3(libraries=["./lib"])
log = wf.logger
sys.exit(wf.run(main))