Mercurial > hg > smartopen
diff smartopen/smartopen.py @ 0:d6fa501af82f
initial commit of smartopen
author | k0s <k0scist@gmail.com> |
---|---|
date | Sat, 21 Nov 2009 15:46:01 -0500 |
parents | |
children | 10fc4904c10f |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/smartopen/smartopen.py Sat Nov 21 15:46:01 2009 -0500 @@ -0,0 +1,57 @@ +#!/usr/bin/python + +""" smart open the data passed in """ + +import os +import sys +import urllib +import urllib2 + +from optparse import OptionParser + +from handlers import * + +def main(args=sys.argv[1:]): + + # parse command line optioins + parser = OptionParser() + parser.add_option('-u', '--url', dest="url", + action='store_true', default=False, + help="print the first url handled") + parser.add_option('-a', '--all', dest="all", + action='store_true', default=False, + help="print all handlers that match the query") + options, args = parser.parse_args(args) + + # sanity check + assert not (options.url and options.all) + + # get data to be operated on + if args: + data = ' '.join(args) + else: + data = sys.stdin.read() + + locations = [ URL, + GoogleMap, + Revision, + TracTicket, + Wikipedia, + Google + ] + + for loc in locations: + loc = loc() + if options.url: # print url + if loc.test(data): + print loc.url(data) + sys.exit(0) + elif options.all: + if loc.test(data): + print '%s: %s' % (loc.__class__.__name__, loc.url(data)) + else: + if loc.open(data): + sys.exit(0) + +if __name__ == '__main__': + main()