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

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

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

 

# Copyright 2016-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/>. 

 

"""Backend-independent qute://* code. 

 

Module attributes: 

pyeval_output: The output of the last :pyeval command. 

_HANDLERS: The handlers registered via decorators. 

""" 

 

import json 

import os 

import time 

import textwrap 

import mimetypes 

import urllib 

import collections 

 

import pkg_resources 

import sip 

from PyQt5.QtCore import QUrlQuery, QUrl 

 

import qutebrowser 

from qutebrowser.config import config, configdata, configexc, configdiff 

from qutebrowser.utils import (version, utils, jinja, log, message, docutils, 

objreg, urlutils) 

from qutebrowser.misc import objects 

 

 

pyeval_output = ":pyeval was never called" 

spawn_output = ":spawn was never called" 

 

 

_HANDLERS = {} 

 

 

class NoHandlerFound(Exception): 

 

"""Raised when no handler was found for the given URL.""" 

 

pass 

 

 

class QuteSchemeOSError(Exception): 

 

"""Called when there was an OSError inside a handler.""" 

 

pass 

 

 

class QuteSchemeError(Exception): 

 

"""Exception to signal that a handler should return an ErrorReply. 

 

Attributes correspond to the arguments in 

networkreply.ErrorNetworkReply. 

 

Attributes: 

errorstring: Error string to print. 

error: Numerical error value. 

""" 

 

def __init__(self, errorstring, error): 

self.errorstring = errorstring 

self.error = error 

super().__init__(errorstring) 

 

 

class Redirect(Exception): 

 

"""Exception to signal a redirect should happen. 

 

Attributes: 

url: The URL to redirect to, as a QUrl. 

""" 

 

def __init__(self, url): 

super().__init__(url.toDisplayString()) 

self.url = url 

 

 

class add_handler: # noqa: N801,N806 pylint: disable=invalid-name 

 

"""Decorator to register a qute://* URL handler. 

 

Attributes: 

_name: The 'foo' part of qute://foo 

backend: Limit which backends the handler can run with. 

""" 

 

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

self._name = name 

self._backend = backend 

self._function = None 

 

def __call__(self, function): 

self._function = function 

_HANDLERS[self._name] = self.wrapper 

return function 

 

def wrapper(self, *args, **kwargs): 

"""Call the underlying function.""" 

119 ↛ 120line 119 didn't jump to line 120, because the condition on line 119 was never true if self._backend is not None and objects.backend != self._backend: 

return self.wrong_backend_handler(*args, **kwargs) 

else: 

return self._function(*args, **kwargs) 

 

def wrong_backend_handler(self, url): 

"""Show an error page about using the invalid backend.""" 

html = jinja.render('error.html', 

title="Error while opening qute://url", 

url=url.toDisplayString(), 

error='{} is not available with this ' 

'backend'.format(url.toDisplayString())) 

return 'text/html', html 

 

 

def data_for_url(url): 

"""Get the data to show for the given URL. 

 

Args: 

url: The QUrl to show. 

 

Return: 

A (mimetype, data) tuple. 

""" 

norm_url = url.adjusted(QUrl.NormalizePathSegments | 

QUrl.StripTrailingSlash) 

145 ↛ 146line 145 didn't jump to line 146, because the condition on line 145 was never true if norm_url != url: 

raise Redirect(norm_url) 

 

path = url.path() 

host = url.host() 

query = urlutils.query_string(url) 

# A url like "qute:foo" is split as "scheme:path", not "scheme:host". 

log.misc.debug("url: {}, path: {}, host {}".format( 

url.toDisplayString(), path, host)) 

154 ↛ 155line 154 didn't jump to line 155, because the condition on line 154 was never true if not path or not host: 

new_url = QUrl() 

new_url.setScheme('qute') 

# When path is absent, e.g. qute://help (with no trailing slash) 

if host: 

new_url.setHost(host) 

# When host is absent, e.g. qute:help 

else: 

new_url.setHost(path) 

 

new_url.setPath('/') 

if query: 

new_url.setQuery(query) 

if new_url.host(): # path was a valid host 

raise Redirect(new_url) 

 

try: 

handler = _HANDLERS[host] 

except KeyError: 

raise NoHandlerFound(url) 

 

try: 

mimetype, data = handler(url) 

177 ↛ 180line 177 didn't jump to line 180 except OSError as e: 

# FIXME:qtwebengine how to handle this? 

raise QuteSchemeOSError(e) 

except QuteSchemeError as e: 

raise 

 

assert mimetype is not None, url 

if mimetype == 'text/html' and isinstance(data, str): 

# We let handlers return HTML as text 

data = data.encode('utf-8', errors='xmlcharrefreplace') 

 

return mimetype, data 

 

 

@add_handler('bookmarks') 

def qute_bookmarks(_url): 

"""Handler for qute://bookmarks. Display all quickmarks / bookmarks.""" 

bookmarks = sorted(objreg.get('bookmark-manager').marks.items(), 

key=lambda x: x[1]) # Sort by title 

