def test_reserved_space_is_integer(): """Make sure that reserved space is returned as an integer.""" def stub_terminal_size(): return (5, 5) old_func = click.get_terminal_size click.get_terminal_size = stub_terminal_size lc = LiteCli() assert isinstance(lc.get_reserved_space(), int) click.get_terminal_size = old_func
def records_prompt_shell(): try: from litecli.main import LiteCli litecli = LiteCli(prompt="Type quit to exit shell.\nPrompt: ") litecli.connect(database=FY_DB_PATH) litecli.run_cli() except: print(huepy.red("sorry, it can't spawn records prompt shell."))
def connect(args: list) -> None: """ Connects to a SQLite database by downloading a copy of the database from the device and storing it locally in a temporary directory. :param args: :return: """ if len(args) <= 0: click.secho('Usage: sqlite connect <remote_file> (optional: --sync)', bold=True) return db_location = args[0] _, local_path = tempfile.mkstemp('.sqlite') # update the full remote path for future syncs full_remote_file = db_location \ if os.path.isabs(db_location) else os.path.join(pwd(), db_location) click.secho('Caching local copy of database file...', fg='green') download([db_location, local_path]) click.secho('Validating SQLite database format', dim=True) with open(local_path, 'rb') as f: header = f.read(16) header = binascii.hexlify(header) if header != b'53514c69746520666f726d6174203300': click.secho( 'File does not appear to be a SQLite3 db. Try downloading and manually inspecting this one.', fg='red') cleanup(local_path) return click.secho('Connected to SQLite database at: {0}'.format(db_location), fg='green') # boot the litecli prompt lite = LiteCli(prompt='SQLite @ {} > '.format(db_location)) lite.connect(local_path) lite.run_cli() if _should_sync_once_done(args): click.secho('Synchronizing changes back...', dim=True) upload([local_path, full_remote_file]) else: click.secho( 'NOT synchronizing changes back to device. Use --sync if you want that.', fg='green') # maak skoon cleanup(local_path)
def output(monkeypatch, terminal_size, testdata, explicit_pager, expect_pager): global clickoutput clickoutput = "" m = LiteCli(liteclirc=default_config_file) class TestOutput: def get_size(self): size = namedtuple("Size", "rows columns") size.columns, size.rows = terminal_size return size class TestExecute: host = "test" user = "******" dbname = "test" port = 0 def server_type(self): return ["test"] class PromptBuffer: output = TestOutput() m.prompt_app = PromptBuffer() m.sqlexecute = TestExecute() m.explicit_pager = explicit_pager def echo_via_pager(s): assert expect_pager global clickoutput clickoutput += s def secho(s): assert not expect_pager global clickoutput clickoutput += s + "\n" monkeypatch.setattr(click, "echo_via_pager", echo_via_pager) monkeypatch.setattr(click, "secho", secho) m.output(testdata) if clickoutput.endswith("\n"): clickoutput = clickoutput[:-1] assert clickoutput == "\n".join(testdata)
def connect(args: list) -> None: """ Connects to a SQLite database by downloading a copy of the database from the device and storing it locally in a temporary directory. :param args: :return: """ if len(args) <= 0: click.secho('Usage: sqlite connect <remote_file> (optional: --sync)', bold=True) return db_location = args[0] _, local_path = tempfile.mkstemp('.sqlite') use_shm = False # does Shared Memory temp file exist ? use_wal = False # does Write-Ahead-Log temp file exist ? use_jnl = False # does Journal temp file exist ? write_back_tmp_sqlite = False # if enabled temporary DB files are re-uploaded, this has not been testes # update the full remote path for future syncs full_remote_file = db_location \ if os.path.isabs(db_location) else os.path.join(pwd(), db_location) click.secho('Caching local copy of database file...', fg='green') download([db_location, local_path]) if path_exists(full_remote_file + '-shm'): click.secho('... caching local copy of database "shm" file...', fg='green') download([db_location + '-shm', local_path + '-shm']) use_shm = True if path_exists(full_remote_file + '-wal'): click.secho('... caching local copy of database "wal" file...', fg='green') download([db_location + '-wal', local_path + '-wal']) use_wal = True if path_exists(full_remote_file + '-journal'): click.secho('... caching local copy of database "journal" file...', fg='green') download([db_location + '-journal', local_path + '-journal']) use_jnl = True click.secho('Validating SQLite database format', dim=True) with open(local_path, 'rb') as f: header = f.read(16) header = binascii.hexlify(header) if header != b'53514c69746520666f726d6174203300': click.secho( 'File does not appear to be a SQLite3 db. Try downloading and manually inspecting this one.', fg='red') cleanup(local_path) return click.secho('Connected to SQLite database at: {0}'.format(db_location), fg='green') # boot the litecli prompt lite = LiteCli(prompt='SQLite @ {} > '.format(db_location)) lite.connect(local_path) lite.run_cli() if _should_sync_once_done(args): click.secho('Synchronizing changes back...', dim=True) upload([local_path, full_remote_file]) # re-uploading temp sqlite files has not been tested and thus is disabled by default if write_back_tmp_sqlite: if use_shm: upload([local_path + '-shm', full_remote_file + '-shm']) if use_wal: upload([local_path + '-wal', full_remote_file + '-wal']) if use_jnl: upload( [local_path + '-journal', full_remote_file + '-journal']) else: click.secho( 'NOT synchronizing changes back to device. Use --sync if you want that.', fg='green') # maak skoon cleanup(local_path) if use_shm: cleanup(local_path + '-shm') if use_wal: cleanup(local_path + '-wal') if use_jnl: cleanup(local_path + '-journal')
def prompt(self) -> None: lite_cli = LiteCli(sqlexecute=self.sqlexecute, liteclirc=home / "config") lite_cli.run_cli() if self.dirty and input("Save? ") == "y": self.save()