From 0501fdbe9049ba07f233059524c823a0865b0761 Mon Sep 17 00:00:00 2001 From: Alasdair Date: Thu, 5 Sep 2013 15:14:57 +0200 Subject: now supports stdin for arguments --- wikiquery | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/wikiquery b/wikiquery index 10356dd..7d1ed5f 100755 --- a/wikiquery +++ b/wikiquery @@ -1,12 +1,13 @@ #! /usr/bin/env python +import shlex import optparse import sys import wikipedia from functools import partial -version = "0.0.2" +version = "0.1.0" def stringify(o): if isinstance(o, list): @@ -22,13 +23,18 @@ def display(o): print stringify(o) def parse_args(): + args = sys.argv[1:] + + if not args: + args = sys.stdin.read().splitlines() + parser = optparse.OptionParser(usage="!wiki [--search|--url]") parser.add_option("--search", action="store_true") parser.add_option("--url", action="store_true") parser.add_option("-v", "--version", action="store_true") - return parser.parse_args() + return parser.parse_args(args) def main(): options, args = parse_args() -- cgit v1.2.3 From 751a4624ebe2e27d43f8e97454f64d6d6a0d6445 Mon Sep 17 00:00:00 2001 From: Alasdair Date: Thu, 5 Sep 2013 16:30:59 +0200 Subject: handling disambiguation error when using --url option and refactoring --- wikiquery | 68 +++++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 24 deletions(-) diff --git a/wikiquery b/wikiquery index 7d1ed5f..b05ccd9 100755 --- a/wikiquery +++ b/wikiquery @@ -7,7 +7,7 @@ import wikipedia from functools import partial -version = "0.1.0" +version = "0.1.1" def stringify(o): if isinstance(o, list): @@ -21,6 +21,45 @@ def stringify(o): def display(o): print stringify(o) + +def search(topic, index): + results = wikipedia.search(topic) + if len(results) == 1: + display(wikipedia.summary(results[0])) + else: + if index is not None: + try: + display(wikipedia.summary(results[index])) + except IndexError: + print "index out of range, options are: [%s]" % stringify(results) + else: + display(results) + +def url(topic, index): + try: + page = wikipedia.page(topic) + except wikipedia.exceptions.DisambiguationError as e: + if index is not None: + try: + display(wikipedia.page(e.options[index]).url) + except IndexError: + print "index out of range, options are: [%s]" % stringify(e.options) + else: + display(e.options) + else: + display(page.url) + +def summary(topic, index): + try: + display(wikipedia.summary(topic)) + except wikipedia.exceptions.DisambiguationError as e: + if index is not None: + try: + display(wikipedia.summary(e.options[index])) + except IndexError: + print "index out of range, options are: [%s]" % stringify(e.options) + else: + display(e.options) def parse_args(): args = sys.argv[1:] @@ -48,31 +87,12 @@ def main(): index = int(args.pop(0)) if args else None if options.search: - results = wikipedia.search(topic) - if len(results) == 1: - display(wikipedia.summary(results[0])) - else: - if index is not None: - try: - display(wikipedia.summary(results[index])) - except IndexError: - print "index out of range, options are: [%s]" % stringify(results) - else: - display(results) + search(topic, index) elif options.url: - page = wikipedia.page(topic) - display(page.url) + url(topic, index) else: - try: - display(wikipedia.summary(topic)) - except wikipedia.exceptions.DisambiguationError as e: - if index is not None: - try: - display(wikipedia.summary(e.options[index])) - except IndexError: - print "index out of range, options are: [%s]" % stringify(e.options) - else: - display(e.options) + summary(topic, index) + if __name__ == "__main__": main() -- cgit v1.2.3 From 7b8f224218e0b53b0b0e73517f55daa7c840d164 Mon Sep 17 00:00:00 2001 From: Alasdair Date: Thu, 5 Sep 2013 16:33:10 +0200 Subject: updating usage string --- wikiquery | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wikiquery b/wikiquery index b05ccd9..b7f7648 100755 --- a/wikiquery +++ b/wikiquery @@ -67,7 +67,7 @@ def parse_args(): if not args: args = sys.stdin.read().splitlines() - parser = optparse.OptionParser(usage="!wiki [--search|--url]") + parser = optparse.OptionParser(usage="!wiki [index] [--search|--url]") parser.add_option("--search", action="store_true") parser.add_option("--url", action="store_true") -- cgit v1.2.3