From 9ba3712da81d97d53b00eced21e98045dda363bd Mon Sep 17 00:00:00 2001 From: Marcin Jaworski Date: Fri, 27 Dec 2024 00:09:45 +0100 Subject: [PATCH 1/4] Search for desktop files in all subdirectories of app dirs --- nwg_panel/icons.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/nwg_panel/icons.py b/nwg_panel/icons.py index 41434b1..83a9606 100644 --- a/nwg_panel/icons.py +++ b/nwg_panel/icons.py @@ -70,10 +70,19 @@ def __process_desktop_file(file_path): def __populate_caches(): - for d in __get_app_dirs(): + app_dirs = __get_app_dirs() + seen_dirs = set() + while app_dirs: + d = app_dirs.pop(0) if os.path.isdir(d): for file_name in os.listdir(d): - file_path = os.path.join(d, file_name) + file_path = os.path.realpath(os.path.join(d, file_name)) + if os.path.isdir(file_path): + if file_path in seen_dirs: + continue + seen_dirs.add(file_path) + app_dirs.insert(0, file_path) + continue __process_desktop_file(file_path) From 6618474d24f7bb6a8a74cd379055a40ff6831d5b Mon Sep 17 00:00:00 2001 From: Marcin Jaworski Date: Fri, 27 Dec 2024 00:21:30 +0100 Subject: [PATCH 2/4] Fix check for already seen directories --- nwg_panel/icons.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/nwg_panel/icons.py b/nwg_panel/icons.py index 83a9606..d31a534 100644 --- a/nwg_panel/icons.py +++ b/nwg_panel/icons.py @@ -74,13 +74,13 @@ def __populate_caches(): seen_dirs = set() while app_dirs: d = app_dirs.pop(0) + if d in seen_dirs: + continue + seen_dirs.add(d) if os.path.isdir(d): for file_name in os.listdir(d): file_path = os.path.realpath(os.path.join(d, file_name)) if os.path.isdir(file_path): - if file_path in seen_dirs: - continue - seen_dirs.add(file_path) app_dirs.insert(0, file_path) continue __process_desktop_file(file_path) From e6804bec051e98d3a6f72a560b4154b8b92e17c3 Mon Sep 17 00:00:00 2001 From: Marcin Jaworski Date: Fri, 27 Dec 2024 11:41:29 +0100 Subject: [PATCH 3/4] Check if file or directory exists before processing them --- nwg_panel/icons.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/nwg_panel/icons.py b/nwg_panel/icons.py index d31a534..071b754 100644 --- a/nwg_panel/icons.py +++ b/nwg_panel/icons.py @@ -80,6 +80,8 @@ def __populate_caches(): if os.path.isdir(d): for file_name in os.listdir(d): file_path = os.path.realpath(os.path.join(d, file_name)) + if not os.path.exists(file_path): + continue if os.path.isdir(file_path): app_dirs.insert(0, file_path) continue From 1e7fbfa5cb03a4ee53215bd717a66c8dd50c8f93 Mon Sep 17 00:00:00 2001 From: Marcin Jaworski Date: Fri, 27 Dec 2024 11:43:14 +0100 Subject: [PATCH 4/4] Handle OSError and UnicodeDecodeError when listing app dirs and opening .desktop files --- nwg_panel/icons.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/nwg_panel/icons.py b/nwg_panel/icons.py index 071b754..1e89820 100644 --- a/nwg_panel/icons.py +++ b/nwg_panel/icons.py @@ -32,8 +32,16 @@ def __get_app_dirs(): def __process_desktop_file(file_path): - with open(file_path, "r", encoding="utf-8") as f: - content = f.read() + try: + with open(file_path, "r", encoding="utf-8") as f: + content = f.read() + except UnicodeDecodeError: + print(f"Warning: Invalid .desktop file '{file_path}'") + return + except OSError as e: + print(f"Warning: Unable to read .desktop file '{file_path}': {e}") + return + icon_name = None app_names = [] startup_wm_class = None @@ -78,7 +86,13 @@ def __populate_caches(): continue seen_dirs.add(d) if os.path.isdir(d): - for file_name in os.listdir(d): + try: + files = os.listdir(d) + except OSError as e: + print(f"Warning: Can't list files in directory '{d}': {e}") + continue + + for file_name in files: file_path = os.path.realpath(os.path.join(d, file_name)) if not os.path.exists(file_path): continue