plutopluto

git clone https://git.ce9e.org/plutopluto.git

commit
b49d7bf2e0f99279e8fc8ee947b7ea178daaf82e
parent
98487b874964f2123b09979a9b5cfa0a40ed4f87
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2023-12-21 09:44
change config format

Diffstat

M example.cfg 15 +++++----------
M plutopluto/__init__.py 42 +++++++++++++++++++++++++++++-------------

2 files changed, 34 insertions, 23 deletions


diff --git a/example.cfg b/example.cfg

@@ -1,10 +1,5 @@
    1    -1 # See http://flask.pocoo.org/docs/0.10/config/#builtin-configuration-values
    2    -1 
    3    -1 # List of feed URLs.
    4    -1 # A URL may contain a {page} placeholder. It is replaced with increasing
    5    -1 # numbers starting with 0.
    6    -1 URLS = [
    7    -1     'https://xkcd.com/atom.xml',
    8    -1     'https://what-if.xkcd.com/feed.atom',
    9    -1     'https://staff.tumblr.com/page/{page}/rss',
   10    -1 ]
   -1     1 # A list of feed or activity pub URLs
   -1     2 https://xkcd.com/atom.xml
   -1     3 https://what-if.xkcd.com/feed.atom
   -1     4 https://staff.tumblr.com/page/{page}/rss
   -1     5 https://lemmy.world/c/world/outbox?page=true

diff --git a/plutopluto/__init__.py b/plutopluto/__init__.py

@@ -2,8 +2,8 @@
    2     2 
    3     3 import argparse
    4     4 import datetime
    5    -1 import os
    6     5 import sys
   -1     6 from pathlib import Path
    7     7 from time import mktime
    8     8 from time import time
    9     9 from xml.sax.saxutils import escape
@@ -159,28 +159,44 @@ def config():
  159   159     })
  160   160 
  161   161 
   -1   162 def parse_config(path):
   -1   163     urls = []
   -1   164     with open(path) as fh:
   -1   165         for line in fh:
   -1   166             url = line.strip()
   -1   167             if url and not url.startswith('#'):
   -1   168                 urls.append(url)
   -1   169     return urls
   -1   170 
   -1   171 
   -1   172 def get_config(args, name='.plutopluto.cfg'):
   -1   173     local_config = Path(name)
   -1   174     home_config = Path.home() / name
   -1   175 
   -1   176     if args.urls:
   -1   177         return args.urls
   -1   178     elif args.config:
   -1   179         return parse_config(args.config)
   -1   180     elif local_config.exists():
   -1   181         return parse_config(local_config)
   -1   182     elif home_config.exists():
   -1   183         return parse_config(home_config)
   -1   184     else:
   -1   185         return []
   -1   186 
   -1   187 
  162   188 def main():
  163   189     parser = argparse.ArgumentParser(description='simple feed aggregator')
  164   190     parser.add_argument('--version', '-V', action='version', version=__version__)
  165   191     parser.add_argument('-d', '--debug', action='store_true')
  166    -1     parser.add_argument('-c', '--config', metavar='FILE')
   -1   192     parser.add_argument('-c', '--config', metavar='FILE', type=Path)
  167   193     parser.add_argument('urls', metavar='URL', nargs='*',
  168   194         help='full feed url, optionally with a {page} placeholder')
  169   195     parser.add_argument('--port', type=int, default=8000)
  170   196     args = parser.parse_args()
  171   197 
  172    -1     config_name = '.plutopluto.cfg'
  173    -1     local_config = os.path.abspath(config_name)
  174    -1     home_config = os.path.expanduser('~/' + config_name)
  175    -1 
  176    -1     if args.config:
  177    -1         app.config.from_pyfile(os.path.abspath(args.config))
  178    -1     elif os.path.exists(local_config):
  179    -1         app.config.from_pyfile(local_config)
  180    -1     elif os.path.exists(home_config):
  181    -1         app.config.from_pyfile(home_config)
  182   198     app.debug = args.debug
  183    -1     app.config['URLS'] = args.urls + app.config.get('URLS', [])
   -1   199     app.config['URLS'] = get_config(args)
  184   200 
  185   201     if not app.config['URLS']:
  186   202         print("Error: No urls provided")