Skip to content

Commit

Permalink
Merge pull request #168 from mjpieters/fix_once
Browse files Browse the repository at this point in the history
Open once_file once per use
  • Loading branch information
amjith authored Oct 3, 2023
2 parents a9f4b27 + f5bf97a commit f424629
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
failing queries get `successful = False` in `query_history`.
* Changed `master` to `main` in CONTRIBUTING.md to reflect GitHubs new default branch
naming.
* Fixed `.once -o <file>` by opening the output file once per statement instead
of for every line of output ([#148](https://github.com/dbcli/litecli/issues/148)).
* Use the sqlite3 API to cancel a running query on interrupt
([#164](https://github.com/dbcli/litecli/issues/164)).


## 1.9.0 - 2022-06-06

### Features
Expand Down
37 changes: 19 additions & 18 deletions litecli/packages/special/iocommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use_expanded_output = False
PAGER_ENABLED = True
tee_file = None
once_file = written_to_once_file = None
once_file = once_file_args = written_to_once_file = None
favoritequeries = FavoriteQueries(ConfigObj())


Expand Down Expand Up @@ -377,37 +377,38 @@ def write_tee(output):
aliases=("\\o", "\\once"),
)
def set_once(arg, **_):
global once_file
global once_file_args

once_file = parseargfile(arg)
once_file_args = parseargfile(arg)

return [(None, None, None, "")]


@export
def write_once(output):
global once_file, written_to_once_file
if output and once_file:
try:
f = open(**once_file)
except (IOError, OSError) as e:
once_file = None
raise OSError(
"Cannot write to file '{}': {}".format(e.filename, e.strerror)
)

with f:
click.echo(output, file=f, nl=False)
click.echo("\n", file=f, nl=False)
if output and once_file_args:
if once_file is None:
try:
once_file = open(**once_file_args)
except (IOError, OSError) as e:
once_file = None
raise OSError(
"Cannot write to file '{}': {}".format(e.filename, e.strerror)
)

click.echo(output, file=once_file, nl=False)
click.echo("\n", file=once_file, nl=False)
written_to_once_file = True


@export
def unset_once_if_written():
"""Unset the once file, if it has been written to."""
global once_file, written_to_once_file
if written_to_once_file:
once_file = written_to_once_file = None
global once_file, once_file_args, written_to_once_file
if once_file and written_to_once_file:
once_file.close()
once_file = once_file_args = written_to_once_file = None


@special_command(
Expand Down

0 comments on commit f424629

Please sign in to comment.