Skip to content

FileSystem

File system operations for reading and writing files.

FileSystem

FileSystem(client: VSCodeClient)

VS Code file system operations.

Source code in vscode_sockpuppet/fs.py
def __init__(self, client: "VSCodeClient"):
    self.client = client

read_file

read_file(uri: str) -> bytes

Read a file.

Parameters:

Name Type Description Default
uri str

The URI of the file to read

required

Returns:

Type Description
bytes

The file contents as bytes

Source code in vscode_sockpuppet/fs.py
def read_file(self, uri: str) -> bytes:
    """
    Read a file.

    Args:
        uri: The URI of the file to read

    Returns:
        The file contents as bytes
    """
    result = self.client._send_request("fs.readFile", {"uri": uri})
    return bytes(result)

write_file

write_file(uri: str, content: bytes) -> None

Write to a file.

Parameters:

Name Type Description Default
uri str

The URI of the file to write

required
content bytes

The content to write

required
Source code in vscode_sockpuppet/fs.py
def write_file(self, uri: str, content: bytes) -> None:
    """
    Write to a file.

    Args:
        uri: The URI of the file to write
        content: The content to write
    """
    self.client._send_request("fs.writeFile", {"uri": uri, "content": list(content)})

delete

delete(uri: str, recursive: bool = False, use_trash: bool = False) -> None

Delete a file or directory.

Parameters:

Name Type Description Default
uri str

The URI of the file/directory to delete

required
recursive bool

Delete recursively if a directory

False
use_trash bool

Use the OS trash/recycle bin

False
Source code in vscode_sockpuppet/fs.py
def delete(self, uri: str, recursive: bool = False, use_trash: bool = False) -> None:
    """
    Delete a file or directory.

    Args:
        uri: The URI of the file/directory to delete
        recursive: Delete recursively if a directory
        use_trash: Use the OS trash/recycle bin
    """
    options = {"recursive": recursive, "useTrash": use_trash}
    self.client._send_request("fs.delete", {"uri": uri, "options": options})

rename

rename(source: str, target: str, overwrite: bool = False) -> None

Rename/move a file or directory.

Parameters:

Name Type Description Default
source str

Source URI

required
target str

Target URI

required
overwrite bool

Overwrite if target exists

False
Source code in vscode_sockpuppet/fs.py
def rename(self, source: str, target: str, overwrite: bool = False) -> None:
    """
    Rename/move a file or directory.

    Args:
        source: Source URI
        target: Target URI
        overwrite: Overwrite if target exists
    """
    options = {"overwrite": overwrite}
    self.client._send_request(
        "fs.rename",
        {"source": source, "target": target, "options": options},
    )

copy

copy(source: str, target: str, overwrite: bool = False) -> None

Copy a file or directory.

Parameters:

Name Type Description Default
source str

Source URI

required
target str

Target URI

required
overwrite bool

Overwrite if target exists

False
Source code in vscode_sockpuppet/fs.py
def copy(self, source: str, target: str, overwrite: bool = False) -> None:
    """
    Copy a file or directory.

    Args:
        source: Source URI
        target: Target URI
        overwrite: Overwrite if target exists
    """
    options = {"overwrite": overwrite}
    self.client._send_request(
        "fs.copy", {"source": source, "target": target, "options": options}
    )

create_directory

create_directory(uri: str) -> None

Create a directory.

Parameters:

Name Type Description Default
uri str

The URI of the directory to create

required
Source code in vscode_sockpuppet/fs.py
def create_directory(self, uri: str) -> None:
    """
    Create a directory.

    Args:
        uri: The URI of the directory to create
    """
    self.client._send_request("fs.createDirectory", {"uri": uri})

read_directory

read_directory(uri: str) -> List[Tuple[str, int]]

Read a directory.

Parameters:

Name Type Description Default
uri str

The URI of the directory to read

required

Returns:

Type Description
List[Tuple[str, int]]

List of (name, type) tuples where type is from FileType

