Mercurial > hg > smartopen
comparison smartopen/smartopen.py @ 1:10fc4904c10f
now can pass data
author | k0s <k0scist@gmail.com> |
---|---|
date | Sat, 21 Nov 2009 17:29:39 -0500 |
parents | d6fa501af82f |
children | 01015b36290a |
comparison
equal
deleted
inserted
replaced
0:d6fa501af82f | 1:10fc4904c10f |
---|---|
1 #!/usr/bin/python | 1 #!/usr/bin/env python |
2 | 2 |
3 """ smart open the data passed in """ | 3 """ smart open the data passed in """ |
4 | 4 |
5 import os | 5 import os |
6 import sys | 6 import sys |
7 import urllib | |
8 import urllib2 | |
9 | 7 |
10 from optparse import OptionParser | 8 from optparse import OptionParser |
9 from pkg_resources import iter_entry_points | |
10 from ConfigParser import ConfigParser | |
11 | 11 |
12 from handlers import * | 12 def locations(names=None, config=None): |
13 """ | |
14 list of 2-tuples of location handlers; | |
15 * names: order names of handlers | |
16 * config: nested dictionary of configuration from names | |
17 """ | |
18 | |
19 _handlers = {} | |
20 _names = [] | |
21 if config is None: | |
22 config = {} | |
23 | |
24 for i in iter_entry_points('smartopen.locations'): | |
25 try: | |
26 handler = i.load() | |
27 except: | |
28 pass | |
29 _handlers[i.name] = handler | |
30 if not names: | |
31 _names.append(i.name) | |
32 | |
33 if not names: | |
34 names = _names | |
35 handlers = [] | |
36 for name in names: | |
37 if ':' in name: | |
38 _name, section = name.split(':', 1) | |
39 else: | |
40 _name = name | |
41 if _name in _handlers: | |
42 try: | |
43 handler = _handlers[_name](**config.get(name, {})) | |
44 except: | |
45 continue | |
46 handlers.append((name, handler)) | |
47 return handlers | |
48 | |
49 def urls(query, handlers=None): | |
50 if handlers is None: | |
51 handlers = locations() | |
52 urls = [] | |
53 for name, handler in handlers: | |
54 if handler.test(query): | |
55 urls.append((name, handler.url(query))) | |
56 return urls | |
57 | |
58 def url(query, handlers=None): | |
59 if handlers is None: | |
60 handlers = locations() | |
61 for name, handler in handlers: | |
62 if handler.test(query): | |
63 return handler.url(query) | |
13 | 64 |
14 def main(args=sys.argv[1:]): | 65 def main(args=sys.argv[1:]): |
15 | 66 |
16 # parse command line optioins | 67 # parse command line optioins |
17 parser = OptionParser() | 68 parser = OptionParser() |
69 parser.add_option('-c', '--config', dest="config", | |
70 help="config file to read") | |
18 parser.add_option('-u', '--url', dest="url", | 71 parser.add_option('-u', '--url', dest="url", |
19 action='store_true', default=False, | 72 action='store_true', default=False, |
20 help="print the first url handled") | 73 help="print the first url handled") |
21 parser.add_option('-a', '--all', dest="all", | 74 parser.add_option('-a', '--all', dest="all", |
22 action='store_true', default=False, | 75 action='store_true', default=False, |
23 help="print all handlers that match the query") | 76 help="print all handlers that match the query") |
77 parser.add_option('-H', '--handler', dest="handlers", | |
78 action='append', | |
79 help="name of the handler to use, in order") | |
80 parser.add_option('--print-handlers', dest="print_handlers", | |
81 action='store_true', | |
82 help="print all handlers in order they would be tried") | |
24 options, args = parser.parse_args(args) | 83 options, args = parser.parse_args(args) |
25 | 84 |
26 # sanity check | 85 # sanity check |
27 assert not (options.url and options.all) | 86 assert not (options.url and options.all) |
87 if not options.handlers: | |
88 options.handlers = None | |
89 | |
90 # config | |
91 config = ConfigParser() | |
92 if options.config and os.path.exists(options.config): | |
93 config.read(options.config) | |
94 if not options.handlers and config.has_option('DEFAULTS', 'handlers'): | |
95 options.handlers = [ i.strip() for i in config.get('DEFAULTS', 'handlers').split(',') ] | |
96 _config = {} | |
97 for section in config.sections(): | |
98 _config[section] = dict(config.items(section)) | |
99 | |
100 # get the handlers | |
101 _locations = locations(options.handlers, _config) | |
102 | |
103 # print the handlers | |
104 if options.print_handlers: | |
105 for name, loc in _locations: | |
106 print name | |
107 sys.exit(0) | |
28 | 108 |
29 # get data to be operated on | 109 # get data to be operated on |
30 if args: | 110 if args: |
31 data = ' '.join(args) | 111 data = ' '.join(args) |
32 else: | 112 else: |
33 data = sys.stdin.read() | 113 data = sys.stdin.read() |
34 | 114 |
35 locations = [ URL, | 115 # print the URLs |
36 GoogleMap, | 116 if options.all: |
37 Revision, | 117 _urls = urls(data, _locations) |
38 TracTicket, | 118 for name, _url in _urls: |
39 Wikipedia, | 119 print '%s: %s' % (name, _url) |
40 Google | 120 sys.exit(0) |
41 ] | |
42 | 121 |
43 for loc in locations: | 122 _url = url(data, _locations) |
44 loc = loc() | 123 |
45 if options.url: # print url | 124 # print a URL |
46 if loc.test(data): | 125 if options.url: |
47 print loc.url(data) | 126 print _url |
48 sys.exit(0) | 127 sys.exit(0) |
49 elif options.all: | 128 |
50 if loc.test(data): | 129 # open the URL in a browser |
51 print '%s: %s' % (loc.__class__.__name__, loc.url(data)) | 130 os.system("firefox '%s'" % _url) |
52 else: | 131 sys.exit(0) |
53 if loc.open(data): | 132 |
54 sys.exit(0) | |
55 | 133 |
56 if __name__ == '__main__': | 134 if __name__ == '__main__': |
57 main() | 135 main() |