/
chooser
executable file
·149 lines (121 loc) · 4.4 KB
/
chooser
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python
"""
Make this script your default browser in order to
choose which browser to use when opening a URI. Make sure to pass
the URI to the script via the "%s" variable.
"""
# Copyright (c) 2012-2017, Lev E. Givon
# All rights reserved.
# Distributed under the terms of the BSD license:
# http://www.opensource.org/licenses/bsd-license
import os
import sys
if sys.version_info.major == 2:
import ConfigParser as configparser
else:
import configparser
import wx
import xdg.BaseDirectory
# Set the URI to the first command line argument (if given):
if len(sys.argv) > 1:
uri = sys.argv[1]
else:
uri = ''
def which(program):
"""
Attempt to expand the path of a specified executable.
"""
def is_exe(fpath):
return os.path.exists(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None
# Load browser names and paths from configuration file if present:
possible_browsers = {}
cfg_dir = xdg.BaseDirectory.load_first_config('chooser')
if cfg_dir is not None:
config = configparser.ConfigParser()
config.optionxform = str
config.read(os.path.join(cfg_dir, 'chooserrc'))
if config.has_section('Browsers'):
for item in config.items('Browsers'):
possible_browsers[item[0]] = item[1]
if possible_browsers == {}:
# Common browsers:
possible_browsers = {'Arora': 'arora',
'Chromium': 'chromium-browser',
'Dillo': 'dillo',
'Epiphany': 'epiphany',
'Firefox': 'firefox',
'Konqueror': 'konqueror',
'Midori': 'midori',
'Opera': 'opera',
'QupZilla': 'qupzilla'}
browsers = {}
for browser in possible_browsers:
name = possible_browsers[browser]
full_path = which(name)
if full_path:
browsers[browser] = (name, full_path)
class AppFrame(wx.Frame):
def __init__(self, parent, id, title):
N = len(browsers.keys())
# Prevent the frame from being resized:
wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition,
wx.Size(80*N, 100),
style=wx.CAPTION | wx.CLOSE_BOX)
panel = wx.Panel(self, wx.ID_ANY)
sizer = wx.BoxSizer(wx.VERTICAL)
grid = wx.GridSizer(1, N, 1, 1)
# Dict to map button ids to browser commands:
img_size = (64, 64)
self.button_to_browser = {}
for browser in browsers.keys():
name, full_path = browsers[browser]
icon = wx.ArtProvider.GetIcon(name, wx.ART_OTHER, img_size)
# Load default web browser icon if no application-specific icon
# exists:
if not icon.IsOk():
icon = wx.ArtProvider.GetIcon('browser', wx.ART_OTHER, img_size)
bmp = wx.Bitmap()
bmp.CopyFromIcon(icon)
button = wx.BitmapButton(panel, -1, bmp, style=wx.NO_BORDER)
button.SetToolTip(browser)
grid.Add(button, 0, wx.EXPAND)
self.Bind(wx.EVT_BUTTON, self.OnClick, button)
self.button_to_browser[button.Id] = full_path
sizer.Add(grid, 0, wx.EXPAND | wx.ALL, 2)
panel.SetSizerAndFit(sizer)
self.Centre()
# Focus on the panel to catch keystrokes:
panel.SetFocusIgnoringChildren()
# Exit if the ESC key is pressed:
panel.Bind(wx.EVT_CHAR_HOOK, self.OnKeyDown)
def OnClick(self, event):
# The first parameter is the name to give to the executed
# process:
os.execl(self.button_to_browser[event.Id], '', uri)
def OnKeyDown(self, event):
keycode = event.GetKeyCode()
if keycode == wx.WXK_ESCAPE:
self.Close()
event.Skip()
class App(wx.App):
def OnInit(self):
# Exit if no browsers are found:
if len(browsers) == 0:
wx.MessageBox('No browsers found', 'Chooser Error')
sys.exit(1)
frame = AppFrame(None, -1, 'Select a Browser')
frame.Show(True)
self.SetTopWindow(frame)
return True
app = App(0)
app.MainLoop()