from pathlib import Path import yaml from box import ConfigBox from box.exceptions import BoxValueError from research_assistant.app_logging import app_logger from research_assistant.entity import articleLoaderConfig def read_yaml(path_to_yaml: Path) -> ConfigBox: """reads yaml file and returns Args: path_to_yaml (str): path like input Raises: ValueError: if yaml file is empty e: empty file Returns: ConfigBox: ConfigBox type """ try: with open(path_to_yaml) as yaml_file: app_logger.info(f"yaml file: {path_to_yaml} loaded successfully") return ConfigBox(yaml.safe_load(yaml_file)) except BoxValueError as e: raise ValueError("yaml file is empty") from e def create_directories(path_to_directories: list, verbose=True): """create list of directories Args: path_to_directories (list): list of path of directories verbose (bool, optional): whether to log the creation of directories. Defaults to True. """ for path in path_to_directories: Path(path).mkdir(parents=True, exist_ok=True) if verbose: app_logger.info(f"created directory at: {path}") def write_to_file(filename, text): """write text to file Args: path (str): file path text (str): text to write """ with open(filename, "w") as file: file.write(text) app_logger.info(f"wrote text to file: {filename}") def write_summary_to_file(config: articleLoaderConfig, text: str): create_directories([config.summary_save_dir]) output_filepath = ( Path(config.summary_save_dir) / f"summary_{Path(config.file_path).stem}.md" ) write_to_file(output_filepath, text)