# Local Provider


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

## LocalFileSystemProvider

Local file system implementation of the FileSystemProvider protocol for
interactive directory browsing.

------------------------------------------------------------------------

<a
href="https://github.com/cj-mills/cjm-fasthtml-file-browser/blob/main/cjm_fasthtml_file_browser/providers/local.py#L21"
target="_blank" style="float:right; font-size:smaller">source</a>

### LocalFileSystemProvider

``` python

def LocalFileSystemProvider(
    extension_mapping:Optional=None, # For file type detection
):

```

*Local file system provider for interactive navigation.*

``` python
import tempfile

# Test LocalFileSystemProvider
provider = LocalFileSystemProvider()

# Test properties
assert provider.name == "local"
assert provider.root_path == "/"
assert provider.path_separator in ["/", "\\"]

# Test with a temporary directory
with tempfile.TemporaryDirectory() as tmpdir:
    # Create some test files
    (Path(tmpdir) / "file1.py").write_text("print('hello')")
    (Path(tmpdir) / "file2.txt").write_text("hello world")
    (Path(tmpdir) / "subdir").mkdir()
    (Path(tmpdir) / ".hidden").write_text("hidden file")
    
    # Test list_directory
    listing = provider.list_directory(tmpdir)
    assert listing.error is None
    assert listing.path == str(Path(tmpdir).resolve())
    assert listing.parent_path is not None
    assert listing.total_items == 4  # file1.py, file2.txt, subdir, .hidden
    
    # Verify files are in listing
    names = [f.name for f in listing.items]
    assert "file1.py" in names
    assert "file2.txt" in names
    assert "subdir" in names
    
    # Test get_file_info
    py_file = Path(tmpdir) / "file1.py"
    info = provider.get_file_info(str(py_file))
    assert info is not None
    assert info.name == "file1.py"
    assert info.is_directory == False
    assert info.extension == "py"
    assert info.file_type == FileType.CODE
    
    # Test directory info
    subdir = Path(tmpdir) / "subdir"
    dir_info = provider.get_file_info(str(subdir))
    assert dir_info is not None
    assert dir_info.is_directory == True
    
    # Test get_parent_path
    parent = provider.get_parent_path(str(subdir))
    assert parent == str(Path(tmpdir).resolve())
    
    # Test root has no parent
    assert provider.get_parent_path("/") is None
    
    # Test join_path
    joined = provider.join_path(tmpdir, "subdir", "file.txt")
    assert "subdir" in joined
    assert "file.txt" in joined
    
    # Test normalize_path
    normalized = provider.normalize_path(tmpdir + "/subdir/../file1.py")
    assert ".." not in normalized
    
    # Test is_valid_path
    valid, error = provider.is_valid_path(tmpdir)
    assert valid == True
    assert error is None
    
    valid, error = provider.is_valid_path("/nonexistent/path/here")
    assert valid == False
    assert error is not None
    
    # Test path_exists
    assert provider.path_exists(tmpdir) == True
    assert provider.path_exists("/nonexistent") == False
    
    # Test is_directory
    assert provider.is_directory(tmpdir) == True
    assert provider.is_directory(str(py_file)) == False
    
    # Test get_home_path
    home = provider.get_home_path()
    assert provider.path_exists(home)
    assert provider.is_directory(home)

print("LocalFileSystemProvider tests passed!")
```

    LocalFileSystemProvider tests passed!

``` python
# Test error handling
provider = LocalFileSystemProvider()

# Test listing non-existent directory
listing = provider.list_directory("/nonexistent/path/here")
assert listing.error is not None
assert "does not exist" in listing.error
assert listing.items == []

# Test listing a file (not a directory)
with tempfile.NamedTemporaryFile() as f:
    listing = provider.list_directory(f.name)
    assert listing.error is not None
    assert "not a directory" in listing.error

# Test get_file_info for non-existent file
info = provider.get_file_info("/nonexistent/file.txt")
assert info is None

print("Error handling tests passed!")
```

    Error handling tests passed!

``` python
from cjm_fasthtml_file_browser.core.protocols import FileSystemProvider

# Verify LocalFileSystemProvider implements the protocol
provider = LocalFileSystemProvider()
assert isinstance(provider, FileSystemProvider)
print("Protocol implementation verified!")
```

    Protocol implementation verified!

``` python
# Test async method (using top-level await for Jupyter compatibility)
provider = LocalFileSystemProvider()

with tempfile.TemporaryDirectory() as tmpdir:
    (Path(tmpdir) / "test.txt").write_text("hello")
    listing = await provider.list_directory_async(tmpdir)
    assert listing.error is None
    assert len(listing.items) == 1

print("Async tests passed!")
```

    Async tests passed!
