Skip to content

Commit 5e6367d

Browse files
committed
Support XDG user files/directories
1 parent ad999e9 commit 5e6367d

File tree

1 file changed

+60
-8
lines changed

1 file changed

+60
-8
lines changed

proplot/config.py

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,20 +1313,72 @@ def local_files():
13131313
return paths[::-1] # sort from decreasing to increasing importantce
13141314

13151315
@staticmethod
1316-
def user_file():
1316+
def _config_folder():
13171317
"""
1318-
Return location of the default user proplotrc file.
1318+
Get the XDG proplot folder.
13191319
"""
1320-
# TODO: Add XDG support here
1321-
return os.path.join(os.path.expanduser('~'), '.proplotrc')
1320+
home = os.path.expanduser('~')
1321+
xdg_base = os.environ.get('XDG_CONFIG_HOME')
1322+
if not xdg_base:
1323+
configdir = os.path.join(home, '.config')
1324+
if sys.platform.startswith(('linux', 'freebsd')):
1325+
configdir = os.path.join(xdg_base, 'proplot')
1326+
else:
1327+
configdir = os.path.join(home, '.proplot')
1328+
return configdir
13221329

13231330
@staticmethod
1324-
def user_folder(subfolder=None):
1331+
def user_file():
13251332
"""
1326-
Return location of the default user proplot folder.
1333+
Return location of the default user proplotrc file. On Linux, this is either
1334+
``$XDG_CONFIG_HOME/proplot/proplotrc`` or ``~/.config/proplot/proplotrc``
1335+
if the `XDG directory <https://wiki.archlinux.org/title/XDG_Base_Directory>`__
1336+
is unset. On other operating systems, this is ``~/.proplot/proplotrc``. The
1337+
location ``~/.proplotrc`` or ``~/.proplot/proplotrc`` is always returned if the
1338+
file exists, regardless of the operating system. If multiple valid locations
1339+
are found, a warning is raised.
1340+
"""
1341+
# Support both loose files and files inside .proplot
1342+
file = os.path.join(Configurator.user_folder(), 'proplotrc')
1343+
universal = os.path.join(os.path.expanduser('~'), '.proplotrc')
1344+
if os.path.isfile(universal) and os.path.isfile(file):
1345+
warnings._warn_proplot(
1346+
'Found conflicting default user proplotrc files at '
1347+
f'{universal!r} and {file!r}. Ignoring the second one.'
1348+
)
1349+
if os.path.isfile(universal):
1350+
return universal
1351+
else:
1352+
return file
1353+
1354+
@staticmethod
1355+
def user_folder(subfolder=None):
13271356
"""
1328-
# TODO: Add XDG support here
1329-
folder = os.path.join(os.path.expanduser('~'), '.proplot')
1357+
Return location of the default user proplot folder. On Linux,
1358+
this is either ``$XDG_CONFIG_HOME/proplot`` or ``~/.config/proplot``
1359+
if the `XDG directory <https://wiki.archlinux.org/title/XDG_Base_Directory>`__
1360+
is unset. On other operating systems, this is ``~/.proplot``. The location
1361+
``~/.proplot`` is always returned if the folder exists, regardless of the
1362+
operating system. If multiple valid locations are found, a warning is raised.
1363+
"""
1364+
# Try the XDG standard location
1365+
# NOTE: This is borrowed from matplotlib.get_configdir
1366+
home = os.path.expanduser('~')
1367+
universal = folder = os.path.join(home, '.proplot')
1368+
if sys.platform.startswith(('linux', 'freebsd')):
1369+
xdg = os.environ.get('XDG_CONFIG_HOME')
1370+
xdg = xdg or os.path.join(home, '.config')
1371+
folder = os.path.join(xdg, 'proplot')
1372+
# Fallback to the loose ~/.proplot if it is present
1373+
# NOTE: This is critical or we might ignore previously stored settings!
1374+
if os.path.isdir(universal):
1375+
if universal != folder and os.path.isdir(folder):
1376+
warnings._warn_proplot(
1377+
'Found conflicting default user proplot folders at '
1378+
f'{universal!r} and {folder!r}. Ignoring the second one.'
1379+
)
1380+
folder = universal
1381+
# Return the folder
13301382
if subfolder:
13311383
folder = os.path.join(folder, subfolder)
13321384
return folder

0 commit comments

Comments
 (0)