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

70 lines
2.1 KiB
Python
Raw Normal View History

2013-11-23 17:24:58 +00:00
#!/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}
2013-11-23 19:58:27 +00:00
unknown: {unknown}\
"""
2013-11-23 17:24:58 +00:00
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)
2013-11-23 17:24:58 +00:00
self.at_list = Plasma.Label(self.applet)
self.layout.addItem(self.at_list)
self.layout.addItem(hs_button)
self.applet.setLayout(self.layout)
2013-11-23 19:28:30 +00:00
self.connect_to_engine()
2013-11-23 17:24:58 +00:00
self.resize(125, 125)
2013-11-23 17:24:58 +00:00
def get_user_list(self):
try:
api_content = urlopen("https://at.hackerspace.pl/api").read()
except (URLError, HTTPError):
pass # TODO:
json_dict = json.loads(api_content)
users = json_dict["users"]
unknown = json_dict["unknown"]
return (x["login"] for x in users), unknown
2013-11-23 17:24:58 +00:00
2013-11-23 19:28:30 +00:00
def connect_to_engine(self):
2013-11-23 17:24:58 +00:00
self.timeEngine = self.dataEngine("time")
2013-11-23 19:28:30 +00:00
self.timeEngine.connectSource("Local",
2013-11-23 17:24:58 +00:00
self, INTERVAL*6000, Plasma.AlignToMinute)
def update_list(self):
users, unknown = self.get_user_list()
users = "\n".join(users)
self.at_list.setText(LABEL_TEMPLATE.format(users=users, unknown=unknown))
2013-11-23 17:24:58 +00:00
@pyqtSignature("dataUpdated(const QString &, const Plasma::DataEngine::Data &)")
def dataUpdated(self, sourceName, data):
self.update_list()
2013-11-23 19:28:30 +00:00
2013-11-23 17:24:58 +00:00
def CreateApplet(parent):
return HelloPython(parent)