Mercurial > hg > smartopen
comparison 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 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:d6fa501af82f |
---|---|
1 #!/usr/bin/python | |
2 | |
3 """ smart open the data passed in """ | |
4 | |
5 import os | |
6 import sys | |
7 import urllib | |
8 import urllib2 | |
9 | |
10 from optparse import OptionParser | |
11 | |
12 from handlers import * | |
13 | |
14 def main(args=sys.argv[1:]): | |
15 | |
16 # parse command line optioins | |
17 parser = OptionParser() | |
18 parser.add_option('-u', '--url', dest="url", | |
19 action='store_true', default=False, | |
20 help="print the first url handled") | |
21 parser.add_option('-a', '--all', dest="all", | |
22 action='store_true', default=False, | |
23 help="print all handlers that match the query") | |
24 options, args = parser.parse_args(args) | |
25 | |
26 # sanity check | |
27 assert not (options.url and options.all) | |
28 | |
29 # get data to be operated on | |
30 if args: | |
31 data = ' '.join(args) | |
32 else: | |
33 data = sys.stdin.read() | |
34 | |
35 locations = [ URL, | |
36 GoogleMap, | |
37 Revision, | |
38 TracTicket, | |
39 Wikipedia, | |
40 Google | |
41 ] | |
42 | |
43 for loc in locations: | |
44 loc = loc() | |
45 if options.url: # print url | |
46 if loc.test(data): | |
47 print loc.url(data) | |
48 sys.exit(0) | |
49 elif options.all: | |
50 if loc.test(data): | |
51 print '%s: %s' % (loc.__class__.__name__, loc.url(data)) | |
52 else: | |
53 if loc.open(data): | |
54 sys.exit(0) | |
55 | |
56 if __name__ == '__main__': | |
57 main() |