Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: 

 

# Copyright 2014-2018 Florian Bruhin (The Compiler) <mail@qutebrowser.org> 

# 

# This file is part of qutebrowser. 

# 

# qutebrowser is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# qutebrowser is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the GNU General Public License 

# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>. 

 

"""Custom useful data types. 

 

Module attributes: 

_UNSET: Used as default argument in the constructor so default can be None. 

""" 

 

import operator 

import collections.abc 

import enum 

 

from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject, QTimer 

 

from qutebrowser.utils import log, qtutils, utils 

 

 

_UNSET = object() 

 

 

class NeighborList(collections.abc.Sequence): 

 

"""A list of items which saves its current position. 

 

Class attributes: 

Modes: Different modes, see constructor documentation. 

 

Attributes: 

fuzzyval: The value which is currently set but not in the list. 

_idx: The current position in the list. 

_items: A list of all items, accessed through item property. 

_mode: The current mode. 

""" 

 

Modes = enum.Enum('Modes', ['edge', 'exception']) 

 

def __init__(self, items=None, default=_UNSET, mode=Modes.exception): 

"""Constructor. 

 

Args: 

items: The list of items to iterate in. 

_default: The initially selected value. 

_mode: Behavior when the first/last item is reached. 

Modes.edge: Go to the first/last item 

Modes.exception: Raise an IndexError. 

""" 

if not isinstance(mode, self.Modes): 

raise TypeError("Mode {} is not a Modes member!".format(mode)) 

if items is None: 

self._items = [] 

else: 

self._items = list(items) 

self._default = default 

if default is not _UNSET: 

self._idx = self._items.index(default) 

else: 

self._idx = None 

self._mode = mode 

self.fuzzyval = None 

 

def __getitem__(self, key): 

return self._items[key] 

 

def __len__(self): 

return len(self._items) 

 

def __repr__(self): 

return utils.get_repr(self, items=self._items, mode=self._mode, 

idx=self._idx, fuzzyval=self.fuzzyval) 

 

def _snap_in(self, offset): 

"""Set the current item to the closest item to self.fuzzyval. 

 

Args: 

offset: negative to get the next smaller item, positive for the 

next bigger one. 

 

Return: 

True if the value snapped in (changed), 

False when the value already was in the list. 

""" 

op = operator.le if offset < 0 else operator.ge 

items = [(idx, e) for (idx, e) in enumerate(self._items) 

if op(e, self.fuzzyval)] 

if items: 

item = min(items, key=lambda tpl: abs(self.fuzzyval - tpl[1])) 

else: 

sorted_items = sorted(((idx, e) for (idx, e) in 

enumerate(self.items)), key=lambda e: e[1]) 

idx = 0 if offset < 0 else -1 

item = sorted_items[idx] 

self._idx = item[0] 

return self.fuzzyval not in self._items 

 

def _get_new_item(self, offset): 

"""Logic for getitem to get the item at offset. 

 

Args: 

offset: The offset of the current item, relative to the last one. 

 

Return: 

The new item. 

""" 

try: 

if self._idx + offset >= 0: 

new = self._items[self._idx + offset] 

else: 

raise IndexError 

except IndexError: 

if self._mode == self.Modes.edge: 

assert offset != 0 

if offset > 0: 

new = self.lastitem() 

else: 

new = self.firstitem() 

elif self._mode == self.Modes.exception: # pragma: no branch 

raise 

else: 

self._idx += offset 

return new 

 

@property 

def items(self): 

"""Getter for items, which should not be set.""" 

return self._items 

 

def getitem(self, offset): 

"""Get the item with a relative position. 

 

Args: 

offset: The offset of the current item, relative to the last one. 

 

Return: 

The new item. 

""" 

log.misc.debug("{} items, idx {}, offset {}".format( 

len(self._items), self._idx, offset)) 

if not self._items: 

raise IndexError("No items found!") 

if self.fuzzyval is not None: 

# Value has been set to something not in the list, so we snap in to 

# the closest value in the right direction and count this as one 

# step towards offset. 

snapped = self._snap_in(offset) 

if snapped and offset > 0: 

offset -= 1 

elif snapped: 

offset += 1 

self.fuzzyval = None 

return self._get_new_item(offset) 

 

def curitem(self): 

"""Get the current item in the list.""" 

if self._idx is not None: 

return self._items[self._idx] 

else: 

raise IndexError("No current item!") 

 

def nextitem(self): 

"""Get the next item in the list.""" 

return self.getitem(1) 

 

def previtem(self): 

"""Get the previous item in the list.""" 

return self.getitem(-1) 

 

def firstitem(self): 

"""Get the first item in the list.""" 

if not self._items: 

raise IndexError("No items found!") 

self._idx = 0 

return self.curitem() 

 

def lastitem(self): 

"""Get the last item in the list.""" 

if not self._items: 

raise IndexError("No items found!") 

self._idx = len(self._items) - 1 

return self.curitem() 

 

def reset(self): 

"""Reset the position to the default.""" 

if self._default is _UNSET: 

raise ValueError("No default set!") 

else: 

self._idx = self._items.index(self._default) 

return self.curitem() 

 

 

# The mode of a Question. 

PromptMode = enum.Enum('PromptMode', ['yesno', 'text', 'user_pwd', 'alert', 

'download']) 

 

 

# Where to open a clicked link. 

