From: Moritz Venn Date: Mon, 9 Nov 2009 17:02:52 +0000 (+0000) Subject: generic cleanup: X-Git-Url: http://code.vuplus.com/gitweb/?p=vuplus_dvbapp-plugin;a=commitdiff_plain;h=851449eb9b255eff2d72b6297d818f72e2e2aaab generic cleanup: - cleanup imports, - use statictext instead of label/button (this changes the skins, sorry! ;-)), - some micro-optimization --- diff --git a/weatherplugin/src/plugin.py b/weatherplugin/src/plugin.py index d6370af..ec17f26 100644 --- a/weatherplugin/src/plugin.py +++ b/weatherplugin/src/plugin.py @@ -21,22 +21,16 @@ from Plugins.Plugin import PluginDescriptor from Screens.Screen import Screen from Components.ActionMap import ActionMap -from Components.Label import Label -from Components.Button import Button -from Tools.LoadPixmap import LoadPixmap -import xml.etree.cElementTree -from twisted.internet import reactor, defer -from twisted.web import client -import urllib +from Components.Sources.StaticText import StaticText +from xml.etree.cElementTree import fromstring as cet_fromstring +from twisted.internet import defer +from twisted.web.client import getPage, downloadPage +from urllib import quote from Components.Pixmap import Pixmap from enigma import ePicLoad -import string -import os -from enigma import getDesktop -from Components.ConfigList import ConfigList, ConfigListScreen +from os import path as os_path, mkdir as os_mkdir from Components.AVSwitch import AVSwitch -from Components.ConfigList import ConfigList, ConfigListScreen -from Components.config import ConfigSubsection, ConfigSubList, ConfigText, ConfigInteger, config +from Components.config import ConfigSubsection, ConfigSubList, ConfigInteger, config from setup import initConfig, WeatherPluginEntriesListConfigScreen config.plugins.WeatherPlugin = ConfigSubsection() @@ -54,10 +48,10 @@ class WeatherIconItem: self.error = error def getXML(url): - return client.getPage(url, agent=UserAgent) + return getPage(url, agent=UserAgent) def download(item): - return client.downloadPage(item.url, file(item.filename, 'wb'), agent=UserAgent) + return downloadPage(item.url, file(item.filename, 'wb'), agent=UserAgent) def main(session,**kwargs): @@ -72,28 +66,27 @@ class WeatherPlugin(Screen): skin = """ - - - - - - + + + + + + - - + + - - + + - - + + - - + + """ def __init__(self, session): - self.session = session Screen.__init__(self, session) self["actions"] = ActionMap(["WizardActions", "DirectionActions", "ColorActions", "EPGSelectActions"], { @@ -103,22 +96,24 @@ class WeatherPlugin(Screen): "left": self.previousItem }, -1) - self["statustext"] = Label() + self["statustext"] = StaticText() + self["caption"] = StaticText() + self["currentTemp"] = StaticText() + self["condition"] = StaticText() + self["wind_condition"] = StaticText() + self["humidity"] = StaticText() - self["caption"] = Label() - self["currentTemp"] = Label() - self["condition"] = Label() - self["wind_condition"] = Label() - self["humidity"] = Label() - - for i in range(1, 5): - self["weekday%s" % i] = Label() + i = 1 + while i < 5: + self["weekday%s" % i] = StaticText() self["weekday%s_icon" %i] = WeatherIcon() - self["weekday%s_temp" % i] = Label() + self["weekday%s_temp" % i] = StaticText() + i += 1 + del i self.appdir = "/usr/lib/enigma2/python/Plugins/Extensions/WeatherPlugin/icons/" - if not os.path.exists(self.appdir): - os.mkdir(self.appdir) + if not os_path.exists(self.appdir): + os_mkdir(self.appdir) self.weatherPluginEntryIndex = -1 self.weatherPluginEntryCount = config.plugins.WeatherPlugin.entriescount.value @@ -132,12 +127,11 @@ class WeatherPlugin(Screen): def startRun(self): if self.weatherPluginEntry is not None: - self["statustext"].setText(_("Getting weather information...")) - url = ("http://www.google.com/ig/api?weather=%s&hl=%s" % (urllib.quote(self.weatherPluginEntry.city.value), self.weatherPluginEntry.language.value)) + self["statustext"].text = _("Getting weather information...") + url = ("http://www.google.com/ig/api?weather=%s&hl=%s" % (quote(self.weatherPluginEntry.city.value), self.weatherPluginEntry.language.value)) getXML(url).addCallback(self.xmlCallback).addErrback(self.error) else: - self["statustext"].setText(_("No locations defined...\nPress 'Menu' to do that.")) - self["statustext"].show() + self["statustext"].text = _("No locations defined...\nPress 'Menu' to do that.") def nextItem(self): if self.weatherPluginEntryCount != 0: @@ -161,15 +155,17 @@ class WeatherPlugin(Screen): self.startRun() def clearFields(self): - self["caption"].setText("") - self["currentTemp"].setText("") - self["condition"].setText("") - self["wind_condition"].setText("") - self["humidity"].setText("") - for i in range(1, 5): - self["weekday%s" % i].setText("") + self["caption"].text = "" + self["currentTemp"].text = "" + self["condition"].text = "" + self["wind_condition"].text = "" + self["humidity"].text = "" + i = 1 + while i < 5: + self["weekday%s" % i].text = "" self["weekday%s_icon" %i].hide() - self["weekday%s_temp" % i].setText("") + self["weekday%s_temp" % i].text = "" + i += 1 def errorIconDownload(self, error = None, item = None): item.error = True @@ -183,21 +179,20 @@ class WeatherPlugin(Screen): self["weekday%s_icon" % index].show() def xmlCallback(self, xmlstring): - self["statustext"].hide() + self["statustext"].text = "" metric = 0 index = 0 UnitSystemText = "F" IconDownloadList = [] - root = xml.etree.cElementTree.fromstring(xmlstring) + root = cet_fromstring(xmlstring) for childs in root.findall("weather"): for items in childs: if items.tag == "problem_cause": - self["statustext"].setText(items.attrib.get("data").encode("utf-8", 'ignore')) - self["statustext"].show() - if items.tag == "forecast_information": + self["statustext"].text = items.attrib.get("data").encode("utf-8", 'ignore') + elif items.tag == "forecast_information": for items2 in items: if items2.tag == "city": - self["caption"].setText(items2.attrib.get("data").encode("utf-8", 'ignore')) + self["caption"].text = items2.attrib.get("data").encode("utf-8", 'ignore') elif items2.tag == "unit_system": if items2.attrib.get("data").encode("utf-8", 'ignore') == "SI": metric = 1 @@ -205,15 +200,15 @@ class WeatherPlugin(Screen): elif items.tag == "current_conditions": for items2 in items: if items2.tag == "condition": - self["condition"].setText(_("Current: %s") % items2.attrib.get("data").encode("utf-8", 'ignore')) + self["condition"].text = _("Current: %s") % items2.attrib.get("data").encode("utf-8", 'ignore') elif items2.tag == "temp_f" and metric == 0: - self["currentTemp"].setText( ("%s °F" % items2.attrib.get("data").encode("utf-8", 'ignore')) ) + self["currentTemp"].text = ("%s °F" % items2.attrib.get("data").encode("utf-8", 'ignore')) elif items2.tag == "temp_c" and metric == 1: - self["currentTemp"].setText( ("%s °C" % items2.attrib.get("data").encode("utf-8", 'ignore')) ) + self["currentTemp"].text = ("%s °C" % items2.attrib.get("data").encode("utf-8", 'ignore')) elif items2.tag == "humidity": - self["humidity"].setText(items2.attrib.get("data").encode("utf-8", 'ignore')) + self["humidity"].text = items2.attrib.get("data").encode("utf-8", 'ignore') elif items2.tag == "wind_condition": - self["wind_condition"].setText(items2.attrib.get("data").encode("utf-8", 'ignore')) + self["wind_condition"].text = items2.attrib.get("data").encode("utf-8", 'ignore') elif items.tag == "forecast_conditions": index = index + 1 lowTemp = "" @@ -221,17 +216,17 @@ class WeatherPlugin(Screen): icon = "" for items2 in items: if items2.tag == "day_of_week": - self["weekday%s" % index].setText(items2.attrib.get("data").encode("utf-8", 'ignore')) - if items2.tag == "low": + self["weekday%s" % index].text = items2.attrib.get("data").encode("utf-8", 'ignore') + elif items2.tag == "low": lowTemp = items2.attrib.get("data").encode("utf-8", 'ignore') - if items2.tag == "high": + elif items2.tag == "high": highTemp = items2.attrib.get("data").encode("utf-8", 'ignore') - self["weekday%s_temp" % index].setText("%s °%s | %s °%s" % (highTemp, UnitSystemText, lowTemp, UnitSystemText)) - if items2.tag == "icon": + self["weekday%s_temp" % index].text = "%s °%s | %s °%s" % (highTemp, UnitSystemText, lowTemp, UnitSystemText) + elif items2.tag == "icon": url = "http://www.google.com%s" % items2.attrib.get("data").encode("utf-8", 'ignore') - parts = string.split(url,"/") + parts = url.split("/") filename = self.appdir + parts[-1] - if not os.path.exists(filename): + if not os_path.exists(filename): IconDownloadList.append(WeatherIconItem(url = url,filename = filename, index = index)) else: self.showIcon(index,filename) @@ -262,10 +257,9 @@ class WeatherPlugin(Screen): def error(self, error = None): if error is not None: self.clearFields() - self["statustext"].setText(str(error.getErrorMessage())) - self["statustext"].show() - - + self["statustext"].text = str(error.getErrorMessage()) + + class WeatherIcon(Pixmap): def __init__(self): Pixmap.__init__(self) diff --git a/weatherplugin/src/setup.py b/weatherplugin/src/setup.py index d204be7..65f6006 100644 --- a/weatherplugin/src/setup.py +++ b/weatherplugin/src/setup.py @@ -17,24 +17,24 @@ # GNU General Public License for more details. # -from enigma import eListboxPythonMultiContent, eListbox, gFont, \ - RT_HALIGN_LEFT, RT_VALIGN_CENTER +from enigma import eListboxPythonMultiContent, gFont, RT_HALIGN_LEFT, \ + RT_VALIGN_CENTER from Screens.Screen import Screen from Screens.MessageBox import MessageBox from Components.MenuList import MenuList -from Components.Button import Button -from Components.config import config -from Components.ActionMap import ActionMap, NumberActionMap +from Components.Sources.StaticText import StaticText +from Components.ActionMap import ActionMap from Components.ConfigList import ConfigList, ConfigListScreen -from Components.config import ConfigSubsection, ConfigSubList, ConfigIP, ConfigInteger, ConfigSelection, ConfigText, ConfigYesNo, getConfigListEntry, configfile +from Components.config import ConfigSubsection, ConfigText, \ + getConfigListEntry, config, configfile def initWeatherPluginEntryConfig(): - config.plugins.WeatherPlugin.Entries.append(ConfigSubsection()) - i = len(config.plugins.WeatherPlugin.Entries) -1 - config.plugins.WeatherPlugin.Entries[i].city = ConfigText(default = "Heidelberg", visible_width = 50, fixed_size = False) - config.plugins.WeatherPlugin.Entries[i].language = ConfigText(default = "de", visible_width = 50, fixed_size = False) - return config.plugins.WeatherPlugin.Entries[i] + s = ConfigSubsection() + s.city = ConfigText(default = "Heidelberg", visible_width = 50, fixed_size = False) + s.language = ConfigText(default = "de", visible_width = 50, fixed_size = False) + config.plugins.WeatherPlugin.Entries.append(s) + return s def initConfig(): count = config.plugins.WeatherPlugin.entriescount.value @@ -47,25 +47,25 @@ def initConfig(): class WeatherPluginEntriesListConfigScreen(Screen): skin = """ - - + + - - - - - - + + + + + + """ % _("WeatherPlugin: List of Entries") def __init__(self, session): Screen.__init__(self, session) - self.session = session - self["city"] = Button(_("City")) - self["language"] = Button(_("Language")) - self["key_red"] = Button(_("Add")) - self["key_yellow"] = Button(_("Edit")) - self["key_blue"] = Button(_("Delete")) + + self["city"] = StaticText(_("City")) + self["language"] = StaticText(_("Language")) + self["key_red"] = StaticText(_("Add")) + self["key_yellow"] = StaticText(_("Edit")) + self["key_blue"] = StaticText(_("Delete")) self["entrylist"] = WeatherPluginEntryList([]) self["actions"] = ActionMap(["WizardActions","MenuActions","ShortcutActions"], { @@ -109,7 +109,7 @@ class WeatherPluginEntriesListConfigScreen(Screen): if not result: return sel = self["entrylist"].l.getCurrentSelection()[0] - config.plugins.WeatherPlugin.entriescount.value = config.plugins.WeatherPlugin.entriescount.value - 1 + config.plugins.WeatherPlugin.entriescount.value -= 1 config.plugins.WeatherPlugin.entriescount.save() config.plugins.WeatherPlugin.Entries.remove(sel) config.plugins.WeatherPlugin.Entries.save() @@ -122,6 +122,7 @@ class WeatherPluginEntryList(MenuList): MenuList.__init__(self, list, enableWrapAround, eListboxPythonMultiContent) self.l.setFont(0, gFont("Regular", 20)) self.l.setFont(1, gFont("Regular", 18)) + def postWidgetCreate(self, instance): MenuList.postWidgetCreate(self, instance) instance.setItemHeight(20) @@ -130,31 +131,34 @@ class WeatherPluginEntryList(MenuList): return self.instance.getCurrentIndex() def buildList(self): - self.list=[] + list = [] for c in config.plugins.WeatherPlugin.Entries: - res = [c] - res.append((eListboxPythonMultiContent.TYPE_TEXT, 5, 0, 150, 20, 1, RT_HALIGN_LEFT|RT_VALIGN_CENTER, str(c.city.value))) - res.append((eListboxPythonMultiContent.TYPE_TEXT, 155, 0, 150, 20, 1, RT_HALIGN_LEFT|RT_VALIGN_CENTER, str(c.language.value))) - self.list.append(res) - self.l.setList(self.list) + res = [ + c, + (eListboxPythonMultiContent.TYPE_TEXT, 5, 0, 150, 20, 1, RT_HALIGN_LEFT|RT_VALIGN_CENTER, str(c.city.value)), + (eListboxPythonMultiContent.TYPE_TEXT, 155, 0, 150, 20, 1, RT_HALIGN_LEFT|RT_VALIGN_CENTER, str(c.language.value)), + ] + list.append(res) + self.list = list + self.l.setList(list) self.moveToIndex(0) class WeatherPluginEntryConfigScreen(ConfigListScreen, Screen): skin = """ - - - + + + - - - - """ % _("WeatherPlugin: Edit Entry") + + + + """ def __init__(self, session, entry): - self.session = session Screen.__init__(self, session) + self.title = _("WeatherPlugin: Edit Entry") self["actions"] = ActionMap(["SetupActions", "ColorActions"], { @@ -164,9 +168,9 @@ class WeatherPluginEntryConfigScreen(ConfigListScreen, Screen): "cancel": self.keyCancel }, -2) - self["key_red"] = Button(_("Cancel")) - self["key_green"] = Button(_("OK")) - self["key_blue"] = Button(_("Delete")) + self["key_red"] = StaticText(_("Cancel")) + self["key_green"] = StaticText(_("OK")) + self["key_blue"] = StaticText(_("Delete")) if entry is None: self.newmode = 1