Support subfeeds.conf and default subfeed

This commit is contained in:
Tim Van Baak 2020-12-30 13:59:45 -08:00
parent ec2382e1bd
commit a678b67019
3 changed files with 62 additions and 12 deletions

View File

@ -7,7 +7,14 @@ import traceback
from flask import Flask, render_template, request, jsonify, abort, redirect, url_for
# Application imports
from inquisitor.configs import logger, DUNGEON_PATH, CACHE_PATH, subfeeds, init_default_logging
from inquisitor.configs import (
DUNGEON_PATH,
SOURCES_PATH,
CACHE_PATH,
subfeeds,
get_subfeed_overrides,
logger,
init_default_logging)
from inquisitor import sources, loader, timestamp
# Globals
@ -35,9 +42,22 @@ def feed():
@app.route("/feed/<string:feed_name>/")
def subfeed(feed_name):
if feed_name not in subfeeds:
# Check for and apply subfeed overrides
subfeed_overrides = get_subfeed_overrides()
subfeed_config = subfeed_overrides or subfeeds or {}
# The built-in inquisitor subfeed contains sources not in another subfeed
if feed_name == 'inquisitor':
all_sources = os.listdir(DUNGEON_PATH)
for subfeed, sources in subfeed_config.items():
for source_name in sources:
if source_name in all_sources:
all_sources.remove(source_name)
return feed_for_sources(all_sources)
if feed_name not in subfeed_config:
return abort(404)
return feed_for_sources(subfeeds[feed_name])
return feed_for_sources(subfeed_config[feed_name])
def feed_for_sources(source_names):
# Determine exclusion filters

View File

@ -1,4 +1,10 @@
from .resolver import data_path as DUNGEON_PATH
from .resolver import source_path as SOURCES_PATH
from .resolver import cache_path as CACHE_PATH
from .resolver import logger, add_logging_handler, init_default_logging, subfeeds
from .resolver import (
logger,
subfeeds)
from .resolver import (
add_logging_handler,
init_default_logging,
get_subfeed_overrides)

View File

@ -31,6 +31,7 @@ DEFAULT_VERBOSITY = 'false'
# sources within a subfeed separated by spaces
CONFIG_SUBFEEDS = 'Subfeeds'
DEFAULT_SUBFEEDS = None
SUBFEED_CONFIG_FILE = 'subfeeds.conf'
def read_config_file(config_path):
@ -67,6 +68,18 @@ def read_config_file(config_path):
return accumulated_configs
def parse_subfeed_value(value):
sf_defs = [sf.strip() for sf in value.split('\n') if sf.strip()]
subfeeds = {}
for sf_def in sf_defs:
if ':' not in sf_def:
raise ValueError(f'Invalid subfeed definition: {sf_def}')
sf_name, sf_sources = sf_def.split(':', maxsplit=1)
sf_sources = sf_sources.split()
subfeeds[sf_name.strip()] = [source.strip() for source in sf_sources]
return subfeeds
# Read envvar for config file location, with fallback to default
config_path = os.path.abspath(
os.environ.get(CONFIG_ENVVAR) or
@ -105,14 +118,25 @@ is_verbose = (is_verbose == 'true')
subfeeds = configs.get(CONFIG_SUBFEEDS) or DEFAULT_SUBFEEDS
if subfeeds:
sf_defs = [sf.strip() for sf in subfeeds.split('\n') if sf.strip()]
subfeeds = {}
for sf_def in sf_defs:
if ':' not in sf_def:
raise ValueError(f'Invalid subfeed definition: {sf_def}')
sf_name, sf_sources = sf_def.split(':', maxsplit=1)
sf_sources = sf_sources.split()
subfeeds[sf_name.strip()] = [source.strip() for source in sf_sources]
subfeeds = parse_subfeed_value(subfeeds)
def get_subfeed_overrides():
"""
Check for and parse the secondary subfeed configuration file
"""
path = os.path.join(source_path, SUBFEED_CONFIG_FILE)
if not os.path.isfile(path):
return None
overrides = read_config_file(path)
if CONFIG_SUBFEEDS not in overrides:
return None
value = overrides[CONFIG_SUBFEEDS]
if not value:
return None
parsed_value = parse_subfeed_value(value)
return parsed_value
# Set up logging
logger = logging.getLogger("inquisitor")