|
|
@@ -4,45 +4,73 @@
|
|
|
Arguments:
|
|
|
name
|
|
|
exec location
|
|
|
+ [icon path]
|
|
|
"""
|
|
|
|
|
|
-import sys
|
|
|
+import os
|
|
|
+from sys import stderr
|
|
|
+import argparse
|
|
|
|
|
|
+class Args(argparse.Namespace):
|
|
|
+ NAME_OF_ENTRY: str = ""
|
|
|
+ EXEC_PATH: str = ""
|
|
|
+ icon_path: str | None = None
|
|
|
+ argsparser: argparse.ArgumentParser
|
|
|
|
|
|
-try:
|
|
|
- name: str = sys.argv[1]
|
|
|
- path: str = sys.argv[2]
|
|
|
-except IndexError:
|
|
|
- print("Usage: dte NAME_OF_ENTRY EXEC_PATH")
|
|
|
- exit(1)
|
|
|
+ def __init__(self):
|
|
|
+ super().__init__()
|
|
|
+ self.argsparser = argparse.ArgumentParser("dte.py")
|
|
|
+ _ = self.argsparser.add_argument("NAME_OF_ENTRY")
|
|
|
+ _ = self.argsparser.add_argument("EXEC_PATH")
|
|
|
+ _ = self.argsparser.add_argument("--icon-path", "-i")
|
|
|
+ self = self.argsparser.parse_args(namespace=self)
|
|
|
|
|
|
-try:
|
|
|
- with open(f"/home/aaro/.local/share/applications/{name}.desktop", "r") as f:
|
|
|
- print("Desktop entry already exists");
|
|
|
- exit(0)
|
|
|
-except FileNotFoundError:
|
|
|
- pass
|
|
|
|
|
|
-msg_str: str = f"""
|
|
|
-This will create a destop entry with the following values:
|
|
|
- Name={name}
|
|
|
- Exec={path}
|
|
|
-"""
|
|
|
+args = Args()
|
|
|
+
|
|
|
+name: str = args.NAME_OF_ENTRY
|
|
|
+path: str = args.EXEC_PATH
|
|
|
+icon_path: str | None = args.icon_path
|
|
|
+entry_path: str = os.path.expanduser(f"~/.local/share/applications/{name}.desktop")
|
|
|
+entry_lines = [
|
|
|
+ "[Desktop Entry]\n",
|
|
|
+ "Type=Application\n",
|
|
|
+ f"Name={name}\n",
|
|
|
+ f"Exec={path}\n"
|
|
|
+]
|
|
|
+if icon_path is not None:
|
|
|
+ entry_lines.append(f"Icon={icon_path}\n")
|
|
|
+
|
|
|
+msg_str: str = f"This will create the following destop entry:\n\n"
|
|
|
+for line in entry_lines:
|
|
|
+ msg_str += line
|
|
|
+msg_str += f"\nIn {entry_path}\n"
|
|
|
print(msg_str)
|
|
|
|
|
|
while True:
|
|
|
- sure: str = input("Are you sure? (y/n): ")
|
|
|
- sure = sure.lower()
|
|
|
+ sure: str = input("Continue? (y/n): ").strip().lower()
|
|
|
if sure == "y" or sure == "yes":
|
|
|
break
|
|
|
elif sure == "n" or sure == "no":
|
|
|
exit(0)
|
|
|
|
|
|
+try:
|
|
|
+ with open(entry_path, "r") as f:
|
|
|
+ print("Desktop entry already exists");
|
|
|
+ while True:
|
|
|
+ choice = input(f"Overwrite entry at {entry_path}? (y/n): ").strip().lower()
|
|
|
+ if choice == "y":
|
|
|
+ try:
|
|
|
+ os.remove(entry_path)
|
|
|
+ break
|
|
|
+ except Exception as e:
|
|
|
+ print(f"Failed to remove entry. Exception: {e}", file=stderr)
|
|
|
+ exit(1)
|
|
|
+ elif choice == "n":
|
|
|
+ exit(0)
|
|
|
+except FileNotFoundError:
|
|
|
+ pass
|
|
|
+
|
|
|
|
|
|
with open(f"/home/aaro/.local/share/applications/{name}.desktop", "x") as f:
|
|
|
- f.writelines([
|
|
|
- "[Desktop Entry]\n",
|
|
|
- "Type=Application\n",
|
|
|
- f"Name={name}\n",
|
|
|
- f"Exec={path}\n"
|
|
|
- ])
|
|
|
+ f.writelines(entry_lines)
|