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:
|
|
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)
|
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()
|
|
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)
|
0
|
108
|
|
109 # get data to be operated on
|
|
110 if args:
|
|
111 data = ' '.join(args)
|
|
112 else:
|
|
113 data = sys.stdin.read()
|
|
114
|
1
|
115 # print the URLs
|
|
116 if options.all:
|
|
117 _urls = urls(data, _locations)
|
|
118 for name, _url in _urls:
|
|
119 print '%s: %s' % (name, _url)
|
|
120 sys.exit(0)
|
|
121
|
|
122 _url = url(data, _locations)
|
0
|
123
|
1
|
124 # print a URL
|
|
125 if options.url:
|
|
126 print _url
|
|
127 sys.exit(0)
|
|
128
|
|
129 # open the URL in a browser
|
|
130 os.system("firefox '%s'" % _url)
|
|
131 sys.exit(0)
|
|
132
|
0
|
133
|
|
134 if __name__ == '__main__':
|
|
135 main()
|