quickmarks = sorted(objreg.get('quickmark-manager').marks.items(), 

key=lambda x: x[0]) # Sort by name 

 

html = jinja.render('bookmarks.html', 

title='Bookmarks', 

bookmarks=bookmarks, 

quickmarks=quickmarks) 

return 'text/html', html 

 

 

@add_handler('tabs') 

def qute_tabs(_url): 

"""Handler for qute://tabs. Display information about all open tabs.""" 

tabs = collections.defaultdict(list) 

for win_id, window in objreg.window_registry.items(): 

if sip.isdeleted(window): 

continue 

tabbed_browser = objreg.get('tabbed-browser', 

scope='window', 

window=win_id) 

for tab in tabbed_browser.widgets(): 

if tab.url() not in [QUrl("qute://tabs/"), QUrl("qute://tabs")]: 

urlstr = tab.url().toDisplayString() 

tabs[str(win_id)].append((tab.title(), urlstr)) 

 

html = jinja.render('tabs.html', 

title='Tabs', 

tab_list_by_window=tabs) 

return 'text/html', html 

 

 

def history_data(start_time, offset=None): 

"""Return history data. 

 

Arguments: 

start_time: select history starting from this timestamp. 

offset: number of items to skip 

""" 

# history atimes are stored as ints, ensure start_time is not a float 

start_time = int(start_time) 

hist = objreg.get('web-history') 

237 ↛ 238line 237 didn't jump to line 238, because the condition on line 237 was never true if offset is not None: 

entries = hist.entries_before(start_time, limit=1000, offset=offset) 

else: 

# end is 24hrs earlier than start 

end_time = start_time - 24*60*60 

entries = hist.entries_between(end_time, start_time) 

 

return [{"url": e.url, "title": e.title or e.url, "time": e.atime} 

for e in entries] 

 

 

@add_handler('history') 

def qute_history(url): 

"""Handler for qute://history. Display and serve history.""" 

251 ↛ 266line 251 didn't jump to line 266, because the condition on line 251 was never false if url.path() == '/data': 

try: 

offset = QUrlQuery(url).queryItemValue("offset") 

offset = int(offset) if offset else None 

except ValueError as e: 

raise QuteSchemeError("Query parameter offset is invalid", e) 

# Use start_time in query or current time. 

try: 

start_time = QUrlQuery(url).queryItemValue("start_time") 

start_time = float(start_time) if start_time else time.time() 

except ValueError as e: 

raise QuteSchemeError("Query parameter start_time is invalid", e) 

 

return 'text/html', json.dumps(history_data(start_time, offset)) 

else: 

if not config.val.content.javascript.enabled: 

return 'text/plain', b'JavaScript is required for qute://history' 

return 'text/html', jinja.render( 

'history.html', 

title='History', 

gap_interval=config.val.history_gap_interval 

) 

 

 

@add_handler('javascript') 

def qute_javascript(url): 

"""Handler for qute://javascript. 

 

Return content of file given as query parameter. 

""" 

path = url.path() 

if path: 

path = "javascript" + os.sep.join(path.split('/')) 

return 'text/html', utils.read_file(path, binary=False) 

else: 

raise QuteSchemeError("No file specified", ValueError()) 

 

 

@add_handler('pyeval') 

def qute_pyeval(_url): 

"""Handler for qute://pyeval.""" 

html = jinja.render('pre.html', title='pyeval', content=pyeval_output) 

return 'text/html', html 

 

 

@add_handler('spawn-output') 

def qute_spawn_output(_url): 

"""Handler for qute://spawn-output.""" 

html = jinja.render('pre.html', title='spawn output', content=spawn_output) 

return 'text/html', html 

 

 

@add_handler('version') 

@add_handler('verizon') 

def qute_version(_url): 

"""Handler for qute://version.""" 

html = jinja.render('version.html', title='Version info', 

version=version.version(), 

copyright=qutebrowser.__copyright__) 

return 'text/html', html 

 

 

@add_handler('plainlog') 

def qute_plainlog(url): 

"""Handler for qute://plainlog. 

 

An optional query parameter specifies the minimum log level to print. 

For example, qute://log?level=warning prints warnings and errors. 

Level can be one of: vdebug, debug, info, warning, error, critical. 

""" 

if log.ram_handler is None: 

text = "Log output was disabled." 

else: 

level = QUrlQuery(url).queryItemValue('level') 

if not level: 

level = 'vdebug' 

text = log.ram_handler.dump_log(html=False, level=level) 

html = jinja.render('pre.html', title='log', content=text) 

return 'text/html', html 

 

 

@add_handler('log') 

def qute_log(url): 

"""Handler for qute://log. 

 

An optional query parameter specifies the minimum log level to print. 

For example, qute://log?level=warning prints warnings and errors. 

Level can be one of: vdebug, debug, info, warning, error, critical. 

""" 

if log.ram_handler is None: 

html_log = None 

else: 

level = QUrlQuery(url).queryItemValue('level') 

if not level: 

level = 'vdebug' 

html_log = log.ram_handler.dump_log(html=True, level=level) 

 

