annotate Lib/webbrowser.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 0127716200c7
children 5a1429e9b621
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
3c65babf322f Clarify synopsis line a bit.
Ka-Ping Yee <ping@zesty.ca>
parents: 16889
diff changeset
2 """Interfaces for launching and remotely controlling Web browsers."""
42138
908580b9c8e1 Fix a few webbrowser.py problems.
Georg Brandl <georg@python.org>
parents: 41262
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
41262
5b711632cae1 Bug #1684254: webbrowser now uses shlex to split any command lines
Georg Brandl <georg@python.org>
parents: 39919
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
41262
5b711632cae1 Bug #1684254: webbrowser now uses shlex to split any command lines
Georg Brandl <georg@python.org>
parents: 39919
diff changeset
37 browser = shlex.split(browser)
5b711632cae1 Bug #1684254: webbrowser now uses shlex to split any command lines
Georg Brandl <georg@python.org>
parents: 39919
diff changeset
38 if browser[-1] == '&':
5b711632cae1 Bug #1684254: webbrowser now uses shlex to split any command lines
Georg Brandl <georg@python.org>
parents: 39919
diff changeset
39 return BackgroundBrowser(browser[:-1])
5b711632cae1 Bug #1684254: webbrowser now uses shlex to split any command lines
Georg Brandl <georg@python.org>
parents: 39919
diff changeset
40 else:
5b711632cae1 Bug #1684254: webbrowser now uses shlex to split any command lines
Georg Brandl <georg@python.org>
parents: 39919
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
55427
2e947245958b use bools for autoraise
Benjamin Peterson <benjamin@python.org>
parents: 42137
diff changeset
58 def open(url, new=0, autoraise=True):
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
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
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
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
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
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
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
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
126 path = os.environ.get("PATH")
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
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
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
129 for d in path.split(os.pathsep):
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
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
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
134
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
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
55427
2e947245958b use bools for autoraise
Benjamin Peterson <benjamin@python.org>
parents: 42137
diff changeset
147 def open(self, url, new=0, autoraise=True):
35692
b61cdd4f888b Fix pychecker warnings
Neal Norwitz <nnorwitz@gmail.com>
parents: 35685
diff changeset
148 raise NotImplementedError
b61cdd4f888b Fix pychecker warnings
Neal Norwitz <nnorwitz@gmail.com>
parents: 35685
diff changeset
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
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
155
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
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
42138
908580b9c8e1 Fix a few webbrowser.py problems.
Georg Brandl <georg@python.org>
parents: 41262
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
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
170
55427
2e947245958b use bools for autoraise
Benjamin Peterson <benjamin@python.org>
parents: 42137
diff changeset
171 def open(self, url, new=0, autoraise=True):
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
55427
2e947245958b use bools for autoraise
Benjamin Peterson <benjamin@python.org>
parents: 42137
diff changeset
188 def open(self, url, new=0, autoraise=True):
36352
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
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
202
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
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
55427
2e947245958b use bools for autoraise
Benjamin Peterson <benjamin@python.org>
parents: 42137
diff changeset
219 autoraise = int(autoraise)
36352
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:
71445
0127716200c7 #12601: fix typo.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
240 # wait five seconds. If the subprocess is not finished, the
36352
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
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
258
55427
2e947245958b use bools for autoraise
Benjamin Peterson <benjamin@python.org>
parents: 42137
diff changeset
259 def open(self, url, new=0, autoraise=True):
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
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
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
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
282
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
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"]
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
288 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
289 remote_action = ""
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
290 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
291 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
292 background = True
25715
2adadc19923c SF # 585913, Adds Galeon support to webbrowser.py
Neal Norwitz <nnorwitz@gmail.com>
parents: 23628
diff changeset
293
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
294 Netscape = Mozilla
25715
2adadc19923c SF # 585913, Adds Galeon support to webbrowser.py
Neal Norwitz <nnorwitz@gmail.com>
parents: 23628
diff changeset
295
2adadc19923c SF # 585913, Adds Galeon support to webbrowser.py
Neal Norwitz <nnorwitz@gmail.com>
parents: 23628
diff changeset
296
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
297 class Galeon(UnixBrowser):
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
298 """Launcher class for Galeon/Epiphany browsers."""
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
299
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
300 raise_opts = ["-noraise", ""]
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
301 remote_args = ['%action', '%s']
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
302 remote_action = "-n"
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
303 remote_action_newwin = "-w"
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
304 background = True
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
305
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 class Opera(UnixBrowser):
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
308 "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
309
67199
1df2021c7c3c Issue 10738: Fix webbrowser.Opera.raise_opts value.
Terry Reedy <tjreedy@udel.edu>
parents: 62435
diff changeset
310 raise_opts = ["-noraise", ""]
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
311 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
312 remote_action = ""
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
313 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
314 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
315 background = True
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
316
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
317
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
318 class Elinks(UnixBrowser):
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
319 "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
320
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
321 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
322 remote_action = ""
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
323 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
324 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
325 background = False
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
326
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
327 # 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
328 # 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
329 redirect_stdout = False
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
330
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
331
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
332 class Konqueror(BaseBrowser):
18720
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
333 """Controller for the KDE File Manager (kfm, or Konqueror).
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
334
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
335 See the output of ``kfmclient --commands``
18720
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
336 for more information on the Konqueror remote-control interface.
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
337 """
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
338
55427
2e947245958b use bools for autoraise
Benjamin Peterson <benjamin@python.org>
parents: 42137
diff changeset
339 def open(self, url, new=0, autoraise=True):
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
340 # 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
341 if new == 2:
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
342 action = "newTab"
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
343 else:
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
344 action = "openURL"
36362
ca6c800ad5a0 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 36352
diff changeset
345
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
346 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
347 # 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
348 # 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
349 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
350 if not setsid:
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
351 setsid = getattr(os, 'setpgrp', None)
36362
ca6c800ad5a0 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 36352
diff changeset
352
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
353 try:
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
354 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
355 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
356 stdout=devnull, stderr=devnull)
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
357 except OSError:
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
358 # 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
359 pass
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
360 else:
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
361 p.wait()
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
362 # 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
363 return True
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
364
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
365 try:
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
366 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
367 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
368 stdout=devnull, stderr=devnull,
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
369 preexec_fn=setsid)
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
370 except OSError:
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
371 # 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
372 pass
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
373 else:
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
374 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
375 # Should be running now.
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
376 return True
36362
ca6c800ad5a0 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 36352
diff changeset
377
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
378 try:
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
379 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
380 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
381 stdout=devnull, stderr=devnull,
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
382 preexec_fn=setsid)
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
383 except OSError:
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
384 return False
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
385 else:
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
386 return (p.poll() is None)
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
387
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
388
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
389 class Grail(BaseBrowser):
18720
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
390 # There should be a way to maintain a connection to Grail, but the
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
391 # 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
392 # point. It probably never will!
18720
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
393 def _find_grail_rc(self):
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
394 import glob
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
395 import pwd
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
396 import socket
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
397 import tempfile
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
398 tempdir = os.path.join(tempfile.gettempdir(),
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
399 ".grail-unix")
20417
c168eda736e4 _os should be os; reported by Neal Norwitz.
Fred Drake <fdrake@acm.org>
parents: 19208
diff changeset
400 user = pwd.getpwuid(os.getuid())[0]
18720
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
401 filename = os.path.join(tempdir, user + "-*")
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
402 maybes = glob.glob(filename)
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
403 if not maybes:
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
404 return None
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
405 s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
406 for fn in maybes:
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
407 # need to PING each one until we find one that's live
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
408 try:
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
409 s.connect(fn)
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
410 except socket.error:
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
411 # no good; attempt to clean it out, but don't fail:
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
412 try:
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
413 os.unlink(fn)
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
414 except IOError:
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
415 pass
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
416 else:
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
417 return s
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
418
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
419 def _remote(self, action):
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
420 s = self._find_grail_rc()
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
421 if not s:
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
422 return 0
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
423 s.send(action)
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
424 s.close()
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
425 return 1
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
426
55427
2e947245958b use bools for autoraise
Benjamin Peterson <benjamin@python.org>
parents: 42137
diff changeset
427 def open(self, url, new=0, autoraise=True):
18720
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
428 if new:
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
429 ok = self._remote("LOADNEW " + url)
18720
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
430 else:
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
431 ok = self._remote("LOAD " + url)
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
432 return ok
18720
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
433
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
434
16535
084b3dc9b6ff Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 16161
diff changeset
435 #
16153
df7bfb34b2a2 Rewrite webbrowser.py to fix various bugs following Ka-Ping Yee's
Eric S. Raymond <esr@thyrsus.com>
parents: 15874
diff changeset
436 # 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
437 #
13476
8f66c5ffa26c New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff changeset
438
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
439 # 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
440 # 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
441
35692
b61cdd4f888b Fix pychecker warnings
Neal Norwitz <nnorwitz@gmail.com>
parents: 35685
diff changeset
442 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
443
01951f6b819d Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents: 39919
diff changeset
444 # The default GNOME browser
01951f6b819d Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents: 39919
diff changeset
445 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
446 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
447
41207
01951f6b819d Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents: 39919
diff changeset
448 # The default KDE browser
01951f6b819d Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents: 39919
diff changeset
449 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
450 register("kfmclient", Konqueror, Konqueror("kfmclient"))
26404
1b47b576e0d4 Fixed bug
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 25745
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 Mozilla/Netscape browsers
35920
e11323189c84 Prefer GNOME browser over mozilla.
Georg Brandl <georg@python.org>
parents: 35851
diff changeset
453 for browser in ("mozilla-firefox", "firefox",
e11323189c84 Prefer GNOME browser over mozilla.
Georg Brandl <georg@python.org>
parents: 35851
diff changeset
454 "mozilla-firebird", "firebird",
38027
34d696c1eebc Add SeaMonkey to the list of Mozilla browsers.
Georg Brandl <georg@python.org>
parents: 36362
diff changeset
455 "seamonkey", "mozilla", "netscape"):
35920
e11323189c84 Prefer GNOME browser over mozilla.
Georg Brandl <georg@python.org>
parents: 35851
diff changeset
456 if _iscommand(browser):
e11323189c84 Prefer GNOME browser over mozilla.
Georg Brandl <georg@python.org>
parents: 35851
diff changeset
457 register(browser, None, Mozilla(browser))
e11323189c84 Prefer GNOME browser over mozilla.
Georg Brandl <georg@python.org>
parents: 35851
diff changeset
458
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
459 # 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
460 if _iscommand("kfm"):
5685ec59f35b Fix errors in _synthesize because of missing basename attribute of
Georg Brandl <georg@python.org>
parents: 35692
diff changeset
461 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
462 elif _iscommand("konqueror"):
5685ec59f35b Fix errors in _synthesize because of missing basename attribute of
Georg Brandl <georg@python.org>
parents: 35692
diff changeset
463 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
464
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
465 # Gnome's Galeon and Epiphany
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
466 for browser in ("galeon", "epiphany"):
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
467 if _iscommand(browser):
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
468 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
469
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
470 # Skipstone, another Gtk/Mozilla based browser
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
471 if _iscommand("skipstone"):
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
472 register("skipstone", None, BackgroundBrowser("skipstone"))
25715
2adadc19923c SF # 585913, Adds Galeon support to webbrowser.py
Neal Norwitz <nnorwitz@gmail.com>
parents: 23628
diff changeset
473
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
474 # Opera, quite popular
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
475 if _iscommand("opera"):
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
476 register("opera", None, Opera("opera"))
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
477
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
478 # Next, Mosaic -- old but still in use.
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
479 if _iscommand("mosaic"):
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
480 register("mosaic", None, BackgroundBrowser("mosaic"))
26404
1b47b576e0d4 Fixed bug
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 25745
diff changeset
481
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
482 # 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
483 if _iscommand("grail"):
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
484 register("grail", Grail, None)
18720
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
485
35692
b61cdd4f888b Fix pychecker warnings
Neal Norwitz <nnorwitz@gmail.com>
parents: 35685
diff changeset
486 # Prefer X browsers if present
b61cdd4f888b Fix pychecker warnings
Neal Norwitz <nnorwitz@gmail.com>
parents: 35685
diff changeset
487 if os.environ.get("DISPLAY"):
b61cdd4f888b Fix pychecker warnings
Neal Norwitz <nnorwitz@gmail.com>
parents: 35685
diff changeset
488 register_X_browsers()
b61cdd4f888b Fix pychecker warnings
Neal Norwitz <nnorwitz@gmail.com>
parents: 35685
diff changeset
489
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
490 # Also try console browsers
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
491 if os.environ.get("TERM"):
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
492 # 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
493 if _iscommand("links"):
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
494 register("links", None, GenericBrowser("links"))
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
495 if _iscommand("elinks"):
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
496 register("elinks", None, Elinks("elinks"))
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
497 # 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
498 if _iscommand("lynx"):
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
499 register("lynx", None, GenericBrowser("lynx"))
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
500 # The w3m browser <http://w3m.sourceforge.net/>
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
501 if _iscommand("w3m"):
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
502 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
503
16153
df7bfb34b2a2 Rewrite webbrowser.py to fix various bugs following Ka-Ping Yee's
Eric S. Raymond <esr@thyrsus.com>
parents: 15874
diff changeset
504 #
df7bfb34b2a2 Rewrite webbrowser.py to fix various bugs following Ka-Ping Yee's
Eric S. Raymond <esr@thyrsus.com>
parents: 15874
diff changeset
505 # 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
506 #
13476
8f66c5ffa26c New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff changeset
507
8f66c5ffa26c New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff changeset
508 if sys.platform[:3] == "win":
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
509 class WindowsDefault(BaseBrowser):
55427
2e947245958b use bools for autoraise
Benjamin Peterson <benjamin@python.org>
parents: 42137
diff changeset
510 def open(self, url, new=0, autoraise=True):
41207
01951f6b819d Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents: 39919
diff changeset
511 try:
01951f6b819d Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents: 39919
diff changeset
512 os.startfile(url)
01951f6b819d Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents: 39919
diff changeset
513 except WindowsError:
01951f6b819d Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents: 39919
diff changeset
514 # [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
515 # 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
516 return False
01951f6b819d Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents: 39919
diff changeset
517 else:
01951f6b819d Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents: 39919
diff changeset
518 return True
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
519
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
520 _tryorder = []
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
521 _browsers = {}
41207
01951f6b819d Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents: 39919
diff changeset
522
01951f6b819d Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents: 39919
diff changeset
523 # 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
524 register("windows-default", WindowsDefault)
01951f6b819d Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents: 39919
diff changeset
525
01951f6b819d Bug #1681228: the webbrowser module now correctly uses the default
Georg Brandl <georg@python.org>
parents: 39919
diff changeset
526 # 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
527 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
528 "Internet Explorer\\IEXPLORE.EXE")
38103
a06b69f8c276 Add seamonkey to list of Windows browsers too.
Georg Brandl <georg@python.org>
parents: 38027
diff changeset
529 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
530 "netscape", "opera", iexplore):
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
531 if _iscommand(browser):
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
532 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
533
13476
8f66c5ffa26c New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff changeset
534 #
16153
df7bfb34b2a2 Rewrite webbrowser.py to fix various bugs following Ka-Ping Yee's
Eric S. Raymond <esr@thyrsus.com>
parents: 15874
diff changeset
535 # 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
536 #
13476
8f66c5ffa26c New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff changeset
537
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
538 if sys.platform == 'darwin':
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
539 # 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
540 class MacOSX(BaseBrowser):
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
541 """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
542
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
543 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
544 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
545 package after installation.
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
546
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
547 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
548 Internet System Preferences panel, will be used.
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
549 """
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
550 def __init__(self, name):
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
551 self.name = name
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
552
55427
2e947245958b use bools for autoraise
Benjamin Peterson <benjamin@python.org>
parents: 42137
diff changeset
553 def open(self, url, new=0, autoraise=True):
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
554 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
555 # hack for local urls
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
556 if not ':' in url:
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
557 url = 'file:'+url
36362
ca6c800ad5a0 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 36352
diff changeset
558
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
559 # new must be 0 or 1
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
560 new = int(bool(new))
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
561 if self.name == "default":
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
562 # 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
563 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
564 else:
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
565 # User called get and chose a browser
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
566 if self.name == "OmniWeb":
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
567 toWindow = ""
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
568 else:
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
569 # Include toWindow parameter of OpenURL command for browsers
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
570 # that support it. 0 == new window; -1 == existing
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
571 toWindow = "toWindow %d" % (new - 1)
35851
413c68b31487 Bug #1338995: fix problem with new webbrowser.py.
Georg Brandl <georg@python.org>
parents: 35707
diff changeset
572 cmd = 'OpenURL "%s"' % url.replace('"', '%22')
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
573 script = '''tell application "%s"
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
574 activate
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
575 %s %s
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
576 end tell''' % (self.name, cmd, toWindow)
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
577 # Open pipe to AppleScript through osascript command
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
578 osapipe = os.popen("osascript", "w")
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
579 if osapipe is None:
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
580 return False
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
581 # Write script to osascript's stdin
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
582 osapipe.write(script)
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
583 rc = osapipe.close()
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
584 return not rc
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
585
61158
21369d189d9e For for issue #7192: with this patch webbrowser.get("firefox")
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58159
diff changeset
586 class MacOSXOSAScript(BaseBrowser):
21369d189d9e For for issue #7192: with this patch webbrowser.get("firefox")
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58159
diff changeset
587 def __init__(self, name):
21369d189d9e For for issue #7192: with this patch webbrowser.get("firefox")
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58159
diff changeset
588 self._name = name
21369d189d9e For for issue #7192: with this patch webbrowser.get("firefox")
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58159
diff changeset
589
21369d189d9e For for issue #7192: with this patch webbrowser.get("firefox")
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58159
diff changeset
590 def open(self, url, new=0, autoraise=True):
21369d189d9e For for issue #7192: with this patch webbrowser.get("firefox")
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58159
diff changeset
591 if self._name == 'default':
21369d189d9e For for issue #7192: with this patch webbrowser.get("firefox")
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58159
diff changeset
592 script = 'open location "%s"' % url.replace('"', '%22') # opens in default browser
21369d189d9e For for issue #7192: with this patch webbrowser.get("firefox")
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58159
diff changeset
593 else:
21369d189d9e For for issue #7192: with this patch webbrowser.get("firefox")
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58159
diff changeset
594 script = '''
21369d189d9e For for issue #7192: with this patch webbrowser.get("firefox")
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58159
diff changeset
595 tell application "%s"
21369d189d9e For for issue #7192: with this patch webbrowser.get("firefox")
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58159
diff changeset
596 activate
21369d189d9e For for issue #7192: with this patch webbrowser.get("firefox")
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58159
diff changeset
597 open location "%s"
21369d189d9e For for issue #7192: with this patch webbrowser.get("firefox")
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58159
diff changeset
598 end
21369d189d9e For for issue #7192: with this patch webbrowser.get("firefox")
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58159
diff changeset
599 '''%(self._name, url.replace('"', '%22'))
21369d189d9e For for issue #7192: with this patch webbrowser.get("firefox")
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58159
diff changeset
600
21369d189d9e For for issue #7192: with this patch webbrowser.get("firefox")
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58159
diff changeset
601 osapipe = os.popen("osascript", "w")
21369d189d9e For for issue #7192: with this patch webbrowser.get("firefox")
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58159
diff changeset
602 if osapipe is None:
21369d189d9e For for issue #7192: with this patch webbrowser.get("firefox")
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58159
diff changeset
603 return False
21369d189d9e For for issue #7192: with this patch webbrowser.get("firefox")
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58159
diff changeset
604
21369d189d9e For for issue #7192: with this patch webbrowser.get("firefox")
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58159
diff changeset
605 osapipe.write(script)
21369d189d9e For for issue #7192: with this patch webbrowser.get("firefox")
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58159
diff changeset
606 rc = osapipe.close()
21369d189d9e For for issue #7192: with this patch webbrowser.get("firefox")
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58159
diff changeset
607 return not rc
21369d189d9e For for issue #7192: with this patch webbrowser.get("firefox")
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58159
diff changeset
608
21369d189d9e For for issue #7192: with this patch webbrowser.get("firefox")
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58159
diff changeset
609
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
610 # 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
611 # (but we prefer using the OS X specific stuff)
61158
21369d189d9e For for issue #7192: with this patch webbrowser.get("firefox")
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58159
diff changeset
612 register("safari", None, MacOSXOSAScript('safari'), -1)
21369d189d9e For for issue #7192: with this patch webbrowser.get("firefox")
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58159
diff changeset
613 register("firefox", None, MacOSXOSAScript('firefox'), -1)
61160
d95df2622f9f Small update to r80698 to ensure that webbrowser.open
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 61158
diff changeset
614 register("MacOSX", None, MacOSXOSAScript('default'), -1)
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
615
16153
df7bfb34b2a2 Rewrite webbrowser.py to fix various bugs following Ka-Ping Yee's
Eric S. Raymond <esr@thyrsus.com>
parents: 15874
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 #
881f49befca3 Patch #484847: Default to netscape.exe on OS/2.
Martin v. Löwis <martin@v.loewis.de>
parents: 20417
diff changeset
618 # 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
619 #
881f49befca3 Patch #484847: Default to netscape.exe on OS/2.
Martin v. Löwis <martin@v.loewis.de>
parents: 20417
diff changeset
620
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
621 if sys.platform[:3] == "os2" and _iscommand("netscape"):
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
622 _tryorder = []
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
623 _browsers = {}
21047
881f49befca3 Patch #484847: Default to netscape.exe on OS/2.
Martin v. Löwis <martin@v.loewis.de>
parents: 20417
diff changeset
624 register("os2netscape", None,
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
625 GenericBrowser(["start", "netscape", "%s"]), -1)
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
626
21047
881f49befca3 Patch #484847: Default to netscape.exe on OS/2.
Martin v. Löwis <martin@v.loewis.de>
parents: 20417
diff changeset
627
16153
df7bfb34b2a2 Rewrite webbrowser.py to fix various bugs following Ka-Ping Yee's
Eric S. Raymond <esr@thyrsus.com>
parents: 15874
diff changeset
628 # 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
629 # 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
630 if "BROWSER" in os.environ:
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
631 _userchoices = os.environ["BROWSER"].split(os.pathsep)
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
632 _userchoices.reverse()
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
633
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
634 # 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
635 # and prepend to _tryorder
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
636 for cmdline in _userchoices:
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
637 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
638 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
639 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
640 register(cmdline, None, GenericBrowser(cmdline), -1)
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
641 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
642 del cmdline
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
643 del _userchoices
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
644
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
645 # what to do if _tryorder is now empty?
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
646
18714
5c73621c9559 this closes patches item 429136 and brings the release21-maint version into
Skip Montanaro <skip@pobox.com>
parents: 17461
diff changeset
647
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
648 def main():
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
649 import getopt
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
650 usage = """Usage: %s [-n | -t] url
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
651 -n: open new window
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
652 -t: open new tab""" % sys.argv[0]
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
653 try:
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
654 opts, args = getopt.getopt(sys.argv[1:], 'ntd')
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
655 except getopt.error, msg:
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
656 print >>sys.stderr, msg
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
657 print >>sys.stderr, usage
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
658 sys.exit(1)
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
659 new_win = 0
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
660 for o, a in opts:
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
661 if o == '-n': new_win = 1
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
662 elif o == '-t': new_win = 2
63528
751c6dd71b5e Merged revisions 77310-77311 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56830
diff changeset
663 if len(args) != 1:
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
664 print >>sys.stderr, usage
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
665 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
666
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
667 url = args[0]
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
668 open(url, new_win)
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
669
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
670 print "\a"
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
671
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
672 if __name__ == "__main__":
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
673 main()