systrayUpdater/systray-updater

125 lines
3.8 KiB
Plaintext
Raw Normal View History

2023-12-08 22:46:36 -06:00
#!/usr/bin/env python
# Get needed modules
2023-12-16 08:09:35 -06:00
import sys
2023-12-08 22:46:36 -06:00
import yaml
import webbrowser
import subprocess
from os import path
from functools import partial
from PyQt5.QtCore import QTimer
2023-12-16 09:10:23 -06:00
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QAction, QApplication, QSystemTrayIcon, QMenu
2023-12-08 22:46:36 -06:00
# Use config.yml file to allow for compatibility with
2023-12-09 10:00:14 -06:00
# most terminal emulators possible, custom timer duration and icons
2023-12-08 22:46:36 -06:00
if path.exists(path.expanduser("~/.config/systrayupdater/config.yml")):
config = path.expanduser("~/.config/systrayupdater/config.yml")
else:
2023-12-09 10:00:14 -06:00
config = '/etc/systrayupdater/config.yml'
2023-12-08 22:46:36 -06:00
2023-12-09 10:00:14 -06:00
try:
f = open(config)
except FileNotFoundError:
2023-12-18 06:49:53 -06:00
print("Error: No config file found\n")
2023-12-16 08:09:35 -06:00
sys.exit(1)
2023-12-09 11:50:24 -06:00
conf = yaml.load(f,yaml.FullLoader)
2023-12-09 10:00:14 -06:00
term = str(conf['terminal'])
opt = str(conf['option'])
wait = int(conf['timer']) * 60000
icn = str(conf['icon'])
f.close()
2023-12-16 08:09:35 -06:00
iconDir = '/usr/share/icons/systrayupdater/'
2023-12-09 10:00:14 -06:00
match icn:
case "white":
2023-12-09 11:50:24 -06:00
icon = QIcon(path.join(iconDir,'arch-white-symbolic-24.svg'))
2023-12-09 10:00:14 -06:00
case "blue":
2023-12-09 11:50:24 -06:00
icon = QIcon(path.join(iconDir,'arch-blue-symbolic-24.svg'))
2023-12-09 10:00:14 -06:00
case "red":
2023-12-09 11:50:24 -06:00
icon = QIcon(path.join(iconDir,'arch-red-symbolic-24.svg'))
2023-12-09 10:00:14 -06:00
case "green":
2023-12-09 11:50:24 -06:00
icon = QIcon(path.join(iconDir,'arch-green-symbolic-24.svg'))
2023-12-09 10:00:14 -06:00
case "purple":
2023-12-09 11:50:24 -06:00
icon = QIcon(path.join(iconDir,'arch-purple-symbolic-24.svg'))
2023-12-09 10:00:14 -06:00
case "yellow":
2023-12-09 11:50:24 -06:00
icon = QIcon(path.join(iconDir,'arch-yellow-symbolic-24.svg'))
2023-12-09 10:00:14 -06:00
case "black":
2023-12-09 11:50:24 -06:00
icon = QIcon(path.join(iconDir,'arch-black-symbolic-24.svg'))
2023-12-09 10:00:14 -06:00
case "gray":
2023-12-09 11:50:24 -06:00
icon = QIcon(path.join(iconDir,'arch-gray-symbolic-24.svg'))
2023-12-09 10:00:14 -06:00
case _:
2023-12-09 11:50:24 -06:00
icon = QIcon(path.join(path.expanduser("~/.config/systrayupdater/"),icn))
2023-12-08 22:46:36 -06:00
# Run checkupdates command
# Populates the available updates and count
2023-12-09 10:00:14 -06:00
# Sets the menu options, list and count in proper spots
2023-12-08 22:46:36 -06:00
def count():
cmd = ['checkupdates']
2023-12-18 06:49:53 -06:00
try:
p1 = subprocess.Popen(cmd,stdout=subprocess.PIPE)
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))
avail = str(outputCount)+" Updates"
tray.setToolTip(avail)
tray.setVisible(True)
except subprocess.CalledProcessError:
2023-12-24 08:35:58 -06:00
tray.setVisible(False)
wait = 1800000
updateTimer.killTimer
updateTimer.start(wait)
print("Error: \'checkupdates\' command unable to retrieve updates\nSetting timer to 30mins\n")
2023-12-18 06:49:53 -06:00
2023-12-08 22:46:36 -06:00
# 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)
2023-12-09 10:00:14 -06:00
# Run system update
2023-12-08 22:46:36 -06:00
def update():
cmd = [ term, opt, 'sudo', 'pacman', '-Syu' ]
subprocess.Popen(cmd)
2023-12-24 08:35:58 -06:00
updateTimer.singleShot(1,count)
2023-12-08 22:46:36 -06:00
# 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)
# 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)
2023-12-09 11:50:24 -06:00
# Create applet and place menu in it
tray = QSystemTrayIcon()
tray.setIcon(icon)
2023-12-09 11:58:23 -06:00
tray.setVisible(True)
2023-12-08 22:46:36 -06:00
tray.setContextMenu(menu)
app.exec_()