Updated to be Python3 Based

This commit is contained in:
2020-04-18 14:46:19 -05:00
parent faf287f57b
commit dfe7b2bb63
18 changed files with 938 additions and 20 deletions

203
src/__init__.py Normal file
View File

@@ -0,0 +1,203 @@
# Python imports
import subprocess
import os
from os.path import isdir, isfile, join
from os import listdir
# Gtk imports
from xdg.DesktopEntry import DesktopEntry
# Application imports
from core import Context
class Main(Context):
"""
This is the start class called from "__main__"
"""
def __init__(self, args):
"""
Initialize it all...
"""
super().__init__(args)
while True:
self.clear()
if not self.menuData:
path = "/usr/share/applications/"
self.menuData = self.getDesktopFilesInfo(path)
group = self.call_method("mainMenu")["group"]
query = ""
if "[ Search ]" in group:
query = self.call_method("searchMenu")["query"]
if "[ Exit ]" in group:
break
self.clear()
progsList = ["[ TO MAIN MENU ]"]
progsList += self.getSubgroup(group, query)
entry = self.call_method("subMenu", [group, progsList])["prog"]
if not "[ TO MAIN MENU ]" is entry:
self.executeProgram(group, entry)
def call_method(self, method_name, data = None):
mName = str(method_name)
method = getattr(self, mName, lambda data: "No valid key passed...\nkey= " + mName + "\nargs= " + data)
return method(data) if data else method()
def getDesktopFilesInfo(self, path):
menuObjs = {
"Accessories": [],
"Multimedia": [],
"Graphics": [],
"Game": [],
"Office": [],
"Development": [],
"Internet": [],
"Settings": [],
"System": [],
"Wine": [],
"Other": []
}
for f in listdir(path):
fPath = path + f
flags = ["mimeinfo.cache", "defaults.list"]
if not f in flags and isfile(fPath):
xdgObj = DesktopEntry(fPath)
title = xdgObj.getName()
groups = xdgObj.getCategories()
comment = xdgObj.getComment()
# icon = xdgObj.getIcon()
mainExec = xdgObj.getExec()
tryExec = xdgObj.getTryExec()
group = ""
if "Accessories" in groups or "Utility" in groups:
group = "Accessories"
elif "Multimedia" in groups or "Video" in groups or "Audio" in groups:
group = "Multimedia"
elif "Development" in groups:
group = "Development"
elif "Game" in groups:
group = "Game"
elif "Internet" in groups or "Network" in groups:
group = "Internet"
elif "Graphics" in groups:
group = "Graphics"
elif "Office" in groups:
group = "Office"
elif "System" in groups:
group = "System"
elif "Settings" in groups:
group = "Settings"
elif "Wine" in groups:
group = "Wine"
else:
group = "Other"
menuObjs[group].append( {"title": title, "groups": groups,
"comment": comment, "exec": mainExec,
"tryExec": tryExec, "fileName": f} )
return menuObjs
def getSubgroup(self, group, query = ""):
"""
Need to refactor and pull out the sub logic that is used in both cases...
"""
desktopObjs = []
if "[ Search ]" in group:
gkeys = self.menuData.keys()
for gkey in gkeys:
for opt in self.menuData[gkey]:
keys = opt.keys()
if "comment" in keys and len(opt["comment"]) > 0 :
if query.lower() in opt["comment"].lower():
desktopObjs.append( opt["title"] + " || " + opt["comment"] )
continue
if query.lower() in opt["title"].lower() or \
query.lower() in opt["fileName"].lower():
desktopObjs.append( opt["title"] + " || " + opt["fileName"].replace(".desktop", "") )
else:
for opt in self.menuData[group]:
keys = opt.keys()
if "comment" in keys and len(opt["comment"]) > 0 :
desktopObjs.append( opt["title"] + " || " + opt["comment"] )
else:
desktopObjs.append( opt["title"] + " || " + opt["fileName"].replace(".desktop", "") )
return desktopObjs
def executeProgram(self, group, entry):
"""
Need to refactor and pull out the sub loop that is used in both cases...
"""
parts = entry.split("||")
program = parts[0].strip()
comment = parts[1].strip()
if "[ Search ]" in group:
gkeys = self.menuData.keys()
for gkey in gkeys:
for opt in self.menuData[gkey]:
if program in opt["title"]:
keys = opt.keys()
if comment in opt["comment"] or comment in opt["fileName"]:
DEVNULL = open(os.devnull, 'w')
execFailed = False
try:
command = opt["tryExec"].split("%")[0]
self.logger.debug(command)
subprocess.Popen(command.split(), start_new_session=True, stdout=DEVNULL, stderr=DEVNULL)
break
except Exception as e:
execFailed = True
if execFailed:
try:
if "exec" in keys and len(opt["exec"]):
command = opt["exec"].split("%")[0]
self.logger.debug(command)
subprocess.Popen(command.split(), start_new_session=True, stdout=DEVNULL, stderr=DEVNULL)
break
except Exception as e:
self.logger.debug(e)
else:
for opt in self.menuData[group]:
if program in opt["title"]:
keys = opt.keys()
if comment in opt["comment"] or comment in opt["fileName"]:
DEVNULL = open(os.devnull, 'w')
execFailed = False
try:
command = opt["tryExec"].split("%")[0]
self.logger.debug(command)
subprocess.Popen(command.split(), start_new_session=True, stdout=DEVNULL, stderr=DEVNULL)
except Exception as e:
execFailed = True
if execFailed:
try:
if "exec" in keys and len(opt["exec"]):
command = opt["exec"].split("%")[0]
self.logger.debug(command)
subprocess.Popen(command.split(), start_new_session=True, stdout=DEVNULL, stderr=DEVNULL)
except Exception as e:
self.logger.debug(e)
def clear(self):
os.system('cls' if os.name == 'nt' else 'clear')

