add support for system xonshrc file to environ.py

Redefines the XONSHRC environment tuple to include the .xonshrc file in
user's home directory as well as a system-wide file that lives in
/etc/xonshrc (for Linux and OSX) and (TODO) somewhere else on Windows.

Then xonshrc_context does more or less what it always has, except now it
checks and places the contents of both files into the xonsh environment.
This commit is contained in:
Gil Forsyth 2015-10-21 14:55:07 -04:00
parent f30c7db25e
commit 3f769c85c5

View file

@ -142,7 +142,8 @@ DEFAULT_VALUES = {
'XDG_CONFIG_HOME': os.path.expanduser(os.path.join('~', '.config')), 'XDG_CONFIG_HOME': os.path.expanduser(os.path.join('~', '.config')),
'XDG_DATA_HOME': os.path.expanduser(os.path.join('~', '.local', 'share')), 'XDG_DATA_HOME': os.path.expanduser(os.path.join('~', '.local', 'share')),
'XONSHCONFIG': xonshconfig, 'XONSHCONFIG': xonshconfig,
'XONSHRC': os.path.expanduser('~/.xonshrc'), 'XONSHRC': (os.path.expanduser('~/.xonshrc'),'/etc/xonshrc') if ON_LINUX \
else (os.path.expanduser('~/.xonshrc'),'/etc/xonshrc'),
'XONSH_CONFIG_DIR': xonsh_config_dir, 'XONSH_CONFIG_DIR': xonsh_config_dir,
'XONSH_DATA_DIR': xonsh_data_dir, 'XONSH_DATA_DIR': xonsh_data_dir,
'XONSH_HISTORY_FILE': os.path.expanduser('~/.xonsh_history.json'), 'XONSH_HISTORY_FILE': os.path.expanduser('~/.xonsh_history.json'),
@ -611,24 +612,29 @@ def load_static_config(ctx):
return conf return conf
def xonshrc_context(rcfile=None, execer=None): def xonshrc_context(rcfiles=None, execer=None):
"""Attempts to read in xonshrc file, and return the contents.""" """Attempts to read in xonshrc file, and return the contents."""
if rcfile is None or execer is None or not os.path.isfile(rcfile): if (rcfiles is None or execer is None
or sum([os.path.isfile(rcfile) for rcfile in rcfiles]) == 0):
return {} return {}
with open(rcfile, 'r') as f: for rcfile in rcfiles:
rc = f.read() try:
if not rc.endswith('\n'): with open(rcfile, 'r') as f:
rc += '\n' rc = f.read()
fname = execer.filename if not rc.endswith('\n'):
env = {} rc += '\n'
try: fname = execer.filename
execer.filename = rcfile env = {}
execer.exec(rc, glbs=env) try:
except SyntaxError as err: execer.filename = rcfile
msg = 'syntax error in xonsh run control file {0!r}: {1!s}' execer.exec(rc, glbs=env)
warn(msg.format(rcfile, err), RuntimeWarning) except SyntaxError as err:
finally: msg = 'syntax error in xonsh run control file {0!r}: {1!s}'
execer.filename = fname warn(msg.format(rcfile, err), RuntimeWarning)
finally:
execer.filename = fname
except:
pass
return env return env