-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
89 lines (73 loc) · 2.96 KB
/
main.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
import logging
from ulauncher.api.client.Extension import Extension
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.shared.event import KeywordQueryEvent, ItemEnterEvent
from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
from ulauncher.api.shared.action.CopyToClipboardAction import CopyToClipboardAction
from ulauncher.api.shared.action.HideWindowAction import HideWindowAction
from ulauncher.api.shared.action.OpenAction import OpenAction
from ulauncher.api.shared.action.ExtensionCustomAction import ExtensionCustomAction
import os
from datetime import datetime
logger = logging.getLogger(__name__)
class DemoExtension(Extension):
def __init__(self):
super(DemoExtension, self).__init__()
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
self.subscribe(ItemEnterEvent, ItemEnterEventListener())
class KeywordQueryEventListener(EventListener):
def on_event(self, event, extension):
items = []
note = {
"data": event.get_argument(),
}
items.append(
ExtensionResultItem(
icon="images/icon.png",
name="Create note",
description="Enter to create note",
on_enter=ExtensionCustomAction(note, keep_app_open=True),
)
)
return RenderResultListAction(items)
def save_note(note, directory, filename_format, file_extension):
file_path = get_file_path(directory, filename_format, file_extension)
f = open(file_path, "a")
f.write("%s\n" % note)
f.close()
def get_file_path(directory, filename_format, file_extension):
home = os.path.expanduser("~")
directory_store = "%s/notes" % home
if not os.path.exists(directory_store):
os.makedirs(directory_store)
if directory:
directory_store = directory
filename = datetime.today().strftime(filename_format)
return directory_store + "/" + filename + file_extension
class ItemEnterEventListener(EventListener):
def on_event(self, event, extension):
note = event.get_data()
save_note(
note["data"],
extension.preferences["directory"],
extension.preferences["filename_format"],
extension.preferences["file_extension"],
)
file_path = get_file_path(
extension.preferences["directory"],
extension.preferences["filename_format"],
extension.preferences["file_extension"],
)
return RenderResultListAction(
[
ExtensionResultItem(
icon="images/open.png",
name="Open note",
description="Enter to open file or Escape to close",
on_enter=OpenAction(file_path),
)
]
)
if __name__ == "__main__":
DemoExtension().run()