"""Module to comb directories and identify AaC files."""importloggingimportosfromaac.in_out.constantsimportYAML_DOCUMENT_EXTENSION,AAC_DOCUMENT_EXTENSIONfromaac.in_out.parserimportparse,ParserErrorfromaac.in_out.files.aac_fileimportAaCFile
[docs]deffind_aac_files(root_directory_to_search:str)->list[AaCFile]:"""Return a list of paths to AaC files in or under the root directory."""aac_files=[]root_dir_abs_path=os.path.abspath(root_directory_to_search)ifos.path.isdir(root_dir_abs_path):directory_results=os.walk(root_dir_abs_path)fordirectory_path,_,filenamesindirectory_results:forfilenameinfilenames:filepath=os.path.join(directory_path,filename)ifis_aac_file(filepath):aac_files.append(AaCFile(filepath,True,False))else:logging.error(f"Root path '{root_dir_abs_path}' for AaC file discovery is not a directory.")returnaac_files
[docs]defis_aac_file(filepath:str)->bool:"""Test if a target file is considered a valid AaC file."""_,file_ext=os.path.splitext(filepath)is_valid_aac_file=Falseiffile_ext==YAML_DOCUMENT_EXTENSIONorfile_ext==AAC_DOCUMENT_EXTENSION:try:parse(filepath)exceptParserErroraserror:logging.error(f"File '{filepath}' is not a valid AaC file. Reason: {error.errors}")exceptExceptionaserror:logging.error(f"File '{filepath}' could not be parsed. Reason: {error}")else:is_valid_aac_file=Truereturnis_valid_aac_file