This repository was archived by the owner on Jun 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 608
Expand file tree
/
Copy pathbrowsers.py
More file actions
81 lines (62 loc) · 2.29 KB
/
browsers.py
File metadata and controls
81 lines (62 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import subprocess as sps
import webbrowser as wbr
import eel.chrome as chm
import eel.electron as ele
import eel.edge as edge
#import eel.firefox as ffx TODO
#import eel.safari as saf TODO
_browser_paths = {}
_browser_modules = {'chrome': chm,
'electron': ele,
'edge': edge}
def _build_url_from_dict(page, options):
scheme = page.get('scheme', 'http')
host = page.get('host', 'localhost')
port = page.get('port', options["port"])
path = page.get('path', '')
return '%s://%s:%d/%s' % (scheme, host, port, path)
def _build_url_from_string(page, options):
base_url = 'http://%s:%d/' % (options['host'], options['port'])
return base_url + page
def _build_urls(start_pages, options):
urls = []
for page in start_pages:
method = _build_url_from_dict if isinstance(
page, dict) else _build_url_from_string
url = method(page, options)
urls.append(url)
return urls
def open(start_pages, options):
# Build full URLs for starting pages (including host and port)
start_urls = _build_urls(start_pages, options)
proclist = []
mode = options.get('mode')
if mode in [None, False]:
# Don't open a browser
pass
elif mode == 'custom':
# Just run whatever command the user provided
procitem = sps.Popen(options['cmdline_args'],
stdout=sps.PIPE, stderr=sps.PIPE, stdin=sps.PIPE)
proclist.append(procitem)
elif mode in _browser_modules:
# Run with a specific browser
browser_module = _browser_modules[mode]
path = _browser_paths.get(mode)
if path is None:
# Don't know this browser's path, try and find it ourselves
path = browser_module.find_path()
_browser_paths[mode] = path
if path is not None:
proclist = browser_module.run(path, options, start_urls)
else:
raise EnvironmentError("Can't find %s installation" % browser_module.name)
else:
# Fall back to system default browser
for url in start_urls:
wbr.open(url)
return proclist
def set_path(browser_name, path):
_browser_paths[browser_name] = path
def get_path(browser_name):
return _browser_paths.get(browser_name)