hs-applet/hs-applet/contents/code/main.py

70 lines
2.2 KiB
Python

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
from PyQt4.QtCore import Qt, pyqtSignature
from PyQt4 import QtCore
from PyQt4.QtGui import QGraphicsLinearLayout, QIcon
from PyKDE4.plasma import Plasma
from PyKDE4 import plasmascript
from urllib2 import urlopen, URLError, HTTPError
import json
INTERVAL = 15
LABEL_TEMPLATE = """\
{users}
unknown: {unknown}\
"""
class HelloPython(plasmascript.Applet):
def init(self):
self.setHasConfigurationInterface(False)
self.setAspectRatioMode(Plasma.IgnoreAspectRatio)
self.theme = Plasma.Svg(self)
self.theme.setImagePath("widgets/background")
self.setBackgroundHints(Plasma.Applet.DefaultBackground)
self.layout = QGraphicsLinearLayout(Qt.Horizontal, self.applet)
hs_button = Plasma.ToolButton();
hs_button.setIcon(QIcon(self.package().path() + "contents/images/syrenka-small.png"))
QtCore.QObject.connect(hs_button, QtCore.SIGNAL('clicked()'), self.update_list)
self.at_list = Plasma.Label(self.applet)
self.layout.addItem(self.at_list)
self.layout.addItem(hs_button)
self.applet.setLayout(self.layout)
self.connect_to_engine()
self.resize(125, 125)
def get_user_list(self):
api_content = urlopen("https://at.hackerspace.pl/api").read()
json_dict = json.loads(api_content)
users = json_dict["users"]
unknown = json_dict["unknown"]
return (x["login"] for x in users), unknown
def connect_to_engine(self):
self.timeEngine = self.dataEngine("time")
self.timeEngine.connectSource("Local",
self, INTERVAL*6000, Plasma.AlignToMinute)
def update_list(self):
try:
users, unknown = self.get_user_list()
users = "\n".join(users)
self.at_list.setText(LABEL_TEMPLATE.format(users=users, unknown=unknown))
except (URLError, HTTPError):
self.at_list.setText("hakerspejs umar")
@pyqtSignature("dataUpdated(const QString &, const Plasma::DataEngine::Data &)")
def dataUpdated(self, sourceName, data):
self.update_list()
def CreateApplet(parent):
return HelloPython(parent)