-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
129 lines (104 loc) · 4.88 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import os
import subprocess
from datetime import datetime
from ulauncher.api.client.Extension import Extension
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.shared.event import (
KeywordQueryEvent,
ItemEnterEvent,
PreferencesEvent,
PreferencesUpdateEvent,
)
from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
from ulauncher.api.shared.action.ExtensionCustomAction import ExtensionCustomAction
# from ulauncher.api.shared.action.DoNothingAction import DoNothingAction
from ulauncher.api.shared.action.OpenAction import OpenAction
class NoteExtension(Extension):
"""The base extension"""
def __init__(self):
super(NoteExtension, self).__init__()
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
self.subscribe(ItemEnterEvent, ItemEnterEventListener())
self.subscribe(PreferencesUpdateEvent, PreferencesUpdateListener())
self.subscribe(PreferencesEvent, PreferencesEventListener())
def notify_user(self, title, message):
""" Function to show a notification to the user using notify-send """
try:
subprocess.run(['notify-send', title, message], check=True)
except FileNotFoundError:
print("Error: notify-send command not found. Please install it.")
def check_write_access_to_note(self, file_path):
""" Function to check if the user has write access to the note file """
file_path = os.path.expanduser(file_path)
try:
with open(file_path, 'a'):
pass
return True
except IOError:
self.notify_user("Error", f"No write access to {file_path}. Please check the file path.")
return False
class KeywordQueryEventListener(EventListener):
"""Listens to keyboard events"""
def on_event(self, event: KeywordQueryEvent, extension: Extension):
file_path = os.path.expanduser(extension.preferences["note_path"])
note_kw = extension.preferences["note_kw"]
open_arg = extension.preferences["open_arg"]
keyword = event.get_keyword()
note_text = event.get_argument()
if note_text:
if keyword == note_kw and note_text == open_arg:
return RenderResultListAction([
ExtensionResultItem(
icon='images/icon.png',
name="Open notes",
description=f"Open notes in {file_path}",
on_enter=OpenAction(file_path)
)
])
return RenderResultListAction([
ExtensionResultItem(
icon='images/icon.png',
name=f"Write '{note_text}' to {file_path}.",
description=f"Append text to {file_path}.",
on_enter=ExtensionCustomAction(note_text, keep_app_open=False)
)
])
return RenderResultListAction([])
class ItemEnterEventListener(EventListener):
"""Listens to enters"""
def on_event(self, event: ItemEnterEvent, extension: Extension):
text_to_write = event.get_data()
file_path = extension.preferences["note_path"]
file_path = os.path.expanduser(file_path)
include_date = extension.preferences["date_timestamp"] == "true"
include_time = extension.preferences["time_timestamp"] == "true"
if include_date and include_time:
text_to_write = "[" + str(datetime.now().strftime("%Y-%m-%d %H:%M:%S")) + "] " + text_to_write
elif include_date:
text_to_write = "[" + str(datetime.now().date()) + "] " + text_to_write
elif include_time:
text_to_write = "[" + str(datetime.now().strftime("%H:%M:%S")) + "] " + text_to_write
append_to_note(file_path, text_to_write)
class PreferencesUpdateListener(EventListener):
"""Executed when preferences are changed"""
def on_event(self, event: PreferencesUpdateEvent, extension: Extension):
if event.id == "note_path":
file_path = event.new_value
if not extension.check_write_access_to_note(file_path):
return None
extension.preferences[event.id] = event.new_value
class PreferencesEventListener(EventListener):
"""Executed on startup"""
def on_event(self, event: PreferencesEvent, extension: Extension):
file_path = event.preferences.get("note_path")
if not extension.check_write_access_to_note(file_path):
return None
extension.preferences.update(event.preferences)
def append_to_note(file_path, text):
""" Function to append text to a note file """
with open(file_path, 'a') as note_file:
note_file.write(text + '\n' + '\n')
if __name__ == '__main__':
"""The main function"""
NoteExtension().run()