pep8 fixes

This commit is contained in:
Florian Bruhin 2014-01-28 23:04:02 +01:00
parent 9caebdda3b
commit 6f3a2448ba
16 changed files with 76 additions and 42 deletions

View File

@ -1 +1 @@
"""A vim like browser based on Qt"""

View File

@ -13,15 +13,16 @@ from qutebrowser.widgets.mainwindow import MainWindow
from qutebrowser.commands.keys import KeyParser
from qutebrowser.utils.appdirs import AppDirs
class QuteBrowser(QApplication):
"""Main object for QuteBrowser"""
dirs = None # AppDirs - config/cache directories
config = None # Config(Parser) object
dirs = None # AppDirs - config/cache directories
config = None # Config(Parser) object
mainwindow = None
commandparser = None
keyparser = None
args = None # ArgumentParser
timer = None # QTimer for python hacks
args = None # ArgumentParser
timer = None # QTimer for python hacks
def __init__(self):
super().__init__(sys.argv)
@ -171,7 +172,7 @@ class QuteBrowser(QApplication):
try:
r = eval(s)
out = repr(r)
except Exception as e: # pylint: disable=broad-except
except Exception as e: # pylint: disable=broad-except
out = ': '.join([e.__class__.__name__, str(e)])
# FIXME we probably want some nicer interface to display these about:

View File

@ -19,79 +19,95 @@ A command class can set the following properties:
from qutebrowser.commands.utils import Command
class Open(Command):
nargs = 1
split_args = False
desc = 'Open a page'
class TabOpen(Command):
nargs = 1
split_args = False
desc = 'Open a page in a new tab'
class TabClose(Command):
nargs = 0
desc = 'Close the current tab'
# FIXME also close [count]th tab
class TabNext(Command):
nargs = 0
desc = 'Switch to the next tab'
# FIXME also support [count]
class TabPrev(Command):
nargs = 0
desc = 'Switch to the previous tab'
# FIXME also support [count]
class Quit(Command):
name = ['quit', 'q']
nargs = 0
desc = 'Quit qutebrowser'
class Reload(Command):
nargs = 0
desc = 'Reload the current page'
class Stop(Command):
nargs = 0
desc = 'Stop loading the current page'
class Back(Command):
nargs = 0
desc = 'Go back one page in the history'
# FIXME also support [count]
class Forward(Command):
nargs = 0
desc = 'Go forward one page in the history'
# FIXME also support [count]
class Print(Command):
nargs = 0
desc = 'Print the current page'
class Scroll(Command):
nargs = 2
count = True
hide = True
class Undo(Command):
nargs = 0
desc = 'Undo closing a tab'
class ScrollPercX(Command):
nargs = '?'
count = True
hide = True
name = 'scroll_perc_x'
class ScrollPercY(Command):
nargs = '?'
count = True
hide = True
name = 'scroll_perc_y'
class PyEval(Command):
nargs = 1
split_args = False

View File

@ -6,9 +6,10 @@ from PyQt5.QtCore import QObject, pyqtSignal
from qutebrowser.commands.utils import (CommandParser, ArgumentCountError,
NoSuchCommandError)
class KeyParser(QObject):
"""Parser for vim-like key sequences"""
keystring = '' # The currently entered key sequence
keystring = '' # The currently entered key sequence
# Signal emitted when the statusbar should set a partial command
set_cmd_text = pyqtSignal(str)
# Signal emitted when the keystring is updated

View File

