Revert "Use global object dict for global objects"

This reverts commit bf2c7831c8.

This was a bad idea as we need to access some of the main attributes
from commands via instance=...
This commit is contained in:
Florian Bruhin 2014-05-05 20:12:20 +02:00
parent 083ad97953
commit c8fd0937b0
11 changed files with 74 additions and 119 deletions

View File

@ -64,7 +64,6 @@ from qutebrowser.browser.cookies import CookieJar
from qutebrowser.utils.message import MessageBridge from qutebrowser.utils.message import MessageBridge
from qutebrowser.utils.appdirs import AppDirs from qutebrowser.utils.appdirs import AppDirs
from qutebrowser.utils.misc import dotted_getattr from qutebrowser.utils.misc import dotted_getattr
from qutebrowser.utils.usertypes import ImmutableDict
from qutebrowser.utils.debug import set_trace # pylint: disable=unused-import from qutebrowser.utils.debug import set_trace # pylint: disable=unused-import
@ -79,7 +78,15 @@ class QuteBrowser(QApplication):
Attributes: Attributes:
mainwindow: The MainWindow QWidget. mainwindow: The MainWindow QWidget.
obj: Dictionary containing global singleton objects. commandmanager: The main CommandManager instance.
searchmanager: The main SearchManager instance.
config: The main ConfigManager
stateconfig: The "state" ReadWriteConfigParser instance.
cmd_history: The "cmd_history" LineConfigParser instance.
messagebridge: The global MessageBridge instance.
modeman: The global ModeManager instance.
networkmanager: The global NetworkManager instance.
cookiejar: The global CookieJar instance.
_keyparsers: A mapping from modes to keyparsers. _keyparsers: A mapping from modes to keyparsers.
_dirs: AppDirs instance for config/cache directories. _dirs: AppDirs instance for config/cache directories.
_args: ArgumentParser instance. _args: ArgumentParser instance.
@ -89,6 +96,9 @@ class QuteBrowser(QApplication):
_opened_urls: List of opened URLs. _opened_urls: List of opened URLs.
""" """
# This also holds all our globals, so we're a bit over the top here.
# pylint: disable=too-many-instance-attributes
def __init__(self): def __init__(self):
super().__init__(sys.argv) super().__init__(sys.argv)
self._quit_status = {} self._quit_status = {}
@ -97,7 +107,11 @@ class QuteBrowser(QApplication):
self._shutting_down = False self._shutting_down = False
self._keyparsers = None self._keyparsers = None
self._dirs = None self._dirs = None
self.obj = ImmutableDict() self.messagebridge = None
self.stateconfig = None
self.modeman = None
self.cmd_history = None
self.config = None
sys.excepthook = self._exception_hook sys.excepthook = self._exception_hook
@ -107,18 +121,18 @@ class QuteBrowser(QApplication):
self._init_config() self._init_config()
self._init_modes() self._init_modes()
websettings.init(self._dirs.user_cache_dir) websettings.init(self._dirs.user_cache_dir)
self.obj['cookiejar'] = CookieJar(self._dirs.user_data_dir) self.cookiejar = CookieJar(self._dirs.user_data_dir)
self.obj['networkmanager'] = NetworkManager(self.obj['cookiejar']) self.networkmanager = NetworkManager(self.cookiejar)
self.obj['commandmanager'] = CommandManager() self.commandmanager = CommandManager()
self.obj['searchmanager'] = SearchManager() self.searchmanager = SearchManager()
self._init_cmds() self._init_cmds()
self.mainwindow = MainWindow() self.mainwindow = MainWindow()
self.installEventFilter(self.obj['modeman']) self.installEventFilter(self.modeman)
self.setQuitOnLastWindowClosed(False) self.setQuitOnLastWindowClosed(False)
self._connect_signals() self._connect_signals()
self.obj['modeman'].enter('normal') self.modeman.enter('normal')
self.mainwindow.show() self.mainwindow.show()
self._python_hacks() self._python_hacks()
@ -154,7 +168,7 @@ class QuteBrowser(QApplication):
else: else:
confdir = self._args.confdir confdir = self._args.confdir
try: try:
self.obj['config'] = ConfigManager(confdir, 'qutebrowser.conf') self.config = ConfigManager(confdir, 'qutebrowser.conf')
except (config.ValidationError, except (config.ValidationError,
config.NoOptionError, config.NoOptionError,
configparser.InterpolationError, configparser.InterpolationError,
@ -171,9 +185,9 @@ class QuteBrowser(QApplication):
msgbox.exec_() msgbox.exec_()
# We didn't really initialize much so far, so we just quit hard. # We didn't really initialize much so far, so we just quit hard.
sys.exit(1) sys.exit(1)
self.obj['stateconfig'] = ReadWriteConfigParser(confdir, 'state') self.stateconfig = ReadWriteConfigParser(confdir, 'state')
self.obj['cmd_history'] = LineConfigParser( self.cmd_history = LineConfigParser(confdir, 'cmd_history',
confdir, 'cmd_history', ('completion', 'history-length')) ('completion', 'history-length'))
def _init_modes(self): def _init_modes(self):
"""Inizialize the mode manager and the keyparsers.""" """Inizialize the mode manager and the keyparsers."""
@ -184,16 +198,16 @@ class QuteBrowser(QApplication):
'passthrough': PassthroughKeyParser('keybind.passthrough', self), 'passthrough': PassthroughKeyParser('keybind.passthrough', self),
'command': PassthroughKeyParser('keybind.command', self), 'command': PassthroughKeyParser('keybind.command', self),
} }
modeman = ModeManager() self.modeman = ModeManager()
modeman.register('normal', self._keyparsers['normal'].handle) self.modeman.register('normal', self._keyparsers['normal'].handle)
modeman.register('hint', self._keyparsers['hint'].handle) self.modeman.register('hint', self._keyparsers['hint'].handle)
modeman.register('insert', self._keyparsers['insert'].handle, self.modeman.register('insert', self._keyparsers['insert'].handle,
passthrough=True) passthrough=True)
modeman.register('passthrough', self._keyparsers['passthrough'].handle, self.modeman.register('passthrough',
self._keyparsers['passthrough'].handle,
passthrough=True) passthrough=True)
modeman.register('command', self._keyparsers['command'].handle, self.modeman.register('command', self._keyparsers['command'].handle,
passthrough=True) passthrough=True)
self.obj['modeman'] = modeman
def _init_log(self): def _init_log(self):
"""Initialisation of the logging output. """Initialisation of the logging output.
@ -217,7 +231,7 @@ class QuteBrowser(QApplication):
os.environ['QT_FATAL_WARNINGS'] = '1' os.environ['QT_FATAL_WARNINGS'] = '1'
self.setApplicationName("qutebrowser") self.setApplicationName("qutebrowser")
self.setApplicationVersion(qutebrowser.__version__) self.setApplicationVersion(qutebrowser.__version__)
self.obj['messagebridge'] = MessageBridge() self.messagebridge = MessageBridge()
def _init_cmds(self): def _init_cmds(self):
"""Initialisation of the qutebrowser commands. """Initialisation of the qutebrowser commands.
@ -246,7 +260,7 @@ class QuteBrowser(QApplication):
for e in self._args.command: for e in self._args.command:
if e.startswith(':'): if e.startswith(':'):
logging.debug("Startup cmd {}".format(e)) logging.debug("Startup cmd {}".format(e))
self.obj['commandmanager'].run_safely(e.lstrip(':')) self.commandmanager.run_safely(e.lstrip(':'))
else: else:
logging.debug("Startup url {}".format(e)) logging.debug("Startup url {}".format(e))
self._opened_urls.append(e) self._opened_urls.append(e)
@ -254,7 +268,7 @@ class QuteBrowser(QApplication):
if self.mainwindow.tabs.count() == 0: if self.mainwindow.tabs.count() == 0:
logging.debug("Opening startpage") logging.debug("Opening startpage")
for url in self.obj['config'].get('general', 'startpage'): for url in self.config.get('general', 'startpage'):
self.mainwindow.tabs.tabopen(url) self.mainwindow.tabs.tabopen(url)
def _python_hacks(self): def _python_hacks(self):
@ -278,10 +292,6 @@ class QuteBrowser(QApplication):
completion = self.mainwindow.completion completion = self.mainwindow.completion
tabs = self.mainwindow.tabs tabs = self.mainwindow.tabs
cmd = self.mainwindow.status.cmd cmd = self.mainwindow.status.cmd
searchmanager = self.obj['searchmanager']
conf = self.obj['config']
messagebridge = self.obj['messagebridge']
modeman = self.obj['modeman']
# misc # misc
self.lastWindowClosed.connect(self.shutdown) self.lastWindowClosed.connect(self.shutdown)
@ -289,17 +299,17 @@ class QuteBrowser(QApplication):
tabs.currentChanged.connect(self.mainwindow.update_inspector) tabs.currentChanged.connect(self.mainwindow.update_inspector)
# status bar # status bar
modeman.entered.connect(status.on_mode_entered) self.modeman.entered.connect(status.on_mode_entered)
modeman.left.connect(status.on_mode_left) self.modeman.left.connect(status.on_mode_left)
modeman.left.connect(status.cmd.on_mode_left) self.modeman.left.connect(status.cmd.on_mode_left)
modeman.key_pressed.connect(status.on_key_pressed) self.modeman.key_pressed.connect(status.on_key_pressed)
# commands # commands
cmd.got_cmd.connect(self.obj['commandmanager'].run_safely) cmd.got_cmd.connect(self.commandmanager.run_safely)
cmd.got_search.connect(searchmanager.search) cmd.got_search.connect(self.searchmanager.search)
cmd.got_search_rev.connect(searchmanager.search_rev) cmd.got_search_rev.connect(self.searchmanager.search_rev)
cmd.returnPressed.connect(tabs.setFocus) cmd.returnPressed.connect(tabs.setFocus)
searchmanager.do_search.connect(tabs.cur.search) self.searchmanager.do_search.connect(tabs.cur.search)
kp['normal'].keystring_updated.connect(status.keystring.setText) kp['normal'].keystring_updated.connect(status.keystring.setText)
# hints # hints
@ -309,16 +319,16 @@ class QuteBrowser(QApplication):
tabs.hint_strings_updated.connect(kp['hint'].on_hint_strings_updated) tabs.hint_strings_updated.connect(kp['hint'].on_hint_strings_updated)
# messages # messages
messagebridge.error.connect(status.disp_error) self.messagebridge.error.connect(status.disp_error)
messagebridge.info.connect(status.txt.set_temptext) self.messagebridge.info.connect(status.txt.set_temptext)
messagebridge.text.connect(status.txt.set_normaltext) self.messagebridge.text.connect(status.txt.set_normaltext)
messagebridge.set_cmd_text.connect(cmd.set_cmd_text) self.messagebridge.set_cmd_text.connect(cmd.set_cmd_text)
# config # config
conf.style_changed.connect(style.invalidate_caches) self.config.style_changed.connect(style.invalidate_caches)
for obj in [tabs, completion, self.mainwindow, self.obj['cmd_history'], for obj in [tabs, completion, self.mainwindow, self.cmd_history,
websettings, kp['normal'], modeman]: websettings, kp['normal'], self.modeman]:
conf.changed.connect(obj.on_config_changed) self.config.changed.connect(obj.on_config_changed)
# statusbar # statusbar
tabs.cur_progress.connect(status.prog.setValue) tabs.cur_progress.connect(status.prog.setValue)
@ -331,7 +341,7 @@ class QuteBrowser(QApplication):
tabs.cur_link_hovered.connect(status.url.set_hover_url) tabs.cur_link_hovered.connect(status.url.set_hover_url)
# command input / completion # command input / completion
modeman.left.connect(tabs.on_mode_left) self.modeman.left.connect(tabs.on_mode_left)
cmd.clear_completion_selection.connect( cmd.clear_completion_selection.connect(
completion.on_clear_completion_selection) completion.on_clear_completion_selection)
cmd.hide_completion.connect(completion.hide) cmd.hide_completion.connect(completion.hide)
@ -364,10 +374,10 @@ class QuteBrowser(QApplication):
"""Save the window geometry to the state config.""" """Save the window geometry to the state config."""
geom = b64encode(bytes(self.mainwindow.saveGeometry())).decode('ASCII') geom = b64encode(bytes(self.mainwindow.saveGeometry())).decode('ASCII')
try: try:
self.obj['stateconfig'].add_section('geometry') self.stateconfig.add_section('geometry')
except configparser.DuplicateSectionError: except configparser.DuplicateSectionError:
pass pass
self.obj['stateconfig']['geometry']['mainwindow'] = geom self.stateconfig['geometry']['mainwindow'] = geom
def _exception_hook(self, exctype, excvalue, tb): def _exception_hook(self, exctype, excvalue, tb):
"""Handle uncaught python exceptions. """Handle uncaught python exceptions.
@ -484,22 +494,22 @@ class QuteBrowser(QApplication):
return return
self._shutting_down = True self._shutting_down = True
logging.debug("Shutting down... (do_quit={})".format(do_quit)) logging.debug("Shutting down... (do_quit={})".format(do_quit))
if self.obj['config'].get('general', 'auto-save-config'): if self.config.get('general', 'auto-save-config'):
try: try:
self.obj['config'].save() self.config.save()
except AttributeError: except AttributeError:
logging.exception("Could not save config.") logging.exception("Could not save config.")
try: try:
self.obj['cmd_history'].save() self.cmd_history.save()
except AttributeError: except AttributeError:
logging.exception("Could not save command history.") logging.exception("Could not save command history.")
try: try:
self._save_geometry() self._save_geometry()
self.obj['stateconfig'].save() self.stateconfig.save()
except AttributeError: except AttributeError:
logging.exception("Could not save window geometry.") logging.exception("Could not save window geometry.")
try: try:
self.obj['cookiejar'].save() self.cookiejar.save()
except AttributeError: except AttributeError:
logging.exception("Could not save cookies.") logging.exception("Could not save cookies.")
try: try:

