mirror of
https://github.com/PR0M3TH3AN/SeedPass.git
synced 2025-09-10 00:09:04 +00:00
Merge pull request #210 from PR0M3TH3AN/codex/update-list_entries-method-for-sorting-and-filtering
Add sorting and filtering support for entry listing
This commit is contained in:
@@ -385,8 +385,10 @@ class EntryManager:
|
|||||||
colored(f"Error: Failed to modify entry at index {index}: {e}", "red")
|
colored(f"Error: Failed to modify entry at index {index}: {e}", "red")
|
||||||
)
|
)
|
||||||
|
|
||||||
def list_entries(self) -> List[Tuple[int, str, Optional[str], Optional[str], bool]]:
|
def list_entries(
|
||||||
"""List all entries in the index."""
|
self, sort_by: str = "index", filter_kind: str | None = None
|
||||||
|
) -> List[Tuple[int, str, Optional[str], Optional[str], bool]]:
|
||||||
|
"""List entries in the index with optional sorting and filtering."""
|
||||||
try:
|
try:
|
||||||
data = self.vault.load_index()
|
data = self.vault.load_index()
|
||||||
entries_data = data.get("entries", {})
|
entries_data = data.get("entries", {})
|
||||||
@@ -396,17 +398,36 @@ class EntryManager:
|
|||||||
print(colored("No entries found.", "yellow"))
|
print(colored("No entries found.", "yellow"))
|
||||||
return []
|
return []
|
||||||
|
|
||||||
entries = []
|
def sort_key(item: Tuple[str, Dict[str, Any]]):
|
||||||
for idx, entry in sorted(entries_data.items(), key=lambda x: int(x[0])):
|
idx_str, entry = item
|
||||||
|
if sort_by == "index":
|
||||||
|
return int(idx_str)
|
||||||
|
if sort_by == "website":
|
||||||
|
return entry.get("website", "").lower()
|
||||||
|
if sort_by == "username":
|
||||||
|
return entry.get("username", "").lower()
|
||||||
|
raise ValueError("sort_by must be 'index', 'website', or 'username'")
|
||||||
|
|
||||||
|
sorted_items = sorted(entries_data.items(), key=sort_key)
|
||||||
|
|
||||||
|
filtered_items: List[Tuple[int, Dict[str, Any]]] = []
|
||||||
|
for idx_str, entry in sorted_items:
|
||||||
|
if (
|
||||||
|
filter_kind is not None
|
||||||
|
and entry.get("type", EntryType.PASSWORD.value) != filter_kind
|
||||||
|
):
|
||||||
|
continue
|
||||||
|
filtered_items.append((int(idx_str), entry))
|
||||||
|
|
||||||
|
entries: List[Tuple[int, str, Optional[str], Optional[str], bool]] = []
|
||||||
|
for idx, entry in filtered_items:
|
||||||
etype = entry.get("type", EntryType.PASSWORD.value)
|
etype = entry.get("type", EntryType.PASSWORD.value)
|
||||||
if etype == EntryType.TOTP.value:
|
if etype == EntryType.TOTP.value:
|
||||||
entries.append(
|
entries.append((idx, entry.get("label", ""), None, None, False))
|
||||||
(int(idx), entry.get("label", ""), None, None, False)
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
entries.append(
|
entries.append(
|
||||||
(
|
(
|
||||||
int(idx),
|
idx,
|
||||||
entry.get("website", ""),
|
entry.get("website", ""),
|
||||||
entry.get("username", ""),
|
entry.get("username", ""),
|
||||||
entry.get("url", ""),
|
entry.get("url", ""),
|
||||||
@@ -415,7 +436,7 @@ class EntryManager:
|
|||||||
)
|
)
|
||||||
|
|
||||||
logger.debug(f"Total entries found: {len(entries)}")
|
logger.debug(f"Total entries found: {len(entries)}")
|
||||||
for idx, entry in sorted(entries_data.items(), key=lambda x: int(x[0])):
|
for idx, entry in filtered_items:
|
||||||
etype = entry.get("type", EntryType.PASSWORD.value)
|
etype = entry.get("type", EntryType.PASSWORD.value)
|
||||||
print(colored(f"Index: {idx}", "cyan"))
|
print(colored(f"Index: {idx}", "cyan"))
|
||||||
if etype == EntryType.TOTP.value:
|
if etype == EntryType.TOTP.value:
|
||||||
@@ -592,12 +613,12 @@ class EntryManager:
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
def list_all_entries(self) -> None:
|
def list_all_entries(
|
||||||
"""
|
self, sort_by: str = "index", filter_kind: str | None = None
|
||||||
Displays all entries in a formatted manner.
|
) -> None:
|
||||||
"""
|
"""Display all entries using :meth:`list_entries`."""
|
||||||
try:
|
try:
|
||||||
entries = self.list_entries()
|
entries = self.list_entries(sort_by=sort_by, filter_kind=filter_kind)
|
||||||
if not entries:
|
if not entries:
|
||||||
print(colored("No entries to display.", "yellow"))
|
print(colored("No entries to display.", "yellow"))
|
||||||
return
|
return
|
||||||
|
55
src/tests/test_list_entries_sort_filter.py
Normal file
55
src/tests/test_list_entries_sort_filter.py
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
from tempfile import TemporaryDirectory
|
||||||
|
|
||||||
|
from helpers import create_vault, TEST_SEED, TEST_PASSWORD
|
||||||
|
|
||||||
|
sys.path.append(str(Path(__file__).resolve().parents[1]))
|
||||||
|
|
||||||
|
from password_manager.entry_management import EntryManager
|
||||||
|
from password_manager.backup import BackupManager
|
||||||
|
from password_manager.config_manager import ConfigManager
|
||||||
|
from password_manager.entry_types import EntryType
|
||||||
|
|
||||||
|
|
||||||
|
def setup_entry_manager(tmp_path: Path) -> EntryManager:
|
||||||
|
vault, _ = create_vault(tmp_path, TEST_SEED, TEST_PASSWORD)
|
||||||
|
cfg_mgr = ConfigManager(vault, tmp_path)
|
||||||
|
backup_mgr = BackupManager(tmp_path, cfg_mgr)
|
||||||
|
return EntryManager(vault, backup_mgr)
|
||||||
|
|
||||||
|
|
||||||
|
def test_sort_by_website():
|
||||||
|
with TemporaryDirectory() as tmpdir:
|
||||||
|
tmp_path = Path(tmpdir)
|
||||||
|
em = setup_entry_manager(tmp_path)
|
||||||
|
idx0 = em.add_entry("b.com", 8, "user1")
|
||||||
|
idx1 = em.add_entry("A.com", 8, "user2")
|
||||||
|
result = em.list_entries(sort_by="website")
|
||||||
|
assert result == [
|
||||||
|
(idx1, "A.com", "user2", "", False),
|
||||||
|
(idx0, "b.com", "user1", "", False),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def test_sort_by_username():
|
||||||
|
with TemporaryDirectory() as tmpdir:
|
||||||
|
tmp_path = Path(tmpdir)
|
||||||
|
em = setup_entry_manager(tmp_path)
|
||||||
|
idx0 = em.add_entry("alpha.com", 8, "Charlie")
|
||||||
|
idx1 = em.add_entry("beta.com", 8, "alice")
|
||||||
|
result = em.list_entries(sort_by="username")
|
||||||
|
assert result == [
|
||||||
|
(idx1, "beta.com", "alice", "", False),
|
||||||
|
(idx0, "alpha.com", "Charlie", "", False),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def test_filter_by_type():
|
||||||
|
with TemporaryDirectory() as tmpdir:
|
||||||
|
tmp_path = Path(tmpdir)
|
||||||
|
em = setup_entry_manager(tmp_path)
|
||||||
|
em.add_entry("site", 8, "user")
|
||||||
|
em.add_totp("Example", TEST_SEED)
|
||||||
|
result = em.list_entries(filter_kind=EntryType.TOTP.value)
|
||||||
|
assert result == [(1, "Example", None, None, False)]
|
Reference in New Issue
Block a user