19
src/__main__.py Normal file
View File

@@ -0,0 +1,19 @@
# Python imports
import argparse
# Application imports
from __init__ import Main
if __name__ == "__main__":
try:
parser = argparse.ArgumentParser()
# Add long and short arguments
parser.add_argument("--theme", "-t", default="default", help="The theme to use for the menu. (default, orange, red, purple, green)")
# Read arguments (If any...)
args = parser.parse_args()
main = Main(args)
except Exception as e:
print( repr(e) )

75
src/core/Context.py Normal file
View File

@@ -0,0 +1,75 @@
# Python imports
from __future__ import print_function, unicode_literals
# from pprint import pprint
import json
# Lib imports
from PyInquirer import style_from_dict, Token, prompt, Separator
# Application imports
from .utils import Logger
from .mixins import StylesMixin
GROUPS = ["[ Search ]", "Accessories", "Multimedia", "Graphics", "Game",
"Office", "Development", "Internet", "Settings",
"System", "Wine", "Other", "[ Exit ]"]
class Context(StylesMixin):
"""
The menu class has sub methods that are called per run.
"""
def __init__(self, args):
"""
Construct a new 'Menu' object which pulls in mixins.
:param args: The terminal passed arguments
:return: returns nothing
"""
self.logger = Logger().get_logger("MAIN")
# Set the theme
self.theme = self.call_method(args.theme)
self.menuData = None
def mainMenu(self, _grouplist = None):
"""
Displays the main menu using the defined GROUPS list...
"""
grouplist = GROUPS if not _grouplist else _grouplist
menu = {
'type': 'list',
'name': 'group',
'message': '[ MAIN MENU ]',
'choices': grouplist
}
return prompt(menu, style=self.theme)
def subMenu(self, data = ["NO GROUP NAME", "NO PROGRAMS PASSED IN"]):
group = data[0]
progList = data[1]
menu = {
'type': 'list',
'name': 'prog',
'message': '[ ' + group + ' ]',
'choices': progList
}
return prompt(menu, style=self.theme)
def searchMenu(self):
menu = {
'type': 'input',
'name': 'query',
'message': 'Program you\'re looking for: ',
}
return prompt(menu, style=self.theme)

