Skip to content

Commit f28ef0f

Browse files
committed
Use local configuration files
1 parent 5e93a7d commit f28ef0f

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

proplot/rctools.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,34 @@
190190
_rcGlobals = {}
191191
_rcExtraParams = {}
192192

193-
# Get default font
194-
# WARNING: Had issues with Helvetica Neue on Linux, weirdly some characters
195-
# failed to render/printed nonsense, but Helvetica was fine.
196-
USER_RC = os.path.join(os.path.expanduser("~"), '.proplotrc')
197-
DEFAULT_RC = os.path.join(os.path.dirname(__file__), '.proplotrc')
198-
if not os.path.exists(DEFAULT_RC):
199-
raise ValueError('Default configuration file does not exist.')
193+
# Configuration files
194+
def _get_rc():
195+
"""Walks successive parent directories searching for ".proplotrc" and
196+
"proplotrc" files."""
197+
# Local configuration
198+
rc = []
199+
idir = os.getcwd()
200+
while idir: # not empty string
201+
for tail in ('.proplotrc', 'proplotrc'):
202+
irc = os.path.join(idir, tail)
203+
if os.path.exists(irc):
204+
rc.append(irc)
205+
ndir, _ = os.path.split(idir)
206+
if ndir == idir:
207+
break
208+
idir = ndir
209+
rc = rc[::-1] # sort from decreasing to increasing importantce
210+
# Home configuration
211+
irc = os.path.join(os.path.expanduser('~'), '.proplotrc')
212+
if os.path.exists(irc) and irc not in rc:
213+
rc.insert(0, irc)
214+
# Global configuration
215+
irc = os.path.join(os.path.dirname(__file__), '.proplotrc')
216+
if not os.path.exists(irc):
217+
raise ValueError('Default configuration file does not exist.')
218+
elif irc not in rc:
219+
rc.insert(0, irc)
220+
return rc
200221

201222
# "Global" settings and the lower-level settings they change
202223
# NOTE: This whole section, declaring dictionaries and sets, takes 1ms
@@ -397,7 +418,7 @@ def __init__(self):
397418
plt.style.use('default')
398419

399420
# Load the defaults from file
400-
for i,file in enumerate((DEFAULT_RC, USER_RC)):
421+
for i,file in enumerate(_get_rc()):
401422
# Load
402423
if not os.path.exists(file):
403424
continue

0 commit comments

Comments
 (0)