2017-04-09 08:30:48 +00:00
# -*- coding: utf-8 -*-
# Copyright © 2015-2017 Carl Chenet <carl.chenet@ohmytux.com>
# 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 <http://www.gnu.org/licenses/
# Get values of the configuration file
''' Get values of the configuration file '''
# standard library imports
2017-07-31 13:58:30 +00:00
from configparser import SafeConfigParser
2017-04-09 08:30:48 +00:00
import logging
import os
import os . path
import sys
2017-07-13 18:19:04 +00:00
import re
2017-04-09 08:30:48 +00:00
# 3rd party library imports
import feedparser
2017-08-01 16:30:24 +00:00
# feed2toot library imports
2017-08-01 21:15:50 +00:00
from feed2toot . confparsers . cache import parsecache
2017-08-01 16:30:24 +00:00
from feed2toot . confparsers . hashtaglist import parsehashtaglist
2017-08-01 21:15:50 +00:00
from feed2toot . confparsers . feedparser import parsefeedparser
2017-08-01 16:30:24 +00:00
from feed2toot . confparsers . plugins import parseplugins
2017-08-01 21:15:50 +00:00
from feed2toot . confparsers . rss . pattern import parsepattern
from feed2toot . confparsers . rss . toot import parsetoot
from feed2toot . confparsers . rss . urilist import parseurilist
2017-08-01 16:30:24 +00:00
2017-07-31 13:58:30 +00:00
class ConfParse :
2017-04-09 08:30:48 +00:00
''' ConfParse class '''
def __init__ ( self , clioptions ) :
''' Constructor of the ConfParse class '''
self . clioptions = clioptions
self . tweetformat = ' '
self . stringsep = ' , '
self . confs = [ ]
self . main ( )
def main ( self ) :
''' Main of the ConfParse class '''
for pathtoconfig in self . clioptions . configs :
options = { }
# read the configuration file
config = SafeConfigParser ( )
if not config . read ( os . path . expanduser ( pathtoconfig ) ) :
sys . exit ( ' Could not read config file ' )
2017-08-01 21:15:50 +00:00
####################
# feedparser section
####################
accept_bozo_exceptions = parsefeedparser ( config )
2017-04-09 08:30:48 +00:00
###########################
# the rss section
###########################
2017-08-01 21:15:50 +00:00
self . tweetformat = parsetoot ( config )
#################################################
# pattern and patter_case_sensitive format option
#################################################
options [ ' patterns ' ] , options [ ' patternscasesensitive ' ] = parsepattern ( config )
#################
# uri_list option
#################
feeds = [ ]
feeds = parseurilist ( config , accept_bozo_exceptions )
2017-04-09 08:30:48 +00:00
section = ' rss '
if config . has_section ( section ) :
2017-08-01 16:30:24 +00:00
############
# uri option
############
2017-04-09 08:30:48 +00:00
if not feeds and not self . clioptions . rss_uri :
confoption = ' uri '
if config . has_option ( section , confoption ) :
2017-08-01 14:55:26 +00:00
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 ' )
2017-04-09 08:30:48 +00:00
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 )
###########################
# the cache section
###########################
2017-08-01 21:15:50 +00:00
options [ ' cachefile ' ] , options [ ' cache_limit ' ] = parsecache ( self . clioptions . cachefile , config )
2017-04-09 08:30:48 +00:00
###########################
# the hashtag section
###########################
2017-08-01 16:30:24 +00:00
options [ ' hashtaglist ' ] = parsehashtaglist ( self . clioptions . hashtaglist , config )
2017-04-09 08:30:48 +00:00
###########################
# the plugins section
###########################
2017-08-01 16:30:24 +00:00
plugins = parseplugins ( config )
########################################
# return the final configurations values
########################################
2017-04-09 08:30:48 +00:00
if feeds :
self . confs . append ( ( options , config , self . tweetformat , feeds , plugins ) )
else :
2017-08-01 14:55:26 +00:00
self . confs . append ( ( options , config , self . tweetformat , [ { ' feed ' : feed , ' patterns ' : [ ] , ' rssobject ' : ' ' , ' feedname ' : feedname } ] , plugins ) )
2017-07-13 18:19:04 +00:00
2017-04-09 08:30:48 +00:00
@property
def confvalues ( self ) :
''' Return the values of the different configuration files '''
return self . confs