Mercurial > hg > smartopen
annotate smartopen/smartopen.py @ 9:4dd12cf64c0e
remove debugging code and state what should be done
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Tue, 04 May 2010 08:49:39 -0700 |
parents | 01015b36290a |
children | a62fbff067f8 |
rev | line source |
---|---|
1 | 1 #!/usr/bin/env python |
0 | 2 |
3 """ smart open the data passed in """ | |
4 | |
5 import os | |
6 import sys | |
7 | |
8 from optparse import OptionParser | |
1 | 9 from pkg_resources import iter_entry_points |
10 from ConfigParser import ConfigParser | |
0 | 11 |
1 | 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: | |
9
4dd12cf64c0e
remove debugging code and state what should be done
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
28 continue # TODO: warn/debug with --verbose flag |
1 | 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) | |
0 | 64 |
65 def main(args=sys.argv[1:]): | |
66 | |
67 # parse command line optioins | |
68 parser = OptionParser() | |
1 | 69 parser.add_option('-c', '--config', dest="config", |
70 help="config file to read") | |
0 | 71 parser.add_option('-u', '--url', dest="url", |
72 action='store_true', default=False, | |
73 help="print the first url handled") | |
74 parser.add_option('-a', '--all', dest="all", | |
75 action='store_true', default=False, | |
76 help="print all handlers that match the query") | |
1 | 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") | |
0 | 83 options, args = parser.parse_args(args) |
84 | |
85 # sanity check | |
86 assert not (options.url and options.all) | |
1 | 87 if not options.handlers: |
88 options.handlers = None | |
89 | |
90 # config | |
91 config = ConfigParser() | |
4
01015b36290a
look for home configuration file if it exists
k0s <k0scist@gmail.com>
parents:
1
diff
changeset
|
92 if not options.config: |
01015b36290a
look for home configuration file if it exists
k0s <k0scist@gmail.com>
parents:
1
diff
changeset
|
93 options.config = os.path.join(os.environ.get('HOME', ''), '.smartopen.ini') |
01015b36290a
look for home configuration file if it exists
k0s <k0scist@gmail.com>
parents:
1
diff
changeset
|
94 if os.path.exists(options.config): |
1 | 95 config.read(options.config) |
96 if not options.handlers and config.has_option('DEFAULTS', 'handlers'): | |
97 options.handlers = [ i.strip() for i in config.get('DEFAULTS', 'handlers').split(',') ] | |
98 _config = {} | |
99 for section in config.sections(): | |
100 _config[section] = dict(config.items(section)) | |
101 | |
102 # get the handlers | |
103 _locations = locations(options.handlers, _config) | |
104 | |
105 # print the handlers | |
106 if options.print_handlers: | |
107 for name, loc in _locations: | |
108 print name | |
109 sys.exit(0) | |
0 | 110 |
111 # get data to be operated on | |
112 if args: | |
113 data = ' '.join(args) | |
114 else: | |
115 data = sys.stdin.read() | |
116 | |
1 | 117 # print the URLs |
118 if options.all: | |
119 _urls = urls(data, _locations) | |
120 for name, _url in _urls: | |
121 print '%s: %s' % (name, _url) | |
122 sys.exit(0) | |
123 | |
124 _url = url(data, _locations) | |
0 | 125 |
1 | 126 # print a URL |
127 if options.url: | |
128 print _url | |
129 sys.exit(0) | |
130 | |
131 # open the URL in a browser | |
132 os.system("firefox '%s'" % _url) | |
133 sys.exit(0) | |
134 | |
0 | 135 |
136 if __name__ == '__main__': | |
137 main() |