-
Notifications
You must be signed in to change notification settings - Fork 20
/
config.py
56 lines (39 loc) · 1.62 KB
/
config.py
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
"""Module to hold the `AutoImportConfig` class definition."""
from pathlib import Path
from typing import Any, Dict, Optional, Tuple
import toml
from autoimport.utils import get_pyproject_path
class Config:
"""Defines the base `Config` and provides accessors to get config values."""
def __init__(
self,
config_dict: Optional[Dict[str, Any]] = None,
config_path: Optional[Path] = None,
) -> None:
"""Initialize the config."""
self._config_dict: Dict[str, Any] = config_dict or {}
self.config_path: Optional[Path] = config_path
def get_option(self, option: str) -> Optional[str]:
"""Return the value of a config option.
Args:
option (str): the config option for which to return the value
Returns:
The value of the given config option or `None` if it doesn't exist
"""
return self._config_dict.get(option)
class AutoImportConfig(Config):
"""Defines the autoimport `Config`."""
def __init__(self, starting_path: Optional[Path] = None) -> None:
"""Initialize the config."""
config_path, config_dict = _find_config(starting_path)
super().__init__(config_dict=config_dict, config_path=config_path)
def _find_config(
starting_path: Optional[Path] = None,
) -> Tuple[Optional[Path], Dict[str, Any]]:
pyproject_path: Optional[Path] = get_pyproject_path(starting_path)
if pyproject_path:
return pyproject_path, toml.load(pyproject_path).get("tool", {}).get(
"autoimport", {}
)
return None, {}
autoimport_config: AutoImportConfig = AutoImportConfig()