Skip to content

file_info

Class info

Classes

Name Children Inherits
MacOSFileInfoCommand
anyenv.os_commands.file_info
macOS file info command implementation using ls -la.
    UnixFileInfoCommand
    anyenv.os_commands.file_info
    Unix/Linux file info command implementation using ls -la.
      WindowsFileInfoCommand
      anyenv.os_commands.file_info
      Windows file info command implementation.

        🛈 DocStrings

        File info command implementations for different operating systems.

        MacOSFileInfoCommand

        Bases: FileInfoCommand

        macOS file info command implementation using ls -la.

        Source code in src/anyenv/os_commands/file_info.py
        class MacOSFileInfoCommand(FileInfoCommand):
            """macOS 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.
        
                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.
        
                BSD ls output format is same as Unix.
        
                Args:
                    output: Raw ls -la command output
                    path: Original path requested
        
                Returns:
                    FileInfo object with parsed information
                """
                # BSD ls output format is same as Unix
                unix_cmd = UnixFileInfoCommand()
                return unix_cmd.parse_command(output, path)
        

        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.

        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.
        
            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.

        BSD ls output format is same as Unix.

        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.
        
            BSD ls output format is same as Unix.
        
            Args:
                output: Raw ls -la command output
                path: Original path requested
        
            Returns:
                FileInfo object with parsed information
            """
            # BSD ls output format is same as Unix
            unix_cmd = UnixFileInfoCommand()
            return unix_cmd.parse_command(output, path)
        

        UnixFileInfoCommand

        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,
            )
        

        WindowsFileInfoCommand

        Bases: FileInfoCommand

        Windows file info command implementation.

        Source code in src/anyenv/os_commands/file_info.py
        class WindowsFileInfoCommand(FileInfoCommand):
            """Windows file info command implementation."""
        
            def create_command(self, path: str) -> str:
                """Generate PowerShell file info command.
        
                Args:
                    path: Path to get information about
        
                Returns:
                    The PowerShell command string
                """
                return powershell(
                    f"$item = Get-Item -LiteralPath {ps_literal(path)} -ErrorAction Stop; "
                    "$item.Name + '||' + $item.Length + '||' + "
                    "($item.GetType().Name) + '||' + "
                    "[int][double]::Parse($item.LastWriteTime.ToString('yyyyMMddHHmmss'))"
                )
        
            def parse_command(self, output: str, path: str) -> FileInfo:
                """Parse PowerShell output format: name||size||type||mtime.
        
                Args:
                    output: Raw PowerShell command output
                    path: Original path requested
        
                Returns:
                    FileInfo object with parsed information
                """
                parts = output.strip().split("||")
                if len(parts) < EXPECTED_POWERSHELL_PARTS:
                    msg = f"Unexpected PowerShell output format: {output}"
                    raise ValueError(msg)
        
                file_type: Literal["file", "directory", "link"] = (
                    "directory" if "directory" in parts[2].lower() else "file"
                )
        
                return FileInfo(
                    name=parts[0],
                    path=path,
                    type=file_type,
                    size=int(parts[1]) if parts[1].isdigit() else 0,
                    mtime=int(parts[3]) if parts[3].isdigit() else 0,
                )
        

        create_command

        create_command(path: str) -> str
        

        Generate PowerShell file info command.

        Parameters:

        Name Type Description Default
        path str

        Path to get information about

        required

        Returns:

        Type Description
        str

        The PowerShell command string

        Source code in src/anyenv/os_commands/file_info.py
        def create_command(self, path: str) -> str:
            """Generate PowerShell file info command.
        
            Args:
                path: Path to get information about
        
            Returns:
                The PowerShell command string
            """
            return powershell(
                f"$item = Get-Item -LiteralPath {ps_literal(path)} -ErrorAction Stop; "
                "$item.Name + '||' + $item.Length + '||' + "
                "($item.GetType().Name) + '||' + "
                "[int][double]::Parse($item.LastWriteTime.ToString('yyyyMMddHHmmss'))"
            )
        

        parse_command

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

        Parse PowerShell output format: name||size||type||mtime.

        Parameters:

        Name Type Description Default
        output str

        Raw PowerShell 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 PowerShell output format: name||size||type||mtime.
        
            Args:
                output: Raw PowerShell command output
                path: Original path requested
        
            Returns:
                FileInfo object with parsed information
            """
            parts = output.strip().split("||")
            if len(parts) < EXPECTED_POWERSHELL_PARTS:
                msg = f"Unexpected PowerShell output format: {output}"
                raise ValueError(msg)
        
            file_type: Literal["file", "directory", "link"] = (
                "directory" if "directory" in parts[2].lower() else "file"
            )
        
            return FileInfo(
                name=parts[0],
                path=path,
                type=file_type,
                size=int(parts[1]) if parts[1].isdigit() else 0,
                mtime=int(parts[3]) if parts[3].isdigit() else 0,
            )