Skip to content

Commit 29d3055

Browse files
committed
shell: Allow to configure history and init files path via env vars
Allow to customize the location of the shell history files via the BEANQUERY_HISTORY environment variables. The environment variable name has been chosen to resemble the ones used by sqlite3 and psql, but with the BEANQUERY_ prefix. Similarly, allow to customize the location of the init file via the BEANQUERY_INIT environment variable. Furthermore, respect the XGD_CONFIG_HOME environment variable when determining the default location of these files. Tilde expansion is performed on the values of all environment variables: a leading path component equal to ~ is expanded to the user's home directory location. Fixes beancount#272.
1 parent aa07762 commit 29d3055

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

beanquery/shell.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@
4141
readline = None
4242

4343

44-
HISTORY_FILENAME = '~/.config/beanquery/history'
45-
INIT_FILENAME = '~/.config/beanquery/init'
44+
_HOME_CONFIG = os.environ.get('XDG_CONFIG_HOME', '~/.config')
45+
_HISTORY_FILENAME = path.expanduser(os.environ.get('BEANQUERY_HISTORY', path.join(_HOME_CONFIG, 'beanquery/history')))
46+
_INIT_FILENAME = path.expanduser(os.environ.get('BEANQUERY_INIT', path.join(_HOME_CONFIG, 'beanquery/init')))
4647

4748

4849
class style:
@@ -181,18 +182,17 @@ def __init__(self, outfile, interactive, runinit, settings):
181182
readline.set_completer_delims(" \t\n\"\\'`@$><=;|&{(")
182183

183184
# Setup ``readline`` history handling.
184-
history_filepath = path.expanduser(HISTORY_FILENAME)
185-
os.makedirs(path.dirname(history_filepath), exist_ok=True)
185+
os.makedirs(path.dirname(_HISTORY_FILENAME), exist_ok=True)
186186
with suppress(FileNotFoundError):
187-
readline.read_history_file(history_filepath)
187+
readline.read_history_file(_HISTORY_FILENAME)
188188
readline.set_history_length(2048)
189-
atexit.register(readline.write_history_file, history_filepath)
189+
atexit.register(readline.write_history_file, _HISTORY_FILENAME)
190190

191191
warnings.showwarning = self.warning
192192

193193
if runinit:
194194
with suppress(FileNotFoundError):
195-
with open(path.expanduser(INIT_FILENAME)) as f:
195+
with open(_INIT_FILENAME) as f:
196196
for line in f:
197197
self.onecmd(line)
198198

0 commit comments

Comments
 (0)