Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions beanquery/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand Down
16 changes: 4 additions & 12 deletions beanquery/shell_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]
Expand Down
Loading