@ -10,20 +10,23 @@ from qutebrowser.utils.completion import CompletionModel
cmd_dict = {}
class ArgumentCountError(TypeError):
pass
class NoSuchCommandError(ValueError):
pass
def register_all():
"""Register and initialize all commands."""
# We do this here to avoid a circular import, since commands.commands
# imports Command from this module.
import qutebrowser.commands
for (name, cls) in inspect.getmembers( # pylint: disable=unused-variable
for (name, cls) in inspect.getmembers( # pylint: disable=unused-variable
qutebrowser.commands, (lambda o: inspect.isclass(o) and
o.__module__ == 'qutebrowser.commands')):
o.__module__ == 'qutebrowser.commands')):
obj = cls()
if isinstance(obj.name, str):
names = [obj.name]
@ -32,12 +35,13 @@ def register_all():
for n in names:
cmd_dict[n] = obj
class CommandParser(QObject):
"""Parser for qutebrowser commandline commands"""
text = ''
cmd = ''
args = []
error = pyqtSignal(str) # Emitted if there's an error
error = pyqtSignal(str) # Emitted if there's an error
def _parse(self, text):
"""Parses a command"""
@ -84,6 +88,7 @@ class CommandParser(QObject):
raise
self._run(count=count)
class CommandCompletionModel(CompletionModel):
# pylint: disable=abstract-method
def __init__(self, parent=None):
@ -96,6 +101,7 @@ class CommandCompletionModel(CompletionModel):
self._data['Commands'] = sorted(cmdlist)
self.init_data()
class Command(QObject):
"""Base skeleton for a command. See the module help for
qutebrowser.commands.commands for details.
@ -113,7 +119,7 @@ class Command(QObject):
split_args = True
signal = pyqtSignal(tuple)
hide = False
desc = "" # FIXME add descriptions everywhere
desc = "" # FIXME add descriptions everywhere
def __init__(self):
super().__init__()
@ -129,8 +135,8 @@ class Command(QObject):
not.
"""
if ((isinstance(self.nargs, int) and len(args) != self.nargs) or
(self.nargs == '?' and len(args) > 1) or
(self.nargs == '+' and len(args) < 1)):
(self.nargs == '?' and len(args) > 1) or
(self.nargs == '+' and len(args) < 1)):
# for nargs == '*', anything is okay
raise ArgumentCountError

View File

@ -1,5 +1,6 @@
from PyQt5.QtCore import QUrl
def qurl(url):
if isinstance(url, QUrl):
return url

View File

@ -3,6 +3,7 @@ from collections import OrderedDict
from PyQt5.QtCore import (QAbstractItemModel, Qt, QModelIndex, QVariant,
QSortFilterProxyModel, pyqtSignal)
class CompletionModel(QAbstractItemModel):
def __init__(self, parent=None):
super().__init__(parent)
@ -110,6 +111,7 @@ class CompletionModel(QAbstractItemModel):
newitem = CompletionItem(item, newcat)
newcat.children.append(newitem)
class CompletionItem():
parent = None
_data = None
@ -146,6 +148,7 @@ class CompletionItem():
return self.parent.children.index(self)
return 0
class CompletionFilterModel(QSortFilterProxyModel):
_pattern = None
pattern_changed = pyqtSignal(str)

View File

