Refactor standarddir.

See #941.
This commit is contained in:
Florian Bruhin 2015-09-11 20:05:13 +02:00
parent b16a482b4c
commit 330f3f8f13

View File

@ -32,33 +32,84 @@ _args = None
def config(): def config():
"""Convenience function to get the config location.""" """Get a location for configs."""
return _get(QStandardPaths.ConfigLocation) typ = QStandardPaths.ConfigLocation
overridden, path = _from_args(typ, _args)
if not overridden:
path = _writable_location(typ)
appname = QCoreApplication.instance().applicationName()
if path.split(os.sep)[-1] != appname:
# WORKAROUND - see
# https://bugreports.qt.io/browse/QTBUG-38872
path = os.path.join(path, appname)
_maybe_create(path)
return path
def data(): def data():
"""Convenience function to get the data location.""" """Get a location for data."""
return _get(QStandardPaths.DataLocation) typ = QStandardPaths.DataLocation
overridden, path = _from_args(typ, _args)
if not overridden:
path = _writable_location(typ)
if 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')
_maybe_create(path)
return path
def cache(): def cache():
"""Convenience function to get the cache location.""" """Get a location for the cache."""
return _get(QStandardPaths.CacheLocation) typ = QStandardPaths.CacheLocation
overridden, path = _from_args(typ, _args)
if not overridden:
path = _writable_location(typ)
_maybe_create(path)
return path
def download(): def download():
"""Convenience function to get the download location.""" """Get a location for downloads."""
return _get(QStandardPaths.DownloadLocation) typ = QStandardPaths.DownloadLocation
overridden, path = _from_args(typ, _args)
if not overridden:
path = _writable_location(typ)
_maybe_create(path)
return path
def runtime(): def runtime():
"""Convenience function to get the runtime location.""" """Get a location for runtime data."""
return _get(QStandardPaths.RuntimeLocation) typ = QStandardPaths.RuntimeLocation
overridden, path = _from_args(typ, _args)
if not overridden:
path = _writable_location(typ)
# This is generic, but per-user.
appname = QCoreApplication.instance().applicationName()
path = os.path.join(path, appname)
_maybe_create(path)
return path
def temp(): def temp():
"""Convenience function to get the temporary files location.""" """Get a location for temporary files."""
return _get(QStandardPaths.TempLocation) typ = QStandardPaths.TempLocation
path = _writable_location(typ)
# "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 IPC sockets, so we
# don't add the username here...
appname = QCoreApplication.instance().applicationName()
path = os.path.join(path, appname)
_maybe_create(path)
return path
def _writable_location(typ): def _writable_location(typ):
@ -122,53 +173,20 @@ def _from_args(typ, args):
return (True, arg_value) return (True, arg_value)
def _get(typ): def _maybe_create(path):
"""Get the directory where files of the given type should be written to. """Create the `path` directory if path is not None.
Args: From the XDG basedir spec:
typ: A member of the QStandardPaths::StandardLocation enum, If, when attempting to write a file, the destination directory is
see http://doc.qt.io/qt-5/qstandardpaths.html#StandardLocation-enum non-existant an attempt should be made to create it with permission
0700. If the destination directory exists already the permissions
should not be changed.
""" """
overridden, path = _from_args(typ, _args)
if not overridden:
path = _writable_location(typ)
appname = QCoreApplication.instance().applicationName()
if (typ == QStandardPaths.ConfigLocation and
path.split(os.sep)[-1] != appname):
# WORKAROUND - see
# https://bugreports.qt.io/browse/QTBUG-38872
path = os.path.join(path, appname)
elif 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')
elif typ == QStandardPaths.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 IPC sockets, so we
# don't add the username here...
subdir = appname
path = os.path.join(path, subdir)
elif typ == QStandardPaths.RuntimeLocation:
# This is generic, but per-user.
path = os.path.join(path, appname)
# From the XDG basedir spec:
# If, when attempting to write a file, the destination directory is
# non-existant an attempt should be made to create it with permission
# 0700. If the destination directory exists already the permissions
# should not be changed.
if path is not None: if path is not None:
try: try:
os.makedirs(path, 0o700) os.makedirs(path, 0o700)
except FileExistsError: except FileExistsError:
pass pass
return path
def init(args): def init(args):