File size: 1,754 Bytes
7b2e5db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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)