View File

@ -41,8 +41,7 @@ class BrowserPage(QWebPage):
self._extension_handlers = { self._extension_handlers = {
QWebPage.ErrorPageExtension: self._handle_errorpage, QWebPage.ErrorPageExtension: self._handle_errorpage,
} }
netman = QApplication.instance().obj['networkmanager'] self.setNetworkAccessManager(QApplication.instance().networkmanager)
self.setNetworkAccessManager(netman)
def _handle_errorpage(self, opt, out): def _handle_errorpage(self, opt, out):
"""Display an error page if needed. """Display an error page if needed.

View File

@ -85,7 +85,7 @@ class Command(QObject):
""" """
# We don't use modeman.instance() here to avoid a circular import # We don't use modeman.instance() here to avoid a circular import
# of qutebrowser.keyinput.modeman. # of qutebrowser.keyinput.modeman.
curmode = QApplication.instance().obj['modeman'].mode curmode = QApplication.instance().modeman.mode
if self.modes is not None and curmode not in self.modes: if self.modes is not None and curmode not in self.modes:
raise PrerequisitesError("{}: This command is only allowed in {} " raise PrerequisitesError("{}: This command is only allowed in {} "
"mode.".format(self.name, "mode.".format(self.name,

View File

@ -42,7 +42,7 @@ from qutebrowser.config._conftypes import ValidationError
def instance(): def instance():
"""Get the global config instance.""" """Get the global config instance."""
return QApplication.instance().obj['config'] return QApplication.instance().config
def get(*args, **kwargs): def get(*args, **kwargs):

View File

@ -34,7 +34,7 @@ import qutebrowser.utils.debug as debug
def instance(): def instance():
"""Get the global modeman instance.""" """Get the global modeman instance."""
return QApplication.instance().obj['modeman'] return QApplication.instance().modeman
def enter(mode): def enter(mode):

View File

@ -51,7 +51,7 @@ class History:
def __init__(self): def __init__(self):
self._tmphist = None self._tmphist = None
history = QApplication.instance().obj['cmd_history'].data history = QApplication.instance().cmd_history.data
if history is None: if history is None:
self._history = [] self._history = []
else: else:

View File

@ -1,44 +0,0 @@
# 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/>.
# pylint: disable=missing-docstring
"""Tests for the FakeDict class."""
import unittest
from unittest import TestCase
from qutebrowser.utils.usertypes import ImmutableDict
class ImmutableDictTests(TestCase):
"""Test the ImmutableDict usertype."""
def setUp(self):
self.d = ImmutableDict()
def test_setitem(self):
self.d['foo'] = 'bar'
self.assertEqual(self.d['foo'], 'bar')
with self.assertRaises(ValueError):
self.d['foo'] = 'baz'
self.assertEqual(self.d['foo'], 'bar')
if __name__ == '__main__':
unittest.main()

View File

@ -23,7 +23,7 @@ from PyQt5.QtWidgets import QApplication
def instance(): def instance():
"""Get the global messagebridge instance.""" """Get the global messagebridge instance."""
return QApplication.instance().obj['messagebridge'] return QApplication.instance().messagebridge
def error(message): def error(message):

View File

@ -182,13 +182,3 @@ class FakeDict:
def __getitem__(self, _key): def __getitem__(self, _key):
return self._val return self._val
class ImmutableDict(dict):
"""Dict where items only can be set once."""
def __setitem__(self, key, value):
if key in self:
raise ValueError("Key {} already has been set.".format(key))
super().__setitem__(key, value)

View File

@ -49,9 +49,9 @@ class MainWindow(QWidget):
super().__init__() super().__init__()
self.setWindowTitle('qutebrowser') self.setWindowTitle('qutebrowser')
stateconfig = QApplication.instance().obj['stateconfig']
try: try:
geom = b64decode(stateconfig['geometry']['mainwindow'], geom = b64decode(
QApplication.instance().stateconfig['geometry']['mainwindow'],
validate=True) validate=True)
except (KeyError, binascii.Error): except (KeyError, binascii.Error):
self._set_default_geometry() self._set_default_geometry()

View File

@ -209,7 +209,7 @@ class WebView(QWebView):
self.destroyed.connect(functools.partial(self._on_destroyed, self)) self.destroyed.connect(functools.partial(self._on_destroyed, self))
self.deleteLater() self.deleteLater()
netman = QApplication.instance().obj['networkmanager'] netman = QApplication.instance().networkmanager
self._destroyed[netman] = False self._destroyed[netman] = False
netman.abort_requests() netman.abort_requests()
netman.destroyed.connect(functools.partial(self._on_destroyed, netman)) netman.destroyed.connect(functools.partial(self._on_destroyed, netman))