Coverage for qutebrowser/utils/standarddir.py : 91%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
# Copyright 2014-2018 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/>.
# The cached locations
'data', 'system_data', 'cache', 'download', 'runtime'])
"""Error raised when QStandardPaths returns an empty value."""
def _unset_organization(): """Temporarily unset QApplication.organizationName().
This is primarily needed in config.py. """ finally:
"""Initialize the location for configs.""" app_data_path = _writable_location( QStandardPaths.AppDataLocation) path = os.path.join(app_data_path, 'config') else:
# Override the normal (non-auto) config on macOS
"""Get the location for the config directory.
If auto=True is given, get the location for the autoconfig.yml directory, which is different on macOS. """
"""Initialize the location for data.""" app_data_path = _writable_location(QStandardPaths.AppDataLocation) path = os.path.join(app_data_path, 'data') # HaikuOS returns an empty value for AppDataLocation else:
# system_data path = '/usr/share/' + APPNAME if os.path.exists(path): _locations[Location.system_data] = path
"""Get the data directory.
If system=True is given, gets the system-wide (probably non-writable) data directory. """
"""Initialize the location for the cache.""" # Local, not Roaming! data_path = _writable_location(QStandardPaths.DataLocation) path = os.path.join(data_path, 'cache') else:
"""Initialize the location for downloads.
Note this is only the default directory as found by Qt. Therefore, we also don't create it. """
"""Initialize location for runtime data.""" typ = QStandardPaths.RuntimeLocation else: # RuntimeLocation is a weird path on macOS and Windows.
# Fall back to TempLocation when RuntimeLocation is misconfigured path = _writable_location(QStandardPaths.TempLocation)
# This is generic, but per-user. # _writable_location makes sure we have a qutebrowser-specific subdir. # # For TempLocation: # "The returned value might be application-specific, shared among # other applications for this user, or even system-wide." # # Unfortunately this path could get too long for sockets (which have a # maximum length of 104 chars), so we don't add the username here...
"""Wrapper around QStandardPaths.writableLocation.
Arguments: typ: A QStandardPaths::StandardLocation member. """
# Types we are sure we handle correctly below. QStandardPaths.ConfigLocation, QStandardPaths.DataLocation, QStandardPaths.CacheLocation, QStandardPaths.DownloadLocation, QStandardPaths.RuntimeLocation, QStandardPaths.TempLocation, # FIXME old Qt getattr(QStandardPaths, 'AppDataLocation', object())], typ_str
# Qt seems to use '/' as path separator even on Windows...
# Add the application name to the given path if needed. # This is in order for this to work without a QApplication (and thus # QStandardsPaths not knowing the application name), as well as a # workaround for https://bugreports.qt.io/browse/QTBUG-38872 path.split(os.sep)[-1] != APPNAME):
"""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 overridden path, or None to turn off storage. """ QStandardPaths.ConfigLocation: 'config', QStandardPaths.DataLocation: 'data', QStandardPaths.CacheLocation: 'cache', QStandardPaths.DownloadLocation: 'download', QStandardPaths.RuntimeLocation: 'runtime', }
except KeyError: # pragma: no cover return (False, None) else:
"""Create the `path` directory.
From the XDG basedir spec: If, when attempting to write a file, the destination directory is non-existent an attempt should be made to create it with permission 0700. If the destination directory exists already the permissions should not be changed. """
"""Create and cache standard directory locations.
Mainly in a separate function because we need to call it in tests. """
"""Initialize all standard dirs.""" # args can be None during tests
if utils.is_mac: # pragma: no cover _move_macos() elif utils.is_windows: # pragma: no cover _move_windows()
"""Move most config files to new location on macOS.""" os.path.join(new_config, f))
"""Move the whole qutebrowser directory from Local to Roaming AppData.""" # %APPDATA%\Local\qutebrowser # %APPDATA%\Roaming\qutebrowser
# data subfolder if not ok: # pragma: no cover return
# config files os.path.join(new_config_dir, f))
"""Create CACHEDIR.TAG if it doesn't exist.
See http://www.brynosaurus.com/cachedir/spec.html """ "qutebrowser.\n") "cachedir/\n")
"""Migrate data from an old to a new directory.
If the old directory does not exist, the migration is skipped. If the new directory already exists, an error is shown.
Return: True if moving succeeded, False otherwise. """
.format(old, new))
old, new, e))
|