local.py 1.23 KB
Newer Older
Weizhi Cui's avatar
Weizhi Cui committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
# @Version     : Python 3.11.4
# @Software    : Sublime Text 4
# @Author      : StudentCWZ
# @Email       : StudentCWZ@outlook.com
# @Date        : 2023/10/28 15:07
# @File        : local.py
# @Description :
"""

import os
from typing import Any

import yaml


class LocalConfig:
    def __init__(self, config_dir: str):
崔为之's avatar
崔为之 committed
21 22 23 24 25
        """
        Initialize a LocalConfig instance.

        :param config_dir: The directory where the config files are located.
        """
Weizhi Cui's avatar
Weizhi Cui committed
26 27 28
        self.config_dir = config_dir

    def load(self, filename: str) -> Any:
崔为之's avatar
崔为之 committed
29 30 31 32 33 34 35 36
        """
        Load a config file and return its content.

        :param filename: The name of the config file to load.
        :return: The content of the config file.
        :raises FileNotFoundError: If the file does not exist.
        """
        # Construct the full path of the config file
Weizhi Cui's avatar
Weizhi Cui committed
37
        filepath = os.path.join(self.config_dir, filename)
崔为之's avatar
崔为之 committed
38
        # Check if the file exists
Weizhi Cui's avatar
Weizhi Cui committed
39
        if not os.path.exists(filepath):
崔为之's avatar
崔为之 committed
40
            raise FileNotFoundError(f'No such file or directory: {filepath}')
崔为之's avatar
崔为之 committed
41
        # Open the file and load its content
Weizhi Cui's avatar
Weizhi Cui committed
42 43 44
        with open(filepath, 'r') as file:
            cfg = yaml.safe_load(file)
        return cfg