| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- #!/bin/python3
- """
- Arguments:
- name
- exec location
- [icon path]
- """
- 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
- 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)
- 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("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(entry_path, "x") as f:
- f.writelines(entry_lines)
|