From: W. Trevor King Date: Thu, 18 Oct 2012 16:56:58 +0000 (-0400) Subject: rss2email: add ability to load feeds from the config file only. X-Git-Tag: v3.0~72^2~6 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=03d4851725f6ac5247a357dbe0f6eecf2558752b;p=rss2email.git rss2email: add ability to load feeds from the config file only. If a feed exists in the config file, but not in the data file, the previous implementation would not load it. This patch initializes such feeds using only the information from the config file (i.e. without dynamic data from the data file). The patch also creates missing data files on demand. As an example use case, if you keep a backup of your config file, but lose the data file, you can restore the config file and an `r2e run` will create and repopulate your data file. --- diff --git a/rss2email.py b/rss2email.py index 2ddabfc..86b16e4 100755 --- a/rss2email.py +++ b/rss2email.py @@ -1320,19 +1320,24 @@ class Feeds (list): while self: self.pop(0) - def load(self, lock=True): + def load(self, lock=True, require=False): LOG.debug('load feed configuration from {}'.format(self.configfiles)) if self.configfiles: self.read_configfiles = self.config.read(self.configfiles) else: self.read_configfiles = [] LOG.debug('loaded confguration from {}'.format(self.read_configfiles)) - self._load_feeds(lock=lock) + self._load_feeds(lock=lock, require=require) - def _load_feeds(self, lock): + def _load_feeds(self, lock, require): LOG.debug('load feed data from {}'.format(self.datafile)) if not _os.path.exists(self.datafile): - raise NoDataFile(feeds=self) + if require: + raise NoDataFile(feeds=self) + LOG.info('feed data file not found at {}'.format(self.datafile)) + LOG.debug('creating an empty data file') + with open(self.datafile, 'wb') as f: + _pickle.dump([], f) try: self._datafile_lock = open(self.datafile, 'rb') except IOError as e: @@ -1359,6 +1364,14 @@ class Feeds (list): for feed in self: feed.load_from_config(self.config) + for section in self.config.sections(): + if section.startswith('feed.'): + name = section[len('feed.'):] + LOG.debug( + ('feed {} not found feed file, initializing from config' + ).format(name)) + self.append(Feed(name=name, config=self.config)) + def save(self): LOG.debug('save feed configuration to {}'.format(self.configfiles[-1])) for feed in self: