annotate Lib/webbrowser.py @ 78069:a8aa77458e12 3.2

Add c_size_t and c_ssize_t to the ctypes summary table.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 12 Jul 2012 20:31:50 +0200
parents 3bc80b6f7cd8
children 6768aa70c2d3 7faa90a6324c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
59522
fcfe324663a6 convert shebang lines: python -> python3
Benjamin Peterson <benjamin@python.org>
parents: 55913
diff changeset
1 #! /usr/bin/env python3
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."""
42193
090588a17490 Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
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
50540
4229a4b14f1d Merged revisions 67653,67655,67682-67683,67724,67755,67780-67783,67786,67789,67841,67843,67865,67881,67884,67925,67929,67931 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50037
diff changeset
5 import io
13476
8f66c5ffa26c New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff changeset
6 import os
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
7 import shlex
13476
8f66c5ffa26c New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff changeset
8 import sys
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
9 import stat
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
10 import subprocess
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
11 import time
13476
8f66c5ffa26c New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff changeset
12
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
13 __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
14
13476
8f66c5ffa26c New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff changeset
15 class Error(Exception):
8f66c5ffa26c New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff changeset
16 pass
8f66c5ffa26c New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff changeset
17
16535
084b3dc9b6ff Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 16161
diff changeset
18 _browsers = {} # Dictionary of available browser controllers
084b3dc9b6ff Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 16161
diff changeset
19 _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
20
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
21 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
22 """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
23 _browsers[name.lower()] = [klass, instance]
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
24 if update_tryorder > 0:
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
25 _tryorder.append(name)
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
26 elif update_tryorder < 0:
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
27 _tryorder.insert(0, name)
13476
8f66c5ffa26c New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff changeset
28
16153
df7bfb34b2a2 Rewrite webbrowser.py to fix various bugs following Ka-Ping Yee's
Eric S. Raymond <esr@thyrsus.com>
parents: 15874
diff changeset
29 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
30 """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
31 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
32 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
33 else:
df7bfb34b2a2 Rewrite webbrowser.py to fix various bugs following Ka-Ping Yee's
Eric S. Raymond <esr@thyrsus.com>
parents: 15874
diff changeset
34 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
35 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
36 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
37 # User gave us a command line, split it into name and args
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
38 browser = shlex.split(browser)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
39 if browser[-1] == '&':
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
40 return BackgroundBrowser(browser[:-1])
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
41 else:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
42 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
43 else:
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
44 # 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
45 try:
a004c62e696e _synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents: 17270
diff changeset
46 command = _browsers[browser.lower()]
a004c62e696e _synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents: 17270
diff changeset
47 except KeyError:
a004c62e696e _synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents: 17270
diff changeset
48 command = _synthesize(browser)
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
49 if command[1] is not None:
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
50 return command[1]
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
51 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
52 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
53 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
54
8f66c5ffa26c New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff changeset
55 # 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
56 # 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
57 # 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
58
55584
5fbac09c0e91 Merged revisions 74264 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50540
diff changeset
59 def open(url, new=0, autoraise=True):
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
60 for name in _tryorder:
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
61 browser = get(name)
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
62 if browser.open(url, new, autoraise):
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
63 return True
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
64 return False
13476
8f66c5ffa26c New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff changeset
65
18720
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
66 def open_new(url):
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
67 return open(url, 1)
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
68
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
69 def open_new_tab(url):
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
70 return open(url, 2)
13476
8f66c5ffa26c New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff changeset
71
17461
a004c62e696e _synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents: 17270
diff changeset
72
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
73 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
74 """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
75
a004c62e696e _synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents: 17270
diff changeset
76 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
77 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
78 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
79 browser in this way.
a004c62e696e _synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents: 17270
diff changeset
80
a004c62e696e _synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents: 17270
diff changeset
81 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
82 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
83
a004c62e696e _synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents: 17270
diff changeset
84 """
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
85 cmd = browser.split()[0]
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
86 if not _iscommand(cmd):
17461
a004c62e696e _synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents: 17270
diff changeset
87 return [None, None]
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
88 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
89 try:
a004c62e696e _synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents: 17270
diff changeset
90 command = _browsers[name.lower()]
a004c62e696e _synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents: 17270
diff changeset
91 except KeyError:
a004c62e696e _synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents: 17270
diff changeset
92 return [None, None]
a004c62e696e _synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents: 17270
diff changeset
93 # 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
94 controller = command[1]
a004c62e696e _synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents: 17270
diff changeset
95 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
96 import copy
a004c62e696e _synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents: 17270
diff changeset
97 controller = copy.copy(controller)
a004c62e696e _synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents: 17270
diff changeset
98 controller.name = browser
a004c62e696e _synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents: 17270
diff changeset
99 controller.basename = os.path.basename(browser)
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
100 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
101 return [None, controller]
19208
7b53fb3ba011 Fix malformed line (PyChecker)
Andrew M. Kuchling <amk@amk.ca>
parents: 18720
diff changeset
102 return [None, None]
17461
a004c62e696e _synthesize(): Helper function: when the users passes a specific
Fred Drake <fdrake@acm.org>
parents: 17270
diff changeset
103
18720
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
104
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
105 if sys.platform[:3] == "win":
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
106 def _isexecutable(cmd):
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
107 cmd = cmd.lower()
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
108 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
109 return True
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
110 for ext in ".exe", ".bat":
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
111 if os.path.isfile(cmd + ext):
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
112 return True
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
113 return False
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
114 else:
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
115 def _isexecutable(cmd):
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
116 if os.path.isfile(cmd):
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
117 mode = os.stat(cmd)[stat.ST_MODE]
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
118 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
119 return True
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
120 return False
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
121
18720
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
122 def _iscommand(cmd):
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
123 """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
124 search path."""
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
125 if _isexecutable(cmd):
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
126 return True
18720
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
127 path = os.environ.get("PATH")
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
128 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
129 return False
18720
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
130 for d in path.split(os.pathsep):
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
131 exe = os.path.join(d, cmd)
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
132 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
133 return True
a9dd5e6d6805 Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents: 22303
diff changeset
134 return False
18720
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
135
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
136
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
137 # General parent classes
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
138
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
139 class BaseBrowser(object):
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
140 """Parent class for all browsers. Do not use directly."""
36362
ca6c800ad5a0 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 36352
diff changeset
141
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
142 args = ['%s']
36362
ca6c800ad5a0 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 36352
diff changeset
143
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
144 def __init__(self, name=""):
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
145 self.name = name
35707
5685ec59f35b Fix errors in _synthesize because of missing basename attribute of
Georg Brandl <georg@python.org>
parents: 35692
diff changeset
146 self.basename = name
36121
686306a67e9b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 35920
diff changeset
147
55584
5fbac09c0e91 Merged revisions 74264 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50540
diff changeset
148 def open(self, url, new=0, autoraise=True):
35692
b61cdd4f888b Fix pychecker warnings
Neal Norwitz <nnorwitz@gmail.com>
parents: 35685
diff changeset
149 raise NotImplementedError
b61cdd4f888b Fix pychecker warnings
Neal Norwitz <nnorwitz@gmail.com>
parents: 35685
diff changeset
150
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
151 def open_new(self, url):
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
152 return self.open(url, 1)
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
153
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
154 def open_new_tab(self, url):
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
155 return self.open(url, 2)
18720
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
156
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
157
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
158 class GenericBrowser(BaseBrowser):
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
159 """Class for all browsers started with a command
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
160 and without remote functionality."""
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
161
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
162 def __init__(self, name):
43577
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 42193
diff changeset
163 if isinstance(name, str):
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
164 self.name = name
42193
090588a17490 Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
165 self.args = ["%s"]
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
166 else:
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
167 # 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
168 self.name = name[0]
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
169 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
170 self.basename = os.path.basename(self.name)
18720
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
171
55584
5fbac09c0e91 Merged revisions 74264 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50540
diff changeset
172 def open(self, url, new=0, autoraise=True):
36362
ca6c800ad5a0 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 36352
diff changeset
173 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
174 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
175 try:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39648
diff changeset
176 if sys.platform[:3] == 'win':
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39648
diff changeset
177 p = subprocess.Popen(cmdline)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39648
diff changeset
178 else:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39648
diff changeset
179 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
180 return not p.wait()
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
181 except OSError:
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
182 return False
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
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
185 class BackgroundBrowser(GenericBrowser):
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
186 """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
187 background."""
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
188
55584
5fbac09c0e91 Merged revisions 74264 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50540
diff changeset
189 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
190 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
191 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
192 try:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39648
diff changeset
193 if sys.platform[:3] == 'win':
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39648
diff changeset
194 p = subprocess.Popen(cmdline)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39648
diff changeset
195 else:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39648
diff changeset
196 setsid = getattr(os, 'setsid', None)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39648
diff changeset
197 if not setsid:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39648
diff changeset
198 setsid = getattr(os, 'setpgrp', None)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39648
diff changeset
199 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
200 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
201 except OSError:
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
202 return False
18720
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
203
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
204
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
205 class UnixBrowser(BaseBrowser):
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
206 """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
207
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
208 raise_opts = None
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
209 remote_args = ['%action', '%s']
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
210 remote_action = None
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
211 remote_action_newwin = None
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
212 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
213 background = False
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
214 redirect_stdout = True
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
215
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
216 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
217 raise_opt = []
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
218 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
219 # use autoraise argument only for remote invocation
55584
5fbac09c0e91 Merged revisions 74264 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50540
diff changeset
220 autoraise = int(autoraise)
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
221 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
222 if opt: raise_opt = [opt]
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
223
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
224 cmdline = [self.name] + raise_opt + args
36362
ca6c800ad5a0 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 36352
diff changeset
225
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
226 if remote or self.background:
49981
27ba09957751 Merged revisions 67539 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46946
diff changeset
227 inout = io.open(os.devnull, "r+")
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
228 else:
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
229 # 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
230 inout = None
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
231 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
232 stdout=(self.redirect_stdout and inout or None),
68522
f3ae657067ef merge d71476b9a55d from tip, use start_new_session instead of os.setsid.
Gregory P. Smith <greg@krypto.org>
parents: 68257
diff changeset
233 stderr=inout, start_new_session=True)
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
234 if remote:
71443
3bc80b6f7cd8 #12601: fix typo.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68522
diff changeset
235 # 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
236 # 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
237 time.sleep(1)
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
238 rc = p.poll()
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
239 if rc is None:
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
240 time.sleep(4)
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
241 rc = p.poll()
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
242 if rc is None:
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
243 return True
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
244 # 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
245 return not rc
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
246 elif self.background:
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
247 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
248 return True
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
249 else:
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
250 return False
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
251 else:
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
252 return not p.wait()
18720
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
253
55584
5fbac09c0e91 Merged revisions 74264 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50540
diff changeset
254 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
255 if new == 0:
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
256 action = self.remote_action
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
257 elif new == 1:
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
258 action = self.remote_action_newwin
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
259 elif new == 2:
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
260 if self.remote_action_newtab is None:
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
261 action = self.remote_action_newwin
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
262 else:
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
263 action = self.remote_action_newtab
18720
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
264 else:
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
265 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
266 "expected 0, 1, or 2, got %s" % new)
36362
ca6c800ad5a0 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 36352
diff changeset
267
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
268 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
269 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
270 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
271 if not success:
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
272 # 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
273 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
274 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
275 else:
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
276 return True
18720
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
277
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
278
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
279 class Mozilla(UnixBrowser):
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
280 """Launcher class for Mozilla/Netscape browsers."""
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
281
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
282 raise_opts = ["-noraise", "-raise"]
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
283 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
284 remote_action = ""
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
285 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
286 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
287 background = True
25715
2adadc19923c SF # 585913, Adds Galeon support to webbrowser.py
Neal Norwitz <nnorwitz@gmail.com>
parents: 23628
diff changeset
288
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
289 Netscape = Mozilla
25715
2adadc19923c SF # 585913, Adds Galeon support to webbrowser.py
Neal Norwitz <nnorwitz@gmail.com>
parents: 23628
diff changeset
290
2adadc19923c SF # 585913, Adds Galeon support to webbrowser.py
Neal Norwitz <nnorwitz@gmail.com>
parents: 23628
diff changeset
291
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
292 class Galeon(UnixBrowser):
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
293 """Launcher class for Galeon/Epiphany browsers."""
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
294
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
295 raise_opts = ["-noraise", ""]
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
296 remote_args = ['%action', '%s']
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
297 remote_action = "-n"
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
298 remote_action_newwin = "-w"
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
299 background = True
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
300
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
301
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
302 class Opera(UnixBrowser):
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
303 "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
304
67198
5bbc1dc459a5 Issue 10738: Fix webbrowser.Opera.raise_opts value.
Terry Reedy <tjreedy@udel.edu>
parents: 55913
diff changeset
305 raise_opts = ["-noraise", ""]
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
306 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
307 remote_action = ""
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
308 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
309 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
310 background = True
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
311
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
312
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
313 class Elinks(UnixBrowser):
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
314 "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
315
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
316 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
317 remote_action = ""
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
318 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
319 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
320 background = False
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
321
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
322 # 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
323 # 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
324 redirect_stdout = False
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
325
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
326
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
327 class Konqueror(BaseBrowser):
18720
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
328 """Controller for the KDE File Manager (kfm, or Konqueror).
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
329
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
330 See the output of ``kfmclient --commands``
18720
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
331 for more information on the Konqueror remote-control interface.
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
332 """
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
333
55584
5fbac09c0e91 Merged revisions 74264 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50540
diff changeset
334 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
335 # 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
336 if new == 2:
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
337 action = "newTab"
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
338 else:
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
339 action = "openURL"
36362
ca6c800ad5a0 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 36352
diff changeset
340
46946
fd23e6d177e9 #Issue2835: Correct an infinite recursion in webbrowser.open(), when KDE Konqueror is tried.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43577
diff changeset
341 devnull = io.open(os.devnull, "r+")
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
342 # 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
343 # 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
344 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
345 if not setsid:
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
346 setsid = getattr(os, 'setpgrp', None)
36362
ca6c800ad5a0 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 36352
diff changeset
347
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
348 try:
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
349 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
350 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
351 stdout=devnull, stderr=devnull)
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
352 except OSError:
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
353 # 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
354 pass
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
355 else:
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
356 p.wait()
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
357 # 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
358 return True
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
359
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
360 try:
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
361 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
362 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
363 stdout=devnull, stderr=devnull,
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
364 preexec_fn=setsid)
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
365 except OSError:
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
366 # 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
367 pass
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
368 else:
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
369 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
370 # Should be running now.
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
371 return True
36362
ca6c800ad5a0 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 36352
diff changeset
372
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
373 try:
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
374 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
375 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
376 stdout=devnull, stderr=devnull,
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
377 preexec_fn=setsid)
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
378 except OSError:
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
379 return False
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
380 else:
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
381 return (p.poll() is None)
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
382
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
383
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
384 class Grail(BaseBrowser):
18720
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
385 # 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
386 # 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
387 # point. It probably never will!
18720
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
388 def _find_grail_rc(self):
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
389 import glob
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
390 import pwd
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
391 import socket
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
392 import tempfile
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
393 tempdir = os.path.join(tempfile.gettempdir(),
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
394 ".grail-unix")
20417
c168eda736e4 _os should be os; reported by Neal Norwitz.
Fred Drake <fdrake@acm.org>
parents: 19208
diff changeset
395 user = pwd.getpwuid(os.getuid())[0]
18720
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
396 filename = os.path.join(tempdir, user + "-*")
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
397 maybes = glob.glob(filename)
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
398 if not maybes:
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
399 return None
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
400 s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
401 for fn in maybes:
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
402 # 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
403 try:
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
404 s.connect(fn)
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
405 except socket.error:
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
406 # 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
407 try:
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
408 os.unlink(fn)
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
409 except IOError:
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
410 pass
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
411 else:
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
412 return s
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
413
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
414 def _remote(self, action):
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
415 s = self._find_grail_rc()
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
416 if not s:
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
417 return 0
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
418 s.send(action)
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
419 s.close()
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
420 return 1
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
421
55584
5fbac09c0e91 Merged revisions 74264 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50540
diff changeset
422 def open(self, url, new=0, autoraise=True):
18720
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
423 if new:
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
424 ok = self._remote("LOADNEW " + url)
18720
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
425 else:
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
426 ok = self._remote("LOAD " + url)
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
427 return ok
18720
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
428
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
429
16535
084b3dc9b6ff Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 16161
diff changeset
430 #
16153
df7bfb34b2a2 Rewrite webbrowser.py to fix various bugs following Ka-Ping Yee's
Eric S. Raymond <esr@thyrsus.com>
parents: 15874
diff changeset
431 # 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
432 #
13476
8f66c5ffa26c New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff changeset
433
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
434 # 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
435 # 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
436
35692
b61cdd4f888b Fix pychecker warnings
Neal Norwitz <nnorwitz@gmail.com>
parents: 35685
diff changeset
437 def register_X_browsers():
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
438
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
439 # The default GNOME browser
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
440 if "GNOME_DESKTOP_SESSION_ID" in os.environ and _iscommand("gnome-open"):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
441 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
442
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
443 # The default KDE browser
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
444 if "KDE_FULL_SESSION" in os.environ and _iscommand("kfmclient"):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
445 register("kfmclient", Konqueror, Konqueror("kfmclient"))
26404
1b47b576e0d4 Fixed bug
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 25745
diff changeset
446
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
447 # The Mozilla/Netscape browsers
35920
e11323189c84 Prefer GNOME browser over mozilla.
Georg Brandl <georg@python.org>
parents: 35851
diff changeset
448 for browser in ("mozilla-firefox", "firefox",
e11323189c84 Prefer GNOME browser over mozilla.
Georg Brandl <georg@python.org>
parents: 35851
diff changeset
449 "mozilla-firebird", "firebird",
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 36362
diff changeset
450 "seamonkey", "mozilla", "netscape"):
35920
e11323189c84 Prefer GNOME browser over mozilla.
Georg Brandl <georg@python.org>
parents: 35851
diff changeset
451 if _iscommand(browser):
e11323189c84 Prefer GNOME browser over mozilla.
Georg Brandl <georg@python.org>
parents: 35851
diff changeset
452 register(browser, None, Mozilla(browser))
e11323189c84 Prefer GNOME browser over mozilla.
Georg Brandl <georg@python.org>
parents: 35851
diff changeset
453
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
454 # 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
455 if _iscommand("kfm"):
5685ec59f35b Fix errors in _synthesize because of missing basename attribute of
Georg Brandl <georg@python.org>
parents: 35692
diff changeset
456 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
457 elif _iscommand("konqueror"):
5685ec59f35b Fix errors in _synthesize because of missing basename attribute of
Georg Brandl <georg@python.org>
parents: 35692
diff changeset
458 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
459
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
460 # Gnome's Galeon and Epiphany
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
461 for browser in ("galeon", "epiphany"):
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
462 if _iscommand(browser):
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
463 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
464
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
465 # Skipstone, another Gtk/Mozilla based browser
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
466 if _iscommand("skipstone"):
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
467 register("skipstone", None, BackgroundBrowser("skipstone"))
25715
2adadc19923c SF # 585913, Adds Galeon support to webbrowser.py
Neal Norwitz <nnorwitz@gmail.com>
parents: 23628
diff changeset
468
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
469 # Opera, quite popular
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
470 if _iscommand("opera"):
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
471 register("opera", None, Opera("opera"))
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
472
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
473 # Next, Mosaic -- old but still in use.
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
474 if _iscommand("mosaic"):
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
475 register("mosaic", None, BackgroundBrowser("mosaic"))
26404
1b47b576e0d4 Fixed bug
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 25745
diff changeset
476
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
477 # 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
478 if _iscommand("grail"):
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
479 register("grail", Grail, None)
18720
baaac94fec54 Remove some false comments.
Fred Drake <fdrake@acm.org>
parents: 18714
diff changeset
480
35692
b61cdd4f888b Fix pychecker warnings
Neal Norwitz <nnorwitz@gmail.com>
parents: 35685
diff changeset
481 # Prefer X browsers if present
b61cdd4f888b Fix pychecker warnings
Neal Norwitz <nnorwitz@gmail.com>
parents: 35685
diff changeset
482 if os.environ.get("DISPLAY"):
b61cdd4f888b Fix pychecker warnings
Neal Norwitz <nnorwitz@gmail.com>
parents: 35685
diff changeset
483 register_X_browsers()
b61cdd4f888b Fix pychecker warnings
Neal Norwitz <nnorwitz@gmail.com>
parents: 35685
diff changeset
484
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
485 # Also try console browsers
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
486 if os.environ.get("TERM"):
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
487 # 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
488 if _iscommand("links"):
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
489 register("links", None, GenericBrowser("links"))
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
490 if _iscommand("elinks"):
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
491 register("elinks", None, Elinks("elinks"))
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
492 # 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
493 if _iscommand("lynx"):
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
494 register("lynx", None, GenericBrowser("lynx"))
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
495 # The w3m browser <http://w3m.sourceforge.net/>
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
496 if _iscommand("w3m"):
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
497 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
498
16153
df7bfb34b2a2 Rewrite webbrowser.py to fix various bugs following Ka-Ping Yee's
Eric S. Raymond <esr@thyrsus.com>
parents: 15874
diff changeset
499 #
df7bfb34b2a2 Rewrite webbrowser.py to fix various bugs following Ka-Ping Yee's
Eric S. Raymond <esr@thyrsus.com>
parents: 15874
diff changeset
500 # 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
501 #
13476
8f66c5ffa26c New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff changeset
502
8f66c5ffa26c New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff changeset
503 if sys.platform[:3] == "win":
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
504 class WindowsDefault(BaseBrowser):
55584
5fbac09c0e91 Merged revisions 74264 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50540
diff changeset
505 def open(self, url, new=0, autoraise=True):
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
506 try:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
507 os.startfile(url)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
508 except WindowsError:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
509 # [Error 22] No application is associated with the specified
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
510 # file for this operation: '<URL>'
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
511 return False
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
512 else:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
513 return True
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
514
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
515 _tryorder = []
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
516 _browsers = {}
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
517
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
518 # First try to use the default Windows browser
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
519 register("windows-default", WindowsDefault)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
520
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
521 # Detect some common Windows browsers, fallback to IE
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
522 iexplore = os.path.join(os.environ.get("PROGRAMFILES", "C:\\Program Files"),
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
523 "Internet Explorer\\IEXPLORE.EXE")
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 36362
diff changeset
524 for browser in ("firefox", "firebird", "seamonkey", "mozilla",
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
525 "netscape", "opera", iexplore):
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
526 if _iscommand(browser):
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
527 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
528
13476
8f66c5ffa26c New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff changeset
529 #
16153
df7bfb34b2a2 Rewrite webbrowser.py to fix various bugs following Ka-Ping Yee's
Eric S. Raymond <esr@thyrsus.com>
parents: 15874
diff changeset
530 # 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
531 #
13476
8f66c5ffa26c New module to control Web browsers; see the documentation for
Fred Drake <fdrake@acm.org>
parents:
diff changeset
532
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
533 if sys.platform == 'darwin':
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
534 # 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
535 class MacOSX(BaseBrowser):
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
536 """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
537
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
538 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
539 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
540 package after installation.
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
541
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
542 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
543 Internet System Preferences panel, will be used.
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
544 """
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
545 def __init__(self, name):
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
546 self.name = name
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
547
55584
5fbac09c0e91 Merged revisions 74264 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50540
diff changeset
548 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
549 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
550 # hack for local urls
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
551 if not ':' in url:
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
552 url = 'file:'+url
36362
ca6c800ad5a0 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 36352
diff changeset
553
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
554 # new must be 0 or 1
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
555 new = int(bool(new))
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
556 if self.name == "default":
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
557 # 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
558 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
559 else:
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
560 # User called get and chose a browser
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
561 if self.name == "OmniWeb":
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
562 toWindow = ""
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
563 else:
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
564 # Include toWindow parameter of OpenURL command for browsers
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
565 # that support it. 0 == new window; -1 == existing
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
566 toWindow = "toWindow %d" % (new - 1)
35851
413c68b31487 Bug #1338995: fix problem with new webbrowser.py.
Georg Brandl <georg@python.org>
parents: 35707
diff changeset
567 cmd = 'OpenURL "%s"' % url.replace('"', '%22')
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
568 script = '''tell application "%s"
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
569 activate
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
570 %s %s
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
571 end tell''' % (self.name, cmd, toWindow)
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
572 # Open pipe to AppleScript through osascript command
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
573 osapipe = os.popen("osascript", "w")
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
574 if osapipe is None:
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
575 return False
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
576 # Write script to osascript's stdin
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
577 osapipe.write(script)
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
578 rc = osapipe.close()
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
579 return not rc
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
580
61159
7d98e0ea9db3 Merged revisions 80698 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 59522
diff changeset
581 class MacOSXOSAScript(BaseBrowser):
7d98e0ea9db3 Merged revisions 80698 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 59522
diff changeset
582 def __init__(self, name):
7d98e0ea9db3 Merged revisions 80698 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 59522
diff changeset
583 self._name = name
7d98e0ea9db3 Merged revisions 80698 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 59522
diff changeset
584
7d98e0ea9db3 Merged revisions 80698 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 59522
diff changeset
585 def open(self, url, new=0, autoraise=True):
7d98e0ea9db3 Merged revisions 80698 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 59522
diff changeset
586 if self._name == 'default':
7d98e0ea9db3 Merged revisions 80698 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 59522
diff changeset
587 script = 'open location "%s"' % url.replace('"', '%22') # opens in default browser
7d98e0ea9db3 Merged revisions 80698 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 59522
diff changeset
588 else:
7d98e0ea9db3 Merged revisions 80698 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 59522
diff changeset
589 script = '''
7d98e0ea9db3 Merged revisions 80698 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 59522
diff changeset
590 tell application "%s"
7d98e0ea9db3 Merged revisions 80698 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 59522
diff changeset
591 activate
7d98e0ea9db3 Merged revisions 80698 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 59522
diff changeset
592 open location "%s"
7d98e0ea9db3 Merged revisions 80698 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 59522
diff changeset
593 end
7d98e0ea9db3 Merged revisions 80698 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 59522
diff changeset
594 '''%(self._name, url.replace('"', '%22'))
7d98e0ea9db3 Merged revisions 80698 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 59522
diff changeset
595
7d98e0ea9db3 Merged revisions 80698 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 59522
diff changeset
596 osapipe = os.popen("osascript", "w")
7d98e0ea9db3 Merged revisions 80698 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 59522
diff changeset
597 if osapipe is None:
7d98e0ea9db3 Merged revisions 80698 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 59522
diff changeset
598 return False
7d98e0ea9db3 Merged revisions 80698 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 59522
diff changeset
599
7d98e0ea9db3 Merged revisions 80698 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 59522
diff changeset
600 osapipe.write(script)
7d98e0ea9db3 Merged revisions 80698 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 59522
diff changeset
601 rc = osapipe.close()
7d98e0ea9db3 Merged revisions 80698 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 59522
diff changeset
602 return not rc
7d98e0ea9db3 Merged revisions 80698 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 59522
diff changeset
603
7d98e0ea9db3 Merged revisions 80698 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 59522
diff changeset
604
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
605 # 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
606 # (but we prefer using the OS X specific stuff)
61159
7d98e0ea9db3 Merged revisions 80698 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 59522
diff changeset
607 register("safari", None, MacOSXOSAScript('safari'), -1)
7d98e0ea9db3 Merged revisions 80698 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 59522
diff changeset
608 register("firefox", None, MacOSXOSAScript('firefox'), -1)
7d98e0ea9db3 Merged revisions 80698 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 59522
diff changeset
609 register("MacOSX", None, MacOSXOSAScript('default'), -1)
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
610
16153
df7bfb34b2a2 Rewrite webbrowser.py to fix various bugs following Ka-Ping Yee's
Eric S. Raymond <esr@thyrsus.com>
parents: 15874
diff changeset
611
21047
881f49befca3 Patch #484847: Default to netscape.exe on OS/2.
Martin v. Löwis <martin@v.loewis.de>
parents: 20417
diff changeset
612 #
881f49befca3 Patch #484847: Default to netscape.exe on OS/2.
Martin v. Löwis <martin@v.loewis.de>
parents: 20417
diff changeset
613 # 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
614 #
881f49befca3 Patch #484847: Default to netscape.exe on OS/2.
Martin v. Löwis <martin@v.loewis.de>
parents: 20417
diff changeset
615
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
616 if sys.platform[:3] == "os2" and _iscommand("netscape"):
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
617 _tryorder = []
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
618 _browsers = {}
21047
881f49befca3 Patch #484847: Default to netscape.exe on OS/2.
Martin v. Löwis <martin@v.loewis.de>
parents: 20417
diff changeset
619 register("os2netscape", None,
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
620 GenericBrowser(["start", "netscape", "%s"]), -1)
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
621
21047
881f49befca3 Patch #484847: Default to netscape.exe on OS/2.
Martin v. Löwis <martin@v.loewis.de>
parents: 20417
diff changeset
622
16153
df7bfb34b2a2 Rewrite webbrowser.py to fix various bugs following Ka-Ping Yee's
Eric S. Raymond <esr@thyrsus.com>
parents: 15874
diff changeset
623 # 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
624 # 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
625 if "BROWSER" in os.environ:
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
626 _userchoices = os.environ["BROWSER"].split(os.pathsep)
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
627 _userchoices.reverse()
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
628
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
629 # 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
630 # and prepend to _tryorder
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
631 for cmdline in _userchoices:
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
632 if cmdline != '':
55913
3fb1c1fbe26c Merged revisions 74745 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55584
diff changeset
633 cmd = _synthesize(cmdline, -1)
3fb1c1fbe26c Merged revisions 74745 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55584
diff changeset
634 if cmd[1] is None:
3fb1c1fbe26c Merged revisions 74745 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55584
diff changeset
635 register(cmdline, None, GenericBrowser(cmdline), -1)
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
636 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
637 del cmdline
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
638 del _userchoices
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
639
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
640 # what to do if _tryorder is now empty?
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
641
18714
5c73621c9559 this closes patches item 429136 and brings the release21-maint version into
Skip Montanaro <skip@pobox.com>
parents: 17461
diff changeset
642
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
643 def main():
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
644 import getopt
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
645 usage = """Usage: %s [-n | -t] url
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
646 -n: open new window
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
647 -t: open new tab""" % sys.argv[0]
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
648 try:
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
649 opts, args = getopt.getopt(sys.argv[1:], 'ntd')
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
650 except getopt.error as msg:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
651 print(msg, file=sys.stderr)
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
652 print(usage, file=sys.stderr)
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
653 sys.exit(1)
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
654 new_win = 0
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
655 for o, a in opts:
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
656 if o == '-n': new_win = 1
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
657 elif o == '-t': new_win = 2
39648
efe3f2c0da5e Killed the <> operator. You must now use !=.
Guido van Rossum <guido@python.org>
parents: 39647
diff changeset
658 if len(args) != 1:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
659 print(usage, file=sys.stderr)
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
660 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
661
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
662 url = args[0]
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
663 open(url, new_win)
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
664
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
665 print("\a")
36352
6e12707371a1 Try to resolve the remaining webbrowser issues (backgrounding, local urls)
Georg Brandl <georg@python.org>
parents: 36121
diff changeset
666
35685
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
667 if __name__ == "__main__":
3be835350082 Patch #754022: Greatly enhanced webbrowser.py.
Georg Brandl <georg@python.org>
parents: 32605
diff changeset
668 main()