html = jinja.render('log.html', title='log', content=html_log) 

return 'text/html', html 

 

 

@add_handler('gpl') 

def qute_gpl(_url): 

"""Handler for qute://gpl. Return HTML content as string.""" 

return 'text/html', utils.read_file('html/license.html') 

 

 

@add_handler('help') 

def qute_help(url): 

"""Handler for qute://help.""" 

urlpath = url.path() 

if not urlpath or urlpath == '/': 

urlpath = 'index.html' 

else: 

urlpath = urlpath.lstrip('/') 

if not docutils.docs_up_to_date(urlpath): 

message.error("Your documentation is outdated! Please re-run " 

"scripts/asciidoc2html.py.") 

 

path = 'html/doc/{}'.format(urlpath) 

if not urlpath.endswith('.html'): 

try: 

bdata = utils.read_file(path, binary=True) 

except OSError as e: 

raise QuteSchemeOSError(e) 

mimetype, _encoding = mimetypes.guess_type(urlpath) 

assert mimetype is not None, url 

return mimetype, bdata 

 

try: 

data = utils.read_file(path) 

except OSError: 

# No .html around, let's see if we find the asciidoc 

asciidoc_path = path.replace('.html', '.asciidoc') 

if asciidoc_path.startswith('html/doc/'): 

asciidoc_path = asciidoc_path.replace('html/doc/', '../doc/help/') 

 

try: 

asciidoc = utils.read_file(asciidoc_path) 

except OSError: 

asciidoc = None 

 

if asciidoc is None: 

raise 

 

preamble = textwrap.dedent(""" 

There was an error loading the documentation! 

 

This most likely means the documentation was not generated 

properly. If you are running qutebrowser from the git repository, 

please (re)run scripts/asciidoc2html.py and reload this page. 

 

If you're running a released version this is a bug, please use 

:report to report it. 

 

Falling back to the plaintext version. 

 

--------------------------------------------------------------- 

 

 

""") 

return 'text/plain', (preamble + asciidoc).encode('utf-8') 

else: 

return 'text/html', data 

 

 

@add_handler('backend-warning') 

def qute_backend_warning(_url): 

"""Handler for qute://backend-warning.""" 

html = jinja.render('backend-warning.html', 

distribution=version.distribution(), 

Distribution=version.Distribution, 

version=pkg_resources.parse_version, 

title="Legacy backend warning") 

return 'text/html', html 

 

 

def _qute_settings_set(url): 

"""Handler for qute://settings/set.""" 

query = QUrlQuery(url) 

option = query.queryItemValue('option', QUrl.FullyDecoded) 

value = query.queryItemValue('value', QUrl.FullyDecoded) 

 

# https://github.com/qutebrowser/qutebrowser/issues/727 

if option == 'content.javascript.enabled' and value == 'false': 

msg = ("Refusing to disable javascript via qute://settings " 

"as it needs javascript support.") 

message.error(msg) 

return 'text/html', b'error: ' + msg.encode('utf-8') 

 

try: 

config.instance.set_str(option, value, save_yaml=True) 

return 'text/html', b'ok' 

except configexc.Error as e: 

message.error(str(e)) 

return 'text/html', b'error: ' + str(e).encode('utf-8') 

 

 

@add_handler('settings') 

def qute_settings(url): 

"""Handler for qute://settings. View/change qute configuration.""" 

if url.path() == '/set': 

return _qute_settings_set(url) 

 

html = jinja.render('settings.html', title='settings', 

configdata=configdata, 

confget=config.instance.get_str) 

return 'text/html', html 

 

 

@add_handler('bindings') 

def qute_bindings(_url): 

"""Handler for qute://bindings. View keybindings.""" 

bindings = {} 

defaults = config.val.bindings.default 

modes = set(defaults.keys()).union(config.val.bindings.commands) 

modes.remove('normal') 

modes = ['normal'] + sorted(list(modes)) 

for mode in modes: 

bindings[mode] = config.key_instance.get_bindings_for(mode) 

 

html = jinja.render('bindings.html', title='Bindings', 

bindings=bindings) 

return 'text/html', html 

 

 

@add_handler('back') 

def qute_back(url): 

"""Handler for qute://back. 

 

Simple page to free ram / lazy load a site, goes back on focusing the tab. 

""" 

html = jinja.render( 

'back.html', 

title='Suspended: ' + urllib.parse.unquote(url.fragment())) 

return 'text/html', html 

 

 

@add_handler('configdiff') 

def qute_configdiff(url): 

"""Handler for qute://configdiff.""" 

if url.path() == '/old': 

try: 

return 'text/html', configdiff.get_diff() 

except OSError as e: 

error = (b'Failed to read old config: ' + 

str(e.strerror).encode('utf-8')) 

return 'text/plain', error 

else: 

data = config.instance.dump_userconfig().encode('utf-8') 

return 'text/plain', data 

 

 

@add_handler('pastebin-version') 

def qute_pastebin_version(_url): 

"""Handler that pastebins the version string.""" 

version.pastebin_version() 

return 'text/plain', b'Paste called.'