systrayUpdater/systrayupdater

104 lines
2.7 KiB
Plaintext
Raw Normal View History

2021-09-02 08:38:26 -05:00
#!/usr/bin/env python
# Get needed modules
import yaml
2023-12-07 18:57:10 -06:00
import webbrowser
2021-09-02 08:38:26 -05:00
import subprocess
2021-09-04 16:24:48 -05:00
from os import path
2023-12-07 18:57:10 -06:00
from time import sleep
from functools import partial
2021-09-02 08:38:26 -05:00
from PyQt5.QtCore import QTimer
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
2023-12-07 18:57:10 -06:00
# Available Updates holders
2021-09-02 08:38:26 -05:00
avail = ""
2023-12-07 18:57:10 -06:00
outputList = ""
2021-09-02 08:38:26 -05:00
# Use config.yml file to allow for compatibility with
2021-09-04 17:00:54 -05:00
# most terminal emulators possible & custom icon / timer duration
2023-12-08 22:30:03 -06:00
if path.exists(path.expanduser("~/.config/systrayupdater/config.yml")):
config = path.expanduser("~/.config/systrayupdater/config.yml")
else:
config = '/etc/systrayupdater/config.yml'
with open(config) as f:
2021-09-02 08:38:26 -05:00
conf = yaml.load(f, yaml.FullLoader)
term = str(conf['terminal'])
opt = str(conf['option'])
2021-09-04 16:24:48 -05:00
wait = int(conf['timer']) * 60000
2023-12-08 22:30:03 -06:00
icn = path.join('/usr/share/icons/hicolor/symbolic/apps/', str(conf['icon']))
2021-09-02 08:38:26 -05:00
f.close()
# Run checkupdates command
2023-12-07 18:57:10 -06:00
# Populates the available updates and count
# Sets the list and count in proper spots
2021-09-02 08:38:26 -05:00
def count():
cmd = ['checkupdates']
p1 = subprocess.Popen(cmd, stdout=subprocess.PIPE)
2023-12-07 18:57:10 -06:00
global outputList
outputList = ((p1.communicate()[0]).decode()).rstrip('\n')
outputCount = len(outputList.splitlines())
if outputCount == 0:
tray.setVisible(False)
else:
2023-12-07 20:01:50 -06:00
menu.clear()
menu.addAction(readNews)
2023-12-07 20:50:40 -06:00
menu.addAction(runUpdate)
2023-12-07 20:01:50 -06:00
menu.addAction(quit)
2023-12-07 18:57:10 -06:00
for i in outputList.splitlines():
menu.addAction(i, partial(infos, i))
global avail
avail = str(outputCount)+" Updates"
tray.setToolTip(avail)
2023-12-07 20:49:55 -06:00
tray.setVisible(True)
2021-09-02 08:38:26 -05:00
2023-12-07 18:57:10 -06:00
# Read the News
# Get pkg info
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)
2021-09-02 08:38:26 -05:00
2023-12-07 18:57:10 -06:00
# Run system updatehttps://archlinux.org/packages/?q='+ pkg[0]
def update():
cmd = [ term, opt, 'sudo', 'pacman', '-Syu' ]
subprocess.Popen(cmd)
2023-12-07 20:28:00 -06:00
count()
2021-09-02 08:38:26 -05:00
2023-12-07 18:57:10 -06:00
# Create an app surface
2021-09-02 08:38:26 -05:00
app = QApplication([])
app.setQuitOnLastWindowClosed(False)
2023-12-07 20:45:12 -06:00
# Set up timer
2023-12-07 18:57:10 -06:00
updateTimer = QTimer()
updateTimer.timeout.connect(count)
updateTimer.singleShot(1,count) # Initially populate available updates
updateTimer.start(wait)
2021-09-02 08:38:26 -05:00
2023-12-07 20:45:12 -06:00
# Adding items to the tray
2021-09-02 08:38:26 -05:00
tray = QSystemTrayIcon()
2023-12-07 18:57:10 -06:00
icon = QIcon(icn)
2021-09-02 08:38:26 -05:00
tray.setIcon(icon)
tray.setToolTip(avail)
tray.setVisible(True)
2023-12-07 20:45:12 -06:00
# Creating the menu/options
2021-09-02 08:38:26 -05:00
menu = QMenu()
2023-12-07 20:01:50 -06:00
runUpdate = QAction("Run Update")
readNews = QAction("Read the News")
2023-12-07 20:45:12 -06:00
quit = QAction("Quit")
2023-12-07 20:01:50 -06:00
runUpdate.triggered.connect(update)
readNews.triggered.connect(news)
2021-09-02 08:38:26 -05:00
quit.triggered.connect(app.quit)
2023-12-07 20:45:12 -06:00
# Place the menu in the System Tray
2021-09-02 08:38:26 -05:00
tray.setContextMenu(menu)
app.exec_()