Move utils.get_standard_dir to its own file.
This is a preparation for #19 and #20 because there are too many functions related to standarddir in utils.
This commit is contained in:
parent
86f08a8536
commit
0e7a60abf6
@ -43,7 +43,7 @@ from qutebrowser.browser import quickmarks, cookies, downloads, cache
|
||||
from qutebrowser.widgets import mainwindow, console, crash
|
||||
from qutebrowser.keyinput import modeman
|
||||
from qutebrowser.utils import (log, version, message, readline, utils, qtutils,
|
||||
urlutils, debug, objreg, usertypes)
|
||||
urlutils, debug, objreg, usertypes, standarddir)
|
||||
# We import utilcmds to run the cmdutils.register decorators.
|
||||
from qutebrowser.utils import utilcmds # pylint: disable=unused-import
|
||||
|
||||
@ -170,7 +170,7 @@ class Application(QApplication):
|
||||
# dialogs at all.
|
||||
# probably irrelevant with single instance support
|
||||
# https://github.com/The-Compiler/qutebrowser/issues/10
|
||||
path = utils.get_standard_dir(QStandardPaths.DataLocation)
|
||||
path = standarddir.get(QStandardPaths.DataLocation)
|
||||
logname = os.path.join(path, 'crash.log')
|
||||
# First check if an old logfile exists.
|
||||
if os.path.exists(logname):
|
||||
@ -205,7 +205,7 @@ class Application(QApplication):
|
||||
|
||||
def _init_crashlogfile(self):
|
||||
"""Start a new logfile and redirect faulthandler to it."""
|
||||
path = utils.get_standard_dir(QStandardPaths.DataLocation)
|
||||
path = standarddir.get(QStandardPaths.DataLocation)
|
||||
logname = os.path.join(path, 'crash.log')
|
||||
self._crashlogfile = open(logname, 'w', encoding='ascii')
|
||||
faulthandler.enable(self._crashlogfile)
|
||||
|
@ -25,7 +25,7 @@ from PyQt5.QtCore import QStandardPaths
|
||||
from PyQt5.QtNetwork import QNetworkDiskCache
|
||||
|
||||
from qutebrowser.config import config
|
||||
from qutebrowser.utils import utils
|
||||
from qutebrowser.utils import utils, standarddir
|
||||
|
||||
|
||||
class DiskCache(QNetworkDiskCache):
|
||||
@ -34,7 +34,7 @@ class DiskCache(QNetworkDiskCache):
|
||||
|
||||
def __init__(self, parent=None):
|
||||
super().__init__(parent)
|
||||
cache_dir = utils.get_standard_dir(QStandardPaths.CacheLocation)
|
||||
cache_dir = standarddir.get(QStandardPaths.CacheLocation)
|
||||
self.setCacheDirectory(os.path.join(cache_dir, 'http'))
|
||||
self.setMaximumCacheSize(config.get('storage', 'cache-size'))
|
||||
|
||||
|
@ -23,7 +23,7 @@ from PyQt5.QtNetwork import QNetworkCookie, QNetworkCookieJar
|
||||
from PyQt5.QtCore import QStandardPaths, QDateTime
|
||||
|
||||
from qutebrowser.config import config, lineparser
|
||||
from qutebrowser.utils import utils
|
||||
from qutebrowser.utils import utils, standarddir
|
||||
|
||||
|
||||
class CookieJar(QNetworkCookieJar):
|
||||
@ -32,7 +32,7 @@ class CookieJar(QNetworkCookieJar):
|
||||
|
||||
def __init__(self, parent=None):
|
||||
super().__init__(parent)
|
||||
datadir = utils.get_standard_dir(QStandardPaths.DataLocation)
|
||||
datadir = standarddir.get(QStandardPaths.DataLocation)
|
||||
self._linecp = lineparser.LineConfigParser(datadir, 'cookies',
|
||||
binary=True)
|
||||
cookies = []
|
||||
|
@ -32,7 +32,7 @@ from PyQt5.QtWebKitWidgets import QWebPage # pylint: disable=unused-import
|
||||
from qutebrowser.config import config
|
||||
from qutebrowser.commands import cmdexc, cmdutils
|
||||
from qutebrowser.utils import (message, http, usertypes, log, utils, urlutils,
|
||||
objreg)
|
||||
objreg, standarddir)
|
||||
|
||||
|
||||
class DownloadItem(QObject):
|
||||
@ -225,7 +225,7 @@ class DownloadItem(QObject):
|
||||
# save it under that filename in the default directory.
|
||||
download_dir = config.get('storage', 'download-directory')
|
||||
if download_dir is None:
|
||||
download_dir = utils.get_standard_dir(
|
||||
download_dir = standarddir.get(
|
||||
QStandardPaths.DownloadLocation)
|
||||
self._filename = os.path.join(download_dir, filename)
|
||||
self.basename = filename
|
||||
|
@ -29,7 +29,7 @@ import collections
|
||||
|
||||
from PyQt5.QtCore import QStandardPaths, QUrl
|
||||
|
||||
from qutebrowser.utils import message, usertypes, utils, urlutils
|
||||
from qutebrowser.utils import message, usertypes, urlutils, standarddir
|
||||
from qutebrowser.commands import cmdexc, cmdutils
|
||||
from qutebrowser.config import lineparser
|
||||
|
||||
@ -41,7 +41,7 @@ linecp = None
|
||||
def init():
|
||||
"""Read quickmarks from the config file."""
|
||||
global linecp
|
||||
confdir = utils.get_standard_dir(QStandardPaths.ConfigLocation)
|
||||
confdir = standarddir.get(QStandardPaths.ConfigLocation)
|
||||
linecp = lineparser.LineConfigParser(confdir, 'quickmarks')
|
||||
for line in linecp:
|
||||
try:
|
||||
|
@ -27,7 +27,7 @@ import select
|
||||
from PyQt5.QtCore import (pyqtSignal, QObject, QThread, QStandardPaths,
|
||||
QProcessEnvironment, QProcess, QUrl)
|
||||
|
||||
from qutebrowser.utils import message, log, utils, objreg
|
||||
from qutebrowser.utils import message, log, objreg, standarddir
|
||||
from qutebrowser.commands import runners, cmdexc
|
||||
|
||||
|
||||
@ -196,7 +196,7 @@ class _POSIXUserscriptRunner(_BaseUserscriptRunner):
|
||||
self._thread = None
|
||||
|
||||
def run(self, cmd, *args, env=None):
|
||||
rundir = utils.get_standard_dir(QStandardPaths.RuntimeLocation)
|
||||
rundir = standarddir.get(QStandardPaths.RuntimeLocation)
|
||||
# tempfile.mktemp is deprecated and discouraged, but we use it here to
|
||||
# create a FIFO since the only other alternative would be to create a
|
||||
# directory and place the FIFO there, which sucks. Since os.kfifo will
|
||||
|
@ -36,11 +36,10 @@ import collections.abc
|
||||
from PyQt5.QtCore import pyqtSignal, QObject, QStandardPaths
|
||||
from PyQt5.QtWidgets import QMessageBox
|
||||
|
||||
from qutebrowser.utils import log
|
||||
from qutebrowser.config import (configdata, iniparsers, configtypes,
|
||||
textwrapper, keyconfparser)
|
||||
from qutebrowser.commands import cmdexc, cmdutils
|
||||
from qutebrowser.utils import message, objreg, utils
|
||||
from qutebrowser.utils import message, objreg, utils, standarddir, log
|
||||
from qutebrowser.utils.usertypes import Completion
|
||||
|
||||
|
||||
@ -87,7 +86,7 @@ def init(args):
|
||||
Args:
|
||||
args: The argparse namespace.
|
||||
"""
|
||||
confdir = utils.get_standard_dir(QStandardPaths.ConfigLocation, args)
|
||||
confdir = standarddir.get(QStandardPaths.ConfigLocation, args)
|
||||
try:
|
||||
app = objreg.get('app')
|
||||
config_obj = ConfigManager(confdir, 'qutebrowser.conf', app)
|
||||
@ -125,7 +124,7 @@ def init(args):
|
||||
else:
|
||||
objreg.register('key-config', key_config)
|
||||
|
||||
datadir = utils.get_standard_dir(QStandardPaths.DataLocation, args)
|
||||
datadir = standarddir.get(QStandardPaths.DataLocation, args)
|
||||
state_config = iniparsers.ReadWriteConfigParser(datadir, 'state')
|
||||
objreg.register('state-config', state_config)
|
||||
# We need to import this here because lineparser needs config.
|
||||
|
@ -32,7 +32,7 @@ from PyQt5.QtWebKit import QWebSettings
|
||||
from PyQt5.QtCore import QStandardPaths
|
||||
|
||||
from qutebrowser.config import config
|
||||
from qutebrowser.utils import usertypes, utils
|
||||
from qutebrowser.utils import usertypes, standarddir
|
||||
|
||||
MapType = usertypes.enum('MapType', ['attribute', 'setter', 'static_setter'])
|
||||
|
||||
@ -176,11 +176,11 @@ def _set_setting(typ, arg, value):
|
||||
|
||||
def init():
|
||||
"""Initialize the global QWebSettings."""
|
||||
cachedir = utils.get_standard_dir(QStandardPaths.CacheLocation)
|
||||
cachedir = standarddir.get(QStandardPaths.CacheLocation)
|
||||
QWebSettings.setIconDatabasePath(cachedir)
|
||||
QWebSettings.setOfflineWebApplicationCachePath(
|
||||
os.path.join(cachedir, 'application-cache'))
|
||||
datadir = utils.get_standard_dir(QStandardPaths.DataLocation)
|
||||
datadir = standarddir.get(QStandardPaths.DataLocation)
|
||||
QWebSettings.globalSettings().setLocalStoragePath(
|
||||
os.path.join(datadir, 'local-storage'))
|
||||
QWebSettings.setOfflineStoragePath(
|
||||
|
156
qutebrowser/test/utils/test_standarddir.py
Normal file
156
qutebrowser/test/utils/test_standarddir.py
Normal file
@ -0,0 +1,156 @@
|
||||
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
||||
|
||||
# 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/>.
|
||||
|
||||
"""Tests for qutebrowser.utils.standarddir."""
|
||||
|
||||
import os
|
||||
import os.path
|
||||
import sys
|
||||
import shutil
|
||||
import unittest
|
||||
import tempfile
|
||||
|
||||
from PyQt5.QtCore import QStandardPaths, QCoreApplication
|
||||
|
||||
from qutebrowser.utils import standarddir
|
||||
from qutebrowser.test import helpers
|
||||
|
||||
|
||||
class GetStandardDirLinuxTests(unittest.TestCase):
|
||||
|
||||
"""Tests for standarddir.get under Linux.
|
||||
|
||||
Attributes:
|
||||
temp_dir: A temporary directory.
|
||||
app: The QCoreApplication used.
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
self.temp_dir = tempfile.mkdtemp()
|
||||
self.app = QCoreApplication([])
|
||||
self.app.setApplicationName('qutebrowser')
|
||||
|
||||
@unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux")
|
||||
def test_data_explicit(self):
|
||||
"""Test data dir with XDG_DATA_HOME explicitely set."""
|
||||
with helpers.environ_set_temp('XDG_DATA_HOME', self.temp_dir):
|
||||
cur_dir = standarddir.get(QStandardPaths.DataLocation)
|
||||
self.assertEqual(cur_dir, os.path.join(self.temp_dir,
|
||||
'qutebrowser'))
|
||||
self.assertTrue(os.path.exists(cur_dir))
|
||||
|
||||
@unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux")
|
||||
def test_config_explicit(self):
|
||||
"""Test config dir with XDG_CONFIG_HOME explicitely set."""
|
||||
with helpers.environ_set_temp('XDG_CONFIG_HOME', self.temp_dir):
|
||||
cur_dir = standarddir.get(QStandardPaths.ConfigLocation)
|
||||
self.assertEqual(cur_dir, os.path.join(self.temp_dir,
|
||||
'qutebrowser'))
|
||||
self.assertTrue(os.path.exists(cur_dir))
|
||||
|
||||
@unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux")
|
||||
def test_cache_explicit(self):
|
||||
"""Test cache dir with XDG_CACHE_HOME explicitely set."""
|
||||
with helpers.environ_set_temp('XDG_CACHE_HOME', self.temp_dir):
|
||||
cur_dir = standarddir.get(QStandardPaths.CacheLocation)
|
||||
self.assertEqual(cur_dir, os.path.join(self.temp_dir,
|
||||
'qutebrowser'))
|
||||
self.assertTrue(os.path.exists(cur_dir))
|
||||
|
||||
@unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux")
|
||||
def test_data(self):
|
||||
"""Test data dir with XDG_DATA_HOME not set."""
|
||||
with helpers.environ_set_temp('HOME', self.temp_dir):
|
||||
cur_dir = standarddir.get(QStandardPaths.DataLocation)
|
||||
self.assertEqual(cur_dir, os.path.join(self.temp_dir, '.local',
|
||||
'share', 'qutebrowser'))
|
||||
self.assertTrue(os.path.exists(cur_dir))
|
||||
|
||||
@unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux")
|
||||
def test_config(self):
|
||||
"""Test config dir with XDG_CONFIG_HOME not set."""
|
||||
with helpers.environ_set_temp('HOME', self.temp_dir):
|
||||
cur_dir = standarddir.get(
|
||||
QStandardPaths.ConfigLocation)
|
||||
self.assertEqual(cur_dir, os.path.join(self.temp_dir, '.config',
|
||||
'qutebrowser'))
|
||||
self.assertTrue(os.path.exists(cur_dir))
|
||||
|
||||
@unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux")
|
||||
def test_cache(self):
|
||||
"""Test cache dir with XDG_CACHE_HOME not set."""
|
||||
with helpers.environ_set_temp('HOME', self.temp_dir):
|
||||
cur_dir = standarddir.get(QStandardPaths.CacheLocation)
|
||||
self.assertEqual(cur_dir, os.path.join(self.temp_dir, '.cache',
|
||||
'qutebrowser'))
|
||||
self.assertTrue(os.path.exists(cur_dir))
|
||||
|
||||
def tearDown(self):
|
||||
self.app.quit()
|
||||
shutil.rmtree(self.temp_dir)
|
||||
|
||||
|
||||
class GetStandardDirWindowsTests(unittest.TestCase):
|
||||
|
||||
"""Tests for standarddir.get under Windows.
|
||||
|
||||
Attributes:
|
||||
app: The QCoreApplication used.
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
self.app = QCoreApplication([])
|
||||
# We can't store the files in a temp dir, so we don't chose qutebrowser
|
||||
self.app.setApplicationName('qutebrowser_test')
|
||||
|
||||
@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
|
||||
def test_data(self):
|
||||
"""Test data dir."""
|
||||
cur_dir = standarddir.get(QStandardPaths.DataLocation)
|
||||
self.assertEqual(cur_dir.split(os.sep)[-1], 'qutebrowser_test',
|
||||
cur_dir)
|
||||
self.assertTrue(os.path.exists(cur_dir))
|
||||
# We clean up here as we don't dare to clean up if the path doesn't end
|
||||
# with qutebrowser_test - it could be *anywhere* after all.
|
||||
shutil.rmtree(cur_dir)
|
||||
|
||||
@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
|
||||
def test_config(self):
|
||||
"""Test config dir."""
|
||||
cur_dir = standarddir.get(QStandardPaths.ConfigLocation)
|
||||
self.assertEqual(cur_dir.split(os.sep)[-1], 'qutebrowser_test',
|
||||
cur_dir)
|
||||
self.assertTrue(os.path.exists(cur_dir))
|
||||
# We clean up here as we don't dare to clean up if the path doesn't end
|
||||
# with qutebrowser_test - it could be *anywhere* after all.
|
||||
shutil.rmtree(cur_dir)
|
||||
|
||||
@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
|
||||
def test_cache(self):
|
||||
"""Test cache dir."""
|
||||
cur_dir = standarddir.get(QStandardPaths.CacheLocation)
|
||||
self.assertEqual(cur_dir.split(os.sep)[-2:],
|
||||
['qutebrowser_test', 'cache'], cur_dir)
|
||||
self.assertTrue(os.path.exists(cur_dir))
|
||||
# We clean up here as we don't dare to clean up if the path doesn't end
|
||||
# with qutebrowser_test - it could be *anywhere* after all.
|
||||
shutil.rmtree(cur_dir)
|
||||
|
||||
def tearDown(self):
|
||||
self.app.quit()
|
@ -19,15 +19,12 @@
|
||||
|
||||
"""Tests for qutebrowser.utils.utils."""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import enum
|
||||
import shutil
|
||||
import unittest
|
||||
import os.path
|
||||
import tempfile
|
||||
|
||||
from PyQt5.QtCore import QStandardPaths, QCoreApplication, Qt
|
||||
from PyQt5.QtCore import Qt
|
||||
from PyQt5.QtGui import QColor
|
||||
|
||||
from qutebrowser.utils import utils, qtutils
|
||||
@ -163,130 +160,6 @@ class SafeShlexSplitTests(unittest.TestCase):
|
||||
self.assertEqual(items, ['one', 'two\\'])
|
||||
|
||||
|
||||
class GetStandardDirLinuxTests(unittest.TestCase):
|
||||
|
||||
"""Tests for get_standard_dir under Linux.
|
||||
|
||||
Attributes:
|
||||
temp_dir: A temporary directory.
|
||||
app: The QCoreApplication used.
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
self.temp_dir = tempfile.mkdtemp()
|
||||
self.app = QCoreApplication([])
|
||||
self.app.setApplicationName('qutebrowser')
|
||||
|
||||
@unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux")
|
||||
def test_data_explicit(self):
|
||||
"""Test data dir with XDG_DATA_HOME explicitely set."""
|
||||
with helpers.environ_set_temp('XDG_DATA_HOME', self.temp_dir):
|
||||
cur_dir = utils.get_standard_dir(QStandardPaths.DataLocation)
|
||||
self.assertEqual(cur_dir, os.path.join(self.temp_dir,
|
||||
'qutebrowser'))
|
||||
self.assertTrue(os.path.exists(cur_dir))
|
||||
|
||||
@unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux")
|
||||
def test_config_explicit(self):
|
||||
"""Test config dir with XDG_CONFIG_HOME explicitely set."""
|
||||
with helpers.environ_set_temp('XDG_CONFIG_HOME', self.temp_dir):
|
||||
cur_dir = utils.get_standard_dir(QStandardPaths.ConfigLocation)
|
||||
self.assertEqual(cur_dir, os.path.join(self.temp_dir,
|
||||
'qutebrowser'))
|
||||
self.assertTrue(os.path.exists(cur_dir))
|
||||
|
||||
@unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux")
|
||||
def test_cache_explicit(self):
|
||||
"""Test cache dir with XDG_CACHE_HOME explicitely set."""
|
||||
with helpers.environ_set_temp('XDG_CACHE_HOME', self.temp_dir):
|
||||
cur_dir = utils.get_standard_dir(QStandardPaths.CacheLocation)
|
||||
self.assertEqual(cur_dir, os.path.join(self.temp_dir,
|
||||
'qutebrowser'))
|
||||
self.assertTrue(os.path.exists(cur_dir))
|
||||
|
||||
@unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux")
|
||||
def test_data(self):
|
||||
"""Test data dir with XDG_DATA_HOME not set."""
|
||||
with helpers.environ_set_temp('HOME', self.temp_dir):
|
||||
cur_dir = utils.get_standard_dir(QStandardPaths.DataLocation)
|
||||
self.assertEqual(cur_dir, os.path.join(self.temp_dir, '.local',
|
||||
'share', 'qutebrowser'))
|
||||
self.assertTrue(os.path.exists(cur_dir))
|
||||
|
||||
@unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux")
|
||||
def test_config(self):
|
||||
"""Test config dir with XDG_CONFIG_HOME not set."""
|
||||
with helpers.environ_set_temp('HOME', self.temp_dir):
|
||||
cur_dir = utils.get_standard_dir(
|
||||
QStandardPaths.ConfigLocation)
|
||||
self.assertEqual(cur_dir, os.path.join(self.temp_dir, '.config',
|
||||
'qutebrowser'))
|
||||
self.assertTrue(os.path.exists(cur_dir))
|
||||
|
||||
@unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux")
|
||||
def test_cache(self):
|
||||
"""Test cache dir with XDG_CACHE_HOME not set."""
|
||||
with helpers.environ_set_temp('HOME', self.temp_dir):
|
||||
cur_dir = utils.get_standard_dir(QStandardPaths.CacheLocation)
|
||||
self.assertEqual(cur_dir, os.path.join(self.temp_dir, '.cache',
|
||||
'qutebrowser'))
|
||||
self.assertTrue(os.path.exists(cur_dir))
|
||||
|
||||
def tearDown(self):
|
||||
self.app.quit()
|
||||
shutil.rmtree(self.temp_dir)
|
||||
|
||||
|
||||
class GetStandardDirWindowsTests(unittest.TestCase):
|
||||
|
||||
"""Tests for get_standard_dir under Windows.
|
||||
|
||||
Attributes:
|
||||
app: The QCoreApplication used.
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
self.app = QCoreApplication([])
|
||||
# We can't store the files in a temp dir, so we don't chose qutebrowser
|
||||
self.app.setApplicationName('qutebrowser_test')
|
||||
|
||||
@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
|
||||
def test_data(self):
|
||||
"""Test data dir."""
|
||||
cur_dir = utils.get_standard_dir(QStandardPaths.DataLocation)
|
||||
self.assertEqual(cur_dir.split(os.sep)[-1], 'qutebrowser_test',
|
||||
cur_dir)
|
||||
self.assertTrue(os.path.exists(cur_dir))
|
||||
# We clean up here as we don't dare to clean up if the path doesn't end
|
||||
# with qutebrowser_test - it could be *anywhere* after all.
|
||||
shutil.rmtree(cur_dir)
|
||||
|
||||
@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
|
||||
def test_config(self):
|
||||
"""Test config dir."""
|
||||
cur_dir = utils.get_standard_dir(QStandardPaths.ConfigLocation)
|
||||
self.assertEqual(cur_dir.split(os.sep)[-1], 'qutebrowser_test',
|
||||
cur_dir)
|
||||
self.assertTrue(os.path.exists(cur_dir))
|
||||
# We clean up here as we don't dare to clean up if the path doesn't end
|
||||
# with qutebrowser_test - it could be *anywhere* after all.
|
||||
shutil.rmtree(cur_dir)
|
||||
|
||||
@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
|
||||
def test_cache(self):
|
||||
"""Test cache dir."""
|
||||
cur_dir = utils.get_standard_dir(QStandardPaths.CacheLocation)
|
||||
self.assertEqual(cur_dir.split(os.sep)[-2:],
|
||||
['qutebrowser_test', 'cache'], cur_dir)
|
||||
self.assertTrue(os.path.exists(cur_dir))
|
||||
# We clean up here as we don't dare to clean up if the path doesn't end
|
||||
# with qutebrowser_test - it could be *anywhere* after all.
|
||||
shutil.rmtree(cur_dir)
|
||||
|
||||
def tearDown(self):
|
||||
self.app.quit()
|
||||
|
||||
|
||||
class InterpolateColorTests(unittest.TestCase):
|
||||
|
||||
"""Tests for interpolate_color.
|
||||
|
106
qutebrowser/utils/standarddir.py
Normal file
106
qutebrowser/utils/standarddir.py
Normal file
@ -0,0 +1,106 @@
|
||||
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
||||
|
||||
# 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/>.
|
||||
|
||||
"""Utilities to get and initialize data/config paths."""
|
||||
|
||||
import os
|
||||
import os.path
|
||||
|
||||
from PyQt5.QtCore import QCoreApplication, QStandardPaths
|
||||
|
||||
|
||||
def _writable_location(typ):
|
||||
"""Wrapper around QStandardPaths.writableLocation."""
|
||||
qapp = QCoreApplication.instance()
|
||||
orgname = qapp.organizationName()
|
||||
# We need to temporarily unset the organisationname here since the
|
||||
# webinspector wants it to be set to store its persistent data correctly,
|
||||
# but we don't want that to happen.
|
||||
qapp.setOrganizationName(None)
|
||||
try:
|
||||
path = QStandardPaths.writableLocation(typ)
|
||||
finally:
|
||||
qapp.setOrganizationName(orgname)
|
||||
if not path:
|
||||
raise ValueError("QStandardPaths returned an empty value!")
|
||||
# Qt seems to use '/' as path separator even on Windows...
|
||||
path = path.replace('/', os.sep)
|
||||
return path
|
||||
|
||||
|
||||
def _from_args(typ, args):
|
||||
"""Get the standard directory from an argparse namespace.
|
||||
|
||||
Args:
|
||||
typ: A member of the QStandardPaths::StandardLocation enum
|
||||
args: An argparse namespace or None.
|
||||
|
||||
Return:
|
||||
A (override, path) tuple.
|
||||
override: boolean, if the user did override the path
|
||||
path: The overriden path, or None to turn off storage.
|
||||
"""
|
||||
typ_to_argparse_arg = {
|
||||
QStandardPaths.ConfigLocation: 'confdir'
|
||||
}
|
||||
if args is None:
|
||||
return (False, None)
|
||||
try:
|
||||
argname = typ_to_argparse_arg[typ]
|
||||
except KeyError:
|
||||
return (False, None)
|
||||
arg_value = getattr(args, argname)
|
||||
if arg_value is None:
|
||||
return (False, None)
|
||||
elif arg_value == '':
|
||||
return (True, None)
|
||||
else:
|
||||
return (True, arg_value)
|
||||
|
||||
|
||||
def get(typ, args=None):
|
||||
"""Get the directory where files of the given type should be written to.
|
||||
|
||||
Args:
|
||||
typ: A member of the QStandardPaths::StandardLocation enum,
|
||||
see http://qt-project.org/doc/qt-5/qstandardpaths.html#StandardLocation-enum
|
||||
args: An argparse namespace which could be used to override the
|
||||
locations.
|
||||
"""
|
||||
overridden, path = _from_args(typ, args)
|
||||
if overridden:
|
||||
return path
|
||||
path = _writable_location(typ)
|
||||
appname = QCoreApplication.instance().applicationName()
|
||||
if (typ == QStandardPaths.ConfigLocation and
|
||||
path.split(os.sep)[-1] != appname):
|
||||
# WORKAROUND - see
|
||||
# https://bugreports.qt-project.org/browse/QTBUG-38872
|
||||
path = os.path.join(path, appname)
|
||||
if typ == QStandardPaths.DataLocation and os.name == 'nt':
|
||||
# Under windows, config/data might end up in the same directory.
|
||||
data_path = QStandardPaths.writableLocation(
|
||||
QStandardPaths.DataLocation)
|
||||
config_path = QStandardPaths.writableLocation(
|
||||
QStandardPaths.ConfigLocation)
|
||||
if data_path == config_path:
|
||||
path = os.path.join(path, 'data')
|
||||
if not os.path.exists(path):
|
||||
os.makedirs(path)
|
||||
return path
|
@ -19,7 +19,6 @@
|
||||
|
||||
"""Other utilities which don't fit anywhere else. """
|
||||
|
||||
import os
|
||||
import io
|
||||
import sys
|
||||
import enum
|
||||
@ -31,7 +30,7 @@ import collections
|
||||
import functools
|
||||
import contextlib
|
||||
|
||||
from PyQt5.QtCore import QCoreApplication, QStandardPaths, Qt
|
||||
from PyQt5.QtCore import Qt
|
||||
from PyQt5.QtGui import QKeySequence, QColor
|
||||
import pkg_resources
|
||||
|
||||
@ -168,87 +167,6 @@ def pastebin(text):
|
||||
return url
|
||||
|
||||
|
||||
def _writable_location(typ):
|
||||
"""Wrapper around QStandardPaths.writableLocation."""
|
||||
qapp = QCoreApplication.instance()
|
||||
orgname = qapp.organizationName()
|
||||
# We need to temporarily unset the organisationname here since the
|
||||
# webinspector wants it to be set to store its persistent data correctly,
|
||||
# but we don't want that to happen.
|
||||
qapp.setOrganizationName(None)
|
||||
try:
|
||||
path = QStandardPaths.writableLocation(typ)
|
||||
finally:
|
||||
qapp.setOrganizationName(orgname)
|
||||
if not path:
|
||||
raise ValueError("QStandardPaths returned an empty value!")
|
||||
# Qt seems to use '/' as path separator even on Windows...
|
||||
path = path.replace('/', os.sep)
|
||||
return path
|
||||
|
||||
|
||||
def _standard_dir_from_args(typ, args):
|
||||
"""Get the standard directory from an argparse namespace.
|
||||
|
||||
Args:
|
||||
typ: A member of the QStandardPaths::StandardLocation enum
|
||||
args: An argparse namespace or None.
|
||||
|
||||
Return:
|
||||
A (override, path) tuple.
|
||||
override: boolean, if the user did override the path
|
||||
path: The overriden path, or None to turn off storage.
|
||||
"""
|
||||
typ_to_argparse_arg = {
|
||||
QStandardPaths.ConfigLocation: 'confdir'
|
||||
}
|
||||
if args is None:
|
||||
return (False, None)
|
||||
try:
|
||||
argname = typ_to_argparse_arg[typ]
|
||||
except KeyError:
|
||||
return (False, None)
|
||||
arg_value = getattr(args, argname)
|
||||
if arg_value is None:
|
||||
return (False, None)
|
||||
elif arg_value == '':
|
||||
return (True, None)
|
||||
else:
|
||||
return (True, arg_value)
|
||||
|
||||
|
||||
def get_standard_dir(typ, args=None):
|
||||
"""Get the directory where files of the given type should be written to.
|
||||
|
||||
Args:
|
||||
typ: A member of the QStandardPaths::StandardLocation enum,
|
||||
see http://qt-project.org/doc/qt-5/qstandardpaths.html#StandardLocation-enum
|
||||
args: An argparse namespace which could be used to override the
|
||||
locations.
|
||||
"""
|
||||
overridden, path = _standard_dir_from_args(typ, args)
|
||||
if overridden:
|
||||
return path
|
||||
path = _writable_location(typ)
|
||||
appname = QCoreApplication.instance().applicationName()
|
||||
if (typ == QStandardPaths.ConfigLocation and
|
||||
path.split(os.sep)[-1] != appname):
|
||||
# WORKAROUND - see
|
||||
# https://bugreports.qt-project.org/browse/QTBUG-38872
|
||||
path = os.path.join(path, appname)
|
||||
if typ == QStandardPaths.DataLocation and os.name == 'nt':
|
||||
# Under windows, config/data might end up in the same directory.
|
||||
data_path = QStandardPaths.writableLocation(
|
||||
QStandardPaths.DataLocation)
|
||||
config_path = QStandardPaths.writableLocation(
|
||||
QStandardPaths.ConfigLocation)
|
||||
if data_path == config_path:
|
||||
path = os.path.join(path, 'data')
|
||||
if not os.path.exists(path):
|
||||
os.makedirs(path)
|
||||
return path
|
||||
|
||||
|
||||
def actute_warning():
|
||||
"""Display a warning about the dead_actute issue if needed."""
|
||||
# WORKAROUND (remove this when we bump the requirements to 5.3.0)
|
||||
|
Loading…
Reference in New Issue
Block a user