DirectoryListing
Result of listing a directory, including the items and metadata about the listing operation.
source
DirectoryListing
def DirectoryListing(
path:str, items:List, parent_path:Optional=None, provider_name:str='local', total_items:int=0,
error:Optional=None
)->None:
Result of listing a directory.
from cjm_file_discovery.core.models import FileType
# Test DirectoryListing
listing = DirectoryListing(
path="/home/user",
items=[
FileInfo(name="file.txt", path="/home/user/file.txt", is_directory=False),
FileInfo(name="folder", path="/home/user/folder", is_directory=True),
],
parent_path="/home"
)
assert listing.path == "/home/user"
assert len(listing.items) == 2
assert listing.total_items == 2
assert listing.parent_path == "/home"
assert listing.error is None
# Test with error
error_listing = DirectoryListing(
path="/restricted",
items=[],
error="Permission denied"
)
assert error_listing.error == "Permission denied"
print("DirectoryListing tests passed!")
DirectoryListing tests passed!
BrowserSelection
Tracks the current selection state in the browser, supporting single and multiple selection modes.
source
BrowserSelection
def BrowserSelection(
selected_paths:List=<factory>, last_selected:Optional=None
)->None:
Current selection state.
# Test BrowserSelection
selection = BrowserSelection()
# Test add
selection.add("/path/to/file1.txt")
assert selection.is_selected("/path/to/file1.txt")
assert selection.last_selected == "/path/to/file1.txt"
selection.add("/path/to/file2.txt")
assert len(selection.selected_paths) == 2
assert selection.last_selected == "/path/to/file2.txt"
# Test duplicate add (should not add twice)
selection.add("/path/to/file1.txt")
assert len(selection.selected_paths) == 2
# Test remove
selection.remove("/path/to/file1.txt")
assert not selection.is_selected("/path/to/file1.txt")
assert len(selection.selected_paths) == 1
# Test toggle
selection.toggle("/path/to/file2.txt") # Remove
assert not selection.is_selected("/path/to/file2.txt")
selection.toggle("/path/to/file2.txt") # Add back
assert selection.is_selected("/path/to/file2.txt")
# Test clear
selection.add("/path/to/file3.txt")
selection.clear()
assert len(selection.selected_paths) == 0
assert selection.last_selected is None
# Test set_single
selection.add("/path/to/file1.txt")
selection.add("/path/to/file2.txt")
selection.set_single("/path/to/only.txt")
assert len(selection.selected_paths) == 1
assert selection.is_selected("/path/to/only.txt")
print("BrowserSelection tests passed!")
BrowserSelection tests passed!
BrowserState
Complete browser state for persistence and restoration. This captures everything needed to restore the browser to a specific state.
source
BrowserState
def BrowserState(
current_path:str, selection:BrowserSelection=<factory>, sort_by:str='name', sort_descending:bool=False,
filter_extensions:Optional=None
)->None:
Complete browser state for persistence/restore.
# Test BrowserState
state = BrowserState(
current_path="/home/user",
sort_by="modified",
sort_descending=True,
filter_extensions=[".py", ".txt"]
)
state.selection.add("/home/user/file.txt")
assert state.current_path == "/home/user"
assert state.sort_by == "modified"
assert state.sort_descending == True
assert state.filter_extensions == [".py", ".txt"]
assert state.selection.is_selected("/home/user/file.txt")
# Test serialization round-trip
state_dict = state.to_dict()
restored_state = BrowserState.from_dict(state_dict)
assert restored_state.current_path == state.current_path
assert restored_state.sort_by == state.sort_by
assert restored_state.sort_descending == state.sort_descending
assert restored_state.filter_extensions == state.filter_extensions
assert restored_state.selection.selected_paths == state.selection.selected_paths
# Test defaults
default_state = BrowserState(current_path="/")
assert default_state.sort_by == "name"
assert default_state.sort_descending == False
assert default_state.filter_extensions is None
print("BrowserState tests passed!")