Reenable args with values

master
pictuga 2014-06-28 00:58:37 +02:00
parent 38b90e0e4c
commit 230659a34b
2 changed files with 41 additions and 13 deletions

View File

@ -30,7 +30,7 @@ GPL3 code.
##Arguments ##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: 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 ####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` For example: `http://morss.example/:clip/https://twitter.com/pictuga`
*(Brackets indicate optional text)* *(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 ###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` For example: `python2.7 morss.py debug http://feeds.bbci.co.uk/news/rss.xml`
*(Brackets indicate optional text)* *(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 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` For example: `python2.7 PATH/TO/MORSS/morss.py http://feeds.bbci.co.uk/news/rss.xml`
*(Brackets indicate optional text)* *(Brackets indicate optional text)*
@ -111,7 +111,7 @@ Using cache and passing arguments:
>>> import morss >>> import morss
>>> url = 'http://feeds.bbci.co.uk/news/rss.xml' >>> url = 'http://feeds.bbci.co.uk/news/rss.xml'
>>> cache = '/tmp/morss-cache' # cache folder, needs write permission >>> 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 = morss.process(url, cache, options)
>>> xml_string[:50] >>> xml_string[:50]
'{"title": "BBC News - Home", "desc": "The latest s' '{"title": "BBC News - Home", "desc": "The latest s'
@ -124,7 +124,7 @@ Doing it step-by-step:
import morss import morss
url = 'http://newspaper.example/feed.xml' 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 cache_path = '/tmp/morss-cache' # cache folder, needs write permission
url, cache = morss.Init(url, cache_path, options) # properly create folders and objects url, cache = morss.Init(url, cache_path, options) # properly create folders and objects

View File

@ -88,11 +88,18 @@ def count_words(txt):
class Options: class Options:
def __init__(self, options=None): def __init__(self, options=None, **args):
self.options = options or [] if len(args):
self.options = args
self.options.update(options or {})
else:
self.options = options or {}
def __getattr__(self, key): 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): def __setitem__(self, key, value):
self.options[key] = value self.options[key] = value
@ -101,6 +108,23 @@ class Options:
return key in self.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: class Cache:
""" Light, error-prone caching system. """ """ Light, error-prone caching system. """
@ -704,13 +728,17 @@ def cgi_app(environ, start_response):
url = re.sub(r'^/?morss.py/', '', url) url = re.sub(r'^/?morss.py/', '', url)
if url.startswith(':'): if url.startswith(':'):
options = url.split('/')[0].split(':')[1:] split = url.split('/', 1)
url = url.split('/', 1)[1] options = split[0].split(':')[1:]
if len(split) > 1:
url = split[1]
else:
url = ''
else: else:
options = [] options = []
# init # init
options = Options(options) options = Options(parseOptions(options))
headers = {} headers = {}
global DEBUG global DEBUG
@ -804,7 +832,7 @@ def cgi_wrapper(environ, start_response):
def cli_app(): def cli_app():
options = Options(sys.argv[1:-1]) options = Options(parseOptions(sys.argv[1:-1]))
url = sys.argv[-1] url = sys.argv[-1]
global DEBUG global DEBUG