qutebrowser/qutebrowser/utils/misc.py

82 lines
2.3 KiB
Python
Raw Normal View History

2014-02-10 17:54:24 +01:00
# Copyright 2014 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/>.
2014-02-17 12:23:52 +01:00
"""Other utilities which don't fit anywhere else."""
2014-02-18 12:10:36 +01:00
import sys
2014-02-14 07:17:36 +01:00
import os.path
2014-02-11 10:26:37 +01:00
from PyQt5.QtCore import pyqtRemoveInputHook
2014-03-03 06:09:23 +01:00
#import qutebrowser.commands.utils as cmdutils
2014-02-11 10:26:37 +01:00
try:
2014-02-13 09:10:53 +01:00
# pylint: disable=import-error
2014-02-11 10:26:37 +01:00
from ipdb import set_trace as pdb_set_trace
except ImportError:
from pdb import set_trace as pdb_set_trace
2014-02-14 16:32:56 +01:00
import qutebrowser
2014-03-03 06:09:23 +01:00
# FIXME we can';t do this because of circular imports
#@cmdutils.register(name='settrace', hide=True)
2014-02-11 10:26:37 +01:00
def set_trace():
2014-03-03 06:09:23 +01:00
"""Set a tracepoint in the Python debugger that works with Qt.
2014-02-11 10:26:37 +01:00
Based on http://stackoverflow.com/a/1745965/2085149
"""
print()
print("When done debugging, remember to execute:")
print(" from PyQt5 import QtCore; QtCore.pyqtRestoreInputHook()")
print("before executing c(ontinue).")
pyqtRemoveInputHook()
return pdb_set_trace()
2014-02-14 07:17:36 +01:00
def read_file(filename):
2014-02-19 10:58:32 +01:00
"""Get the contents of a file contained with qutebrowser.
Args:
filename: The filename to open as string.
Return:
The file contents as string.
"""
2014-02-14 16:32:56 +01:00
fn = os.path.join(qutebrowser.basedir, filename)
with open(fn, 'r', encoding='UTF-8') as f:
return f.read()
2014-02-18 12:10:36 +01:00
def trace_lines(do_trace):
2014-02-19 10:58:32 +01:00
"""Turn on/off printing each executed line.
Args:
do_trace: Whether to start tracing (True) or stop it (False).
"""
2014-02-18 14:21:39 +01:00
def trace(frame, event, _):
"""Trace function passed to sys.settrace."""
2014-02-18 12:10:36 +01:00
print("{}, {}:{}".format(event, frame.f_code.co_filename,
frame.f_lineno))
return trace
if do_trace:
sys.settrace(trace)
else:
sys.settrace(None)