ClickTarget = enum.Enum('ClickTarget', ['normal', 'tab', 'tab_bg', 'window', 

'hover']) 

 

 

# Key input modes 

KeyMode = enum.Enum('KeyMode', ['normal', 'hint', 'command', 'yesno', 'prompt', 

'insert', 'passthrough', 'caret', 'set_mark', 

'jump_mark', 'record_macro', 'run_macro']) 

 

 

# Exit statuses for errors. Needs to be an int for sys.exit. 

Exit = enum.IntEnum('Exit', ['ok', 'reserved', 'exception', 'err_ipc', 

'err_init', 'err_config', 'err_key_config'], 

start=0) 

 

 

# Load status of a tab 

LoadStatus = enum.Enum('LoadStatus', ['none', 'success', 'success_https', 

'error', 'warn', 'loading']) 

 

 

# Backend of a tab 

Backend = enum.Enum('Backend', ['QtWebKit', 'QtWebEngine']) 

 

 

# JS world for QtWebEngine 

JsWorld = enum.Enum('JsWorld', ['main', 'application', 'user', 'jseval']) 

 

 

# Log level of a JS message. This needs to match up with the keys allowed for 

# the content.javascript.log setting. 

JsLogLevel = enum.Enum('JsLogLevel', ['unknown', 'info', 'warning', 'error']) 

 

 

MessageLevel = enum.Enum('MessageLevel', ['error', 'warning', 'info']) 

 

 

class Question(QObject): 

 

"""A question asked to the user, e.g. via the status bar. 

 

Note the creator is responsible for cleaning up the question after it 

doesn't need it anymore, e.g. via connecting Question.completed to 

Question.deleteLater. 

 

Attributes: 

mode: A PromptMode enum member. 

yesno: A question which can be answered with yes/no. 

text: A question which requires a free text answer. 

user_pwd: A question for a username and password. 

default: The default value. 

For yesno, None (no default), True or False. 

For text, a default text as string. 

For user_pwd, a default username as string. 

title: The question title to show. 

text: The prompt text to display to the user. 

url: Any URL referenced in prompts. 

answer: The value the user entered (as password for user_pwd). 

is_aborted: Whether the question was aborted. 

interrupted: Whether the question was interrupted by another one. 

 

Signals: 

answered: Emitted when the question has been answered by the user. 

arg: The answer to the question. 

cancelled: Emitted when the question has been cancelled by the user. 

aborted: Emitted when the question was aborted programmatically. 

In this case, cancelled is not emitted. 

answered_yes: Convenience signal emitted when a yesno question was 

answered with yes. 

answered_no: Convenience signal emitted when a yesno question was 

answered with no. 

completed: Emitted when the question was completed in any way. 

""" 

 

answered = pyqtSignal(object) 

cancelled = pyqtSignal() 

aborted = pyqtSignal() 

answered_yes = pyqtSignal() 

answered_no = pyqtSignal() 

completed = pyqtSignal() 

 

def __init__(self, parent=None): 

super().__init__(parent) 

self._mode = None 

self.default = None 

self.title = None 

self.text = None 

self.url = None 

self.answer = None 

self.is_aborted = False 

self.interrupted = False 

 

def __repr__(self): 

return utils.get_repr(self, title=self.title, text=self.text, 

mode=self._mode, default=self.default) 

 

@property 

def mode(self): 

"""Getter for mode so we can define a setter.""" 

return self._mode 

 

@mode.setter 

def mode(self, val): 

"""Setter for mode to do basic type checking.""" 

if not isinstance(val, PromptMode): 

raise TypeError("Mode {} is no PromptMode member!".format(val)) 

self._mode = val 

 

@pyqtSlot() 

def done(self): 

"""Must be called when the question was answered completely.""" 

self.answered.emit(self.answer) 

if self.mode == PromptMode.yesno: 

if self.answer: 

self.answered_yes.emit() 

else: 

self.answered_no.emit() 

self.completed.emit() 

 

@pyqtSlot() 

def cancel(self): 

"""Cancel the question (resulting from user-input).""" 

self.cancelled.emit() 

self.completed.emit() 

 

@pyqtSlot() 

def abort(self): 

"""Abort the question.""" 

if self.is_aborted: 

log.misc.debug("Question was already aborted") 

return 

self.is_aborted = True 

self.aborted.emit() 

self.completed.emit() 

 

 

class Timer(QTimer): 

 

"""A timer which has a name to show in __repr__ and checks for overflows. 

 

Attributes: 

_name: The name of the timer. 

""" 

 

def __init__(self, parent=None, name=None): 

super().__init__(parent) 

if name is None: 

self._name = "unnamed" 

else: 

self.setObjectName(name) 

self._name = name 

 

def __repr__(self): 

return utils.get_repr(self, name=self._name) 

 

def setInterval(self, msec): 

"""Extend setInterval to check for overflows.""" 

qtutils.check_overflow(msec, 'int') 

super().setInterval(msec) 

 

def start(self, msec=None): 

"""Extend start to check for overflows.""" 

if msec is not None: 

qtutils.check_overflow(msec, 'int') 

super().start(msec) 

else: 

super().start() 

 

 

class AbstractCertificateErrorWrapper: 

 

"""A wrapper over an SSL/certificate error.""" 

 

def __init__(self, error): 

self._error = error 

 

def __str__(self): 

raise NotImplementedError 

 

def __repr__(self): 

raise NotImplementedError 

 

def is_overridable(self): 

raise NotImplementedError