annotate smartopen/smartopen.py @ 14:a62fbff067f8

start bringing this whole thing up to speed
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 01 May 2013 06:56:12 -0700
parents 4dd12cf64c0e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
1 #!/usr/bin/env python
0
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
2
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
3 """
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
4 open what you give in your browser
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
5 """
0
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
6
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
7 import os
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
8 import subprocess
0
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
9 import sys
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
10
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
11 from optparse import OptionParser
1
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
12 from pkg_resources import iter_entry_points
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
13 from ConfigParser import ConfigParser
0
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
14
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
15
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
16 ### method to open a browser
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
17
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
18 ### It turns out finding the default browser is very hard :(
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
19 # webbrowser is standard...just...what does that mean? for gnome-terminal,
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
20 # I get firefox; however, despite what
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
21 # http://stackoverflow.com/questions/4216985/python-call-to-operating-system-to-open-url
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
22 # says (and also because of it), I actually get `links` from xdg-open(!)
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
23 # (which of course it doesn't even tell me)
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
24
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
25 # xdg-settings --list
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
26 # Known properties:
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
27 # default-web-browser Default web browser
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
28 # xdg-settings get default-web-browser
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
29 # xdg-settings: unknown desktop environment
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
30
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
31 # The solution....is here:
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
32 # http://askubuntu.com/questions/96080/how-to-set-google-chrome-as-the-default-browser/96081#96081
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
33 # and indeed in ~/.local/share/applications/mimeapps.list
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
34 # I do have::
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
35
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
36 # text/html=firefox.desktop;thunderbird.desktop;
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
37
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
38 # in `[Added Associations]`
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
39 # (fwiw, ``pprint(mailcap.getcaps())`` was interesting but not very useful,
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
40 # referencing /usr/bin/sensible-browser which is in fact links (!)
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
41
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
42 # TODO:
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
43
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
44 # probably some fancy module should be written that *actually* does what
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
45 # its supposed to here; maybe https://pypi.python.org/pypi/desktop is that
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
46 # fancy pants module; I don't know! pypi doesn't let me browse tarballs!
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
47 # in any case, this is mostly for me and it must be portable;
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
48
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
49 # So what we'll actually do:
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
50 # (Not surprisingly, explicit wins)
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
51 # - if specified on the command line (which it can't be yet) that wins
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
52 # - use browser/BROWSER if set in config
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
53 # - if not...you're back to guessing
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
54 # - if `which` was stdlib I'd probably guess on that since I know I can't rely
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
55 # on `xdg-open`...
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
56 # - ...and there are other platforms; again, I don't care....
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
57 # - so we're back to Firefox being #1
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
58
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
59 # Which despite my love for Firefox, I'd love to help with autoselection if
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
60 # anyone would throw their hat in
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
61
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
62 # * let's not forget http://freedesktop.org/wiki/Software/pyxdg if we're
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
63 # talking about non-stdlib solutions for linux only
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
64
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
65 def open_in_browser(url, browser=None):
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
66 """yeah, that's right..."""
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
67
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
68 if browser is None:
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
69 browser = 'firefox' # we'll cheat because of course its easy
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
70
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
71 # control via env variable; might as well keep it set o_O
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
72 os.environ.setdefault('BROWSER', browser)
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
73 # see e.g. http://hg.python.org/cpython/file/2.7/Lib/webbrowser.py
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
74 # sadly these are different::
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
75 # xdg-open http://k0s.org/
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
76 # BROWSER=firefox xdg-open http://k0s.org/
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
77
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
78 # now invoke the damn thing
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
79 import webbrowser
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
80 return webbrowser.open(url) # XXX lord almighty
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
81 # why bother returning it even?
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
82
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
83
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
84 ### methods for inspecting the locations
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
85
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
86 def locations(names=None, config=None, verbose=False):
1
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
87 """
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
88 list of 2-tuples of location handlers;
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
89 * names: order names of handlers
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
90 * config: nested dictionary of configuration from names
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
91 """
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
92
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
93 # setup
1
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
94 _handlers = {}
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
95 _names = []
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
96 if config is None:
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
97 config = {}
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
98
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
99 # load the handlers
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
100 # (really, these should come of a dict that is shared with an entry point)
1
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
101 for i in iter_entry_points('smartopen.locations'):
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
102 try:
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
103 handler = i.load()
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
104 except Exception, e:
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
105 if verbose:
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
106 print >> sys.stderr, "Error loading handler:\n%s" % e
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
107 continue
1
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
108 _handlers[i.name] = handler
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
109 if not names:
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
110 _names.append(i.name)
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
111
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
112 if not names:
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
113 names = _names
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
114 handlers = []
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
115 for name in names:
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
116 if ':' in name:
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
117 _name, section = name.split(':', 1)
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
118 else:
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
119 _name = name
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
120 if _name in _handlers:
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
121 try:
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
122 handler = _handlers[_name](**config.get(name, {}))
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
123 except Exception, e:
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
124 if verbose:
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
125 print >> sys.stderr, "blah blah blah"
1
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
126 continue
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
127 handlers.append((name, handler))
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
128 return handlers
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
129
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
130
1
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
131 def urls(query, handlers=None):
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
132 """returns available urls in order of preference for a query"""
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
133
1
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
134 if handlers is None:
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
135 handlers = locations()
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
136 urls = []
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
137 for name, handler in handlers:
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
138 if handler.test(query):
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
139 urls.append((name, handler.url(query)))
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
140 return urls
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
141
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
142
1
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
143 def url(query, handlers=None):
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
144 """return the top url for a query"""
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
145
1
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
146 if handlers is None:
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
147 handlers = locations()
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
148 for name, handler in handlers:
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
149 if handler.test(query):
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
150 return handler.url(query)
0
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
151
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
152 ### command line entry point
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
153
0
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
154 def main(args=sys.argv[1:]):
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
155
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
156 # parse command line optioins
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
157 default_browser = os.environ.get('BROWSER', 'firefox') # ^ see LONG note above
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
158 usage = '%prog [options] query'
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
159 parser = OptionParser(usage=usage)
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
160 parser.add_option('-b', '--browser', dest='browser',
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
161 default=None,
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
162 help="browser to use; can also be set in config and BROWSER [default: %default]")
1
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
163 parser.add_option('-c', '--config', dest="config",
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
164 help="config file to read")
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
165 parser.add_option('-u', '--url', dest="url",
0
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
166 action='store_true', default=False,
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
167 help="print the first url handled")
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
168 parser.add_option('-a', '--all', dest="all",
0
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
169 action='store_true', default=False,
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
170 help="print all handlers that match the query")
1
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
171 parser.add_option('-H', '--handler', dest="handlers",
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
172 action='append',
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
173 help="name of the handler to use, in order")
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
174 parser.add_option('-v', '--verbose', dest='verbose',
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
175 action='store_true', default=True,
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
176 help="be verbose with output")
1
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
177 parser.add_option('--print-handlers', dest="print_handlers",
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
178 action='store_true',
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
179 help="print all handlers in order they would be tried")
0
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
180 options, args = parser.parse_args(args)
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
181
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
182 # sanity check
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
183 assert not (options.url and options.all)
1
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
184 if not options.handlers:
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
185 options.handlers = None
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
186
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
187 # read config, if available
1
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
188 config = ConfigParser()
4
01015b36290a look for home configuration file if it exists
k0s <k0scist@gmail.com>
parents: 1
diff changeset
189 if not options.config:
01015b36290a look for home configuration file if it exists
k0s <k0scist@gmail.com>
parents: 1
diff changeset
190 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
191 if os.path.exists(options.config):
1
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
192 config.read(options.config)
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
193
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
194 # select handlers
1
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
195 if not options.handlers and config.has_option('DEFAULTS', 'handlers'):
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
196 options.handlers = [i.strip() for i in config.get('DEFAULTS', 'handlers').split(',')]
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
197
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
198 # select browser
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
199 if not options.browser:
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
200 for key in 'BROWSER', 'browser':
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
201 if config.has_option('DEFAULTS', key):
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
202 options.browser = config.get('DEFAULTS', key)
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
203 break
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
204
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
205 # the remaining config is arguments to the handlers
1
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
206 _config = {}
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
207 for section in config.sections():
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
208 _config[section] = dict(config.items(section))
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
209
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
210 # get the handlers
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
211 _locations = locations(options.handlers, _config, verbose=options.verbose)
1
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
212
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
213 if options.print_handlers:
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
214 # print the handlers
1
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
215 for name, loc in _locations:
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
216 print name
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
217 sys.exit(0)
0
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
218
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
219 # get data to be operated on
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
220 if args:
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
221 data = ' '.join(args)
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
222 else:
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
223 # read from stdin
0
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
224 data = sys.stdin.read()
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
225
1
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
226 if options.all:
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
227 # print the URLs
1
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
228 _urls = urls(data, _locations)
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
229 for name, _url in _urls:
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
230 print '%s: %s' % (name, _url)
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
231 sys.exit(0)
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
232
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
233 # select the url
1
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
234 _url = url(data, _locations)
0
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
235
1
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
236 if options.url:
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
237 # print a URL
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
238 print _url or 'No handler found for your query'
1
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
239 sys.exit(0)
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
240
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
241 # open the URL in a browser
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
242 if _url:
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
243 open_in_browser(_url)
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
244 else:
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
245 parser.error("No handler found")
0
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
246
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
247 if __name__ == '__main__':
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
248 main()