From 1a0b790119b9a003fa3769b2677bc329040dd337 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Thu, 29 Aug 2013 21:38:59 +0200 Subject: [PATCH 1/1] always load the local configuration file --- README.rst | 11 +++++++++++ src/AtomFeed.py | 28 +++++++++++++++++++++++++--- src/CurlyTx.py | 37 ++++++++++++++++++++++++++++++++++--- src/CurlyTxSettings.py | 23 +++++------------------ src/config.py | 20 ++++++++++++++++++++ 5 files changed, 95 insertions(+), 24 deletions(-) diff --git a/README.rst b/README.rst index ee22bcd..0726c32 100644 --- a/README.rst +++ b/README.rst @@ -56,6 +56,9 @@ up/down buttons and press "OK" - the page edit window will open. Press the green button and the settings will be saved. You're back on the main window now and the URL you just configured will be loaded. +Note that with version 1.1, CurlyTx supports opening local files. +Prepend them with ``file://``, e.g. ``file:///tmp/test.txt``. + Adding many pages ================= @@ -92,6 +95,14 @@ Start CurlyTx, go to the settings and write the feed URL in the Then press "OK" and the feed's pages will be loaded into the settings window. +Local configuration file +======================== +CurlyTx looks for a local page feed file on the harddisk on every run, +at ``/etc/enigma2/curlytx-pagefeed.xml``. +If this file exists, it gets loaded unconditionally and overwrites +the current page configuration. + + ================= Modifying CurlyTx ================= diff --git a/src/AtomFeed.py b/src/AtomFeed.py index 9e76e9f..51da7f8 100644 --- a/src/AtomFeed.py +++ b/src/AtomFeed.py @@ -4,21 +4,39 @@ # License: GPLv3 or later from twisted.web.client import getPage -from xml.etree.cElementTree import fromstring +from xml.etree.cElementTree import fromstring, ParseError + +import os class AtomFeed: """ Simple XML parser that extracts pages from a atom feed """ ns = "{http://www.w3.org/2005/Atom}" + errorCallback = None + def __init__(self, url, callback, errorCallback): """ Fetches the URL Parsed pages are sent back to callback by parse() """ - getPage(url).addCallback(self.parse, callback).addErrback(errorCallback) + self.errorCallback = errorCallback + if (url.startswith('file://')): + file = url[7:] + if not os.path.exists(file): + errorCallback('Settings atom feed file does not exist: ' + file) + return + + with open(file, 'r') as f: + self.parse(f.read(), callback) + else: + getPage(url).addCallback(self.parse, callback).addErrback(self.onError) def parse(self, data, callback): """ Parse atom feed data into pages list and run callback """ - xml = fromstring(data) + try: + xml = fromstring(data) + except ParseError: + return self.errorCallback("Invalid XML") + pages = [] for entry in xml.findall("{0}entry".format(self.ns)): titleE = entry.find("{0}title".format(self.ns)) @@ -54,3 +72,7 @@ class AtomFeed: elif type == "": return 2 return 1 + + def onError(self, error): + """ Pass the error message only """ + self.errorCallback(error.getErrorMessage()) diff --git a/src/CurlyTx.py b/src/CurlyTx.py index 8e18c0f..3572601 100644 --- a/src/CurlyTx.py +++ b/src/CurlyTx.py @@ -7,21 +7,22 @@ from . import _ from Screens.Screen import Screen from Screens.HelpMenu import HelpableScreen -from Screens.MessageBox import MessageBox from Components.Label import Label from Components.ScrollLabel import ScrollLabel from Components.ActionMap import ActionMap from Components.Sources.StaticText import StaticText +from Tools import Directories from twisted.web import client from twisted.web.client import _makeGetterFactory, HTTPClientFactory from enigma import gFont from . import config +from config import createPage, loadDefaultPageOptions, feedPagesToConfig, savePageConfig from Components.config import config import os -class CurlyTx(Screen,HelpableScreen): +class CurlyTx(Screen, HelpableScreen): skin = """ @@ -40,6 +41,7 @@ class CurlyTx(Screen,HelpableScreen): currentFontSize = 20 httpGetterFactory = None showingHeaders = False + staticPageFeedFile = 'curlytx-pagefeed.xml' def __init__(self, session, args = None): #self.skin = CurlyTx.skin @@ -72,10 +74,11 @@ class CurlyTx(Screen,HelpableScreen): self.loadHelp() self.loadButtons() - self.onLayoutFinish.append(self.afterLayout) + self.onFirstExecBegin.append(self.afterLayout) def afterLayout(self): self.setTextFont + self.loadStaticConfig() self.loadUrl(config.plugins.CurlyTx.defaultPage.value) def loadHelp(self): @@ -262,3 +265,31 @@ class CurlyTx(Screen,HelpableScreen): contextFactory=contextFactory, *args, **kwargs) return self.httpGetterFactory.deferred + + def loadStaticConfig(self): + """ + Always try to load the static config file from + /etc/enigma2/curlytx-pagefeed.xml + """ + staticFeedPath = Directories.resolveFilename(Directories.SCOPE_CONFIG, self.staticPageFeedFile) + if not os.path.exists(staticFeedPath): + return + + from AtomFeed import AtomFeed + AtomFeed( + 'file://' + staticFeedPath, + self.saveStaticConfig, self.loadStaticConfigFail + ) + + def loadStaticConfigFail(self, errorMessage): + """ Loading the page url feed failed somehow """ + from Screens.MessageBox import MessageBox + self.session.open( + MessageBox, + _("Error loading page feed:") + "\n\n" + str(errorMessage), + type = MessageBox.TYPE_ERROR + ) + + def saveStaticConfig(self, pages): + feedPagesToConfig(pages) + savePageConfig() diff --git a/src/CurlyTxSettings.py b/src/CurlyTxSettings.py index c04acb2..ba2281c 100644 --- a/src/CurlyTxSettings.py +++ b/src/CurlyTxSettings.py @@ -12,7 +12,7 @@ from Components.Sources.StaticText import StaticText from Screens.MessageBox import MessageBox from . import config -from config import createPage, loadDefaultPageOptions +from config import createPage, loadDefaultPageOptions, feedPagesToConfig, savePageConfig from Components.config import config, getConfigListEntry, ConfigSelection from Components.ConfigList import ConfigList, ConfigListScreen @@ -153,32 +153,19 @@ class CurlyTxSettings(ConfigListScreen, HelpableScreen, Screen): self["config"].setList(self.getConfigList()) def feedPagesReceived(self, pages): - if len(pages) == 0: - return - - del config.plugins.CurlyTx.pages[:] - - for pageData in pages: - page = createPage() - config.plugins.CurlyTx.pages.append(page) - page.title.setValue(pageData["title"]) - page.uri.setValue(pageData["url"]) - + feedPagesToConfig(pages) self["config"].setList(self.getConfigList()) - def feedPagesFail(self, failure): + def feedPagesFail(self, errorMessage): """ Downloading the page url feed failed somehow """ self.session.open( MessageBox, - _("Error loading page feed:") + "\n\n" + str(failure.getErrorMessage()), + _("Error loading page feed:") + "\n\n" + str(errorMessage), MessageBox.TYPE_ERROR ) def keySave(self): - for i in range(0, len(config.plugins.CurlyTx.pages)): - config.plugins.CurlyTx.pages[i].save() - - config.plugins.CurlyTx.pages.save() + savePageConfig() ConfigListScreen.keySave(self) def cancelConfirm(self, result): diff --git a/src/config.py b/src/config.py index e32e27b..7b11142 100644 --- a/src/config.py +++ b/src/config.py @@ -25,6 +25,26 @@ def loadDefaultPageOptions(): else: config.plugins.CurlyTx.defaultPage = ConfigSelection(defaults, "0") +def feedPagesToConfig(pages): + """ save pages from atom feed into config. """ + if len(pages) == 0: + return + + del config.plugins.CurlyTx.pages[:] + + for pageData in pages: + page = createPage() + config.plugins.CurlyTx.pages.append(page) + page.title.setValue(pageData["title"]) + page.uri.setValue(pageData["url"]) + +def savePageConfig(): + for i in range(0, len(config.plugins.CurlyTx.pages)): + config.plugins.CurlyTx.pages[i].save() + + config.plugins.CurlyTx.pages.save() + + #configuration setup config.plugins.CurlyTx = ConfigSubsection() config.plugins.CurlyTx.menuMain = ConfigYesNo(default = True) -- 2.30.2