8
src/core/__init__.py Normal file
View File

@@ -0,0 +1,8 @@
"""
Core module
"""
from .mixins import StylesMixin
from .utils import Logger
from .Context import Context

View File

@@ -0,0 +1,70 @@
# Python imports
# Lib imports
from PyInquirer import style_from_dict, Token
# Application imports
class StylesMixin:
"""
The StylesMixin has style methods that get called and
return their respective objects.
"""
def default(self):
return style_from_dict({
Token.Separator: '#6C6C6C',
Token.QuestionMark: '#FF9D00 bold',
# Token.Selected: '', # default
Token.Selected: '#5F819D',
Token.Pointer: '#FF9D00 bold',
Token.Instruction: '', # default
Token.Answer: '#5F819D bold',
Token.Question: '',
})
def orange(self):
return style_from_dict({
Token.Pointer: '#6C6C6C bold',
Token.QuestionMark: '#FF9D00 bold',
Token.Separator: '#FF9D00',
Token.Selected: '#FF9D00',
Token.Instruction: '', # default
Token.Answer: '#FF9D00 bold',
Token.Question: '', # default
})
def red(self):
return style_from_dict({
Token.Pointer: '#c70e0e bold',
Token.QuestionMark: '#c70e0e bold',
Token.Separator: '#c70e0e',
Token.Selected: '#c70e0e',
Token.Instruction: '', # default
Token.Answer: '#c70e0e bold',
Token.Question: '', # default
})
def purple(self):
return style_from_dict({
Token.Pointer: '#673ab7 bold',
Token.QuestionMark: '#673ab7 bold',
Token.Selected: '#673ab7',
Token.Separator: '#673ab7',
Token.Instruction: '', # default
Token.Answer: '#673ab7 bold',
Token.Question: '', # default
})
def green(self):
return style_from_dict({
Token.Pointer: '#ffde00 bold',
Token.QuestionMark: '#29a116 bold',
Token.Selected: '#29a116',
Token.Separator: '#29a116',
Token.Instruction: '', # default
Token.Answer: '#29a116 bold',
Token.Question: '', # default
})

View File

@@ -0,0 +1,4 @@
"""
Mixins module
"""
from .StylesMixin import StylesMixin

55
src/core/utils/Logger.py Normal file
View File

@@ -0,0 +1,55 @@
# Python imports
import os, logging
# Application imports
class Logger:
def __init__(self):
pass
def get_logger(self, loggerName = "NO_LOGGER_NAME_PASSED", createFile = True):
"""
Create a new logging object and return it.
:note:
NOSET # Don't know the actual log level of this... (defaulting or literally none?)
Log Levels (From least to most)
Type Value
CRITICAL 50
ERROR 40
WARNING 30
INFO 20
DEBUG 10
:param loggerName: Sets the name of the logger object. (Used in log lines)
:param createFile: Whether we create a log file or just pump to terminal
:return: the logging object we created
"""
globalLogLvl = logging.DEBUG # Keep this at highest so that handlers can filter to their desired levels
chLogLevel = logging.CRITICAL # Prety musch the only one we change ever
fhLogLevel = logging.DEBUG
log = logging.getLogger(loggerName)
# Set our log output styles
fFormatter = logging.Formatter('[%(asctime)s] %(pathname)s:%(lineno)d %(levelname)s - %(message)s', '%m-%d %H:%M:%S')
cFormatter = logging.Formatter('%(pathname)s:%(lineno)d] %(levelname)s - %(message)s')
ch = logging.StreamHandler()
ch.setLevel(level=chLogLevel)
ch.setFormatter(cFormatter)
log.addHandler(ch)
if createFile:
folder = "core/logs"
file = folder + "/shellmen.log"
if not os.path.exists(folder):
os.mkdir(folder)
fh = logging.FileHandler(file)
fh.setLevel(level=fhLogLevel)
fh.setFormatter(fFormatter)
log.addHandler(fh)
return log

View File

@@ -0,0 +1,5 @@
"""
Utils module
"""
from .Logger import Logger