From 230659a34b29ff2cd997994349e9683646082f92 Mon Sep 17 00:00:00 2001 From: pictuga Date: Sat, 28 Jun 2014 00:58:37 +0200 Subject: [PATCH] Reenable args with values --- README.md | 12 ++++++------ morss/morss.py | 42 +++++++++++++++++++++++++++++++++++------- 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index e48f46a..b6b3ca3 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ GPL3 code. ##Arguments -morss accepts some arguments, to lightly alter the output of morss. Arguments are all boolean. In the different "Use cases" below is detailed how to pass those arguments to morss. +morss accepts some arguments, to lightly alter the output of morss. Arguments may need to have a value (usually a string or a number). In the different "Use cases" below is detailed how to pass those arguments to morss. The arguments are: @@ -74,7 +74,7 @@ Morss can run its own HTTP server. The later should start when you run morss wit ####Passing arguments -Then visit: **`http://PATH/TO/MORSS/[morss.py/][:argwithoutvalue[...]]/FEEDURL`** +Then visit: **`http://PATH/TO/MORSS/[morss.py/][:argwithoutvalue[:argwithvalue=value[...]]]/FEEDURL`** For example: `http://morss.example/:clip/https://twitter.com/pictuga` *(Brackets indicate optional text)* @@ -84,7 +84,7 @@ Works like a charm with [Tiny Tiny RSS](http://tt-rss.org/redmine/projects/tt-rs ###As a CLI application -Run: **`[python2.7] morss.py [argwithoutvalue] [...] FEEDURL`** +Run: **`[python2.7] morss.py [argwithoutvalue] [argwithvalue=value] [...] FEEDURL`** For example: `python2.7 morss.py debug http://feeds.bbci.co.uk/news/rss.xml` *(Brackets indicate optional text)* @@ -92,7 +92,7 @@ For example: `python2.7 morss.py debug http://feeds.bbci.co.uk/news/rss.xml` To use it, the newsreader [Liferea](http://lzone.de/liferea/) is required (unless other newsreaders provide the same kind of feature), since custom scripts can be run on top of the RSS feed, using its [output](http://lzone.de/liferea/scraping.htm) as an RSS feed. -To use this script, you have to enable "(Unix) command" in liferea feed settings, and use the command: **`[python2.7] PATH/TO/MORSS/morss.py [argwithoutvalue] [...] FEEDURL`** +To use this script, you have to enable "(Unix) command" in liferea feed settings, and use the command: **`[python2.7] PATH/TO/MORSS/morss.py [argwithoutvalue] [argwithvalue=value] [...] FEEDURL`** For example: `python2.7 PATH/TO/MORSS/morss.py http://feeds.bbci.co.uk/news/rss.xml` *(Brackets indicate optional text)* @@ -111,7 +111,7 @@ Using cache and passing arguments: >>> import morss >>> url = 'http://feeds.bbci.co.uk/news/rss.xml' >>> cache = '/tmp/morss-cache' # cache folder, needs write permission ->>> options = ['csv', 'md'] # simple list() +>>> options = {'csv':True, 'md':True} >>> xml_string = morss.process(url, cache, options) >>> xml_string[:50] '{"title": "BBC News - Home", "desc": "The latest s' @@ -124,7 +124,7 @@ Doing it step-by-step: import morss url = 'http://newspaper.example/feed.xml' -options = morss.Options(['force', 'quiet']) # arguments +options = morss.Options(csv=True, md=True) # arguments cache_path = '/tmp/morss-cache' # cache folder, needs write permission url, cache = morss.Init(url, cache_path, options) # properly create folders and objects diff --git a/morss/morss.py b/morss/morss.py index b8e0544..cc0b6ca 100644 --- a/morss/morss.py +++ b/morss/morss.py @@ -88,11 +88,18 @@ def count_words(txt): class Options: - def __init__(self, options=None): - self.options = options or [] + def __init__(self, options=None, **args): + if len(args): + self.options = args + self.options.update(options or {}) + else: + self.options = options or {} def __getattr__(self, key): - return key in self.options + if key in self.options: + return self.options[key] + else: + return False def __setitem__(self, key, value): self.options[key] = value @@ -101,6 +108,23 @@ class Options: return key in self.options +def parseOptions(options): + """ Turns ['md=True'] into {'md':True} """ + out = {} + for option in options: + split = option.split('=', 1) + if len(split) > 1: + if split[0].lower() == 'true': + out[split[0]] = True + elif split[0].lower() == 'false': + out[split[0]] = False + else: + out[split[0]] = split[1] + else: + out[split[0]] = True + return out + + class Cache: """ Light, error-prone caching system. """ @@ -704,13 +728,17 @@ def cgi_app(environ, start_response): url = re.sub(r'^/?morss.py/', '', url) if url.startswith(':'): - options = url.split('/')[0].split(':')[1:] - url = url.split('/', 1)[1] + split = url.split('/', 1) + options = split[0].split(':')[1:] + if len(split) > 1: + url = split[1] + else: + url = '' else: options = [] # init - options = Options(options) + options = Options(parseOptions(options)) headers = {} global DEBUG @@ -804,7 +832,7 @@ def cgi_wrapper(environ, start_response): def cli_app(): - options = Options(sys.argv[1:-1]) + options = Options(parseOptions(sys.argv[1:-1])) url = sys.argv[-1] global DEBUG