Skip to content

UnixFileInfoCommand

Base classes

Name Children Inherits
FileInfoCommand
anyenv.os_commands.base
Base class for file info commands.

â‹” Inheritance diagram

graph TD
  3481821082["file_info.UnixFileInfoCommand"]
  3903279879["base.FileInfoCommand"]
  925645236["abc.ABC"]
  2751680898["builtins.object"]
  3903279879 --> 3481821082
  925645236 --> 3903279879
  2751680898 --> 925645236

🛈 DocStrings

Bases: FileInfoCommand

Unix/Linux file info command implementation using ls -la.

Source code in src/anyenv/os_commands/file_info.py
class UnixFileInfoCommand(FileInfoCommand):
    """Unix/Linux file info command implementation using ls -la."""

    def create_command(self, path: str) -> str:
        """Generate ls -la command for file info.

        Uses ls -la instead of stat to avoid shell expansion issues with
        format specifiers like %s, %F, etc.

        Args:
            path: Path to get information about

        Returns:
            The ls command string
        """
        return f"ls -lad {sh_path(path)}"

    def parse_command(self, output: str, path: str) -> FileInfo:
        """Parse ls -la output for a single file/directory.

        Args:
            output: Raw ls -la command output
            path: Original path requested

        Returns:
            FileInfo object with parsed information
        """
        line = output.strip()
        if not line:
            msg = f"Empty output for path: {path}"
            raise ValueError(msg)

        parts = line.split()
        if len(parts) < MIN_LS_PARTS_2_TIMESTAMP:
            msg = f"Unexpected ls output format: {output}"
            raise ValueError(msg)

        permissions = parts[0]
        size = int(parts[4]) if parts[4].isdigit() else 0

        # Determine file type from permissions
        file_type: Literal["file", "directory", "link"]
        if permissions.startswith("d"):
            file_type = "directory"
        elif permissions.startswith("l"):
            file_type = "link"
        else:
            file_type = "file"

        # Extract timestamp - handle different formats
        # Format can be: "Mon DD HH:MM" or "Mon DD YYYY" or "YYYY-MM-DD HH:MM"
        if len(parts) >= MIN_LS_PARTS:
            # 3-part timestamp: month day time/year
            timestamp_str = f"{parts[5]} {parts[6]} {parts[7]}"
        else:
            # 2-part timestamp fallback
            timestamp_str = f"{parts[5]} {parts[6]}"

        # Convert timestamp to unix time (approximate - ls doesn't give precise timestamps)
        # For now, just use 0 since we can't reliably parse all date formats
        mtime = 0

        return FileInfo(
            name=Path(path).name,
            path=path,
            type=file_type,
            size=size,
            mtime=mtime,
            permissions=permissions,
            timestamp=timestamp_str,
        )

create_command

create_command(path: str) -> str

Generate ls -la command for file info.

Uses ls -la instead of stat to avoid shell expansion issues with format specifiers like %s, %F, etc.

Parameters:

Name Type Description Default
path str

Path to get information about

required

Returns:

Type Description
str

The ls command string

Source code in src/anyenv/os_commands/file_info.py
def create_command(self, path: str) -> str:
    """Generate ls -la command for file info.

    Uses ls -la instead of stat to avoid shell expansion issues with
    format specifiers like %s, %F, etc.

    Args:
        path: Path to get information about

    Returns:
        The ls command string
    """
    return f"ls -lad {sh_path(path)}"

parse_command

parse_command(output: str, path: str) -> FileInfo

Parse ls -la output for a single file/directory.

Parameters:

Name Type Description Default
output str

Raw ls -la command output

required
path str

Original path requested

required

Returns:

Type Description
FileInfo

FileInfo object with parsed information

Source code in src/anyenv/os_commands/file_info.py
def parse_command(self, output: str, path: str) -> FileInfo:
    """Parse ls -la output for a single file/directory.

    Args:
        output: Raw ls -la command output
        path: Original path requested

    Returns:
        FileInfo object with parsed information
    """
    line = output.strip()
    if not line:
        msg = f"Empty output for path: {path}"
        raise ValueError(msg)

    parts = line.split()
    if len(parts) < MIN_LS_PARTS_2_TIMESTAMP:
        msg = f"Unexpected ls output format: {output}"
        raise ValueError(msg)

    permissions = parts[0]
    size = int(parts[4]) if parts[4].isdigit() else 0

    # Determine file type from permissions
    file_type: Literal["file", "directory", "link"]
    if permissions.startswith("d"):
        file_type = "directory"
    elif permissions.startswith("l"):
        file_type = "link"
    else:
        file_type = "file"

    # Extract timestamp - handle different formats
    # Format can be: "Mon DD HH:MM" or "Mon DD YYYY" or "YYYY-MM-DD HH:MM"
    if len(parts) >= MIN_LS_PARTS:
        # 3-part timestamp: month day time/year
        timestamp_str = f"{parts[5]} {parts[6]} {parts[7]}"
    else:
        # 2-part timestamp fallback
        timestamp_str = f"{parts[5]} {parts[6]}"

    # Convert timestamp to unix time (approximate - ls doesn't give precise timestamps)
    # For now, just use 0 since we can't reliably parse all date formats
    mtime = 0

    return FileInfo(
        name=Path(path).name,
        path=path,
        type=file_type,
        size=size,
        mtime=mtime,
        permissions=permissions,
        timestamp=timestamp_str,
    )

:fa-brands-github: Show source on GitHub