From 9837af7a0c0712ce6be6b82de1f2d12fe9d08308 Mon Sep 17 00:00:00 2001 From: itdominator <1itdominator@gmail.com> Date: Sun, 13 Feb 2022 01:10:08 -0600 Subject: [PATCH] Refactored execute and collection logic. --- src/signal_classes/Controller.py | 91 ++++++++++----------- src/signal_classes/Controller_Data.py | 2 + src/signal_classes/Menu.py | 3 +- src/signal_classes/mixins/ProcessorMixin.py | 41 ++++------ src/utils/Settings.py | 6 +- 5 files changed, 66 insertions(+), 77 deletions(-) diff --git a/src/signal_classes/Controller.py b/src/signal_classes/Controller.py index 82229d9..bb4480f 100644 --- a/src/signal_classes/Controller.py +++ b/src/signal_classes/Controller.py @@ -1,5 +1,5 @@ # Python imports -import threading, subprocess +import threading, subprocess, traceback from os.path import isfile from os import listdir @@ -46,7 +46,7 @@ class Controller(ProcessorMixin, Menu, Controller_Data): fixed_programs_list.append({'name': program}) self.favorites = self.call_method("set_favorites_menu", [fixed_programs_list])["set_faves"] - self.save_faves(self.favorites) + self.settings.save_faves(self.favorites) continue if "[ Exit ]" in group: break @@ -55,47 +55,38 @@ class Controller(ProcessorMixin, Menu, Controller_Data): programs_list += self.get_sub_group(group, query) entry = self.call_method("sub_menu", [group, programs_list])["prog"] - self.logger.debug(entry) if entry not in base_options: - self.logger.info(f"[Executing Program] Group: {group} Entry: {entry}") - self.execute_program(group, entry) + self.execute_program(self.flat_menu_data, entry) except Exception as e: - self.logger.error(e.printStackTrace()) + self.logger.debug(f"Traceback: {traceback.print_exc()}") + self.logger.debug(f"Exception: {e}") def get_desktop_files_info(self, paths): menu_objects = { - "Accessories": [], - "Multimedia": [], - "Graphics": [], - "Game": [], - "Office": [], - "Development": [], - "Internet": [], - "Settings": [], - "System": [], - "Wine": [], - "Other": [] + "Accessories": {}, + "Multimedia": {}, + "Graphics": {}, + "Game": {}, + "Office": {}, + "Development": {}, + "Internet": {}, + "Settings": {}, + "System": {}, + "Wine": {}, + "Other": {} } for path in paths: - if not "/opt/" in path: - self.list_and_update_desktop_iles(path, menu_objects); - else: - for folder in listdir(path): - try: - full_path = f"{path}{folder}/" - self.list_and_update_desktop_iles(full_path, menu_objects); - except Exception as e: - self.logger.debug(e) + self.list_and_update_desktop_iles(path, menu_objects); return menu_objects def list_and_update_desktop_iles(self, path, menu_objects): try: for f in listdir(path): - full_path = f"{path}{f}" + full_path = f"{path}/{f}" if isfile(full_path) and f.endswith(".desktop"): xdg_object = DesktopEntry(full_path) hidden = xdg_object.getHidden() @@ -107,13 +98,13 @@ class Controller(ProcessorMixin, Menu, Controller_Data): continue if type == "Application" and groups != "": - title = xdg_object.getName() - comment = xdg_object.getComment() - # icon = xdg_object.getIcon() - mainExec = xdg_object.getExec() - tryExec = xdg_object.getTryExec() + title = xdg_object.getName() + comment = xdg_object.getComment() + # icon = xdg_object.getIcon() + main_exec = xdg_object.getExec() + try_exec = xdg_object.getTryExec() - group = "" + group = "" if "Accessories" in groups or "Utility" in groups: group = "Accessories" elif "Multimedia" in groups or "Video" in groups or "Audio" in groups: @@ -137,10 +128,14 @@ class Controller(ProcessorMixin, Menu, Controller_Data): else: group = "Other" - menu_objects[group].append( {"title": title, "groups": groups, - "comment": comment, "exec": mainExec, - "tryExec": tryExec, "fileName": f - }) + chunk_data = { + "groups": groups, "comment": comment, + "exec": main_exec, "try_exec": try_exec, + "fileName": f + } + + menu_objects[group][title] = chunk_data + self.flat_menu_data[title] = chunk_data except Exception as e: self.logger.debug(e) @@ -148,24 +143,24 @@ class Controller(ProcessorMixin, Menu, Controller_Data): def get_sub_group(self, group, query = ""): desktop_objects = [] if "Search..." in group: - group_keys = self.menu_data.keys() - for group_key in group_keys: - for option in self.menu_data[group_key]: - keys = option.keys() - if "comment" in keys and len(option["comment"]) > 0 : - if query.lower() in option["comment"].lower(): - desktop_objects.append( option["title"] + " || " + option["comment"] ) - if query.lower() in option["title"].lower() or query.lower() in option["fileName"].lower(): - desktop_objects.append( option["title"] + " || " + option["fileName"].replace(".desktop", "") ) + for key in self.flat_menu_data.keys(): + option = self.flat_menu_data[key] + keys = option.keys() + + if "comment" in keys and len(option["comment"]) > 0: + if query.lower() in option["comment"].lower(): + desktop_objects.append( f"{key} || {option['comment']}" ) + elif query.lower() in key.lower() or query.lower() in option["fileName"].lower(): + desktop_objects.append( f"{key} || {option['fileName'].replace('.desktop', '')}" ) elif "Favorites" in group: desktop_objects = self.favorites else: for option in self.menu_data[group]: keys = option.keys() if "comment" in keys and len(option["comment"]) > 0 : - desktop_objects.append( option["title"] + " || " + option["comment"] ) + desktop_objects.append( f"{option['title']} || {option['comment']}" ) else: - desktop_objects.append( option["title"] + " || " + option["fileName"].replace(".desktop", "") ) + desktop_objects.append( f"{option['title']} || {option['fileName'].replace('.desktop', '')}" ) return desktop_objects diff --git a/src/signal_classes/Controller_Data.py b/src/signal_classes/Controller_Data.py index ab017f0..13b2122 100644 --- a/src/signal_classes/Controller_Data.py +++ b/src/signal_classes/Controller_Data.py @@ -26,5 +26,7 @@ class Controller_Data: self.app_paths = self.settings.get_app_paths() self.favorites_path = self.settings.get_favorites_path() self.favorites = self.settings.get_favorites() + self.menu_data = None + self.flat_menu_data = {} GLib.unix_signal_add(GLib.PRIORITY_DEFAULT, signal.SIGINT, self.tear_down) diff --git a/src/signal_classes/Menu.py b/src/signal_classes/Menu.py index bf42059..4516b22 100644 --- a/src/signal_classes/Menu.py +++ b/src/signal_classes/Menu.py @@ -33,7 +33,6 @@ class Menu(StylesMixin): """ self.logger = settings.get_logger() self.theme = self.call_method(args.theme) - self.menu_data = None def main_menu(self, _group_list = None): @@ -58,7 +57,7 @@ class Menu(StylesMixin): 'type': 'checkbox', 'qmark': '>', 'message': 'Select Favorites', - 'name': 'setFaves', + 'name': 'set_faves', 'choices': group_list } diff --git a/src/signal_classes/mixins/ProcessorMixin.py b/src/signal_classes/mixins/ProcessorMixin.py index 804d770..8f12231 100644 --- a/src/signal_classes/mixins/ProcessorMixin.py +++ b/src/signal_classes/mixins/ProcessorMixin.py @@ -8,32 +8,25 @@ import os, subprocess class ProcessorMixin: - def execute_program(self, group, entry): - parts = entry.split("||") - program = parts[0].strip() - comment = parts[1].strip() + def execute_program(self, data, entry): + parts = entry.split("||") + title = parts[0].strip() + comment = parts[1].strip() + chunk_data = data[title.strip()] - if group in ["Search...", "Favorites"]: - group_keys = self.menu_data.keys() - for group_key in group_keys: - self.pre_execute(self.menu_data[group_key], program, comment) - else: - self.pre_execute(self.menu_data[group], program, comment) + self.logger.info(f"[Executing Program]\n\t\tEntry: {entry}\n\t\tChunk Data: {chunk_data}") + self.pre_execute(chunk_data) - - def pre_execute(self, options, program, comment): - for option in options: - if program in option["title"]: - keys = option.keys() - if comment in [option["comment"], option["fileName"]]: - try: - self.execute(opt["tryExec"]) - except Exception as e: - try: - if "exec" in keys and len(option["exec"]): - self.execute(option["exec"]) - except Exception as e: - self.logger.debug(e) + def pre_execute(self, option): + try: + self.execute(option["tryExec"]) + except Exception as e: + self.logger.info(f"[Executing Program]\n\t\t Try exec failed!\n{e}") + try: + if option["exec"] and len(option["exec"]) > 0: + self.execute(option["exec"]) + except Exception as e: + self.logger.debug(e) def execute(self, option): diff --git a/src/utils/Settings.py b/src/utils/Settings.py index 3d7b0c6..ce6eedb 100644 --- a/src/utils/Settings.py +++ b/src/utils/Settings.py @@ -14,8 +14,8 @@ class Settings: self._USER_HOME = os.path.expanduser('~') self._CONFIG_PATH = f"{self._USER_HOME}/.config/{app_name.lower()}" self._FAVORITES_FILE = f"{self._CONFIG_PATH}/favorites.json" - self._HOME_APPS = f"{self._USER_HOME}/.local/share/applications/" - self._APP_PATHS = ["/opt/", "/usr/share/applications/", self._HOME_APPS] + self._HOME_APPS = f"{self._USER_HOME}/.local/share/applications" + self._APP_PATHS = ["/usr/share/applications", self._HOME_APPS] self._logger = Logger(self._CONFIG_PATH).get_logger() self._faves = [] @@ -40,7 +40,7 @@ class Settings: def save_faves(self, data = None): with open(self._FAVORITES_FILE, 'w') as f: - json.dump(data, f) + json.dump(data, f, separators=(',', ':'), indent=4) f.close()