mirror of
https://github.com/PR0M3TH3AN/SeedPass.git
synced 2025-09-09 15:58:48 +00:00
Run sync in background for TUI actions
This commit is contained in:
@@ -1102,6 +1102,19 @@ class PasswordManager:
|
|||||||
self._relay_thread = threading.Thread(target=_worker, daemon=True)
|
self._relay_thread = threading.Thread(target=_worker, daemon=True)
|
||||||
self._relay_thread.start()
|
self._relay_thread.start()
|
||||||
|
|
||||||
|
def start_background_vault_sync(self, alt_summary: str | None = None) -> None:
|
||||||
|
"""Publish the vault to Nostr in a background thread."""
|
||||||
|
if getattr(self, "offline_mode", False):
|
||||||
|
return
|
||||||
|
|
||||||
|
def _worker() -> None:
|
||||||
|
try:
|
||||||
|
self.sync_vault(alt_summary=alt_summary)
|
||||||
|
except Exception as exc:
|
||||||
|
logging.error(f"Background vault sync failed: {exc}", exc_info=True)
|
||||||
|
|
||||||
|
threading.Thread(target=_worker, daemon=True).start()
|
||||||
|
|
||||||
def sync_index_from_nostr_if_missing(self) -> None:
|
def sync_index_from_nostr_if_missing(self) -> None:
|
||||||
"""Retrieve the password database from Nostr if it doesn't exist locally.
|
"""Retrieve the password database from Nostr if it doesn't exist locally.
|
||||||
|
|
||||||
@@ -1225,7 +1238,7 @@ class PasswordManager:
|
|||||||
# Automatically push the updated encrypted index to Nostr so the
|
# Automatically push the updated encrypted index to Nostr so the
|
||||||
# latest changes are backed up remotely.
|
# latest changes are backed up remotely.
|
||||||
try:
|
try:
|
||||||
self.sync_vault()
|
self.start_background_vault_sync()
|
||||||
logging.info("Encrypted index posted to Nostr after entry addition.")
|
logging.info("Encrypted index posted to Nostr after entry addition.")
|
||||||
except Exception as nostr_error:
|
except Exception as nostr_error:
|
||||||
logging.error(
|
logging.error(
|
||||||
@@ -1299,7 +1312,7 @@ class PasswordManager:
|
|||||||
TotpManager.print_qr_code(uri)
|
TotpManager.print_qr_code(uri)
|
||||||
print(color_text(f"Secret: {secret}\n", "deterministic"))
|
print(color_text(f"Secret: {secret}\n", "deterministic"))
|
||||||
try:
|
try:
|
||||||
self.sync_vault()
|
self.start_background_vault_sync()
|
||||||
except Exception as nostr_error:
|
except Exception as nostr_error:
|
||||||
logging.error(
|
logging.error(
|
||||||
f"Failed to post updated index to Nostr: {nostr_error}",
|
f"Failed to post updated index to Nostr: {nostr_error}",
|
||||||
@@ -1348,7 +1361,7 @@ class PasswordManager:
|
|||||||
)
|
)
|
||||||
TotpManager.print_qr_code(uri)
|
TotpManager.print_qr_code(uri)
|
||||||
try:
|
try:
|
||||||
self.sync_vault()
|
self.start_background_vault_sync()
|
||||||
except Exception as nostr_error:
|
except Exception as nostr_error:
|
||||||
logging.error(
|
logging.error(
|
||||||
f"Failed to post updated index to Nostr: {nostr_error}",
|
f"Failed to post updated index to Nostr: {nostr_error}",
|
||||||
@@ -1411,7 +1424,7 @@ class PasswordManager:
|
|||||||
print(colored("Private Key:", "cyan"))
|
print(colored("Private Key:", "cyan"))
|
||||||
print(color_text(priv_pem, "deterministic"))
|
print(color_text(priv_pem, "deterministic"))
|
||||||
try:
|
try:
|
||||||
self.sync_vault()
|
self.start_background_vault_sync()
|
||||||
except Exception as nostr_error:
|
except Exception as nostr_error:
|
||||||
logging.error(
|
logging.error(
|
||||||
f"Failed to post updated index to Nostr: {nostr_error}",
|
f"Failed to post updated index to Nostr: {nostr_error}",
|
||||||
@@ -1479,7 +1492,7 @@ class PasswordManager:
|
|||||||
|
|
||||||
TotpManager.print_qr_code(encode_seedqr(phrase))
|
TotpManager.print_qr_code(encode_seedqr(phrase))
|
||||||
try:
|
try:
|
||||||
self.sync_vault()
|
self.start_background_vault_sync()
|
||||||
except Exception as nostr_error:
|
except Exception as nostr_error:
|
||||||
logging.error(
|
logging.error(
|
||||||
f"Failed to post updated index to Nostr: {nostr_error}",
|
f"Failed to post updated index to Nostr: {nostr_error}",
|
||||||
@@ -1545,7 +1558,7 @@ class PasswordManager:
|
|||||||
print(colored(f"Fingerprint: {fingerprint}", "cyan"))
|
print(colored(f"Fingerprint: {fingerprint}", "cyan"))
|
||||||
print(color_text(priv_key, "deterministic"))
|
print(color_text(priv_key, "deterministic"))
|
||||||
try:
|
try:
|
||||||
self.sync_vault()
|
self.start_background_vault_sync()
|
||||||
except Exception as nostr_error: # pragma: no cover - best effort
|
except Exception as nostr_error: # pragma: no cover - best effort
|
||||||
logging.error(
|
logging.error(
|
||||||
f"Failed to post updated index to Nostr: {nostr_error}",
|
f"Failed to post updated index to Nostr: {nostr_error}",
|
||||||
@@ -1601,7 +1614,7 @@ class PasswordManager:
|
|||||||
):
|
):
|
||||||
TotpManager.print_qr_code(nsec)
|
TotpManager.print_qr_code(nsec)
|
||||||
try:
|
try:
|
||||||
self.sync_vault()
|
self.start_background_vault_sync()
|
||||||
except Exception as nostr_error: # pragma: no cover - best effort
|
except Exception as nostr_error: # pragma: no cover - best effort
|
||||||
logging.error(
|
logging.error(
|
||||||
f"Failed to post updated index to Nostr: {nostr_error}",
|
f"Failed to post updated index to Nostr: {nostr_error}",
|
||||||
@@ -1676,7 +1689,7 @@ class PasswordManager:
|
|||||||
else:
|
else:
|
||||||
print(color_text(f"Value: {value}", "deterministic"))
|
print(color_text(f"Value: {value}", "deterministic"))
|
||||||
try:
|
try:
|
||||||
self.sync_vault()
|
self.start_background_vault_sync()
|
||||||
except Exception as nostr_error: # pragma: no cover - best effort
|
except Exception as nostr_error: # pragma: no cover - best effort
|
||||||
logging.error(
|
logging.error(
|
||||||
f"Failed to post updated index to Nostr: {nostr_error}",
|
f"Failed to post updated index to Nostr: {nostr_error}",
|
||||||
@@ -1737,7 +1750,7 @@ class PasswordManager:
|
|||||||
|
|
||||||
TotpManager.print_qr_code(encode_seedqr(seed))
|
TotpManager.print_qr_code(encode_seedqr(seed))
|
||||||
try:
|
try:
|
||||||
self.sync_vault()
|
self.start_background_vault_sync()
|
||||||
except Exception as nostr_error: # pragma: no cover - best effort
|
except Exception as nostr_error: # pragma: no cover - best effort
|
||||||
logging.error(
|
logging.error(
|
||||||
f"Failed to post updated index to Nostr: {nostr_error}",
|
f"Failed to post updated index to Nostr: {nostr_error}",
|
||||||
@@ -2772,7 +2785,7 @@ class PasswordManager:
|
|||||||
|
|
||||||
# Push the updated index to Nostr so changes are backed up.
|
# Push the updated index to Nostr so changes are backed up.
|
||||||
try:
|
try:
|
||||||
self.sync_vault()
|
self.start_background_vault_sync()
|
||||||
logging.info(
|
logging.info(
|
||||||
"Encrypted index posted to Nostr after entry modification."
|
"Encrypted index posted to Nostr after entry modification."
|
||||||
)
|
)
|
||||||
@@ -3045,7 +3058,7 @@ class PasswordManager:
|
|||||||
|
|
||||||
# Push updated index to Nostr after deletion
|
# Push updated index to Nostr after deletion
|
||||||
try:
|
try:
|
||||||
self.sync_vault()
|
self.start_background_vault_sync()
|
||||||
logging.info("Encrypted index posted to Nostr after entry deletion.")
|
logging.info("Encrypted index posted to Nostr after entry deletion.")
|
||||||
except Exception as nostr_error:
|
except Exception as nostr_error:
|
||||||
logging.error(
|
logging.error(
|
||||||
|
@@ -52,7 +52,9 @@ def test_handle_add_totp(monkeypatch, capsys):
|
|||||||
]
|
]
|
||||||
)
|
)
|
||||||
monkeypatch.setattr("builtins.input", lambda *args, **kwargs: next(inputs))
|
monkeypatch.setattr("builtins.input", lambda *args, **kwargs: next(inputs))
|
||||||
monkeypatch.setattr(pm, "sync_vault", lambda: None)
|
monkeypatch.setattr(
|
||||||
|
pm, "start_background_vault_sync", lambda *a, **k: pm.sync_vault(*a, **k)
|
||||||
|
)
|
||||||
|
|
||||||
pm.handle_add_totp()
|
pm.handle_add_totp()
|
||||||
out = capsys.readouterr().out
|
out = capsys.readouterr().out
|
||||||
|
@@ -71,6 +71,12 @@ def test_manager_workflow(monkeypatch):
|
|||||||
)
|
)
|
||||||
monkeypatch.setattr("builtins.input", lambda *args, **kwargs: next(inputs))
|
monkeypatch.setattr("builtins.input", lambda *args, **kwargs: next(inputs))
|
||||||
|
|
||||||
|
monkeypatch.setattr(
|
||||||
|
pm,
|
||||||
|
"start_background_vault_sync",
|
||||||
|
lambda *a, **k: pm.sync_vault(*a, **k),
|
||||||
|
)
|
||||||
|
|
||||||
pm.handle_add_password()
|
pm.handle_add_password()
|
||||||
assert pm.is_dirty is False
|
assert pm.is_dirty is False
|
||||||
backups = list((tmp_path / "backups").glob("entries_db_backup_*.json.enc"))
|
backups = list((tmp_path / "backups").glob("entries_db_backup_*.json.enc"))
|
||||||
|
@@ -74,6 +74,11 @@ def test_add_and_delete_entry(monkeypatch):
|
|||||||
|
|
||||||
inputs = iter([str(index)])
|
inputs = iter([str(index)])
|
||||||
monkeypatch.setattr("builtins.input", lambda *_a, **_k: next(inputs))
|
monkeypatch.setattr("builtins.input", lambda *_a, **_k: next(inputs))
|
||||||
|
monkeypatch.setattr(
|
||||||
|
pm,
|
||||||
|
"start_background_vault_sync",
|
||||||
|
lambda *a, **k: pm.sync_vault(*a, **k),
|
||||||
|
)
|
||||||
|
|
||||||
pm.delete_entry()
|
pm.delete_entry()
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user