aboutsummaryrefslogblamecommitdiffstats
path: root/xpybar/config/mynews.py
blob: a65396ef34dc8690c1810e89fa783df100e9fa7b (plain) (tree)












































                                                                                                    
# -*- python -*-
import os
from common import *

class MyNews(Entry):
    def __init__(self, *args, status_path = None, **kwargs):
        if status_path is not None:
            self.status_path = status_path
        else:
            self.status_path = HOME + '/.var/lib/featherweight/status'
        self.get_news()
        Entry.__init__(self, *args, **kwargs)
        xasync(self.refresh, name = 'news')
    
    def action(self, col, button, x, y):
        if button == LEFT_BUTTON:
            xasync(lambda : subprocess.Popen([G.TERMINAL, '-e', 'featherweight']).wait())
        elif button == RIGHT_BUTTON:
            xasync(lambda : subprocess.Popen(['featherweight', '--update', '--system']).wait())
    
    def get_news(self):
        try:
            with open(self.status_path, 'rb') as file:
                status = int(file.read().decode('utf-8', 'replace').replace('\n', ''))
        except:
            status = 0
        colour = '31'
        if   status <=  0:  colour = '39'
        elif status <=  5:  colour = '32'
        elif status <= 10:  colour = '33'
        self.text = 'News: \033[%sm%i\033[0m' % (colour, status)
    
    def refresh(self):
        while True:
            if os.path.exists(self.status_path):
                try:
                    spawn_read(*pdeath('HUP', 'inotifywait', self.status_path, '-e', 'close_write'))
                    self.get_news()
                except:
                    time.sleep(1)
            else:
                    time.sleep(1)
    
    def function(self):
        return self.text