Mercurial > cpython-lru_cache-weakref
annotate Lib/webbrowser.py @ 58925:eecf1bef052d 2.6
Merged revisions 78207 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line
#7930: fix stripid
........
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Tue, 16 Feb 2010 23:29:44 +0000 |
| parents | 97d7cd365dd5 |
| children | 751c6dd71b5e |
| rev | line source |
|---|---|
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
1 #! /usr/bin/env python |
| 16926 | 2 """Interfaces for launching and remotely controlling Web browsers.""" |
|
42137
dccf14fe00ec
Fix a few webbrowser.py problems.
Georg Brandl <georg@python.org>
parents:
41261
diff
changeset
|
3 # Maintained by Georg Brandl. |
|
13476
8f66c5ffa26c
New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
4 |
|
8f66c5ffa26c
New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
5 import os |
|
41261
459e36b2ea45
Fix #1684254: split BROWSER contents with shlex to avoid displaying 'URL'.
Georg Brandl <georg@python.org>
parents:
41207
diff
changeset
|
6 import shlex |
|
13476
8f66c5ffa26c
New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
7 import sys |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
8 import stat |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
9 import subprocess |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
10 import time |
|
13476
8f66c5ffa26c
New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
11 |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
12 __all__ = ["Error", "open", "open_new", "open_new_tab", "get", "register"] |
|
16889
0feb63490093
final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents:
16833
diff
changeset
|
13 |
|
13476
8f66c5ffa26c
New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
14 class Error(Exception): |
|
8f66c5ffa26c
New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
15 pass |
|
8f66c5ffa26c
New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
16 |
|
16535
084b3dc9b6ff
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
16161
diff
changeset
|
17 _browsers = {} # Dictionary of available browser controllers |
|
084b3dc9b6ff
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
16161
diff
changeset
|
18 _tryorder = [] # Preference order of available browsers |
|
13476
8f66c5ffa26c
New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
19 |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
20 def register(name, klass, instance=None, update_tryorder=1): |
|
13476
8f66c5ffa26c
New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
21 """Register a browser connector and, optionally, connection.""" |
|
8f66c5ffa26c
New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
22 _browsers[name.lower()] = [klass, instance] |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
23 if update_tryorder > 0: |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
24 _tryorder.append(name) |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
25 elif update_tryorder < 0: |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
26 _tryorder.insert(0, name) |
|
13476
8f66c5ffa26c
New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
27 |
|
16153
df7bfb34b2a2
Rewrite webbrowser.py to fix various bugs following Ka-Ping Yee's
Eric S. Raymond <esr@thyrsus.com>
parents:
15874
diff
changeset
|
28 def get(using=None): |
|
df7bfb34b2a2
Rewrite webbrowser.py to fix various bugs following Ka-Ping Yee's
Eric S. Raymond <esr@thyrsus.com>
parents:
15874
diff
changeset
|
29 """Return a browser launcher instance appropriate for the environment.""" |
|
23628
ea979c5206b7
Replaced boolean tests with is None.
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
30 if using is not None: |
|
16153
df7bfb34b2a2
Rewrite webbrowser.py to fix various bugs following Ka-Ping Yee's
Eric S. Raymond <esr@thyrsus.com>
parents:
15874
diff
changeset
|
31 alternatives = [using] |
|
df7bfb34b2a2
Rewrite webbrowser.py to fix various bugs following Ka-Ping Yee's
Eric S. Raymond <esr@thyrsus.com>
parents:
15874
diff
changeset
|
32 else: |
|
df7bfb34b2a2
Rewrite webbrowser.py to fix various bugs following Ka-Ping Yee's
Eric S. Raymond <esr@thyrsus.com>
parents:
15874
diff
changeset
|
33 alternatives = _tryorder |
|
df7bfb34b2a2
Rewrite webbrowser.py to fix various bugs following Ka-Ping Yee's
Eric S. Raymond <esr@thyrsus.com>
parents:
15874
diff
changeset
|
34 for browser in alternatives: |
|
31967
3d046d063a84
Replace str.find()!=1 with the more readable "in" operator.
Raymond Hettinger <python@rcn.com>
parents:
26404
diff
changeset
|
35 if '%s' in browser: |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
36 # User gave us a command line, split it into name and args |
|
41261
459e36b2ea45
Fix #1684254: split BROWSER contents with shlex to avoid displaying 'URL'.
Georg Brandl <georg@python.org>
parents:
41207
diff
changeset
|
37 browser = shlex.split(browser) |
|
459e36b2ea45
Fix #1684254: split BROWSER contents with shlex to avoid displaying 'URL'.
Georg Brandl <georg@python.org>
parents:
41207
diff
changeset
|
38 if browser[-1] == '&': |
|
459e36b2ea45
Fix #1684254: split BROWSER contents with shlex to avoid displaying 'URL'.
Georg Brandl <georg@python.org>
parents:
41207
diff
changeset
|
39 return BackgroundBrowser(browser[:-1]) |
|
459e36b2ea45
Fix #1684254: split BROWSER contents with shlex to avoid displaying 'URL'.
Georg Brandl <georg@python.org>
parents:
41207
diff
changeset
|
40 else: |
|
459e36b2ea45
Fix #1684254: split BROWSER contents with shlex to avoid displaying 'URL'.
Georg Brandl <georg@python.org>
parents:
41207
diff
changeset
|
41 return GenericBrowser(browser) |
|
16153
df7bfb34b2a2
Rewrite webbrowser.py to fix various bugs following Ka-Ping Yee's
Eric S. Raymond <esr@thyrsus.com>
parents:
15874
diff
changeset
|
42 else: |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
43 # User gave us a browser name or path. |
|
17461
a004c62e696e
_synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents:
17270
diff
changeset
|
44 try: |
|
a004c62e696e
_synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents:
17270
diff
changeset
|
45 command = _browsers[browser.lower()] |
|
a004c62e696e
_synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents:
17270
diff
changeset
|
46 except KeyError: |
|
a004c62e696e
_synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents:
17270
diff
changeset
|
47 command = _synthesize(browser) |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
48 if command[1] is not None: |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
49 return command[1] |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
50 elif command[0] is not None: |
|
16153
df7bfb34b2a2
Rewrite webbrowser.py to fix various bugs following Ka-Ping Yee's
Eric S. Raymond <esr@thyrsus.com>
parents:
15874
diff
changeset
|
51 return command[0]() |
|
df7bfb34b2a2
Rewrite webbrowser.py to fix various bugs following Ka-Ping Yee's
Eric S. Raymond <esr@thyrsus.com>
parents:
15874
diff
changeset
|
52 raise Error("could not locate runnable browser") |
|
13476
8f66c5ffa26c
New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
53 |
|
8f66c5ffa26c
New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
54 # Please note: the following definition hides a builtin function. |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
55 # It is recommended one does "import webbrowser" and uses webbrowser.open(url) |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
56 # instead of "from webbrowser import *". |
|
13476
8f66c5ffa26c
New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
57 |
|
16155
b2c8795a3448
Expose the autoraise capability. Improve the documentation.
Eric S. Raymond <esr@thyrsus.com>
parents:
16153
diff
changeset
|
58 def open(url, new=0, autoraise=1): |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
59 for name in _tryorder: |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
60 browser = get(name) |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
61 if browser.open(url, new, autoraise): |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
62 return True |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
63 return False |
|
13476
8f66c5ffa26c
New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
64 |
| 18720 | 65 def open_new(url): |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
66 return open(url, 1) |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
67 |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
68 def open_new_tab(url): |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
69 return open(url, 2) |
|
13476
8f66c5ffa26c
New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
70 |
|
17461
a004c62e696e
_synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents:
17270
diff
changeset
|
71 |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
72 def _synthesize(browser, update_tryorder=1): |
|
17461
a004c62e696e
_synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents:
17270
diff
changeset
|
73 """Attempt to synthesize a controller base on existing controllers. |
|
a004c62e696e
_synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents:
17270
diff
changeset
|
74 |
|
a004c62e696e
_synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents:
17270
diff
changeset
|
75 This is useful to create a controller when a user specifies a path to |
|
a004c62e696e
_synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents:
17270
diff
changeset
|
76 an entry in the BROWSER environment variable -- we can copy a general |
|
a004c62e696e
_synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents:
17270
diff
changeset
|
77 controller to operate using a specific installation of the desired |
|
a004c62e696e
_synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents:
17270
diff
changeset
|
78 browser in this way. |
|
a004c62e696e
_synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents:
17270
diff
changeset
|
79 |
|
a004c62e696e
_synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents:
17270
diff
changeset
|
80 If we can't create a controller in this way, or if there is no |
|
a004c62e696e
_synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents:
17270
diff
changeset
|
81 executable for the requested browser, return [None, None]. |
|
a004c62e696e
_synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents:
17270
diff
changeset
|
82 |
|
a004c62e696e
_synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents:
17270
diff
changeset
|
83 """ |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
84 cmd = browser.split()[0] |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
85 if not _iscommand(cmd): |
|
17461
a004c62e696e
_synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents:
17270
diff
changeset
|
86 return [None, None] |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
87 name = os.path.basename(cmd) |
|
17461
a004c62e696e
_synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents:
17270
diff
changeset
|
88 try: |
|
a004c62e696e
_synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents:
17270
diff
changeset
|
89 command = _browsers[name.lower()] |
|
a004c62e696e
_synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents:
17270
diff
changeset
|
90 except KeyError: |
|
a004c62e696e
_synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents:
17270
diff
changeset
|
91 return [None, None] |
|
a004c62e696e
_synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents:
17270
diff
changeset
|
92 # now attempt to clone to fit the new name: |
|
a004c62e696e
_synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents:
17270
diff
changeset
|
93 controller = command[1] |
|
a004c62e696e
_synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents:
17270
diff
changeset
|
94 if controller and name.lower() == controller.basename: |
|
a004c62e696e
_synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents:
17270
diff
changeset
|
95 import copy |
|
a004c62e696e
_synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents:
17270
diff
changeset
|
96 controller = copy.copy(controller) |
|
a004c62e696e
_synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents:
17270
diff
changeset
|
97 controller.name = browser |
|
a004c62e696e
_synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents:
17270
diff
changeset
|
98 controller.basename = os.path.basename(browser) |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
99 register(browser, None, controller, update_tryorder) |
|
17461
a004c62e696e
_synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents:
17270
diff
changeset
|
100 return [None, controller] |
|
19208
7b53fb3ba011
Fix malformed line (PyChecker)
Andrew M. Kuchling <amk@amk.ca>
parents:
18720
diff
changeset
|
101 return [None, None] |
|
17461
a004c62e696e
_synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents:
17270
diff
changeset
|
102 |
| 18720 | 103 |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
104 if sys.platform[:3] == "win": |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
105 def _isexecutable(cmd): |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
106 cmd = cmd.lower() |
|
38704
ba6eebf7c719
Make use of new str.startswith/endswith semantics.
Georg Brandl <georg@python.org>
parents:
38103
diff
changeset
|
107 if os.path.isfile(cmd) and cmd.endswith((".exe", ".bat")): |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
108 return True |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
109 for ext in ".exe", ".bat": |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
110 if os.path.isfile(cmd + ext): |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
111 return True |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
112 return False |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
113 else: |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
114 def _isexecutable(cmd): |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
115 if os.path.isfile(cmd): |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
116 mode = os.stat(cmd)[stat.ST_MODE] |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
117 if mode & stat.S_IXUSR or mode & stat.S_IXGRP or mode & stat.S_IXOTH: |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
118 return True |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
119 return False |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
120 |
| 18720 | 121 def _iscommand(cmd): |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
122 """Return True if cmd is executable or can be found on the executable |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
123 search path.""" |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
124 if _isexecutable(cmd): |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
125 return True |
| 18720 | 126 path = os.environ.get("PATH") |
| 127 if not path: | |
|
22830
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
22303
diff
changeset
|
128 return False |
| 18720 | 129 for d in path.split(os.pathsep): |
| 130 exe = os.path.join(d, cmd) | |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
131 if _isexecutable(exe): |
|
22830
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
22303
diff
changeset
|
132 return True |
|
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
22303
diff
changeset
|
133 return False |
| 18720 | 134 |
| 135 | |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
136 # General parent classes |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
137 |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
138 class BaseBrowser(object): |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
139 """Parent class for all browsers. Do not use directly.""" |
|
36362
ca6c800ad5a0
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
36352
diff
changeset
|
140 |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
141 args = ['%s'] |
|
36362
ca6c800ad5a0
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
36352
diff
changeset
|
142 |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
143 def __init__(self, name=""): |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
144 self.name = name |
|
35707
5685ec59f35b
Fix errors in _synthesize because of missing basename attribute of
Georg Brandl <georg@python.org>
parents:
35692
diff
changeset
|
145 self.basename = name |
|
36121
686306a67e9b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
35920
diff
changeset
|
146 |
| 35692 | 147 def open(self, url, new=0, autoraise=1): |
| 148 raise NotImplementedError | |
| 149 | |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
150 def open_new(self, url): |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
151 return self.open(url, 1) |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
152 |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
153 def open_new_tab(self, url): |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
154 return self.open(url, 2) |
| 18720 | 155 |
| 156 | |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
157 class GenericBrowser(BaseBrowser): |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
158 """Class for all browsers started with a command |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
159 and without remote functionality.""" |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
160 |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
161 def __init__(self, name): |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
162 if isinstance(name, basestring): |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
163 self.name = name |
|
42137
dccf14fe00ec
Fix a few webbrowser.py problems.
Georg Brandl <georg@python.org>
parents:
41261
diff
changeset
|
164 self.args = ["%s"] |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
165 else: |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
166 # name should be a list with arguments |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
167 self.name = name[0] |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
168 self.args = name[1:] |
|
35707
5685ec59f35b
Fix errors in _synthesize because of missing basename attribute of
Georg Brandl <georg@python.org>
parents:
35692
diff
changeset
|
169 self.basename = os.path.basename(self.name) |
| 18720 | 170 |
| 171 def open(self, url, new=0, autoraise=1): | |
|
36362
ca6c800ad5a0
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
36352
diff
changeset
|
172 cmdline = [self.name] + [arg.replace("%s", url) |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
173 for arg in self.args] |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
174 try: |
|
39879
d99753314453
Fix webbrowser.BackgroundBrowser on Windows.
Georg Brandl <georg@python.org>
parents:
39393
diff
changeset
|
175 if sys.platform[:3] == 'win': |
|
d99753314453
Fix webbrowser.BackgroundBrowser on Windows.
Georg Brandl <georg@python.org>
parents:
39393
diff
changeset
|
176 p = subprocess.Popen(cmdline) |
|
d99753314453
Fix webbrowser.BackgroundBrowser on Windows.
Georg Brandl <georg@python.org>
parents:
39393
diff
changeset
|
177 else: |
|
d99753314453
Fix webbrowser.BackgroundBrowser on Windows.
Georg Brandl <georg@python.org>
parents:
39393
diff
changeset
|
178 p = subprocess.Popen(cmdline, close_fds=True) |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
179 return not p.wait() |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
180 except OSError: |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
181 return False |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
182 |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
183 |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
184 class BackgroundBrowser(GenericBrowser): |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
185 """Class for all browsers which are to be started in the |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
186 background.""" |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
187 |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
188 def open(self, url, new=0, autoraise=1): |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
189 cmdline = [self.name] + [arg.replace("%s", url) |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
190 for arg in self.args] |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
191 try: |
|
39879
d99753314453
Fix webbrowser.BackgroundBrowser on Windows.
Georg Brandl <georg@python.org>
parents:
39393
diff
changeset
|
192 if sys.platform[:3] == 'win': |
|
d99753314453
Fix webbrowser.BackgroundBrowser on Windows.
Georg Brandl <georg@python.org>
parents:
39393
diff
changeset
|
193 p = subprocess.Popen(cmdline) |
|
d99753314453
Fix webbrowser.BackgroundBrowser on Windows.
Georg Brandl <georg@python.org>
parents:
39393
diff
changeset
|
194 else: |
|
d99753314453
Fix webbrowser.BackgroundBrowser on Windows.
Georg Brandl <georg@python.org>
parents:
39393
diff
changeset
|
195 setsid = getattr(os, 'setsid', None) |
|
d99753314453
Fix webbrowser.BackgroundBrowser on Windows.
Georg Brandl <georg@python.org>
parents:
39393
diff
changeset
|
196 if not setsid: |
|
d99753314453
Fix webbrowser.BackgroundBrowser on Windows.
Georg Brandl <georg@python.org>
parents:
39393
diff
changeset
|
197 setsid = getattr(os, 'setpgrp', None) |
|
d99753314453
Fix webbrowser.BackgroundBrowser on Windows.
Georg Brandl <georg@python.org>
parents:
39393
diff
changeset
|
198 p = subprocess.Popen(cmdline, close_fds=True, preexec_fn=setsid) |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
199 return (p.poll() is None) |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
200 except OSError: |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
201 return False |
| 18720 | 202 |
| 203 | |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
204 class UnixBrowser(BaseBrowser): |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
205 """Parent class for all Unix browsers with remote functionality.""" |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
206 |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
207 raise_opts = None |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
208 remote_args = ['%action', '%s'] |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
209 remote_action = None |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
210 remote_action_newwin = None |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
211 remote_action_newtab = None |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
212 background = False |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
213 redirect_stdout = True |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
214 |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
215 def _invoke(self, args, remote, autoraise): |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
216 raise_opt = [] |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
217 if remote and self.raise_opts: |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
218 # use autoraise argument only for remote invocation |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
219 autoraise = int(bool(autoraise)) |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
220 opt = self.raise_opts[autoraise] |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
221 if opt: raise_opt = [opt] |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
222 |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
223 cmdline = [self.name] + raise_opt + args |
|
36362
ca6c800ad5a0
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
36352
diff
changeset
|
224 |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
225 if remote or self.background: |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
226 inout = file(os.devnull, "r+") |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
227 else: |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
228 # for TTY browsers, we need stdin/out |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
229 inout = None |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
230 # if possible, put browser in separate process group, so |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
231 # keyboard interrupts don't affect browser as well as Python |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
232 setsid = getattr(os, 'setsid', None) |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
233 if not setsid: |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
234 setsid = getattr(os, 'setpgrp', None) |
|
36362
ca6c800ad5a0
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
36352
diff
changeset
|
235 |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
236 p = subprocess.Popen(cmdline, close_fds=True, stdin=inout, |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
237 stdout=(self.redirect_stdout and inout or None), |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
238 stderr=inout, preexec_fn=setsid) |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
239 if remote: |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
240 # wait five secons. If the subprocess is not finished, the |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
241 # remote invocation has (hopefully) started a new instance. |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
242 time.sleep(1) |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
243 rc = p.poll() |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
244 if rc is None: |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
245 time.sleep(4) |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
246 rc = p.poll() |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
247 if rc is None: |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
248 return True |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
249 # if remote call failed, open() will try direct invocation |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
250 return not rc |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
251 elif self.background: |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
252 if p.poll() is None: |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
253 return True |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
254 else: |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
255 return False |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
256 else: |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
257 return not p.wait() |
| 18720 | 258 |
| 259 def open(self, url, new=0, autoraise=1): | |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
260 if new == 0: |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
261 action = self.remote_action |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
262 elif new == 1: |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
263 action = self.remote_action_newwin |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
264 elif new == 2: |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
265 if self.remote_action_newtab is None: |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
266 action = self.remote_action_newwin |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
267 else: |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
268 action = self.remote_action_newtab |
| 18720 | 269 else: |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
270 raise Error("Bad 'new' parameter to open(); " + |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
271 "expected 0, 1, or 2, got %s" % new) |
|
36362
ca6c800ad5a0
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
36352
diff
changeset
|
272 |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
273 args = [arg.replace("%s", url).replace("%action", action) |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
274 for arg in self.remote_args] |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
275 success = self._invoke(args, True, autoraise) |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
276 if not success: |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
277 # remote invocation failed, try straight way |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
278 args = [arg.replace("%s", url) for arg in self.args] |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
279 return self._invoke(args, False, False) |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
280 else: |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
281 return True |
| 18720 | 282 |
| 283 | |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
284 class Mozilla(UnixBrowser): |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
285 """Launcher class for Mozilla/Netscape browsers.""" |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
286 |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
287 raise_opts = ["-noraise", "-raise"] |
|
25715
2adadc19923c
SF # 585913, Adds Galeon support to webbrowser.py
Neal Norwitz <nnorwitz@gmail.com>
parents:
23628
diff
changeset
|
288 |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
289 remote_args = ['-remote', 'openURL(%s%action)'] |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
290 remote_action = "" |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
291 remote_action_newwin = ",new-window" |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
292 remote_action_newtab = ",new-tab" |
|
36362
ca6c800ad5a0
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
36352
diff
changeset
|
293 |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
294 background = True |
|
25715
2adadc19923c
SF # 585913, Adds Galeon support to webbrowser.py
Neal Norwitz <nnorwitz@gmail.com>
parents:
23628
diff
changeset
|
295 |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
296 Netscape = Mozilla |
|
25715
2adadc19923c
SF # 585913, Adds Galeon support to webbrowser.py
Neal Norwitz <nnorwitz@gmail.com>
parents:
23628
diff
changeset
|
297 |
|
2adadc19923c
SF # 585913, Adds Galeon support to webbrowser.py
Neal Norwitz <nnorwitz@gmail.com>
parents:
23628
diff
changeset
|
298 |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
299 class Galeon(UnixBrowser): |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
300 """Launcher class for Galeon/Epiphany browsers.""" |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
301 |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
302 raise_opts = ["-noraise", ""] |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
303 remote_args = ['%action', '%s'] |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
304 remote_action = "-n" |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
305 remote_action_newwin = "-w" |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
306 |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
307 background = True |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
308 |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
309 |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
310 class Opera(UnixBrowser): |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
311 "Launcher class for Opera browser." |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
312 |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
313 raise_opts = ["", "-raise"] |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
314 |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
315 remote_args = ['-remote', 'openURL(%s%action)'] |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
316 remote_action = "" |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
317 remote_action_newwin = ",new-window" |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
318 remote_action_newtab = ",new-page" |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
319 background = True |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
320 |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
321 |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
322 class Elinks(UnixBrowser): |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
323 "Launcher class for Elinks browsers." |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
324 |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
325 remote_args = ['-remote', 'openURL(%s%action)'] |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
326 remote_action = "" |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
327 remote_action_newwin = ",new-window" |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
328 remote_action_newtab = ",new-tab" |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
329 background = False |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
330 |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
331 # elinks doesn't like its stdout to be redirected - |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
332 # it uses redirected stdout as a signal to do -dump |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
333 redirect_stdout = False |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
334 |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
335 |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
336 class Konqueror(BaseBrowser): |
| 18720 | 337 """Controller for the KDE File Manager (kfm, or Konqueror). |
| 338 | |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
339 See the output of ``kfmclient --commands`` |
| 18720 | 340 for more information on the Konqueror remote-control interface. |
| 341 """ | |
| 342 | |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
343 def open(self, url, new=0, autoraise=1): |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
344 # XXX Currently I know no way to prevent KFM from opening a new win. |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
345 if new == 2: |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
346 action = "newTab" |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
347 else: |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
348 action = "openURL" |
|
36362
ca6c800ad5a0
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
36352
diff
changeset
|
349 |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
350 devnull = file(os.devnull, "r+") |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
351 # if possible, put browser in separate process group, so |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
352 # keyboard interrupts don't affect browser as well as Python |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
353 setsid = getattr(os, 'setsid', None) |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
354 if not setsid: |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
355 setsid = getattr(os, 'setpgrp', None) |
|
36362
ca6c800ad5a0
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
36352
diff
changeset
|
356 |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
357 try: |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
358 p = subprocess.Popen(["kfmclient", action, url], |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
359 close_fds=True, stdin=devnull, |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
360 stdout=devnull, stderr=devnull) |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
361 except OSError: |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
362 # fall through to next variant |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
363 pass |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
364 else: |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
365 p.wait() |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
366 # kfmclient's return code unfortunately has no meaning as it seems |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
367 return True |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
368 |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
369 try: |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
370 p = subprocess.Popen(["konqueror", "--silent", url], |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
371 close_fds=True, stdin=devnull, |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
372 stdout=devnull, stderr=devnull, |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
373 preexec_fn=setsid) |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
374 except OSError: |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
375 # fall through to next variant |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
376 pass |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
377 else: |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
378 if p.poll() is None: |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
379 # Should be running now. |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
380 return True |
|
36362
ca6c800ad5a0
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
36352
diff
changeset
|
381 |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
382 try: |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
383 p = subprocess.Popen(["kfm", "-d", url], |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
384 close_fds=True, stdin=devnull, |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
385 stdout=devnull, stderr=devnull, |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
386 preexec_fn=setsid) |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
387 except OSError: |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
388 return False |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
389 else: |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
390 return (p.poll() is None) |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
391 |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
392 |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
393 class Grail(BaseBrowser): |
| 18720 | 394 # There should be a way to maintain a connection to Grail, but the |
| 395 # Grail remote control protocol doesn't really allow that at this | |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
396 # point. It probably never will! |
| 18720 | 397 def _find_grail_rc(self): |
| 398 import glob | |
| 399 import pwd | |
| 400 import socket | |
| 401 import tempfile | |
| 402 tempdir = os.path.join(tempfile.gettempdir(), | |
| 403 ".grail-unix") | |
|
20417
c168eda736e4
_os should be os; reported by Neal Norwitz.
Fred Drake <fdrake@acm.org>
parents:
19208
diff
changeset
|
404 user = pwd.getpwuid(os.getuid())[0] |
| 18720 | 405 filename = os.path.join(tempdir, user + "-*") |
| 406 maybes = glob.glob(filename) | |
| 407 if not maybes: | |
| 408 return None | |
| 409 s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) | |
| 410 for fn in maybes: | |
| 411 # need to PING each one until we find one that's live | |
| 412 try: | |
| 413 s.connect(fn) | |
| 414 except socket.error: | |
| 415 # no good; attempt to clean it out, but don't fail: | |
| 416 try: | |
| 417 os.unlink(fn) | |
| 418 except IOError: | |
| 419 pass | |
| 420 else: | |
| 421 return s | |
| 422 | |
| 423 def _remote(self, action): | |
| 424 s = self._find_grail_rc() | |
| 425 if not s: | |
| 426 return 0 | |
| 427 s.send(action) | |
| 428 s.close() | |
| 429 return 1 | |
| 430 | |
| 431 def open(self, url, new=0, autoraise=1): | |
| 432 if new: | |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
433 ok = self._remote("LOADNEW " + url) |
| 18720 | 434 else: |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
435 ok = self._remote("LOAD " + url) |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
436 return ok |
| 18720 | 437 |
| 438 | |
|
16535
084b3dc9b6ff
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
16161
diff
changeset
|
439 # |
|
16153
df7bfb34b2a2
Rewrite webbrowser.py to fix various bugs following Ka-Ping Yee's
Eric S. Raymond <esr@thyrsus.com>
parents:
15874
diff
changeset
|
440 # Platform support for Unix |
|
df7bfb34b2a2
Rewrite webbrowser.py to fix various bugs following Ka-Ping Yee's
Eric S. Raymond <esr@thyrsus.com>
parents:
15874
diff
changeset
|
441 # |
|
13476
8f66c5ffa26c
New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
442 |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
443 # These are the right tests because all these Unix browsers require either |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
444 # a console terminal or an X display to run. |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
445 |
| 35692 | 446 def register_X_browsers(): |
|
41207
01951f6b819d
Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents:
39919
diff
changeset
|
447 |
|
01951f6b819d
Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents:
39919
diff
changeset
|
448 # The default GNOME browser |
|
01951f6b819d
Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents:
39919
diff
changeset
|
449 if "GNOME_DESKTOP_SESSION_ID" in os.environ and _iscommand("gnome-open"): |
|
01951f6b819d
Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents:
39919
diff
changeset
|
450 register("gnome-open", None, BackgroundBrowser("gnome-open")) |
|
13476
8f66c5ffa26c
New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
451 |
|
41207
01951f6b819d
Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents:
39919
diff
changeset
|
452 # The default KDE browser |
|
01951f6b819d
Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents:
39919
diff
changeset
|
453 if "KDE_FULL_SESSION" in os.environ and _iscommand("kfmclient"): |
|
01951f6b819d
Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents:
39919
diff
changeset
|
454 register("kfmclient", Konqueror, Konqueror("kfmclient")) |
| 26404 | 455 |
|
41207
01951f6b819d
Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents:
39919
diff
changeset
|
456 # The Mozilla/Netscape browsers |
|
35920
e11323189c84
Prefer GNOME browser over mozilla.
Georg Brandl <georg@python.org>
parents:
35851
diff
changeset
|
457 for browser in ("mozilla-firefox", "firefox", |
|
e11323189c84
Prefer GNOME browser over mozilla.
Georg Brandl <georg@python.org>
parents:
35851
diff
changeset
|
458 "mozilla-firebird", "firebird", |
|
38027
34d696c1eebc
Add SeaMonkey to the list of Mozilla browsers.
Georg Brandl <georg@python.org>
parents:
36362
diff
changeset
|
459 "seamonkey", "mozilla", "netscape"): |
|
35920
e11323189c84
Prefer GNOME browser over mozilla.
Georg Brandl <georg@python.org>
parents:
35851
diff
changeset
|
460 if _iscommand(browser): |
|
e11323189c84
Prefer GNOME browser over mozilla.
Georg Brandl <georg@python.org>
parents:
35851
diff
changeset
|
461 register(browser, None, Mozilla(browser)) |
|
e11323189c84
Prefer GNOME browser over mozilla.
Georg Brandl <georg@python.org>
parents:
35851
diff
changeset
|
462 |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
463 # Konqueror/kfm, the KDE browser. |
|
35707
5685ec59f35b
Fix errors in _synthesize because of missing basename attribute of
Georg Brandl <georg@python.org>
parents:
35692
diff
changeset
|
464 if _iscommand("kfm"): |
|
5685ec59f35b
Fix errors in _synthesize because of missing basename attribute of
Georg Brandl <georg@python.org>
parents:
35692
diff
changeset
|
465 register("kfm", Konqueror, Konqueror("kfm")) |
|
5685ec59f35b
Fix errors in _synthesize because of missing basename attribute of
Georg Brandl <georg@python.org>
parents:
35692
diff
changeset
|
466 elif _iscommand("konqueror"): |
|
5685ec59f35b
Fix errors in _synthesize because of missing basename attribute of
Georg Brandl <georg@python.org>
parents:
35692
diff
changeset
|
467 register("konqueror", Konqueror, Konqueror("konqueror")) |
|
16153
df7bfb34b2a2
Rewrite webbrowser.py to fix various bugs following Ka-Ping Yee's
Eric S. Raymond <esr@thyrsus.com>
parents:
15874
diff
changeset
|
468 |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
469 # Gnome's Galeon and Epiphany |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
470 for browser in ("galeon", "epiphany"): |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
471 if _iscommand(browser): |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
472 register(browser, None, Galeon(browser)) |
|
16153
df7bfb34b2a2
Rewrite webbrowser.py to fix various bugs following Ka-Ping Yee's
Eric S. Raymond <esr@thyrsus.com>
parents:
15874
diff
changeset
|
473 |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
474 # Skipstone, another Gtk/Mozilla based browser |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
475 if _iscommand("skipstone"): |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
476 register("skipstone", None, BackgroundBrowser("skipstone")) |
|
25715
2adadc19923c
SF # 585913, Adds Galeon support to webbrowser.py
Neal Norwitz <nnorwitz@gmail.com>
parents:
23628
diff
changeset
|
477 |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
478 # Opera, quite popular |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
479 if _iscommand("opera"): |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
480 register("opera", None, Opera("opera")) |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
481 |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
482 # Next, Mosaic -- old but still in use. |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
483 if _iscommand("mosaic"): |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
484 register("mosaic", None, BackgroundBrowser("mosaic")) |
| 26404 | 485 |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
486 # Grail, the Python browser. Does anybody still use it? |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
487 if _iscommand("grail"): |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
488 register("grail", Grail, None) |
| 18720 | 489 |
| 35692 | 490 # Prefer X browsers if present |
| 491 if os.environ.get("DISPLAY"): | |
| 492 register_X_browsers() | |
| 493 | |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
494 # Also try console browsers |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
495 if os.environ.get("TERM"): |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
496 # The Links/elinks browsers <http://artax.karlin.mff.cuni.cz/~mikulas/links/> |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
497 if _iscommand("links"): |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
498 register("links", None, GenericBrowser("links")) |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
499 if _iscommand("elinks"): |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
500 register("elinks", None, Elinks("elinks")) |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
501 # The Lynx browser <http://lynx.isc.org/>, <http://lynx.browser.org/> |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
502 if _iscommand("lynx"): |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
503 register("lynx", None, GenericBrowser("lynx")) |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
504 # The w3m browser <http://w3m.sourceforge.net/> |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
505 if _iscommand("w3m"): |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
506 register("w3m", None, GenericBrowser("w3m")) |
|
13476
8f66c5ffa26c
New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
507 |
|
16153
df7bfb34b2a2
Rewrite webbrowser.py to fix various bugs following Ka-Ping Yee's
Eric S. Raymond <esr@thyrsus.com>
parents:
15874
diff
changeset
|
508 # |
|
df7bfb34b2a2
Rewrite webbrowser.py to fix various bugs following Ka-Ping Yee's
Eric S. Raymond <esr@thyrsus.com>
parents:
15874
diff
changeset
|
509 # Platform support for Windows |
|
df7bfb34b2a2
Rewrite webbrowser.py to fix various bugs following Ka-Ping Yee's
Eric S. Raymond <esr@thyrsus.com>
parents:
15874
diff
changeset
|
510 # |
|
13476
8f66c5ffa26c
New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
511 |
|
8f66c5ffa26c
New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
512 if sys.platform[:3] == "win": |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
513 class WindowsDefault(BaseBrowser): |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
514 def open(self, url, new=0, autoraise=1): |
|
41207
01951f6b819d
Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents:
39919
diff
changeset
|
515 try: |
|
01951f6b819d
Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents:
39919
diff
changeset
|
516 os.startfile(url) |
|
01951f6b819d
Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents:
39919
diff
changeset
|
517 except WindowsError: |
|
01951f6b819d
Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents:
39919
diff
changeset
|
518 # [Error 22] No application is associated with the specified |
|
01951f6b819d
Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents:
39919
diff
changeset
|
519 # file for this operation: '<URL>' |
|
01951f6b819d
Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents:
39919
diff
changeset
|
520 return False |
|
01951f6b819d
Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents:
39919
diff
changeset
|
521 else: |
|
01951f6b819d
Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents:
39919
diff
changeset
|
522 return True |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
523 |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
524 _tryorder = [] |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
525 _browsers = {} |
|
41207
01951f6b819d
Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents:
39919
diff
changeset
|
526 |
|
01951f6b819d
Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents:
39919
diff
changeset
|
527 # First try to use the default Windows browser |
|
01951f6b819d
Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents:
39919
diff
changeset
|
528 register("windows-default", WindowsDefault) |
|
01951f6b819d
Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents:
39919
diff
changeset
|
529 |
|
01951f6b819d
Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents:
39919
diff
changeset
|
530 # Detect some common Windows browsers, fallback to IE |
|
01951f6b819d
Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents:
39919
diff
changeset
|
531 iexplore = os.path.join(os.environ.get("PROGRAMFILES", "C:\\Program Files"), |
|
01951f6b819d
Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents:
39919
diff
changeset
|
532 "Internet Explorer\\IEXPLORE.EXE") |
|
38103
a06b69f8c276
Add seamonkey to list of Windows browsers too.
Georg Brandl <georg@python.org>
parents:
38027
diff
changeset
|
533 for browser in ("firefox", "firebird", "seamonkey", "mozilla", |
|
41207
01951f6b819d
Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents:
39919
diff
changeset
|
534 "netscape", "opera", iexplore): |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
535 if _iscommand(browser): |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
536 register(browser, None, BackgroundBrowser(browser)) |
|
16153
df7bfb34b2a2
Rewrite webbrowser.py to fix various bugs following Ka-Ping Yee's
Eric S. Raymond <esr@thyrsus.com>
parents:
15874
diff
changeset
|
537 |
|
13476
8f66c5ffa26c
New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
538 # |
|
16153
df7bfb34b2a2
Rewrite webbrowser.py to fix various bugs following Ka-Ping Yee's
Eric S. Raymond <esr@thyrsus.com>
parents:
15874
diff
changeset
|
539 # Platform support for MacOS |
|
df7bfb34b2a2
Rewrite webbrowser.py to fix various bugs following Ka-Ping Yee's
Eric S. Raymond <esr@thyrsus.com>
parents:
15874
diff
changeset
|
540 # |
|
13476
8f66c5ffa26c
New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
541 |
|
8f66c5ffa26c
New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
542 try: |
|
8f66c5ffa26c
New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
543 import ic |
|
8f66c5ffa26c
New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
544 except ImportError: |
|
8f66c5ffa26c
New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
545 pass |
|
8f66c5ffa26c
New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
546 else: |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
547 class InternetConfig(BaseBrowser): |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
548 def open(self, url, new=0, autoraise=1): |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
549 ic.launchurl(url) |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
550 return True # Any way to get status? |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
551 |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
552 register("internet-config", InternetConfig, update_tryorder=-1) |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
553 |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
554 if sys.platform == 'darwin': |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
555 # Adapted from patch submitted to SourceForge by Steven J. Burr |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
556 class MacOSX(BaseBrowser): |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
557 """Launcher class for Aqua browsers on Mac OS X |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
558 |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
559 Optionally specify a browser name on instantiation. Note that this |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
560 will not work for Aqua browsers if the user has moved the application |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
561 package after installation. |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
562 |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
563 If no browser is specified, the default browser, as specified in the |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
564 Internet System Preferences panel, will be used. |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
565 """ |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
566 def __init__(self, name): |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
567 self.name = name |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
568 |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
569 def open(self, url, new=0, autoraise=1): |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
570 assert "'" not in url |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
571 # hack for local urls |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
572 if not ':' in url: |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
573 url = 'file:'+url |
|
36362
ca6c800ad5a0
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
36352
diff
changeset
|
574 |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
575 # new must be 0 or 1 |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
576 new = int(bool(new)) |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
577 if self.name == "default": |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
578 # User called open, open_new or get without a browser parameter |
|
35851
413c68b31487
Bug #1338995: fix problem with new webbrowser.py.
Georg Brandl <georg@python.org>
parents:
35707
diff
changeset
|
579 script = 'open location "%s"' % url.replace('"', '%22') # opens in default browser |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
580 else: |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
581 # User called get and chose a browser |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
582 if self.name == "OmniWeb": |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
583 toWindow = "" |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
584 else: |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
585 # Include toWindow parameter of OpenURL command for browsers |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
586 # that support it. 0 == new window; -1 == existing |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
587 toWindow = "toWindow %d" % (new - 1) |
|
35851
413c68b31487
Bug #1338995: fix problem with new webbrowser.py.
Georg Brandl <georg@python.org>
parents:
35707
diff
changeset
|
588 cmd = 'OpenURL "%s"' % url.replace('"', '%22') |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
589 script = '''tell application "%s" |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
590 activate |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
591 %s %s |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
592 end tell''' % (self.name, cmd, toWindow) |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
593 # Open pipe to AppleScript through osascript command |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
594 osapipe = os.popen("osascript", "w") |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
595 if osapipe is None: |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
596 return False |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
597 # Write script to osascript's stdin |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
598 osapipe.write(script) |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
599 rc = osapipe.close() |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
600 return not rc |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
601 |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
602 # Don't clear _tryorder or _browsers since OS X can use above Unix support |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
603 # (but we prefer using the OS X specific stuff) |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
604 register("MacOSX", None, MacOSX('default'), -1) |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
605 |
|
16153
df7bfb34b2a2
Rewrite webbrowser.py to fix various bugs following Ka-Ping Yee's
Eric S. Raymond <esr@thyrsus.com>
parents:
15874
diff
changeset
|
606 |
|
21047
881f49befca3
Patch #484847: Default to netscape.exe on OS/2.
Martin v. Löwis <martin@v.loewis.de>
parents:
20417
diff
changeset
|
607 # |
|
881f49befca3
Patch #484847: Default to netscape.exe on OS/2.
Martin v. Löwis <martin@v.loewis.de>
parents:
20417
diff
changeset
|
608 # Platform support for OS/2 |
|
881f49befca3
Patch #484847: Default to netscape.exe on OS/2.
Martin v. Löwis <martin@v.loewis.de>
parents:
20417
diff
changeset
|
609 # |
|
881f49befca3
Patch #484847: Default to netscape.exe on OS/2.
Martin v. Löwis <martin@v.loewis.de>
parents:
20417
diff
changeset
|
610 |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
611 if sys.platform[:3] == "os2" and _iscommand("netscape"): |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
612 _tryorder = [] |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
613 _browsers = {} |
|
21047
881f49befca3
Patch #484847: Default to netscape.exe on OS/2.
Martin v. Löwis <martin@v.loewis.de>
parents:
20417
diff
changeset
|
614 register("os2netscape", None, |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
615 GenericBrowser(["start", "netscape", "%s"]), -1) |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
616 |
|
21047
881f49befca3
Patch #484847: Default to netscape.exe on OS/2.
Martin v. Löwis <martin@v.loewis.de>
parents:
20417
diff
changeset
|
617 |
|
16153
df7bfb34b2a2
Rewrite webbrowser.py to fix various bugs following Ka-Ping Yee's
Eric S. Raymond <esr@thyrsus.com>
parents:
15874
diff
changeset
|
618 # OK, now that we know what the default preference orders for each |
|
df7bfb34b2a2
Rewrite webbrowser.py to fix various bugs following Ka-Ping Yee's
Eric S. Raymond <esr@thyrsus.com>
parents:
15874
diff
changeset
|
619 # platform are, allow user to override them with the BROWSER variable. |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
22830
diff
changeset
|
620 if "BROWSER" in os.environ: |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
621 _userchoices = os.environ["BROWSER"].split(os.pathsep) |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
622 _userchoices.reverse() |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
623 |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
624 # Treat choices in same way as if passed into get() but do register |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
625 # and prepend to _tryorder |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
626 for cmdline in _userchoices: |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
627 if cmdline != '': |
|
56830
97d7cd365dd5
Merged revisions 73206,73232,73299,73683,74020,74185,74544,74643,74647,74817,74838-74839,74865,74946,75402,75459,75604,75696 via svnmerge from
Georg Brandl <georg@python.org>
parents:
42137
diff
changeset
|
628 cmd = _synthesize(cmdline, -1) |
|
97d7cd365dd5
Merged revisions 73206,73232,73299,73683,74020,74185,74544,74643,74647,74817,74838-74839,74865,74946,75402,75459,75604,75696 via svnmerge from
Georg Brandl <georg@python.org>
parents:
42137
diff
changeset
|
629 if cmd[1] is None: |
|
97d7cd365dd5
Merged revisions 73206,73232,73299,73683,74020,74185,74544,74643,74647,74817,74838-74839,74865,74946,75402,75459,75604,75696 via svnmerge from
Georg Brandl <georg@python.org>
parents:
42137
diff
changeset
|
630 register(cmdline, None, GenericBrowser(cmdline), -1) |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
631 cmdline = None # to make del work if _userchoices was empty |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
632 del cmdline |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
633 del _userchoices |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
634 |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
635 # what to do if _tryorder is now empty? |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
636 |
|
18714
5c73621c9559
this closes patches item 429136 and brings the release21-maint version into
Skip Montanaro <skip@pobox.com>
parents:
17461
diff
changeset
|
637 |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
638 def main(): |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
639 import getopt |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
640 usage = """Usage: %s [-n | -t] url |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
641 -n: open new window |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
642 -t: open new tab""" % sys.argv[0] |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
643 try: |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
644 opts, args = getopt.getopt(sys.argv[1:], 'ntd') |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
645 except getopt.error, msg: |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
646 print >>sys.stderr, msg |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
647 print >>sys.stderr, usage |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
648 sys.exit(1) |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
649 new_win = 0 |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
650 for o, a in opts: |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
651 if o == '-n': new_win = 1 |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
652 elif o == '-t': new_win = 2 |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
653 if len(args) <> 1: |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
654 print >>sys.stderr, usage |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
655 sys.exit(1) |
|
18714
5c73621c9559
this closes patches item 429136 and brings the release21-maint version into
Skip Montanaro <skip@pobox.com>
parents:
17461
diff
changeset
|
656 |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
657 url = args[0] |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
658 open(url, new_win) |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
659 |
|
36352
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
660 print "\a" |
|
6e12707371a1
Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents:
36121
diff
changeset
|
661 |
|
35685
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
662 if __name__ == "__main__": |
|
3be835350082
Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents:
32605
diff
changeset
|
663 main() |