@ -60,6 +60,7 @@ _MONOSPACE = ['Monospace', 'DejaVu Sans Mono', 'Consolas', 'Monaco',
MONOSPACE = ', '.join(_MONOSPACE)
def init(confdir):
global config, colordict
config = Config(confdir)
@ -68,9 +69,11 @@ def init(confdir):
except KeyError:
colordict = ColorDict()
def get_stylesheet(template):
return template.strip().format(color=colordict, monospace=MONOSPACE)
class ColorDict(dict):
def __getitem__(self, key):
try:
@ -90,6 +93,7 @@ class ColorDict(dict):
except KeyError:
return None
class Config(ConfigParser):
"""Our own ConfigParser"""
configdir = None
@ -98,7 +102,7 @@ class Config(ConfigParser):
def __init__(self, configdir):
"""configdir: directory to store the config in"""
super().__init__()
self.optionxform = lambda opt: opt # be case-insensitive
self.optionxform = lambda opt: opt # be case-insensitive
self.configdir = configdir
if self.configdir is None:
self.init_config()
@ -115,7 +119,7 @@ class Config(ConfigParser):
self.read_dict(default_config)
return
cp = ConfigParser()
cp.optionxform = lambda opt: opt # be case-insensitive
cp.optionxform = lambda opt: opt # be case-insensitive
cp.read_dict(default_config)
if not os.path.exists(self.configdir):
os.makedirs(self.configdir, 0o755)

View File

@ -1,7 +1,7 @@
import logging
from PyQt5.QtWidgets import QShortcut
from PyQt5.QtCore import QUrl, pyqtSignal, Qt, QPoint, QEvent
from PyQt5.QtCore import QUrl, pyqtSignal, Qt, QEvent
from PyQt5.QtPrintSupport import QPrintPreviewDialog
from PyQt5.QtWebKitWidgets import QWebView, QWebPage
@ -9,18 +9,19 @@ import qutebrowser.utils as utils
import qutebrowser.utils.config as config
from qutebrowser.widgets.tabbar import TabWidget
class TabbedBrowser(TabWidget):
"""A TabWidget with QWebViews inside"""
cur_progress = pyqtSignal(int) # Progress of the current tab changed
cur_load_started = pyqtSignal() # Current tab started loading
cur_load_finished = pyqtSignal(bool) # Current tab finished loading
cur_statusbar_message = pyqtSignal(str) # Status bar message
cur_progress = pyqtSignal(int) # Progress of the current tab changed
cur_load_started = pyqtSignal() # Current tab started loading
cur_load_finished = pyqtSignal(bool) # Current tab finished loading
cur_statusbar_message = pyqtSignal(str) # Status bar message
# FIXME we need to store this in our browser object
# Current tab changed scroll position
cur_scroll_perc_changed = pyqtSignal(int, int)
keypress = pyqtSignal('QKeyEvent')
_url_stack = [] # Stack of URLs of closed tabs
_url_stack = [] # Stack of URLs of closed tabs
def __init__(self, parent):
super().__init__(parent)
@ -204,12 +205,13 @@ class TabbedBrowser(TabWidget):
round(100 * y / m[1]) if m[1] != 0 else 0)
self.cur_scroll_perc_changed.emit(*perc)
class BrowserTab(QWebView):
"""One browser tab in TabbedBrowser"""
progress = 0
scroll_pos_changed = pyqtSignal(int, int)
_scroll_pos = (-1, -1)
open_new_tab = False # open new tab for the next action
open_new_tab = False # open new tab for the next action
open_tab = pyqtSignal('QUrl')
def __init__(self, parent):

View File

