-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfiles.py
More file actions
29 lines (23 loc) · 874 Bytes
/
files.py
File metadata and controls
29 lines (23 loc) · 874 Bytes
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
from abc import abstractmethod
from pathlib import Path
from typing import Any
from config.common import ConfigurationSource
from config.errors import MissingConfigurationFileError
PathType = Path | str
class FileConfigurationSource(ConfigurationSource):
def __init__(self, file_path: PathType, optional: bool = False) -> None:
super().__init__()
self.file_path = Path(file_path)
self.optional = optional
@abstractmethod
def read_source(self) -> dict[str, Any]:
"""
Reads values from the source file path. This method is not
used if the file does not exist.
"""
def get_values(self) -> dict[str, Any]:
if not self.file_path.exists():
if self.optional:
return {}
raise MissingConfigurationFileError(self.file_path)
return self.read_source()