-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto-opener.py
executable file
·271 lines (226 loc) · 8.74 KB
/
auto-opener.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/usr/bin/python3
import sys
import os
import re
import subprocess
import argparse
import dbus
# --- Configuration ---
TOP_LEVEL_COMMANDS = ["conf", "list", "help"]
SUB_COMMANDS = ["list", "add", "remove"]
def setup():
env = os.environ.copy()
env.pop("WAYLAND_DISPLAY", None)
env["DISPLAY"] = ":0"
# --- Error Handling ---
def error(message):
print(message)
if args.notifications: send_notification(message)
def fatal_error(message):
error(message)
sys.exit(1)
def send_notification(message, error=False):
title = f"{os.path.basename(sys.argv[0])}{"ERROR" if error else ""}"
match sys.platform:
case "linux" | "linux2":
session_bus = dbus.SessionBus()
notify_object = session_bus.get_object('org.freedesktop.Notifications', '/org/freedesktop/Notifications')
notify_interface = dbus.Interface(notify_object, 'org.freedesktop.Notifications')
notify_interface.Notify(title, 0, '', title, message, [], {}, -1)
case "darwin":
CMD = '''
on run argv
display notification (item 2 of argv) with title (item 1 of argv)
end run
'''
subprocess.call([
'osascript', '-e', CMD, title, message])
case "win32":
pass # not implemented
case _:
fatal_error("Couldn't detect OS platform")
# --- Utility Functions ---
def exist_path(filepath):
return os.path.exists(filepath)
def valid_link(link):
link = link.strip()
return link.startswith("http") or exist_path(link)
def generate_key_list(config):
return [key for key in config]
def make_hyperlink(url, text=None):
if not url.startswith("http"):
text = url
url = f"file://{url}"
return f'\033]8;;{url}\033\\{text}\033]8;;\033\\'
def cardinal_print(to_print, as_hyperlink=False):
for i, element in enumerate(to_print, 0):
if as_hyperlink: element = make_hyperlink(element, element)
print(f"{i}) {element}")
def print_key_list(config):
key_list = generate_key_list(config)
print("Titles in config file:")
cardinal_print(key_list)
return key_list
def display_help():
top_level_commands_str = ", ".join(TOP_LEVEL_COMMANDS)
sub_commands_str = ", ".join(SUB_COMMANDS)
help_text = (
"Help:\n"
f" ao <title> As default usage opens links related to title.\n"
f" ao <top-level command>. Top-level commands: [{top_level_commands_str}]\n"
f" ao <title> OPTIONAL <sub-command>. Sub commands: [{sub_commands_str}]\n"
f"Please note: add and remove commands are user-interactive."
)
print(help_text)
# --- Command line args and configuration ---
def get_config_path():
path = ""
match sys.platform:
case "linux" | "linux2" | "darwin":
path = os.path.expanduser("~/.config/auto-opener/config.config")
case "win32":
separator = "\\"
path = separator.join(os.path.realpath(__file__).split(separator)[:-1] + ["template.config"])
case _:
fatal_error("Couldn't detect OS platform")
return path
def parse_args():
parser = argparse.ArgumentParser(
description="Auto Opener: Manage and open URLs or file paths from a configuration file.",
usage="%(prog)s [title] [sub-command]"
)
parser.add_argument(
'title_or_command',
nargs='?',
help="A title from the config or a top-level command. Top-level commands: conf, list, help."
)
# Optional sub-command
parser.add_argument(
'sub_command',
nargs='?',
choices=SUB_COMMANDS,
help="Sub-command for a title: list, add, or remove."
)
parser.add_argument(
'--notifications',
action='store_true',
help="Enable notifications for actions."
)
return parser.parse_args()
def parse_config(filepath):
config = {}
title_pattern = r'^\[(.+)\]$'
with open(filepath, 'r') as file:
current_title = None
for line_number, line in enumerate(file, start=1):
line = line.strip()
match = re.match(title_pattern, line)
if match:
current_title = match.group(1)
if current_title in config:
fatal_error(f"Bad config at {filepath}, duplicate title found: '{current_title}' on line {line_number}")
config[current_title] = []
elif line:
if current_title is None:
fatal_error(f"Bad config at {filepath}, line without title at line {line_number}: '{line}'")
config[current_title].append(line)
return config
def rewrite_config(filepath, config):
with open(filepath, "w") as f:
for key, links in config.items():
f.write(f"[{key}]\n")
f.writelines(f"{link}\n" for link in links)
f.write("\n")
# --- Link Opening ---
def open_default(path):
match sys.platform:
case "linux" | "linux2":
os.system(f"xdg-open '{path}'")
case "darwin":
os.system(f"open '{path}'")
case "win32":
try:
os.startfile(f"'{path}'")
except: # TODO: FIX logs the same thing for every error in windows
print("Path in config file are not formatted for Windows, ignoring.")
case _:
fatal_error("Couldn't detect OS platform")
def open_links(links):
for link in links:
if valid_link(link):
open_default(link)
print(f"Opened successfully {link}")
else:
error(f"`{link}` is not a valid URL or filepath.")
# --- User Input Handling ---
def controlled_input(upper_limit, message):
valid_input = ""
while not valid_input.isnumeric() or 0 > int(valid_input) or int(valid_input) >= upper_limit:
valid_input = input(f"Insert a valid number between 0 and {upper_limit - 1}: ")
return int(valid_input)
# --- Top-Level Command Handling ---
def handle_top_level_command(config, command):
if command == "conf":
open_default(path)
print("Opened config.")
elif command == "help":
display_help()
elif command == "list":
print_key_list(config)
# --- Sub-Command Handling ---
def handle_sub_command(config, command, title_to_open):
modified_config = False
if command == "list":
links = config.get(title_to_open, [])
print(f"Links of title {title_to_open}:")
if len(links) == 0:
error(f"no links associated with this title.")
else:
cardinal_print(links, as_hyperlink=True)
elif command == "add":
new_link = input(f"Insert filepath/url to {command} to {title_to_open}{' (new)' if title_to_open not in config else ''}: ")
config.setdefault(title_to_open, []).append(new_link)
modified_config = True
print(f"{new_link} added successfully to {title_to_open}")
elif command == "remove":
if title_to_open in config:
links_list = config.get(title_to_open, [])
if len(links_list) == 0:
remove_title = input(f"{title_to_open} has no links associated with it. Want to remove the title from configuration? [y/N] ")
if remove_title.strip().lower() == "y":
config.pop(title_to_open)
modified_config = True
print(f"Removed {title_to_open} from configuration.")
else:
print("Current titles' list:")
cardinal_print(links_list, as_hyperlink=True)
index = controlled_input(len(links_list), "Insert index of the element to remove: ")
element = links_list.pop(index)
modified_config = True
print(f"{element} removed successfully from {title_to_open}")
else:
fatal_error(f"Can't remove: {title_to_open} is not a title in config.")
if modified_config:
rewrite_config(path, config)
# --- Main Functionality ---
def open_title(config, to_open):
if to_open in config:
links = config[to_open]
open_links(links)
else:
error(f"ERR: {to_open} not in config file.")
if __name__ == "__main__":
setup()
args = parse_args()
path = get_config_path()
config = parse_config(path)
if args.title_or_command in TOP_LEVEL_COMMANDS:
handle_top_level_command(config, args.title_or_command)
elif args.title_or_command:
title = args.title_or_command
if args.sub_command:
handle_sub_command(config, args.sub_command, title)
else:
open_title(config, title)
else:
fatal_error("Invalid command or missing title. Use --help for usage.")