systrayUpdater/systray-updater

128 lines
3.5 KiB
Python
Executable File

#!/usr/bin/env python
# Get needed modules
import yaml
import webbrowser
import subprocess
from os import path
from time import sleep
from functools import partial
from PyQt5.QtCore import QTimer
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
# Available Updates holders
avail = ""
outputList = ""
# Use config.yml file to allow for compatibility with
# most terminal emulators possible, custom timer duration and icons
if path.exists(path.expanduser("~/.config/systrayupdater/config.yml")):
config = path.expanduser("~/.config/systrayupdater/config.yml")
else:
config = '/etc/systrayupdater/config.yml'
try:
f = open(config)
except FileNotFoundError:
print("No config file found\n")
sys.exit()
conf = yaml.load(f, yaml.FullLoader)
term = str(conf['terminal'])
opt = str(conf['option'])
wait = int(conf['timer']) * 60000
icn = str(conf['icon'])
f.close()
iconDir = '/usr/share/icons/hicolor/symbolic/apps/'
match icn:
case "white":
iconFile = path.join(iconDir, 'arch-white-symbolic-24.svg')
case "blue":
iconFile = path.join(iconDir, 'arch-blue-symbolic-24.svg')
case "red":
iconFile = path.join(iconDir, 'arch-red-symbolic-24.svg')
case "green":
iconFile = path.join(iconDir, 'arch-green-symbolic-24.svg')
case "purple":
iconFile = path.join(iconDir, 'arch-purple-symbolic-24.svg')
case "yellow":
iconFile = path.join(iconDir, 'arch-yellow-symbolic-24.svg')
case "black":
iconFile = path.join(iconDir, 'arch-black-symbolic-24.svg')
case "gray":
iconFile = path.join(iconDir, 'arch-gray-symbolic-24.svg')
case _:
iconFile = path.join(path.expanduser("~/.config/systrayupdater/"), icn)
# Run checkupdates command
# Populates the available updates and count
# Sets the menu options, list and count in proper spots
def count():
cmd = ['checkupdates']
p1 = subprocess.Popen(cmd, stdout=subprocess.PIPE)
global outputList
outputList = ((p1.communicate()[0]).decode()).rstrip('\n')
outputCount = len(outputList.splitlines())
if outputCount == 0:
tray.setVisible(False)
else:
menu.clear()
menu.addAction(readNews)
menu.addAction(runUpdate)
menu.addAction(quit)
for i in outputList.splitlines():
menu.addAction(i, partial(infos, i))
global avail
avail = str(outputCount)+" Updates"
tray.setToolTip(avail)
tray.setVisible(True)
# Read the News
def news():
url = 'https://archlinux.org/news/'
webbrowser.open_new_tab(url)
# Get pkg info
def infos(pkgString):
pkg = pkgString.split(' ')
url = 'https://archlinux.org/packages/?q='+ pkg[0]
webbrowser.open_new_tab(url)
# Run system update
def update():
cmd = [ term, opt, 'sudo', 'pacman', '-Syu' ]
subprocess.Popen(cmd)
count()
# Create an app surface
app = QApplication([])
app.setQuitOnLastWindowClosed(False)
# Set up timer
updateTimer = QTimer()
updateTimer.timeout.connect(count)
updateTimer.singleShot(1,count) # Initially populate available updates
updateTimer.start(wait)
# Adding items to the tray
tray = QSystemTrayIcon()
icon = QIcon(iconFile)
tray.setIcon(icon)
tray.setToolTip(avail)
tray.setVisible(True)
# Creating the menu/options
menu = QMenu()
runUpdate = QAction("Run Update")
readNews = QAction("Read the News")
quit = QAction("Quit")
runUpdate.triggered.connect(update)
readNews.triggered.connect(news)
quit.triggered.connect(app.quit)
# Place the menu in the System Tray
tray.setContextMenu(menu)
app.exec_()