Euler source The code that runs the solver shell and this site, version 4.34.2 Repository on GitHub

List solution directory contents.

43 lines of Python Raw file Open on GitHub

#!/usr/bin/env python3.14
# -*- coding: utf-8 -*-
"""List solution directory contents."""
from __future__ import annotations

import datetime

from solver.config import ExitCodes
from solver.core.listing import listable, mimetype
from solver.core.problems import Problem
from solver.shell import console, register
from solver.utils.path_utils import canonical_path, iterdir_recursive


@register(requires='reader', quietable=True)
def ls(problem: Problem) -> int:
    """List the files in a problem's solution directory.

    Walks the directory recursively and prints each file's canonical path, size, modified
    time and MIME type, sorted by path; a solution file (`pNNNN_sK.*`) is flagged with a
    trailing `*`. Files with no extension, and anything under `__pycache__`, are skipped. Fails when the directory holds
    nothing to list.

    Args:
        problem: [problem] The problem whose solution directory to list.
    """
    solution_prefix: str = f'p{problem.number:04d}_s'
    files: list[tuple[str, int, float, str, bool]] = [
        (canonical_path(file), f_stat.st_size, f_stat.st_ctime, mimetype(file) or '',
         file.name.startswith(solution_prefix))
        for file in sorted(iterdir_recursive(problem.solution_dir, rt='path'))
        if listable(file, problem.solution_dir)  # hides files without an extension, and __pycache__
        if (f_stat := file.stat())
    ]
    if not files:
        console.print('[error]error:[/error] no files found in the solution directory')
        return ExitCodes.EXIT_ERROR
    max_filename_length: int = max(len(fileinfo[0]) for fileinfo in files)
    for filename, size, mtime, mime, is_a_solution in files:
        mark = ' [accent]*[/accent]' if is_a_solution else ''
        modified: str = datetime.datetime.fromtimestamp(mtime).strftime('%Y-%m-%d %H:%M:%S')
        console.print(f'{filename:<{max_filename_length}} {size:6d} bytes {modified} {mime}{mark}', highlight=True)
    return ExitCodes.EXIT_OK