diff options
Diffstat (limited to 'litecli/packages/special/iocommands.py')
-rw-r--r-- | litecli/packages/special/iocommands.py | 41 |
1 files changed, 21 insertions, 20 deletions
diff --git a/litecli/packages/special/iocommands.py b/litecli/packages/special/iocommands.py index 43c3577..ee254f0 100644 --- a/litecli/packages/special/iocommands.py +++ b/litecli/packages/special/iocommands.py @@ -21,7 +21,7 @@ from litecli.packages.prompt_utils import confirm_destructive_query 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()) @@ -286,7 +286,7 @@ def delete_favorite_query(arg, **_): return [(None, None, None, status)] -@special_command("system", "system [command]", "Execute a system shell commmand.") +@special_command("system", "system [command]", "Execute a system shell command.") def execute_system_command(arg, **_): """Execute a system shell command.""" usage = "Syntax: system [command].\n" @@ -377,9 +377,9 @@ 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, "")] @@ -387,27 +387,28 @@ def set_once(arg, **_): @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 - if written_to_once_file: - 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( @@ -461,7 +462,7 @@ def watch_query(arg, **kwargs): # Somewhere in the code the pager its activated after every yield, # so we disable it in every iteration set_pager_enabled(False) - for (sql, title) in sql_list: + for sql, title in sql_list: cur.execute(sql) if cur.description: headers = [x[0] for x in cur.description] |