Refactored execute and collection logic.
This commit is contained in:
parent
a4c13bfc53
commit
9837af7a0c
|
@ -1,5 +1,5 @@
|
||||||
# Python imports
|
# Python imports
|
||||||
import threading, subprocess
|
import threading, subprocess, traceback
|
||||||
from os.path import isfile
|
from os.path import isfile
|
||||||
from os import listdir
|
from os import listdir
|
||||||
|
|
||||||
|
@ -46,7 +46,7 @@ class Controller(ProcessorMixin, Menu, Controller_Data):
|
||||||
fixed_programs_list.append({'name': program})
|
fixed_programs_list.append({'name': program})
|
||||||
|
|
||||||
self.favorites = self.call_method("set_favorites_menu", [fixed_programs_list])["set_faves"]
|
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
|
continue
|
||||||
if "[ Exit ]" in group:
|
if "[ Exit ]" in group:
|
||||||
break
|
break
|
||||||
|
@ -55,47 +55,38 @@ class Controller(ProcessorMixin, Menu, Controller_Data):
|
||||||
programs_list += self.get_sub_group(group, query)
|
programs_list += self.get_sub_group(group, query)
|
||||||
entry = self.call_method("sub_menu", [group, programs_list])["prog"]
|
entry = self.call_method("sub_menu", [group, programs_list])["prog"]
|
||||||
|
|
||||||
self.logger.debug(entry)
|
|
||||||
if entry not in base_options:
|
if entry not in base_options:
|
||||||
self.logger.info(f"[Executing Program] Group: {group} Entry: {entry}")
|
self.execute_program(self.flat_menu_data, entry)
|
||||||
self.execute_program(group, entry)
|
|
||||||
except Exception as e:
|
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):
|
def get_desktop_files_info(self, paths):
|
||||||
menu_objects = {
|
menu_objects = {
|
||||||
"Accessories": [],
|
"Accessories": {},
|
||||||
"Multimedia": [],
|
"Multimedia": {},
|
||||||
"Graphics": [],
|
"Graphics": {},
|
||||||
"Game": [],
|
"Game": {},
|
||||||
"Office": [],
|
"Office": {},
|
||||||
"Development": [],
|
"Development": {},
|
||||||
"Internet": [],
|
"Internet": {},
|
||||||
"Settings": [],
|
"Settings": {},
|
||||||
"System": [],
|
"System": {},
|
||||||
"Wine": [],
|
"Wine": {},
|
||||||
"Other": []
|
"Other": {}
|
||||||
}
|
}
|
||||||
|
|
||||||
for path in paths:
|
for path in paths:
|
||||||
if not "/opt/" in path:
|
|
||||||
self.list_and_update_desktop_iles(path, menu_objects);
|
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)
|
|
||||||
|
|
||||||
return menu_objects
|
return menu_objects
|
||||||
|
|
||||||
def list_and_update_desktop_iles(self, path, menu_objects):
|
def list_and_update_desktop_iles(self, path, menu_objects):
|
||||||
try:
|
try:
|
||||||
for f in listdir(path):
|
for f in listdir(path):
|
||||||
full_path = f"{path}{f}"
|
full_path = f"{path}/{f}"
|
||||||
if isfile(full_path) and f.endswith(".desktop"):
|
if isfile(full_path) and f.endswith(".desktop"):
|
||||||
xdg_object = DesktopEntry(full_path)
|
xdg_object = DesktopEntry(full_path)
|
||||||
hidden = xdg_object.getHidden()
|
hidden = xdg_object.getHidden()
|
||||||
|
@ -110,8 +101,8 @@ class Controller(ProcessorMixin, Menu, Controller_Data):
|
||||||
title = xdg_object.getName()
|
title = xdg_object.getName()
|
||||||
comment = xdg_object.getComment()
|
comment = xdg_object.getComment()
|
||||||
# icon = xdg_object.getIcon()
|
# icon = xdg_object.getIcon()
|
||||||
mainExec = xdg_object.getExec()
|
main_exec = xdg_object.getExec()
|
||||||
tryExec = xdg_object.getTryExec()
|
try_exec = xdg_object.getTryExec()
|
||||||
|
|
||||||
group = ""
|
group = ""
|
||||||
if "Accessories" in groups or "Utility" in groups:
|
if "Accessories" in groups or "Utility" in groups:
|
||||||
|
@ -137,10 +128,14 @@ class Controller(ProcessorMixin, Menu, Controller_Data):
|
||||||
else:
|
else:
|
||||||
group = "Other"
|
group = "Other"
|
||||||
|
|
||||||
menu_objects[group].append( {"title": title, "groups": groups,
|
chunk_data = {
|
||||||
"comment": comment, "exec": mainExec,
|
"groups": groups, "comment": comment,
|
||||||
"tryExec": tryExec, "fileName": f
|
"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:
|
except Exception as e:
|
||||||
self.logger.debug(e)
|
self.logger.debug(e)
|
||||||
|
|
||||||
|
@ -148,24 +143,24 @@ class Controller(ProcessorMixin, Menu, Controller_Data):
|
||||||
def get_sub_group(self, group, query = ""):
|
def get_sub_group(self, group, query = ""):
|
||||||
desktop_objects = []
|
desktop_objects = []
|
||||||
if "Search..." in group:
|
if "Search..." in group:
|
||||||
group_keys = self.menu_data.keys()
|
for key in self.flat_menu_data.keys():
|
||||||
for group_key in group_keys:
|
option = self.flat_menu_data[key]
|
||||||
for option in self.menu_data[group_key]:
|
|
||||||
keys = option.keys()
|
keys = option.keys()
|
||||||
|
|
||||||
if "comment" in keys and len(option["comment"]) > 0:
|
if "comment" in keys and len(option["comment"]) > 0:
|
||||||
if query.lower() in option["comment"].lower():
|
if query.lower() in option["comment"].lower():
|
||||||
desktop_objects.append( option["title"] + " || " + option["comment"] )
|
desktop_objects.append( f"{key} || {option['comment']}" )
|
||||||
if query.lower() in option["title"].lower() or query.lower() in option["fileName"].lower():
|
elif query.lower() in key.lower() or query.lower() in option["fileName"].lower():
|
||||||
desktop_objects.append( option["title"] + " || " + option["fileName"].replace(".desktop", "") )
|
desktop_objects.append( f"{key} || {option['fileName'].replace('.desktop', '')}" )
|
||||||
elif "Favorites" in group:
|
elif "Favorites" in group:
|
||||||
desktop_objects = self.favorites
|
desktop_objects = self.favorites
|
||||||
else:
|
else:
|
||||||
for option in self.menu_data[group]:
|
for option in self.menu_data[group]:
|
||||||
keys = option.keys()
|
keys = option.keys()
|
||||||
if "comment" in keys and len(option["comment"]) > 0 :
|
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:
|
else:
|
||||||
desktop_objects.append( option["title"] + " || " + option["fileName"].replace(".desktop", "") )
|
desktop_objects.append( f"{option['title']} || {option['fileName'].replace('.desktop', '')}" )
|
||||||
|
|
||||||
return desktop_objects
|
return desktop_objects
|
||||||
|
|
||||||
|
|
|
@ -26,5 +26,7 @@ class Controller_Data:
|
||||||
self.app_paths = self.settings.get_app_paths()
|
self.app_paths = self.settings.get_app_paths()
|
||||||
self.favorites_path = self.settings.get_favorites_path()
|
self.favorites_path = self.settings.get_favorites_path()
|
||||||
self.favorites = self.settings.get_favorites()
|
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)
|
GLib.unix_signal_add(GLib.PRIORITY_DEFAULT, signal.SIGINT, self.tear_down)
|
||||||
|
|
|
@ -33,7 +33,6 @@ class Menu(StylesMixin):
|
||||||
"""
|
"""
|
||||||
self.logger = settings.get_logger()
|
self.logger = settings.get_logger()
|
||||||
self.theme = self.call_method(args.theme)
|
self.theme = self.call_method(args.theme)
|
||||||
self.menu_data = None
|
|
||||||
|
|
||||||
|
|
||||||
def main_menu(self, _group_list = None):
|
def main_menu(self, _group_list = None):
|
||||||
|
@ -58,7 +57,7 @@ class Menu(StylesMixin):
|
||||||
'type': 'checkbox',
|
'type': 'checkbox',
|
||||||
'qmark': '>',
|
'qmark': '>',
|
||||||
'message': 'Select Favorites',
|
'message': 'Select Favorites',
|
||||||
'name': 'setFaves',
|
'name': 'set_faves',
|
||||||
'choices': group_list
|
'choices': group_list
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,29 +8,22 @@ import os, subprocess
|
||||||
|
|
||||||
|
|
||||||
class ProcessorMixin:
|
class ProcessorMixin:
|
||||||
def execute_program(self, group, entry):
|
def execute_program(self, data, entry):
|
||||||
parts = entry.split("||")
|
parts = entry.split("||")
|
||||||
program = parts[0].strip()
|
title = parts[0].strip()
|
||||||
comment = parts[1].strip()
|
comment = parts[1].strip()
|
||||||
|
chunk_data = data[title.strip()]
|
||||||
|
|
||||||
if group in ["Search...", "Favorites"]:
|
self.logger.info(f"[Executing Program]\n\t\tEntry: {entry}\n\t\tChunk Data: {chunk_data}")
|
||||||
group_keys = self.menu_data.keys()
|
self.pre_execute(chunk_data)
|
||||||
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)
|
|
||||||
|
|
||||||
|
def pre_execute(self, option):
|
||||||
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:
|
try:
|
||||||
self.execute(opt["tryExec"])
|
self.execute(option["tryExec"])
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
self.logger.info(f"[Executing Program]\n\t\t Try exec failed!\n{e}")
|
||||||
try:
|
try:
|
||||||
if "exec" in keys and len(option["exec"]):
|
if option["exec"] and len(option["exec"]) > 0:
|
||||||
self.execute(option["exec"])
|
self.execute(option["exec"])
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.debug(e)
|
self.logger.debug(e)
|
||||||
|
|
|
@ -14,8 +14,8 @@ class Settings:
|
||||||
self._USER_HOME = os.path.expanduser('~')
|
self._USER_HOME = os.path.expanduser('~')
|
||||||
self._CONFIG_PATH = f"{self._USER_HOME}/.config/{app_name.lower()}"
|
self._CONFIG_PATH = f"{self._USER_HOME}/.config/{app_name.lower()}"
|
||||||
self._FAVORITES_FILE = f"{self._CONFIG_PATH}/favorites.json"
|
self._FAVORITES_FILE = f"{self._CONFIG_PATH}/favorites.json"
|
||||||
self._HOME_APPS = f"{self._USER_HOME}/.local/share/applications/"
|
self._HOME_APPS = f"{self._USER_HOME}/.local/share/applications"
|
||||||
self._APP_PATHS = ["/opt/", "/usr/share/applications/", self._HOME_APPS]
|
self._APP_PATHS = ["/usr/share/applications", self._HOME_APPS]
|
||||||
|
|
||||||
self._logger = Logger(self._CONFIG_PATH).get_logger()
|
self._logger = Logger(self._CONFIG_PATH).get_logger()
|
||||||
self._faves = []
|
self._faves = []
|
||||||
|
@ -40,7 +40,7 @@ class Settings:
|
||||||
|
|
||||||
def save_faves(self, data = None):
|
def save_faves(self, data = None):
|
||||||
with open(self._FAVORITES_FILE, 'w') as f:
|
with open(self._FAVORITES_FILE, 'w') as f:
|
||||||
json.dump(data, f)
|
json.dump(data, f, separators=(',', ':'), indent=4)
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue