Clean up get_standard_dir
This commit is contained in:
parent
e74fa71a0a
commit
a8304d03ea
@ -168,6 +168,55 @@ def pastebin(text):
|
|||||||
return url
|
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):
|
def get_standard_dir(typ, args=None):
|
||||||
"""Get the directory where files of the given type should be written to.
|
"""Get the directory where files of the given type should be written to.
|
||||||
|
|
||||||
@ -177,55 +226,27 @@ def get_standard_dir(typ, args=None):
|
|||||||
args: An argparse namespace which could be used to override the
|
args: An argparse namespace which could be used to override the
|
||||||
locations.
|
locations.
|
||||||
"""
|
"""
|
||||||
# First check if it's been overridden
|
overridden, path = _standard_dir_from_args(typ, args)
|
||||||
typ_to_argparse_arg = {
|
if overridden:
|
||||||
QStandardPaths.ConfigLocation: 'confdir'
|
|
||||||
}
|
|
||||||
if args is not None:
|
|
||||||
try:
|
|
||||||
argname = typ_to_argparse_arg[typ]
|
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
arg_value = getattr(args, argname)
|
|
||||||
if arg_value is None:
|
|
||||||
pass
|
|
||||||
elif arg_value == '':
|
|
||||||
return None
|
|
||||||
else:
|
|
||||||
return arg_value
|
|
||||||
# If not, get it from Qt
|
|
||||||
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)
|
|
||||||
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)
|
|
||||||
appname = qapp.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
|
return path
|
||||||
finally:
|
path = _writable_location(typ)
|
||||||
qapp.setOrganizationName(orgname)
|
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():
|
def actute_warning():
|
||||||
|
Loading…
Reference in New Issue
Block a user