From 26d3423ccc765532ea96d390cb05a993ab15d69d Mon Sep 17 00:00:00 2001 From: Daniele Nicolodi Date: Sat, 28 Mar 2026 11:19:08 +0100 Subject: [PATCH 1/2] 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. Setting this environment variable to the empty string disable saving the shell history. Similarly, allow to customize the location of the init file via the BEANQUERY_INIT environment variable. Setting this environment variable to the empty string disable processing of the init file. 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 #272. --- beanquery/shell.py | 24 ++++++++++++------------ beanquery/shell_test.py | 16 ++++------------ 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/beanquery/shell.py b/beanquery/shell.py index c4675bf1..c28ab3b2 100644 --- a/beanquery/shell.py +++ b/beanquery/shell.py @@ -41,10 +41,6 @@ readline = None -HISTORY_FILENAME = '~/.config/beanquery/history' -INIT_FILENAME = '~/.config/beanquery/init' - - class style: ERROR = '\033[31;1m' WARNING = '\033[31;1m' @@ -160,6 +156,10 @@ def __init__(self, outfile, interactive, runinit, settings): self.color = interactive and os.environ.get('TERM', 'dumb') != 'dumb' self.add_help() + home_config = os.environ.get('XDG_CONFIG_HOME', '~/.config') + history_filename = path.expanduser(os.environ.get('BEANQUERY_HISTORY', path.join(home_config, 'beanquery/history'))) + init_filename = path.expanduser(os.environ.get('BEANQUERY_INIT', path.join(home_config, 'beanquery/init'))) + if interactive and readline is not None: # Setup completion on ``tab``. This needs to be done differently @@ -181,18 +181,18 @@ def __init__(self, outfile, interactive, runinit, settings): readline.set_completer_delims(" \t\n\"\\'`@$><=;|&{(") # Setup ``readline`` history handling. - history_filepath = path.expanduser(HISTORY_FILENAME) - os.makedirs(path.dirname(history_filepath), exist_ok=True) - with suppress(FileNotFoundError): - readline.read_history_file(history_filepath) - readline.set_history_length(2048) - atexit.register(readline.write_history_file, history_filepath) + if history_filename: + os.makedirs(path.dirname(history_filename), exist_ok=True) + with suppress(FileNotFoundError): + readline.read_history_file(history_filename) + readline.set_history_length(2048) + atexit.register(readline.write_history_file, history_filename) warnings.showwarning = self.warning - if runinit: + if runinit and init_filename: with suppress(FileNotFoundError): - with open(path.expanduser(INIT_FILENAME)) as f: + with open(init_filename) as f: for line in f: self.onecmd(line) diff --git a/beanquery/shell_test.py b/beanquery/shell_test.py index edf174e3..bbb8dfb2 100644 --- a/beanquery/shell_test.py +++ b/beanquery/shell_test.py @@ -326,18 +326,10 @@ class ClickTestCase(unittest.TestCase): """Base class for command-line program test cases.""" def main(self, *args): - init_filename = shell.INIT_FILENAME - history_filename = shell.HISTORY_FILENAME - try: - shell.INIT_FILENAME = '' - shell.HISTORY_FILENAME = '' - runner = click.testing.CliRunner() - result = runner.invoke(shell.main, args, catch_exceptions=False) - self.assertEqual(result.exit_code, 0) - return result - finally: - shell.INIT_FILENAME = init_filename - shell.HISTORY_FILENAME = history_filename + runner = click.testing.CliRunner(env={'BEANQUERY_HISTORY': '', 'BEANQUERY_INIT': ''}) + result = runner.invoke(shell.main, args, catch_exceptions=False) + self.assertEqual(result.exit_code, 0) + return result class TestShell(ClickTestCase): From 0b48a34396100ae180284e80d12fab6fefd21f6c Mon Sep 17 00:00:00 2001 From: Daniele Nicolodi Date: Sat, 28 Mar 2026 19:00:23 +0100 Subject: [PATCH 2/2] ruff: Disable rule RUF059 --- pyproject.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 2778f7ee..2ff7aac0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -83,7 +83,8 @@ ignore = [ 'PLW1641', # eq-without-hash 'PLW2901', 'RUF012', - 'RUF023', # unsorted-dunder-slots + 'RUF023', # unsorted-dunder-slots + 'RUF059', # unused-unpacked-variable 'UP007', 'UP032', ]