@ -1,4 +1,3 @@
import logging
import html
from PyQt5.QtWidgets import (QTreeView, QStyledItemDelegate, QStyle,
@ -12,6 +11,7 @@ import qutebrowser.utils.config as config
from qutebrowser.utils.completion import CompletionFilterModel
from qutebrowser.commands.utils import CommandCompletionModel
class CompletionView(QTreeView):
_stylesheet = """
QTreeView {{
@ -72,7 +72,7 @@ class CompletionView(QTreeView):
def resizeEvent(self, e):
width = e.size().width()
for i in range(self.model.columnCount()):
self.setColumnWidth(i, width/2)
self.setColumnWidth(i, width / 2)
super().resizeEvent(e)
def setmodel(self, model):
@ -166,6 +166,7 @@ class CompletionView(QTreeView):
# Item is a real item, not a category header -> success
return idx
class CompletionItemDelegate(QStyledItemDelegate):
opt = None
style = None
@ -203,7 +204,6 @@ class CompletionItemDelegate(QStyledItemDelegate):
self.opt.icon.paint(self.painter, icon_rect,
self.opt.decorationAlignment, mode, state)
def _draw_text(self, index):
if not self.opt.text:
return

View File

@ -4,6 +4,7 @@ from qutebrowser.widgets.statusbar import StatusBar
from qutebrowser.widgets.browser import TabbedBrowser
from qutebrowser.widgets.completion import CompletionView
class MainWindow(QMainWindow):
"""The main window of QuteBrowser"""
cwidget = None
@ -35,7 +36,7 @@ class MainWindow(QMainWindow):
self.tabs.cur_progress.connect(self.status.prog.set_progress)
self.tabs.cur_load_finished.connect(self.status.prog.load_finished)
self.tabs.cur_load_started.connect(lambda:
self.status.prog.set_progress(0))
self.status.prog.set_progress(0))
self.tabs.cur_scroll_perc_changed.connect(self.status.txt.set_perc)
self.tabs.cur_statusbar_message.connect(self.status.txt.set_text)
self.status.cmd.esc_pressed.connect(self.tabs.setFocus)
@ -47,4 +48,3 @@ class MainWindow(QMainWindow):
#self.retranslateUi(MainWindow)
#self.tabWidget.setCurrentIndex(0)
#QtCore.QMetaObject.connectSlotsByName(MainWindow)

View File

@ -1,5 +1,3 @@
import logging
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import QHBoxLayout, QWidget
@ -8,6 +6,7 @@ from qutebrowser.widgets.statusbar.command import Command
from qutebrowser.widgets.statusbar.text import Text
from qutebrowser.widgets.statusbar.progress import Progress
class StatusBar(QWidget):
"""The statusbar at the bottom of the mainwindow"""
hbox = None

View File

@ -4,22 +4,22 @@ from PyQt5.QtWidgets import QLineEdit, QShortcut
from PyQt5.QtCore import pyqtSignal, Qt
from PyQt5.QtGui import QValidator, QKeySequence
from qutebrowser.commands.utils import CommandCompletionModel
class Command(QLineEdit):
"""The commandline part of the statusbar"""
got_cmd = pyqtSignal(str) # Emitted when a command is triggered by the user
bar = None # The status bar object
esc_pressed = pyqtSignal() # Emitted when escape is pressed
tab_pressed = pyqtSignal(bool) # Emitted when tab is pressed (arg: shift)
hide_completion = pyqtSignal() # Hide completion window
history = [] # The command history, with newer commands at the bottom
# Emitted when a command is triggered by the user
got_cmd = pyqtSignal(str)
bar = None # The status bar object
esc_pressed = pyqtSignal() # Emitted when escape is pressed
tab_pressed = pyqtSignal(bool) # Emitted when tab is pressed (arg: shift)
hide_completion = pyqtSignal() # Hide completion window
history = [] # The command history, with newer commands at the bottom
_tmphist = []
_histpos = None
# FIXME won't the tab key switch to the next widget?
# See http://www.saltycrane.com/blog/2008/01/how-to-capture-tab-key-press-event-with/
# See
# noqa http://www.saltycrane.com/blog/2008/01/how-to-capture-tab-key-press-event-with/
# for a possible fix.
def __init__(self, bar):
@ -103,7 +103,7 @@ class Command(QLineEdit):
def key_down_handler(self):
logging.debug("history up [pre]: pos {}".format(self._histpos,
self._tmphist, len(self._tmphist), self._histpos))
self._tmphist, len(self._tmphist), self._histpos))
if (self._histpos is None or
self._histpos >= len(self._tmphist) - 1 or
not self._tmphist):
@ -119,6 +119,7 @@ class Command(QLineEdit):
def key_stab_handler(self):
self.tab_pressed.emit(True)
class Validator(QValidator):
"""Validator to prevent the : from getting deleted"""
def validate(self, string, pos):
@ -126,4 +127,3 @@ class Validator(QValidator):
return (QValidator.Acceptable, string, pos)
else:
return (QValidator.Invalid, string, pos)

View File

@ -3,6 +3,7 @@ import qutebrowser.utils.config as config
from PyQt5.QtWidgets import QProgressBar, QSizePolicy
from PyQt5.QtCore import QSize
class Progress(QProgressBar):
""" The progress bar part of the status bar"""
bar = None
@ -59,4 +60,3 @@ class Progress(QProgressBar):
self.hide()
else:
self.color = config.colordict.getraw('statusbar.progress.bg.error')

View File

@ -1,6 +1,7 @@
import logging
from PyQt5.QtWidgets import QLabel
class Text(QLabel):
"""The text part of the status bar, composed of several 'widgets'"""
keystring = ''
@ -37,4 +38,3 @@ class Text(QLabel):
"""Update the text displayed"""
self.setText(' '.join([self.keystring, self.error, self.text,
self.scrollperc]))

View File

@ -3,6 +3,7 @@ from PyQt5.QtCore import Qt
import qutebrowser.utils.config as config
class TabWidget(QTabWidget):
"""The tabwidget used for TabbedBrowser"""