From fd4ded438e3baa1984256c3179996dedc1d2f040 Mon Sep 17 00:00:00 2001 From: cswimr Date: Thu, 12 Dec 2024 11:51:19 -0500 Subject: [PATCH] add a wrapper script around `does-desktop-entry-exist` so it can be easily ran from the command line --- scripts/py/common/common.py | 10 ++++++--- scripts/py/does-desktop-entry-exist.py | 29 ++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) create mode 100755 scripts/py/does-desktop-entry-exist.py diff --git a/scripts/py/common/common.py b/scripts/py/common/common.py index fd6e9d6..d06a2ee 100644 --- a/scripts/py/common/common.py +++ b/scripts/py/common/common.py @@ -47,7 +47,7 @@ def read_secret_file(secret: str, home: bool = False) -> str: return secret -def does_desktop_entry_exist(desktop_entry: str) -> bool: +def does_desktop_entry_exist(desktop_entry: str, show_all_paths: bool = True) -> bool: if not desktop_entry: raise ValueError("Please provide the full filename of the desktop entry.") @@ -71,11 +71,15 @@ def does_desktop_entry_exist(desktop_entry: str) -> bool: entry_paths = [os.path.join(path, "applications") for path in xdg_data_dirs] entry_paths.append(os.path.expanduser("~/.local/share/applications")) - print(f"Checking the following paths for {desktop_entry}:\n{entry_paths}\n{'-'*20}") + if show_all_paths: + print( + f"Checking the following paths for {desktop_entry}:\n{entry_paths}\n{'-'*20}" + ) for entry_path in entry_paths: entry_file = Path(entry_path) / f"{desktop_entry}" - print(f"Checking for {entry_file}") + if show_all_paths: + print(f"Checking for {entry_file}") if entry_file.is_file(): print(f"{desktop_entry} found in {entry_path}") return True diff --git a/scripts/py/does-desktop-entry-exist.py b/scripts/py/does-desktop-entry-exist.py new file mode 100755 index 0000000..20a1127 --- /dev/null +++ b/scripts/py/does-desktop-entry-exist.py @@ -0,0 +1,29 @@ +#! /usr/bin/env nix-shell +#! nix-shell /etc/nixos/scripts/nix/python.nix -i python + +import argparse + +from common.common import does_desktop_entry_exist # type: ignore + +if __name__ == "__main__": + parser = argparse.ArgumentParser( + prog="does-desktop-entry-exist.py", + description="Check if a desktop entry exists.", + epilog="Example usage: does-desktop-entry-exist.py org.kde.konsole", + ) + parser.add_argument( + "desktop_entry", + help="The desktop entry to check for.", + ) + parser.add_argument( + "-v", + "--verbose", + help="Prints all of the paths that will be checked by the script. Exists so NixOS users don't get their terminals flooded when running the script.", + action="store_true", + ) + + args = parser.parse_args() + + does_desktop_entry_exist( + desktop_entry=args.desktop_entry, show_all_paths=args.verbose + )