Coverage for src/iptvtools/config.py: 73%

15 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-31 13:48 +0000

1#!/usr/bin/env python 

2"""Configuration for iptvtools. 

3 

4File: config.py 

5Author: huxuan 

6Email: i(at)huxuan.org 

7""" 

8 

9import json 

10import os 

11import os.path 

12from pathlib import Path 

13from typing import Any 

14 

15 

16class MetaConfig(type): 

17 """Configuration for iptvtools.""" 

18 

19 config: dict[str, Any] = {} 

20 

21 @classmethod 

22 def init(cls, config_file: str | Path) -> None: 

23 """Initialize configuration.""" 

24 if os.path.isfile(config_file): 

25 with open(config_file) as fin: 

26 cls.config = json.load(fin) 

27 

28 def __getattr__(cls, key: str) -> Any: 

29 """Get configuration with key.""" 

30 return cls.config.get(key, {}) 

31 

32 

33class Config(metaclass=MetaConfig): # pylint: disable=R0903 

34 """Configuration for iptvtools."""