#!/usr/bin/env python3.14# -*- coding: utf-8 -*-"""List solution directory contents."""from__future__importannotationsimportdatetimefromsolver.configimportExitCodesfromsolver.core.listingimportlistable,mimetypefromsolver.core.problemsimportProblemfromsolver.shellimportconsole,registerfromsolver.utils.path_utilsimportcanonical_path,iterdir_recursive@register(requires='reader',quietable=True)defls(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))forfileinsorted(iterdir_recursive(problem.solution_dir,rt='path'))iflistable(file,problem.solution_dir)# hides files without an extension, and __pycache__if(f_stat:=file.stat())]ifnotfiles:console.print('[error]error:[/error] no files found in the solution directory')returnExitCodes.EXIT_ERRORmax_filename_length:int=max(len(fileinfo[0])forfileinfoinfiles)forfilename,size,mtime,mime,is_a_solutioninfiles:mark=' [accent]*[/accent]'ifis_a_solutionelse''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)returnExitCodes.EXIT_OK