From ad5f3ec9404079567206d65b6784efd8e87e8b2f Mon Sep 17 00:00:00 2001 From: Carl Chenet Date: Tue, 1 Aug 2017 23:34:11 +0200 Subject: [PATCH] conf parsing for uri option of the rss section --- feed2toot/confparse.py | 40 +++------------------ feed2toot/confparsers/rss/uri.py | 62 ++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 35 deletions(-) create mode 100644 feed2toot/confparsers/rss/uri.py diff --git a/feed2toot/confparse.py b/feed2toot/confparse.py index fb49ee2..c6b9e20 100644 --- a/feed2toot/confparse.py +++ b/feed2toot/confparse.py @@ -34,6 +34,7 @@ from feed2toot.confparsers.feedparser import parsefeedparser from feed2toot.confparsers.plugins import parseplugins from feed2toot.confparsers.rss.pattern import parsepattern from feed2toot.confparsers.rss.toot import parsetoot +from feed2toot.confparsers.rss.uri import parseuri from feed2toot.confparsers.rss.urilist import parseurilist class ConfParse: @@ -71,41 +72,10 @@ class ConfParse: ################# feeds = [] feeds = parseurilist(config, accept_bozo_exceptions) - section = 'rss' - if config.has_section(section): - ############ - # uri option - ############ - if not feeds and not self.clioptions.rss_uri: - confoption = 'uri' - if config.has_option(section, confoption): - urifeed = config.get('rss', 'uri') - feedname = None - if '<' in urifeed: - matches = re.match('(.*) <(.*)>', urifeed) - if not matches: - sys.exit('This uri to parse is not formatted correctly: {urifeed}'.format(urifeed)) - feedname, finaluri = matches.groups() - options['rss_uri'] = finaluri - else: - options['rss_uri'] = config.get('rss', 'uri') - else: - sys.exit('{confoption} parameter in the [{section}] section of the configuration file is mandatory. Exiting.'.format(section=section, confoption=confoption)) - else: - options['rss_uri'] = self.clioptions.rss_uri - # get the rss feed for rss parameter of [rss] section - feed = feedparser.parse(options['rss_uri']) - if not feed: - sys.exit('Unable to parse the feed at the following url: {rss}'.format(rss=rss)) - - ######################################### - # no_uri_pattern_no_global_pattern option - ######################################### - currentoption = 'no_uri_pattern_no_global_pattern' - # default value - options['nopatternurinoglobalpattern'] = False - if config.has_option(section, currentoption): - options['nopatternurinoglobalpattern'] = config.getboolean(section, currentoption) + ############ + # uri option + ############ + options['rss_uri'], feed, feedname, options['nopatternurinoglobalpattern'] = parseuri(config, self.clioptions.rss_uri, feeds) ########################### # the cache section ########################### diff --git a/feed2toot/confparsers/rss/uri.py b/feed2toot/confparsers/rss/uri.py new file mode 100644 index 0000000..d574849 --- /dev/null +++ b/feed2toot/confparsers/rss/uri.py @@ -0,0 +1,62 @@ +# -*- coding: utf-8 -*- +# Copyright © 2015-2017 Carl Chenet +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see ', urifeed) + if not matches: + sys.exit('This uri to parse is not formatted correctly: {urifeed}'.format(urifeed)) + feedname, finaluri = matches.groups() + rssuri = finaluri + else: + rssuri = config.get('rss', 'uri') + else: + sys.exit('{confoption} parameter in the [{section}] section of the configuration file is mandatory. Exiting.'.format(section=section, confoption=confoption)) + else: + rssuri = clioption + # get the rss feed for rss parameter of [rss] section + feed = feedparser.parse(rssuri) + if not feed: + sys.exit('Unable to parse the feed at the following url: {rss}'.format(rss=rss)) + ######################################### + # no_uri_pattern_no_global_pattern option + ######################################### + currentoption = 'no_uri_pattern_no_global_pattern' + # default value + if config.has_option(section, currentoption): + nopatternurinoglobalpattern = config.getboolean(section, currentoption) + return rssuri, feed, feedname, nopatternurinoglobalpattern