Source code in vscode_sockpuppet/fs.py
def read_directory(self, uri: str) -> List[Tuple[str, int]]:
    """
    Read a directory.

    Args:
        uri: The URI of the directory to read

    Returns:
        List of (name, type) tuples where type is from FileType
    """
    result = self.client._send_request("fs.readDirectory", {"uri": uri})
    return [(entry["name"], entry["type"]) for entry in result]

stat

stat(uri: str) -> FileStat

Get file metadata.

Parameters:

Name Type Description Default
uri str

The URI of the file

required

Returns:

Type Description
FileStat

File metadata

Source code in vscode_sockpuppet/fs.py
def stat(self, uri: str) -> FileStat:
    """
    Get file metadata.

    Args:
        uri: The URI of the file

    Returns:
        File metadata
    """
    result = self.client._send_request("fs.stat", {"uri": uri})
    return FileStat(result)

read_text

read_text(uri: str, encoding: str = 'utf-8') -> str

Read a text file.

Parameters:

Name Type Description Default
uri str

The URI of the file

required
encoding str

Text encoding (default: utf-8)

'utf-8'

Returns:

Type Description
str

File contents as string

Source code in vscode_sockpuppet/fs.py
def read_text(self, uri: str, encoding: str = "utf-8") -> str:
    """
    Read a text file.

    Args:
        uri: The URI of the file
        encoding: Text encoding (default: utf-8)

    Returns:
        File contents as string
    """
    content = self.read_file(uri)
    return content.decode(encoding)

write_text

write_text(uri: str, text: str, encoding: str = 'utf-8') -> None

Write a text file.

Parameters:

Name Type Description Default
uri str

The URI of the file

required
text str

Text content to write

required
encoding str

Text encoding (default: utf-8)

'utf-8'
Source code in vscode_sockpuppet/fs.py
def write_text(self, uri: str, text: str, encoding: str = "utf-8") -> None:
    """
    Write a text file.

    Args:
        uri: The URI of the file
        text: Text content to write
        encoding: Text encoding (default: utf-8)
    """
    content = text.encode(encoding)
    self.write_file(uri, content)

exists

exists(uri: str) -> bool

Check if a file or directory exists.

Parameters:

Name Type Description Default
uri str

The URI to check

required

Returns:

Type Description
bool

True if exists, False otherwise

Source code in vscode_sockpuppet/fs.py
def exists(self, uri: str) -> bool:
    """
    Check if a file or directory exists.

    Args:
        uri: The URI to check

    Returns:
        True if exists, False otherwise
    """
    try:
        self.stat(uri)
        return True
    except Exception:
        return False

is_directory

is_directory(uri: str) -> bool

Check if a URI points to a directory.

Parameters:

Name Type Description Default
uri str

The URI to check

required

Returns:

Type Description
bool

True if directory, False otherwise

Source code in vscode_sockpuppet/fs.py
def is_directory(self, uri: str) -> bool:
    """
    Check if a URI points to a directory.

    Args:
        uri: The URI to check

    Returns:
        True if directory, False otherwise
    """
    try:
        stat = self.stat(uri)
        return stat.type == FileType.Directory
    except Exception:
        return False

is_file

is_file(uri: str) -> bool

Check if a URI points to a file.

Parameters:

Name Type Description Default
uri str

The URI to check

required

Returns:

Type Description
bool

True if file, False otherwise

Source code in vscode_sockpuppet/fs.py
def is_file(self, uri: str) -> bool:
    """
    Check if a URI points to a file.

    Args:
        uri: The URI to check

    Returns:
        True if file, False otherwise
    """
    try:
        stat = self.stat(uri)
        return stat.type == FileType.File
    except Exception:
        return False

FileStat

FileStat

FileStat(data: dict)

File metadata.

Source code in vscode_sockpuppet/fs.py
def __init__(self, data: dict):
    self.type: int = data["type"]
    self.ctime: int = data["ctime"]
    self.mtime: int = data["mtime"]
    self.size: int = data["size"]

FileType

FileType

File type enumeration.