Move qtutils.unset_organization to standarddir

This commit is contained in:
Florian Bruhin 2017-09-17 20:44:08 +02:00
parent f40103cbba
commit 70b8585e95
4 changed files with 43 additions and 35 deletions

View File

@ -35,7 +35,6 @@ import contextlib
from PyQt5.QtCore import (qVersion, QEventLoop, QDataStream, QByteArray, from PyQt5.QtCore import (qVersion, QEventLoop, QDataStream, QByteArray,
QIODevice, QSaveFile, QT_VERSION_STR) QIODevice, QSaveFile, QT_VERSION_STR)
from PyQt5.QtWidgets import QApplication
try: try:
from PyQt5.QtWebKit import qWebKitVersion from PyQt5.QtWebKit import qWebKitVersion
except ImportError: # pragma: no cover except ImportError: # pragma: no cover
@ -242,23 +241,6 @@ def savefile_open(filename, binary=False, encoding='utf-8'):
raise QtOSError(f, msg="Commit failed!") raise QtOSError(f, msg="Commit failed!")
@contextlib.contextmanager
def unset_organization():
"""Temporarily unset QApplication.organizationName().
This is primarily needed in config.py.
"""
qapp = QApplication.instance()
if qapp is not None:
orgname = qapp.organizationName()
qapp.setOrganizationName(None)
try:
yield
finally:
if qapp is not None:
qapp.setOrganizationName(orgname)
class PyQIODevice(io.BufferedIOBase): class PyQIODevice(io.BufferedIOBase):
"""Wrapper for a QIODevice which provides a python interface. """Wrapper for a QIODevice which provides a python interface.

View File

@ -23,10 +23,12 @@ import os
import sys import sys
import shutil import shutil
import os.path import os.path
import contextlib
from PyQt5.QtCore import QStandardPaths from PyQt5.QtCore import QStandardPaths
from PyQt5.QtWidgets import QApplication
from qutebrowser.utils import log, qtutils, debug, usertypes, message from qutebrowser.utils import log, debug, usertypes, message
# The cached locations # The cached locations
_locations = {} _locations = {}
@ -45,6 +47,23 @@ class EmptyValueError(Exception):
"""Error raised when QStandardPaths returns an empty value.""" """Error raised when QStandardPaths returns an empty value."""
@contextlib.contextmanager
def _unset_organization():
"""Temporarily unset QApplication.organizationName().
This is primarily needed in config.py.
"""
qapp = QApplication.instance()
if qapp is not None:
orgname = qapp.organizationName()
qapp.setOrganizationName(None)
try:
yield
finally:
if qapp is not None:
qapp.setOrganizationName(orgname)
def _init_config(args): def _init_config(args):
"""Initialize the location for configs.""" """Initialize the location for configs."""
typ = QStandardPaths.ConfigLocation typ = QStandardPaths.ConfigLocation
@ -204,7 +223,7 @@ def _writable_location(typ):
# FIXME old Qt # FIXME old Qt
getattr(QStandardPaths, 'AppDataLocation', object())], typ_str getattr(QStandardPaths, 'AppDataLocation', object())], typ_str
with qtutils.unset_organization(): with _unset_organization():
path = QStandardPaths.writableLocation(typ) path = QStandardPaths.writableLocation(typ)
log.misc.debug("writable location for {}: {}".format(typ_str, path)) log.misc.debug("writable location for {}: {}".format(typ_str, path))

View File

@ -530,21 +530,6 @@ class TestSavefileOpen:
assert data == b'foo\nbar\nbaz' assert data == b'foo\nbar\nbaz'
@pytest.mark.parametrize('orgname, expected', [(None, ''), ('test', 'test')])
def test_unset_organization(qapp, orgname, expected):
"""Test unset_organization.
Args:
orgname: The organizationName to set initially.
expected: The organizationName which is expected when reading back.
"""
qapp.setOrganizationName(orgname)
assert qapp.organizationName() == expected # sanity check
with qtutils.unset_organization():
assert qapp.organizationName() == ''
assert qapp.organizationName() == expected
if test_file is not None and sys.platform != 'darwin': if test_file is not None and sys.platform != 'darwin':
# If we were able to import Python's test_file module, we run some code # If we were able to import Python's test_file module, we run some code
# here which defines unittest TestCases to run the python tests over # here which defines unittest TestCases to run the python tests over

View File

@ -56,6 +56,28 @@ def clear_standarddir_cache_and_patch(qapp, monkeypatch):
monkeypatch.setattr(standarddir, '_locations', {}) monkeypatch.setattr(standarddir, '_locations', {})
@pytest.mark.parametrize('orgname, expected', [(None, ''), ('test', 'test')])
def test_unset_organization(qapp, orgname, expected):
"""Test unset_organization.
Args:
orgname: The organizationName to set initially.
expected: The organizationName which is expected when reading back.
"""
qapp.setOrganizationName(orgname)
assert qapp.organizationName() == expected # sanity check
with standarddir._unset_organization():
assert qapp.organizationName() == ''
assert qapp.organizationName() == expected
def test_unset_organization_no_qapp(monkeypatch):
"""Without a QApplication, _unset_organization should do nothing."""
monkeypatch.setattr(standarddir.QApplication, 'instance', lambda: None)
with standarddir._unset_organization():
pass
def test_fake_mac_config(tmpdir, monkeypatch): def test_fake_mac_config(tmpdir, monkeypatch):
"""Test standardir.config on a fake Mac.""" """Test standardir.config on a fake Mac."""
monkeypatch.setattr(sys, 'platform', 'darwin') monkeypatch.setattr(sys, 'platform', 'darwin')